Deployment的典型用例:
1. 使用Deployment来启动(上线/部署)一个Pod或者ReplicaSet
2. 检查一个Deployment是否成功执行
3. 更新Deployment来重新创建相应的Pods(例如,需要使用一个新的Image)
4. 如果现有的Deployment不稳定,那么回滚到一个早期的稳定的Deployment版本
5. 暂停或者恢复一个Deployment

deployment 部署过程

创建一个deployment

  1. yaml文件

    apiVersion: extensions/v1beta1
    kind: Deployment
    metadata:
      name: nginx-deployment
    spec:
      replicas: 3
      template:
        metadata:
          labels:
             app: nginx
        spec:
           containers:
             - name: nginx
               image: docker.io/nginx
               ports:
                - containerPort: 80
  2. 创建deployment

    kubectl create -f nginx-deployment.yaml  —record

    —record选项是为了记录当前执行的命令所创建/更新的资源,即使用—record选型执行的deployment都会被记录下来,
    用于以后查看每次deployment的细节,查看所有Deployment的历史,或者回滚到某一历史版本。

  3. 查看deployment的状态

    [root@controller01 deployment]# kubectl get deployments nginx-deployment
    NAME               DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
    nginx-deployment   3         3         3            3           29s
  4. 查看pod的label

    [root@controller01 deployment]# kubectl get pods --show-labels | grep ^nginx
    nginx-deployment-4087004473-8lvh9 0/1 ContainerCreating 0 6m app=nginx,pod-template-hash=4087004473
    nginx-deployment-4087004473-d8fwd 0/1 ContainerCreating 0 6m app=nginx,pod-template-hash=4087004473
    nginx-deployment-4087004473-xwfkz 0/1 ContainerCreating 0 6m app=nginx,pod-template-hash=4087004473
  5. 查看deployment的状态,生成yaml文件

    [root@controller01 deployment]# kubectl get deployment nginx-deployment -o yaml

升级操作

  1. yaml文件

    apiVersion: extensions/v1beta1
    kind: Deployment
    metadata:
    name: nginx-deployment
    spec:
    replicas: 3
    template:
    metadata:
     labels:
         app: nginx
    spec:
       containers:
         - name: nginx
           image: docker.io/bitnami/nginx
           ports:
            - containerPort: 80
      区别:更改了nginx的image,相当于nginx的升级
    
  2. 操作

    kubectl apply -f update-nginx-deployment.yaml 
    
  3. 检验升级是否成功

    kubectl get deployments nginx-deployment  -o yaml
    

    可以看到image已经修改

    [root@controller01 deployment]# kubectl get po | grep ^nginx
    nginx-deployment-3905994866-f8bjp   1/1       Running            0          1h
    nginx-deployment-3905994866-j7w0d   1/1       Running            0          36m
    nginx-deployment-3905994866-qc5f3   1/1       Running            0          1h
    

    可以看到服务已经升级结束

    服务升级失败

    [root@controller01 deployment]# kubectl get po | grep ^nginx
    nginx-deployment-3446456637-vdj97   0/1       ImagePullBackOff   0          30m
    nginx-deployment-3446456637-zllkx   0/1       ImagePullBackOff   0          30m
    nginx-deployment-3905994866-f8bjp   1/1       Running            0          51m
    nginx-deployment-3905994866-qc5f3   1/1       Running            0          51m

我们发现最新一次Deployment出现了问题,没有找到相应的镜像,部署失败。对于这种情形,我们选择将Deployment恢复到上一版本,
注意,这里不是通过恢复之前的RC或者Pods或者RS,而是回滚Deployment。

失败回滚

  1. 查看可以回滚的版本

    [root@controller01 deployment]# kubectl rollout history deployment nginx-deployment
    deployments "nginx-deployment"
    REVISION    CHANGE-CAUSE
    1       <none>
    2       <none>
    3       <none>

    可以看到一共有三个版本,由于前面没有加 --record,没有看到版本信息

  2. 回滚到指定版本

    [root@controller01 deployment]# kubectl rollout undo deployment nginx-deployment --to-revision=2
    deployment "nginx-deployment" rolled back
  3. 查看运行是否正常

    [root@controller01 deployment]# kubectl get rs | grep ^nginx
    nginx-deployment-3446456637   0         0         0         41m
    nginx-deployment-3905994866   3         3         3         1h
    nginx-deployment-67353951     0         0         0         5h

暂停服务和重启服务


kubectl rollout pause deployment nginx-deployment
kubectl rollout resume deployment/nginx-deployment

deployment 和 Rs关系

Replica Set(简称RS)是k8s新一代的Pod controller。与RC相比仅有selector存在差异,RS支持了set-based selector(可以使用in、notin、key存在、key不存在四种方式来选择满足条件的label集合)。Deployment是基于RS实现的。

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