Kubernetes 201

扩展应用

通过修改 Deployment 中副本的数量(replicas),可以动态扩展或收缩应用:

scale

这些自动扩展的容器会自动加入到 service 中,而收缩回收的容器也会自动从 service 中删除。

  1. $ kubectl scale --replicas=3 deployment/nginx-app
  2. $ kubectl get deploy
  3. NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
  4. nginx-app 3 3 3 3 10m

滚动升级

滚动升级(Rolling Update)通过逐个容器替代升级的方式来实现无中断的服务升级:

  1. kubectl rolling-update frontend-v1 frontend-v2 --image=image:v2

update1

update2

update3

update4

在滚动升级的过程中,如果发现了失败或者配置错误,还可以随时回滚:

  1. kubectl rolling-update frontend-v1 frontend-v2 --rollback

需要注意的是,kubectl rolling-update 只针对 ReplicationController。对于更新策略是 RollingUpdate 的 Deployment(Deployment 可以在 spec 中设置更新策略为 RollingUpdate,默认就是 RollingUpdate),更新应用后会自动滚动升级:

  1. spec:
  2. replicas: 3
  3. selector:
  4. matchLabels:
  5. run: nginx-app
  6. strategy:
  7. rollingUpdate:
  8. maxSurge: 1
  9. maxUnavailable: 1
  10. type: RollingUpdate

而更新应用的话,就可以直接用 kubectl set 命令:

  1. kubectl set image deployment/nginx-app nginx-app=nginx:1.9.1

滚动升级的过程可以用 rollout 命令查看:

  1. $ kubectl rollout status deployment/nginx-app
  2. Waiting for rollout to finish: 2 out of 3 new replicas have been updated...
  3. Waiting for rollout to finish: 2 of 3 updated replicas are available...
  4. Waiting for rollout to finish: 2 of 3 updated replicas are available...
  5. Waiting for rollout to finish: 2 of 3 updated replicas are available...
  6. Waiting for rollout to finish: 2 of 3 updated replicas are available...
  7. Waiting for rollout to finish: 2 of 3 updated replicas are available...
  8. deployment "nginx-app" successfully rolled out

Deployment 也支持回滚:

  1. $ kubectl rollout history deployment/nginx-app
  2. deployments "nginx-app"
  3. REVISION CHANGE-CAUSE
  4. 1 <none>
  5. 2 <none>
  6. $ kubectl rollout undo deployment/nginx-app
  7. deployment "nginx-app" rolled back

资源限制

Kubernetes 通过 cgroups 提供容器资源管理的功能,可以限制每个容器的 CPU 和内存使用,比如对于刚才创建的 deployment,可以通过下面的命令限制 nginx 容器最多只用 50% 的 CPU 和 128MB 的内存:

  1. $ kubectl set resources deployment nginx-app -c=nginx --limits=cpu=500m,memory=128Mi
  2. deployment "nginx" resource requirements updated

这等同于在每个 Pod 中设置 resources limits:

  1. apiVersion: v1
  2. kind: Pod
  3. metadata:
  4. labels:
  5. app: nginx
  6. name: nginx
  7. spec:
  8. containers:
  9. - image: nginx
  10. name: nginx
  11. resources:
  12. limits:
  13. cpu: "500m"
  14. memory: "128Mi"

健康检查

Kubernetes 作为一个面向应用的集群管理工具,需要确保容器在部署后确实处在正常的运行状态。Kubernetes 提供了两种探针(Probe,支持 exec、tcpSocket 和 http 方式)来探测容器的状态:

  • LivenessProbe:探测应用是否处于健康状态,如果不健康则删除并重新创建容器
  • ReadinessProbe:探测应用是否启动完成并且处于正常服务状态,如果不正常则不会接收来自 Kubernetes Service 的流量

对于已经部署的 deployment,可以通过 kubectl edit deployment/nginx-app 来更新 manifest,增加健康检查部分:

  1. apiVersion: extensions/v1beta1
  2. kind: Deployment
  3. metadata:
  4. labels:
  5. app: nginx
  6. name: nginx-default
  7. spec:
  8. replicas: 3
  9. selector:
  10. matchLabels:
  11. app: nginx
  12. template:
  13. metadata:
  14. labels:
  15. app: nginx
  16. spec:
  17. containers:
  18. - image: nginx
  19. imagePullPolicy: Always
  20. name: http
  21. resources: {}
  22. terminationMessagePath: /dev/termination-log
  23. terminationMessagePolicy: File
  24. resources:
  25. limits:
  26. cpu: "500m"
  27. memory: "128Mi"
  28. livenessProbe:
  29. httpGet:
  30. path: /
  31. port: 80
  32. initialDelaySeconds: 15
  33. timeoutSeconds: 1
  34. readinessProbe:
  35. httpGet:
  36. path: /
  37. port: 80
  38. initialDelaySeconds: 5
  39. timeoutSeconds: 1