参考:https://blog.51cto.com/wutengfei/2116663

创建和管理多个Pod--Deployment

  Deployment 为 Pod 和 ReplicaSet 提供了一个声明式定义(declarative)方法,用来替代以前的ReplicationController 来方便的管理应用。
你只需要在 Deployment 中描述想要的目标状态是什么,Deployment controller 就会帮你将 Pod 和ReplicaSet 的实际状态改变到你的目标状态。你可以定义一个全新的 Deployment 来创建 ReplicaSet 或者删除已有的 Deployment 并创建一个新的来替换。

  复制控制器(Replication Controller,RC)
RC是K8s集群中最早的保证Pod高可用的API对象。通过监控运行中的Pod来保证集群中运行指定数目的Pod副本。指定的数目可以是多个也可以是1个;少于指定数目,RC就会启动运行新的Pod副本;多于指定数目,RC就会杀死多余的Pod副本。即使在指定数目为1的情况下,通过RC运行Pod也比直接运行Pod更明智,因为RC也可以发挥它高可用的能力,保证永远有1个Pod在运行。RC是K8s较早期的技术概念,只适用于长期伺服型的业务类型,比如控制小机器人提供高可用的Web服务。

  副本集(Replica Set,RS)
RS是新一代RC,提供同样的高可用能力,区别主要在于RS后来居上,能支持更多种类的匹配模式。副本集对象一般不单独使用,而是作为Deployment的理想状态参数使用。

  Deployment典型的应用场景包括
(1)定义Deployment来创建Pod和ReplicaSet
(2)滚动升级和回滚应用;如果当前状态不稳定,回滚到之前的Deployment revision。每次回滚都会更新Deployment的revision。
(3)扩容和缩容,扩容Deployment以满足更高的负载。
(4)暂停和继续Deployment,暂停Deployment来应用PodTemplateSpec的多个修复,然后恢复上线。

实战Deployment

1.创建

定义deployment yaml,用于发布应用服务

[root@k8s-master ~]# pwd
/root
[root@k8s-master ~]# cat deployment-hello.yaml 
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: hello
spec:
  replicas: 4
  template:
    metadata:
      labels:
        run: hello
    spec:
      containers:
       - name: hello
         image: tomcat:8 #确保node节点上有该镜像且可正常运行,注意是node节点机器上,不是master机器
         imagePullPolicy: IfNotPresent ##Always,IfNotPresent,Never
         ports:
         - name: http
           containerPort: 8080

布署

[root@k8s-master ~]# kubectl create -f deployment-hello.yaml
deployment.extensions/hello created

查看部署

root@k8s-master ~]# kubectl get deployments #查看deploy是否成功
NAME    READY   UP-TO-DATE   AVAILABLE   AGE
hello   0/4     4            0           55s

查看rs(副本集)

[root@k8s-master ~]# kubectl get rs ##因为deployment是三层架构,看rs是否成功,我们看自动创建4个rs.名称后面的字符串是 模板的哈希值。是不会发生变化的,最后pod的是随机值
NAME DESIRED CURRENT READY AGE
hello-68df45bc79 4 4 0 8m29s

查看容器组

[root@k8s-master ~]# kubectl get pods #查看最后一层pod
NAME                     READY   STATUS              RESTARTS   AGE
hello-7d46c7db4c-42bwl   0/1     ContainerCreating   0          67s
hello-7d46c7db4c-rg9fq   0/1     ContainerCreating   0          67s
hello-7d46c7db4c-tclps   0/1     ContainerCreating   0          67s
hello-7d46c7db4c-whvsw   0/1     ContainerCreating   0   

2.扩容

将容器组个数扩充至8个

#语法格式 kubectl scale deployment + [deploy的name] + --replicas [count] + --namespace=命名空间
##说明 如果集群支持 horizontal pod autoscaling 的话,还可以为Deployment设置自动扩展:

kubectl autoscale deployment nginx-test --min=10 --max=15 --cpu-percent=80 --namespace=test

 

