1. 8.3 Deployment控制器

Deployment 为 Pod 和 ReplicaSet(下一代 Replication Controller)提供声明式更新,有了Deployment之后我们就不再单独的使用RC和RS了。 您只需要在 Deployment 中描述您想要的目标状态是什么,Deployment controller 就会帮您将 Pod 和 ReplicaSet 的实际状态改变到您的目标状态。 Deployment集成了上线部署、滚动升级、创建副本、暂停上线任务,恢复上线任务,回滚到以前某一版本(成功/稳定)的Deployment等功能,在某种程度上,Deployment可以帮我们实现无人值守的上线,大大降低我们的上线过程的复杂沟通、操作风险。

1.1.1. 创建Deployment

Deployment对象的创建同样是依靠YAML的配置文件,具体的写法,稍后介绍,先来一睹Deployment的风采。

[root@linux-node1 example]# cat nginx-deployment.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.13.12
        ports:
        - containerPort: 80

使用kubectl创建Deployment

[root@linux-node1 ~]# kubectl create -f nginx-deployment.yaml
deployment "nginx-demo" created

将 kubectl 的 —record 的 flag 设置为 true 可以在 annotation 中记录当前命令创建或者升级了该资源。这在未来会很有用,例如,查看在每个 Deployment revision 中执行了哪些命令。

  1. 查看Deployment
[root@linux-node1 ~]# kubectl get deployment
NAME               DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
nginx-deployment   3         3         3            3           49m
  • UP-TO-DATE 的 replica 的数目已经达到了配置中要求的数目。
  • CURRENT 的 replica 数表示 Deployment 管理的 replica 数量
  • AVAILABLE 的 replica 数是当前可用的 replica 数量。

  • 查看Deployment扩展信息

[root@linux-node1 ~]# kubectl get pod -o wide
NAME                                READY     STATUS    RESTARTS   AGE       IP           NODE
nginx-deployment-656485bf95-47krr   1/1       Running   0          10m       10.2.97.16   192.168.56.22
nginx-deployment-656485bf95-cx4n8   1/1       Running   0          10m       10.2.57.14   192.168.56.21
nginx-deployment-656485bf95-d4nqv   1/1       Running   0          10m       10.2.57.15   192.168.56.21

Pod在运行中可能会重建,IP地址就会发生改变。

[root@linux-node1 ~]# kubectl get pods --show-labels
NAME                                READY     STATUS    RESTARTS   AGE       LABELS
nginx-deployment-6c45fc49cb-2mp5x   1/1       Running   0          14m       app=nginx,pod-template-hash=2701970576
nginx-deployment-6c45fc49cb-44j94   1/1       Running   0          14m       app=nginx,pod-template-hash=2701970576
nginx-deployment-6c45fc49cb-65zwf   1/1       Running   0          14m       app=nginx,pod-template-hash=2701970576
Pod-template-hash label

注意: pod-template-has这个 label 不是用户指定的!

注意上面示例输出中的 pod label 里的 pod-template-hash label。当 Deployment 创建或者接管 ReplicaSet 时,Deployment controller 会自动为 Pod 添加 pod-template-hash label。这样做的目的是防止 Deployment 的子ReplicaSet 的 pod 名字重复。通过将 ReplicaSet 的 PodTemplate 进行哈希散列,使用生成的哈希值作为 label 的值,并添加到 ReplicaSet selector 里、 pod template label 和 ReplicaSet 管理中的 Pod 上。

  1. 测试POD的访问
    [root@linux-node1 ~]# curl --head http://10.2.97.16/
    HTTP/1.1 200 OK
    Server: nginx/1.10.3
    Date: Wed, 28 Mar 2018 19:55:39 GMT
    Content-Type: text/html
    Content-Length: 612
    Last-Modified: Tue, 31 Jan 2017 15:01:11 GMT
    Connection: keep-alive
    ETag: "5890a6b7-264"
    Accept-Ranges: bytes
    

1.1.2. 更新Deployment

更新Deployment,并增加--record的参数

[root@linux-node1 ~]# kubectl set image deployment/nginx-deployment nginx=nginx:1.14.2 --record
deployment "nginx-deployment" image updated

查看更新状态

[root@linux-node1 ~]# kubectl rollout status deployment/nginx-deployment

查看更新后的Deploymnet

[root@linux-node1 ~]# kubectl get deployment -o wide
NAME               DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE       CONTAINERS   IMAGES         SELECTOR
nginx-deployment   3         3         3            3           15m       nginx        nginx:1.12.2   app=nginx

1.1.3. Deployment升级历史

1.查看升级历史

[root@linux-node1 ~]# kubectl rollout history deployment/nginx-deployment
deployments "nginx-deployment"
REVISION  CHANGE-CAUSE
1         <none>
2         kubectl set image deployment/nginx-deployment nginx=nginx:1.12.2 --record=true

因为我们创建 Deployment 的时候使用了--record参数可以记录命令,我们可以很方便的查看每次 revision 的变化。查看单个revision 的详细信息:

[root@linux-node1 ~]# kubectl rollout history deployment/nginx-deployment --revision=1
deployments "nginx-deployment" with revision #1
Pod Template:
  Labels:    app=nginx
    pod-template-hash=2120416951
  Containers:
   nginx:
    Image:    nginx:1.13.12
    Port:    80/TCP
    Environment:    <none>
    Mounts:    <none>
  Volumes:    <none>

1.1.4. Deployment版本回滚

1.回滚到上一个版本

[root@linux-node1 ~]# kubectl rollout undo deployment/nginx-deployment
deployment "nginx-deployment" 
[root@linux-node1 ~]# kubectl get deployment -o wide
NAME               DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE       CONTAINERS   IMAGES         SELECTOR
nginx-deployment   3         3         3            3           19m       nginx        nginx:1.13.12   app=nginx

也可以使用 --revision参数指定某个历史版本:

[root@linux-node1 ~]# kubectl rollout undo deployment/nginx-deployment --to-revision=2

1.1.5. Deployment扩容

[root@linux-node1 ~]# kubectl scale deployment nginx-deployment --replicas 5
deployment "nginx-deployment" scaled
[root@linux-node1 ~]# kubectl get pod
NAME                               READY     STATUS    RESTARTS   AGE
nginx-deployment-9898d9674-hlnp7   1/1       Running   0          32s
nginx-deployment-9898d9674-l7ckn   1/1       Running   0          30s
nginx-deployment-9898d9674-m52ff   1/1       Running   0          4s
nginx-deployment-9898d9674-m6mjz   1/1       Running   0          33s
nginx-deployment-9898d9674-tp42v   1/1       Running   0          4s

暂停回滚 Deployment

[root@linux-node1 ~]# kubectl rollout pause deployment/nginx-deployment

[root@linux-node1 ~]# kubectl rollout resume deployment nginx-deployment
Copyright © 赵班长@新运维社区 2019 all right reserved,powered by Gitbook该文件修订时间: 2024-06-18 22:25:30

results matching ""

    No results matching ""