2. 工作负载和调度 15%¶
1. 了解部署以及如何执行滚动更新和回滚¶
Understand deployments and how to perform rolling update and rollbacks
-
部署
-
ReplicaSet
- StatefulSet
- DaemonSet
- Job
-
CronJob
-
更新
-
版本更新
-
回滚
kubectl rollout status deployment/nginx-deployment
kubectl rollout history deployment/nginx-deployment
kubectl rollout undo deployment/nginx-deployment
2. 使用 ConfigMaps 和 Secrets 配置应用程序¶
Use ConfigMaps and Secrets to configure applications
都是 key-value ,区别在于是否存储机密信息,Secrets 内容都需要 base64 编码;
-
ConfigMaps
kubectl create configmap kubectl create configmap game-config --from-file=docs/user-guide/configmap/kubectl # 通过目录创建 --from-file # 目录、文件 --from-literal # k-v --from-env-file # .env 文件 kubectl get secret secret-docker -o jsonpath='{.data
-
Secrets
-
Service Account:用来访问Kubernetes API,由Kubernetes自动创建,并且会自动挂载到 Pod 的/run/secrets/kubernetes.io/serviceaccount目录中;
- Opaque:base64编码格式的Secret,用来存储密码、密钥等;
-
kubernetes.io/dockerconfigjson:用来存储私有docker registry的认证信息。
kubectl create secret docker-registry NAME --docker-username=user --docker-password=password --docker-email=email [--docker-server=string] [--from-literal=key1=value1] [--dry-run] [options]
3. 了解如何扩展应用程序¶
Know how to scale applications
4. 了解用于创建健壮的、自修复的应用程序部署的原语¶
Understand the primitives used to create robust, self-healing, application deployments
5. 了解资源限制如何影响Pod调度¶
Understand how resource limits can affect Pod scheduling
- 设置污点: kubectl taint node [node] key=value:[effect]
- 其中[effect] 可取值:[ NoSchedule | PreferNoSchedule | NoExecute ]:
- NoSchedule :一定不能被调度。
- PreferNoSchedule:尽量不要调度。
- NoExecute:不仅不会调度,还会驱逐 Node 上已有的 Pod。
-
去除污点:kubectl taint node [node] key:[effect]-
-
node.kubernetes.io/not-ready:节点未准备好。这相当于节点状态 Ready 的值为 "False"。
- node.kubernetes.io/unreachable:节点控制器访问不到节点. 这相当于节点状态 Ready 的值为 "Unknown"。
- node.kubernetes.io/out-of-disk:节点磁盘耗尽。
- node.kubernetes.io/memory-pressure:节点存在内存压力。
- node.kubernetes.io/disk-pressure:节点存在磁盘压力。
- node.kubernetes.io/network-unavailable:节点网络不可用。
- node.kubernetes.io/unschedulable: 节点不可调度。
-
node.cloudprovider.kubernetes.io/uninitialized:如果 kubelet 启动时指定了一个 "外部" 云平台驱动, 它将给当前节点添加一个污点将其标志为不可用。在 - cloud-controller-manager 的一个控制器初始化这个节点后,kubelet 将删除这个污点。
containers: - name: app image: xxx resources: requests: memory: "64Mi" cpu: "250m" limits: memory: "128Mi" cpu: "500m"
6. 了解清单管理和通用模板工具¶
Awareness of manifest management and common templating tools
- yaml 文件
- kustomize
考点¶
-
Deployment 部署、更新:cli、yml、滚动升级、回滚及查看
kubectl rollout status deploy mkt -w -n longan # 查看状态 kubectl rollout history deploy mkt -n longan --revision=5 # 查看历史(参数指定版本) kubectl rollout undo deploy mkt -n longan --to-revision=2 # 回滚 kubectl scale deploy mkt --replicas=2 -n longan # 扩容 kubectl set image deploy mkt mkt=nginx:1.16.1
-
Label、Annotate
kubectl label pods -l old new kubectl annotate pods my-nginx-v4-9gw19 update
-
Init Container
-
污点与容忍度
kubectl taint nodes node1 key1=value1:NoSchedule # 创建污点
kubectl taint nodes node1 key1=value1:NoSchedule- # 删除污点
参考¶
题目¶
-
创建一个 pod 名称为 nginx,并将其调度到节点为 disk=stat 上
kubectl run nginx --image=nginx --generator=run-pod/v1 --overrides='{"spec": {"nodeSelector": {"disk": "stat"}}}' -n exam -o yaml > exam1.yml
-
提供一个 pod 的 yaml,要求添加 Init Container,Init Container 的作用是创建一个空文件,pod 的 Containers 判断文件是否存在,不存在则退出
apiVersion: apps/v1 kind: Pod metadata: name: exam2 namespace: exam spec: initContainers: - name: exam2Create image: busybox command: ['sh', '-c', 'touch /tmp/check.txt'] volumeMounts: - name: exma2v mountPath: /tmp containers: - name: exam2Check image: busybox command: ['sh', '-c', 'if [ ! -f "/tmp/check.txt" ]; then echo 'exist'; fi;'] volumeMounts: - name: exma2v mountPath: /tmp volumes: - name: exma2v emptyDir: {}
-
指定在命名空间内创建一个pod名称为test,内含四个指定的镜像nginx、redis、memcached、busybox
kubectl run pod test --container --generator=un-pod/v1
-
将deployment为nginx-app的副本数从1变成4
kubectl create deploy nginx-app --image=nginx
-
创建nginx-app的deployment ,使用镜像为nginx:1.11.0-alpine ,修改镜像为1.11.3-alpine,并记录升级,再使用回滚,将镜像回滚至nginx:1.11.0-alpine
- 创建Secret 名为mysecret,内含有password字段,值为bob,然后 在pod1里 使用ENV进行调用,Pod2里使用Volume挂载在/data 下
- Set configuration context $ kubectl config use-context k8s Create a pod named kucc4 with a single container for each of the following images running inside(there may be between 1 and 4 images specified):nginx +redis+Memcached+consul
- 列出pod并排序
- 创建一个pod ,并调度到某个节点上
-
提供一个pod,添加init-container ,在container中添加一个空文件,启动的时候。在另一个containre中检测是否有这个文件,否则退出
livenessProbe
-
在一个pod中创建2个容器,如redis+nginx
- 创建一个简单的daemonset
- deployment的扩容 ,scale命令
- 创建secret,有一个paasword字段(手动base64加密),创建两个pod引用该secret,一个用env ,一个用volume来调用
- 先将nginx:1.9的deployment,升级到nginx:1.11,记录下来(—record),然后回滚到1.9
- static pod的使用
- 在一个新的namespace创建pod
- 使用node selector,选择disk为ssd的机器调度
- 把一个node弄成unavailable 并且把上边的pod重新调度去新的node上
- tls bootstrap加入节点
- 创建一个nginx的Workload,保证其在每个节点上运行,注意不要覆盖节点原有的Tolerations