[root@k8s-master ~]# kubectl get deployments
NAME    READY   UP-TO-DATE   AVAILABLE   AGE
hello   4/4     4            4           7h1m
[root@k8s-master ~]# kubectl scale deployment hello --replicas 8 --namespace=default
deployment.extensions/hello scaled
[root@k8s-master ~]# kubectl get deployments
NAME    READY   UP-TO-DATE   AVAILABLE   AGE
hello   4/8     8            4           7h1m
[root@k8s-master ~]# kubectl get rs
NAME               DESIRED   CURRENT   READY   AGE
hello-57b49c67cf   8         8         4       7h2m
[root@k8s-master ~]# kubectl get pods ##正在扩容
NAME                     READY   STATUS              RESTARTS   AGE
hello-57b49c67cf-4r5wf   0/1     ContainerCreating   0          64s
hello-57b49c67cf-6hh59   1/1     Running             0          7h2m
hello-57b49c67cf-6ml78   1/1     Running             0          7h2m
hello-57b49c67cf-8lgcl   0/1     ContainerCreating   0          64s
hello-57b49c67cf-8xqgw   1/1     Running             0          7h2m
hello-57b49c67cf-dg5lr   0/1     ContainerCreating   0          64s
hello-57b49c67cf-pd826   1/1     Running             0          7h2m
hello-57b49c67cf-rj28m   0/1     ContainerCreating   0          64s
[root@k8s-master ~]# 

3.缩容

将容器组缩减至2个

[root@k8s-master ~]# kubectl scale deployment hello --replicas 2 --namespace=default
deployment.extensions/hello scaled
[root@k8s-master ~]# kubectl get deployment --namespace=default
NAME    READY   UP-TO-DATE   AVAILABLE   AGE
hello   2/2     2            2           7h5m
[root@k8s-master ~]# kubectl get rs --namespace=default
NAME               DESIRED   CURRENT   READY   AGE
hello-57b49c67cf   2         2         0       7h5m
[root@k8s-master ~]# kubectl get pods --namespace=default ##正在缩容
NAME                     READY   STATUS        RESTARTS   AGE
hello-57b49c67cf-4r5wf   1/1     Running       0          5m29s
hello-57b49c67cf-6hh59   1/1     Terminating   1          7h6m
hello-57b49c67cf-6ml78   1/1     Terminating   1          7h6m
hello-57b49c67cf-8lgcl   0/1     Terminating   0          5m29s
hello-57b49c67cf-8xqgw   1/1     Terminating   1          7h6m
hello-57b49c67cf-dg5lr   1/1     Running       0          5m29s
hello-57b49c67cf-pd826   1/1     Terminating   1          7h6m
hello-57b49c67cf-rj28m   0/1     Terminating   0          5m29s

[root@k8s-master ~]# kubectl get pods --namespace=default  ##缩容完成
NAME READY STATUS RESTARTS AGE
hello-57b49c67cf-4r5wf 1/1 Running 0 7m7s
hello-57b49c67cf-dg5lr 1/1 Running 0 7m7s

 

4.滚动升级

将image: mytomcat:v8 升级为mytomcat:v9 #语法格式 kubectl set image deployment/deployment-name container-name=newimage:version --namespace=命名空间
也可以直接
kubectl edit deployment/hello修改 .spec.template.spec.containers[0].image 

[root@k8s-master ~]# kubectl get deployment -o wide ##触发rollout
NAME    READY   UP-TO-DATE   AVAILABLE   AGE     CONTAINERS   IMAGES        SELECTOR
hello   2/2     2            2           7h14m   hello        mytomcat:v8   run=hello
[root@k8s-master ~]# docker tag mytomcat:v8 mytomcat:v9
[root@k8s-master ~]# docker images
REPOSITORY                           TAG                 IMAGE ID            CREATED             SIZE
mytomcat                             v8                  f1332ae3f570        4 days ago          463MB
mytomcat                             v9                  f1332ae3f570        4 days ago          463MB
[root@k8s-master ~]# kubectl set image deployment/hello hello=mytomcat:v9 
deployment.extensions/hello image updated

[root@k8s-master ~]# kubectl rollout status deployment/hello ##查看升级进度
Waiting for deployment "hello" rollout to finish: 1 old replicas are pending termination...
Waiting for deployment "hello" rollout to finish: 1 old replicas are pending termination...
Waiting for deployment "hello" rollout to finish: 1 old replicas are pending termination...
Waiting for deployment "hello" rollout to finish: 1 of 2 updated replicas are available...
deployment "hello" successfully rolled out


[root@k8s-master ~]# kubectl rollout status deployment/hello
deployment "hello" successfully rolled out


[root@k8s-master ~]# kubectl get deployments -o wide
NAME    READY   UP-TO-DATE   AVAILABLE   AGE     CONTAINERS   IMAGES        SELECTOR
hello   1/2     2            1           7h46m   hello        mytomcat:v9   run=hello

 

