Skip to content

2. 工作负载和调度 15%

1. 了解部署以及如何执行滚动更新和回滚

Understand deployments and how to perform rolling update and rollbacks

  1. 部署

  2. ReplicaSet

  3. StatefulSet
  4. DaemonSet
  5. Job
  6. CronJob

  7. 更新

  8. 版本更新

  9. 回滚

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 编码;

  1. 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
    
  2. Secrets

  3. Service Account:用来访问Kubernetes API,由Kubernetes自动创建,并且会自动挂载到 Pod 的/run/secrets/kubernetes.io/serviceaccount目录中;

  4. Opaque:base64编码格式的Secret,用来存储密码、密钥等;
  5. 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

Kustomize

考点

  1. 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
    
  2. Label、Annotate

    kubectl label pods -l old new
    kubectl annotate pods my-nginx-v4-9gw19 update
    
  3. Init Container

  4. 污点与容忍度

kubectl taint nodes node1 key1=value1:NoSchedule  # 创建污点
kubectl taint nodes node1 key1=value1:NoSchedule-  # 删除污点

参考

题目

  1. 创建一个 pod 名称为 nginx,并将其调度到节点为 disk=stat 上

    kubectl run nginx --image=nginx --generator=run-pod/v1 --overrides='{"spec": {"nodeSelector": {"disk": "stat"}}}' -n exam -o yaml > exam1.yml
    
  2. 提供一个 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: {}
    
  3. 指定在命名空间内创建一个pod名称为test,内含四个指定的镜像nginx、redis、memcached、busybox

    kubectl run pod test --container --generator=un-pod/v1
    
  4. 将deployment为nginx-app的副本数从1变成4

    kubectl create deploy nginx-app --image=nginx
    
  5. 创建nginx-app的deployment ,使用镜像为nginx:1.11.0-alpine ,修改镜像为1.11.3-alpine,并记录升级,再使用回滚,将镜像回滚至nginx:1.11.0-alpine

  6. 创建Secret 名为mysecret,内含有password字段,值为bob,然后 在pod1里 使用ENV进行调用,Pod2里使用Volume挂载在/data 下
  7. 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
  8. 列出pod并排序
  9. 创建一个pod ,并调度到某个节点上
  10. 提供一个pod,添加init-container ,在container中添加一个空文件,启动的时候。在另一个containre中检测是否有这个文件,否则退出

    livenessProbe
    
  11. 在一个pod中创建2个容器,如redis+nginx

  12. 创建一个简单的daemonset
  13. deployment的扩容 ,scale命令
  14. 创建secret,有一个paasword字段(手动base64加密),创建两个pod引用该secret,一个用env ,一个用volume来调用
  15. 先将nginx:1.9的deployment,升级到nginx:1.11,记录下来(—record),然后回滚到1.9
  16. static pod的使用
  17. 在一个新的namespace创建pod
  18. 使用node selector,选择disk为ssd的机器调度
  19. 把一个node弄成unavailable 并且把上边的pod重新调度去新的node上
  20. tls bootstrap加入节点
  21. 创建一个nginx的Workload,保证其在每个节点上运行,注意不要覆盖节点原有的Tolerations