使用Deployment运行一个无状态应用

本文介绍通过Kubernetes Deployment对象如何去运行一个应用.

教程目标

  • 创建一个nginx deployment.
  • 使用kubectl列举关于deployment信息.
  • 更新deployment.

准备开始

你必须拥有一个 Kubernetes 的集群,同时你的 Kubernetes 集群必须带有 kubectl 命令行工具。 如果你还没有集群,你可以通过 Minikube 构建一 个你自己的集群,或者你可以使用下面任意一个 Kubernetes 工具构建:

要获知版本信息,请输入 kubectl version.

创建和探究一个nginx deployment

你可以通过创建一个Kubernetes Deployment对象来运行一个应用, 可以在一个YAML文件中描述Deployment. 例如, 下面这个YAML文件描述了一个运行nginx:1.7.9 Docker镜像的Deployment:

application/deployment.yaml 使用Deployment运行一个无状态应用 - 图1
  1. apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2
  2. kind: Deployment
  3. metadata:
  4. name: nginx-deployment
  5. spec:
  6. selector:
  7. matchLabels:
  8. app: nginx
  9. replicas: 2 # tells deployment to run 2 pods matching the template
  10. template:
  11. metadata:
  12. labels:
  13. app: nginx
  14. spec:
  15. containers:
  16. - name: nginx
  17. image: nginx:1.14.2
  18. ports:
  19. - containerPort: 80
  1. 通过YAML文件创建一个Deployment:

    1. kubectl apply -f https://k8s.io/examples/application/deployment.yaml
  2. 展示Deployment相关信息:

    kubectl describe deployment nginx-deployment

    1. user@computer:~/website$ kubectl describe deployment nginx-deployment
    2. Name: nginx-deployment
    3. Namespace: default
    4. CreationTimestamp: Tue, 30 Aug 2016 18:11:37 -0700
    5. Labels: app=nginx
    6. Annotations: deployment.kubernetes.io/revision=1
    7. Selector: app=nginx
    8. Replicas: 2 desired | 2 updated | 2 total | 2 available | 0 unavailable
    9. StrategyType: RollingUpdate
    10. MinReadySeconds: 0
    11. RollingUpdateStrategy: 1 max unavailable, 1 max surge
    12. Pod Template:
    13. Labels: app=nginx
    14. Containers:
    15. nginx:
    16. Image: nginx:1.7.9
    17. Port: 80/TCP
    18. Environment: <none>
    19. Mounts: <none>
    20. Volumes: <none>
    21. Conditions:
    22. Type Status Reason
    23. ---- ------ ------
    24. Available True MinimumReplicasAvailable
    25. Progressing True NewReplicaSetAvailable
    26. OldReplicaSets: <none>
    27. NewReplicaSet: nginx-deployment-1771418926 (2/2 replicas created)
    28. No events.
  3. 列出deployment创建的pods:

    kubectl get pods -l app=nginx

    1. NAME READY STATUS RESTARTS AGE
    2. nginx-deployment-1771418926-7o5ns 1/1 Running 0 16h
    3. nginx-deployment-1771418926-r18az 1/1 Running 0 16h
  4. 展示某一个pod信息:

    kubectl describe pod

    该处 <pod-name> 指某一pod的名称.

更新deployment

你可以通过更新一个新的YAML文件来更新deployment. 下面的YAML文件指定该deployment镜像更新为nginx 1.8.

application/deployment-update.yaml 使用Deployment运行一个无状态应用 - 图2
  1. apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2
  2. kind: Deployment
  3. metadata:
  4. name: nginx-deployment
  5. spec:
  6. selector:
  7. matchLabels:
  8. app: nginx
  9. replicas: 2
  10. template:
  11. metadata:
  12. labels:
  13. app: nginx
  14. spec:
  15. containers:
  16. - name: nginx
  17. image: nginx:1.16.1 # Update the version of nginx from 1.14.2 to 1.16.1
  18. ports:
  19. - containerPort: 80
  1. 应用新的YAML:

    1. kubectl apply -f https://k8s.io/examples/application/deployment-update.yaml
  2. 查看该deployment创建的pods以新的名称同时删除旧的pods:

    1. kubectl get pods -l app=nginx

通过增加副本数来弹缩应用

你可以通过应用新的YAML文件来增加Deployment中pods的数量. 该YAML文件将replicas设置为4, 指定该Deployment应有4个pods:

application/deployment-scale.yaml 使用Deployment运行一个无状态应用 - 图3
  1. apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2
  2. kind: Deployment
  3. metadata:
  4. name: nginx-deployment
  5. spec:
  6. selector:
  7. matchLabels:
  8. app: nginx
  9. replicas: 4 # Update the replicas from 2 to 4
  10. template:
  11. metadata:
  12. labels:
  13. app: nginx
  14. spec:
  15. containers:
  16. - name: nginx
  17. image: nginx:1.14.2
  18. ports:
  19. - containerPort: 80
  1. 应用新的YAML文件:

    1. kubectl apply -f https://k8s.io/examples/application/deployment-scale.yaml
  2. 验证Deployment有4个pods:

    1. kubectl get pods -l app=nginx

    输出的结果类似于:

    1. NAME READY STATUS RESTARTS AGE
    2. nginx-deployment-148880595-4zdqq 1/1 Running 0 25s
    3. nginx-deployment-148880595-6zgi1 1/1 Running 0 25s
    4. nginx-deployment-148880595-fxcez 1/1 Running 0 2m
    5. nginx-deployment-148880595-rwovn 1/1 Running 0 2m

删除deployment

通过名称删除deployment:

  1. kubectl delete deployment nginx-deployment

ReplicationControllers – 旧的方式

创建一个多副本应用首选方法是使用Deployment,反过来使用ReplicaSet. 在Deployment和ReplicaSet加入到Kubernetes之前, 多副本应用通过ReplicationController来配置.

接下来