查看历史版本

[root@k8s-master ~]# kubectl rollout history deployment/hello
deployment.extensions/hello 
REVISION  CHANGE-CAUSE
1         <none>
2         <none>

查看pod

[root@k8s-master ~]# kubectl get pods
NAME                     READY   STATUS             RESTARTS   AGE
hello-57b49c67cf-4r5wf   1/1     Running            0          52m
hello-5cd4456b66-2wc9q   0/1     ImagePullBackOff   0          8m10s
hello-5cd4456b66-hzdns   0/1     ImagePullBackOff   0          8m9s
[root@k8s-master ~]# kubectl describe pod hello-5cd4456b66-2wc9q

Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 10m default-scheduler Successfully assigned default/hello-5cd4456b66-2wc9q to k8s-node1
Normal Pulling 8m54s (x4 over 10m) kubelet, k8s-node1 Pulling image "mytomcat:v9"
Warning Failed 8m35s (x4 over 10m) kubelet, k8s-node1 Failed to pull image "mytomcat:v9": rpc error: code = Unknown desc = Error response from daemon: pull access denied for mytomcat, repository does not exist or may require 'docker login'

Warning Failed 8m35s (x4 over 10m) kubelet, k8s-node1 Error: ErrImagePull
Normal BackOff 8m8s (x7 over 10m) kubelet, k8s-node1 Back-off pulling image "mytomcat:v9"
Warning Failed 5m53s (x16 over 10m) kubelet, k8s-node1 Error: ImagePullBackOff



报ImagePullBackOff错,到节点k8s-node1主机上执行docker tag mytomcat:v8 mytomcat:v9

[root@k8s-master ~]# kubectl get pods -o wide
NAME                     READY   STATUS    RESTARTS   AGE   IP            NODE        NOMINATED NODE   READINESS GATES
hello-5cd4456b66-2wc9q   1/1     Running   0          10m   10.244.1.30   k8s-node1   <none>           <none>
hello-5cd4456b66-hzdns   1/1     Running   0          10m   10.244.1.31   k8s-node1   <none>           <none>
[root@k8s-master ~]# kubectl get deployments
NAME    READY   UP-TO-DATE   AVAILABLE   AGE
hello   2/2     2            2           8h

[root@k8s-master ~]# kubectl rollout status deployment/hello
deployment "hello" successfully rolled out


5.回滚应用

[root@k8s-master ~]# kubectl get deployments -o wide
NAME    READY   UP-TO-DATE   AVAILABLE   AGE   CONTAINERS   IMAGES        SELECTOR
hello   2/2     2            2           8h    hello        mytomcat:v9   run=hello
[root@k8s-master ~]# kubectl rollout undo deployment/hello --namespace=default
deployment.extensions/hello rolled back
[root@k8s-master ~]# kubectl rollout status deployment/hello 
deployment "hello" successfully rolled out
[root@k8s-master ~]# kubectl rollout history deployment/hello
deployment.extensions/hello 
REVISION  CHANGE-CAUSE
2         <none>
3         <none>

[root@k8s-master ~]# kubectl get deployments -o wide
NAME    READY   UP-TO-DATE   AVAILABLE   AGE   CONTAINERS   IMAGES        SELECTOR
hello   2/2     2            2           8h    hello        mytomcat:v8   run=hello
[root@k8s-master ~]# kubectl get pods
NAME                     READY   STATUS    RESTARTS   AGE
hello-57b49c67cf-gghms   1/1     Running   0          93s
hello-57b49c67cf-l6fk2   1/1     Running   0          94s
[root@k8s-master ~]# kubectl describe pod hello-57b49c67cf-gghms
Image:          mytomcat:v8

 6.暂停或恢复

你可以在触发一次或多次更新前暂停一个Deployment,然后再恢复它。这样你就能多次暂停和恢复Deployment,在此期间进行一些修复工作,而不会出发不必要的rollout。

 kubectl rollout pause deployment/nginx-deployment
kubectl set image deploy/hello hello=mytomcat:v9
##kubectl edit deployment/hello
kubectl set resources deployment hello -c=hello --limits=cpu=200m,memory=512Mi ##命令详解 -c为容器名限定kubectl get deployments -o wide中的containers
kubectl rollout resume deploy hello


 
posted on 2019-04-03 18:32  pu20065226  阅读(844)  评论(0编辑  收藏  举报