什么是deployment

本文通过实操来体会deployment和replicaset之间的关系

一、查看节点

root@k8s:~# kubectl get nodes
NAME                STATUS   ROLES    AGE     VERSION
k8s-control-plane   Ready    master   3m30s   v1.15.3
k8s-worker          Ready    <none>   2m44s   v1.15.3
k8s-worker2         Ready    <none>   2m46s   v1.15.3
k8s-worker3         Ready    <none>   2m46s   v1.15.3

二、创建deployment

root@k8s:~# kubectl create deployment mynginx --image=docker.io/nginx:1.15-alpine
deployment.apps/mynginx created
root@k8s:~# kubectl get deploy,rs,pod -l app=mynginx
NAME                            READY   UP-TO-DATE   AVAILABLE   AGE
deployment.extensions/mynginx   1/1     1            1           12s

NAME                                       DESIRED   CURRENT   READY   AGE
replicaset.extensions/mynginx-8559d9b8bc   1         1         1       12s

NAME                           READY   STATUS    RESTARTS   AGE
pod/mynginx-8559d9b8bc-wj2ff   1/1     Running   0          12s

可以发现层级关系,deployment(1) -> replicaset(1) -> pod(1)

三、扩展deployment

root@k8s:~# kubectl scale deploy mynginx --replicas=3
deployment.extensions/mynginx scaled
root@k8s:~# kubectl get deploy,rs,pod -l app=mynginx
NAME                            READY   UP-TO-DATE   AVAILABLE   AGE
deployment.extensions/mynginx   1/3     3            1           2m27s

NAME                                       DESIRED   CURRENT   READY   AGE
replicaset.extensions/mynginx-8559d9b8bc   3         3         1       2m27s

NAME                           READY   STATUS              RESTARTS   AGE
pod/mynginx-8559d9b8bc-9gg27   0/1     ContainerCreating   0          3s
pod/mynginx-8559d9b8bc-gs2bf   0/1     ContainerCreating   0          3s
pod/mynginx-8559d9b8bc-wj2ff   1/1     Running             0          2m27s

这次的层级关系发生变化,deployment(1) -> replicaset(1) -> pod(3)

三、更新deployment

root@k8s:~# kubectl set image deployment mynginx nginx=nginx:1.16-alpine
deployment.extensions/mynginx image updated
root@k8s:~# kubectl get deploy,rs,pod -l app=mynginx
NAME                            READY   UP-TO-DATE   AVAILABLE   AGE
deployment.extensions/mynginx   3/3     3            3           4m47s

NAME                                       DESIRED   CURRENT   READY   AGE
replicaset.extensions/mynginx-75cd85c66f   3         3         3       46s
replicaset.extensions/mynginx-8559d9b8bc   0         0         0       4m47s

NAME                           READY   STATUS    RESTARTS   AGE
pod/mynginx-75cd85c66f-5stdh   1/1     Running   0          37s
pod/mynginx-75cd85c66f-kxpqm   1/1     Running   0          29s
pod/mynginx-75cd85c66f-ss26f   1/1     Running   0          46s

这次的层级关系又发生变化,deployment(1) -> replicaset(2) -> pod(3')

四、回滚deployment

root@k8s:~# kubectl rollout history deploy mynginx
deployment.extensions/mynginx
REVISION  CHANGE-CAUSE
1         <none>
2         <none>
root@k8s:~# kubectl rollout undo deployment mynginx --to-revision=1
deployment.extensions/mynginx rolled back
root@k8s:~# kubectl get deploy,rs,pod -l app=mynginx
NAME                            READY   UP-TO-DATE   AVAILABLE   AGE
deployment.extensions/mynginx   3/3     3            3           8m33s

NAME                                       DESIRED   CURRENT   READY   AGE
replicaset.extensions/mynginx-75cd85c66f   0         0         0       4m32s
replicaset.extensions/mynginx-8559d9b8bc   3         3         3       8m33s

NAME                           READY   STATUS    RESTARTS   AGE
pod/mynginx-8559d9b8bc-7dlxg   1/1     Running   0          27s
pod/mynginx-8559d9b8bc-f4tkf   1/1     Running   0          24s
pod/mynginx-8559d9b8bc-gkxzg   1/1     Running   0          26s

可以发现,deployment又回滚到上一个版本了。

更详细的文章请查看Deployment Rolling Update and Rollback

Now, finally, rolling updates and rollbacks
are not specific for deployments, only.
They are supported by other controllers, as well,
such as DaemonSets and StatefulSets.

推荐阅读更多精彩内容