kubernetes(8):新副本控制器—deployment
新副本控制器—deployment
https://www.cnblogs.com/luoahong/p/10302601.html
https://jimmysong.io/kubernetes-handbook/concepts/deployment.html
http://www.bubuko.com/infodetail-2612308.html
1 Deployment简介
Deployment主要职责同样是为了保证pod的数量和健康,90%的功能与Replication Controller完全一样,可以看做新一代的Replication Controller。但是,它又具备了Replication Controller之外的新特性:
- Replication Controller全部功能:Deployment继承了上面描述的Replication Controller全部功能。
- 事件和状态查看:可以查看Deployment的升级详细进度和状态。
- 回滚:当升级pod镜像或者相关参数的时候发现问题,可以使用回滚操作回滚到上一个稳定的版本或者指定的版本。
- 版本记录: 每一次对Deployment的操作,都能保存下来,给予后续可能的回滚使用。
- 暂停和启动:对于每一次升级,都能够随时暂停和启动。
- 多种升级方案:Recreate:删除所有已存在的pod,重新创建新的; RollingUpdate:滚动升级,逐步替换的策略,同时滚动升级时,支持更多的附加参数,例如设置最大不可用pod数量,最小升级间隔时间等等。
2 通过文件创建deployment
2.1 创建deployment文件
#[root@k8s-master k8s]# cat nginx-deployment.yaml apiVersion: extensions/v1beta1 kind: Deployment metadata: name: nginx-deployment spec: replicas: 3 template: metadata: labels: app: nginx spec: containers: - name: nginx image: 192.168.0.136:5000/nginx:1.15 ports: - containerPort: 80
2.2 启动deployment
[root@k8s-master k8s]# kubectl create -f nginx-deployment.yaml deployment "nginx-deployment" created [root@k8s-master k8s]# [root@k8s-master k8s]# kubectl get pods -o wide NAME READY STATUS RESTARTS AGE IP NODE nginx-deployment-4019830974-6lbk9 1/1 Running 0 22s 172.16.47.2 k8s-node-2 nginx-deployment-4019830974-8wb75 1/1 Running 0 22s 172.16.73.3 k8s-node-1 nginx-deployment-4019830974-drn36 1/1 Running 0 22s 172.16.73.2 k8s-node-1 [root@k8s-master k8s]#
2.3 查看deployment状态
[root@k8s-master k8s]# kubectl get all NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE deploy/nginx-deployment 3 3 3 3 58s NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE svc/kubernetes 10.254.0.1 <none> 443/TCP 2d NAME DESIRED CURRENT READY AGE rs/nginx-deployment-4019830974 3 3 3 58s NAME READY STATUS RESTARTS AGE po/nginx-deployment-4019830974-6lbk9 1/1 Running 0 58s po/nginx-deployment-4019830974-8wb75 1/1 Running 0 58s po/nginx-deployment-4019830974-drn36 1/1 Running 0 58s [root@k8s-master k8s]# kubectl get rs NAME DESIRED CURRENT READY AGE nginx-deployment-4019830974 3 3 3 1m [root@k8s-master k8s]# kubectl get pods -o wide NAME READY STATUS RESTARTS AGE IP NODE nginx-deployment-4019830974-6lbk9 1/1 Running 0 1m 172.16.47.2 k8s-node-2 nginx-deployment-4019830974-8wb75 1/1 Running 0 1m 172.16.73.3 k8s-node-1 nginx-deployment-4019830974-drn36 1/1 Running 0 1m 172.16.73.2 k8s-node-1 [root@k8s-master k8s]# [root@k8s-master k8s]# kubectl get deployment NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE nginx-deployment 3 3 3 3 8s [root@k8s-master k8s]#
kubectl get deployments的输出,其含义:
- DESIRED : Pod副本数量的期望值,即Deployment里定义的Replica。
- CURRENT: 当前实际的Replica数量,当这个值不断增加,达到DESIRED时表明部署完成。
- UP-TO-DATE:最新版本的Pod副本数量,用于指示在滚动升级过程中有多少Pod副本已经成功升级。
- AVAILABLE: 当前集群中可用的Pod副本数量(当前存活的Pod数量)
2.4 自动加载deployment配置
deployment编辑配置之后可以自动加载,不想rc还需要删除再启动。
执行 edit命令修改Deployment配置,修改了副本数量和image地址
[root@k8s-master k8s]# kubectl edit deployment nginx-deployment deployment "nginx-deployment" edited [root@k8s-master k8s]# kubectl get pods -o wide NAME READY STATUS RESTARTS AGE IP NODE nginx-deployment-2562048646-1pscs 0/1 ContainerCreating 0 3s <none> k8s-node-1 nginx-deployment-2562048646-h9fvj 0/1 ContainerCreating 0 3s <none> k8s-node-2 nginx-deployment-4019830974-1ctkn 0/1 ContainerCreating 0 4s <none> k8s-node-2 nginx-deployment-4019830974-6lbk9 1/1 Running 0 4m 172.16.47.2 k8s-node-2 nginx-deployment-4019830974-8wb75 1/1 Running 0 4m 172.16.73.3 k8s-node-1 nginx-deployment-4019830974-drn36 1/1 Running 0 4m 172.16.73.2 k8s-node-1 nginx-deployment-4019830974-tmw2r 0/1 Terminating 0 4s <none> k8s-node-1 [root@k8s-master k8s]# [root@k8s-master k8s]# kubectl get pods -o wide NAME READY STATUS RESTARTS AGE IP NODE nginx-deployment-2562048646-1pscs 0/1 ContainerCreating 0 17s <none> k8s-node-1 nginx-deployment-2562048646-h9fvj 0/1 ContainerCreating 0 17s <none> k8s-node-2 nginx-deployment-4019830974-1ctkn 1/1 Running 0 18s 172.16.47.3 k8s-node-2 nginx-deployment-4019830974-6lbk9 1/1 Running 0 4m 172.16.47.2 k8s-node-2 nginx-deployment-4019830974-8wb75 1/1 Running 0 4m 172.16.73.3 k8s-node-1 nginx-deployment-4019830974-drn36 1/1 Running 0 4m 172.16.73.2 k8s-node-1 nginx-deployment-4019830974-tmw2r 0/1 Terminating 0 18s <none> k8s-node-1 [root@k8s-master k8s]# kubectl get pods -o wide NAME READY STATUS RESTARTS AGE IP NODE nginx-deployment-2562048646-1pscs 1/1 Running 0 27s 172.16.73.5 k8s-node-1 nginx-deployment-2562048646-h9fvj 1/1 Running 0 27s 172.16.47.4 k8s-node-2 nginx-deployment-2562048646-j3bd4 1/1 Running 0 10s 172.16.47.5 k8s-node-2 nginx-deployment-2562048646-rv9nk 1/1 Running 0 4s 172.16.47.3 k8s-node-2 nginx-deployment-2562048646-wnnd2 1/1 Running 0 4s 172.16.73.4 k8s-node-1 [root@k8s-master k8s]# kubectl get rs NAME DESIRED CURRENT READY AGE nginx-deployment-2562048646 5 5 5 3m nginx-deployment-4019830974 0 0 0 7m [root@k8s-master k8s]# [root@k8s-master k8s]# curl -I 172.16.47.4 HTTP/1.1 200 OK Server: nginx/1.17.3 Date: Thu, 22 Aug 2019 08:21:48 GMT Content-Type: text/html Content-Length: 612 Last-Modified: Tue, 13 Aug 2019 08:50:00 GMT Connection: keep-alive ETag: "5d5279b8-264" Accept-Ranges: bytes [root@k8s-master k8s]#
2.5 删除deployment
[root@k8s-master k8s]# kubectl delete deployment nginx-deployment deployment "nginx-deployment" deleted [root@k8s-master k8s]# kubectl get rs No resources found. [root@k8s-master k8s]# kubectl get pods No resources found. [root@k8s-master k8s]#
3 通过命令行创建deployment
3.1 命令行创建
[root@k8s-master k8s]# kubectl run nginx-deployment2 --image=192.168.0.136:5000/nginx:1.15 --replicas=5 --record deployment "nginx-deployment2" created
3.2 查看deployment状态
[root@k8s-master k8s]# kubectl get deployment nginx-deployment2 NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE nginx-deployment2 5 5 5 5 10s [root@k8s-master k8s]# kubectl get rs NAME DESIRED CURRENT READY AGE nginx-deployment2-3516246156 5 5 5 18s [root@k8s-master k8s]# kubectl get pods -o wide NAME READY STATUS RESTARTS AGE IP NODE nginx-deployment2-3516246156-1h8g5 1/1 Running 0 32s 172.16.47.4 k8s-node-2 nginx-deployment2-3516246156-4cjck 1/1 Running 0 32s 172.16.47.3 k8s-node-2 nginx-deployment2-3516246156-bmlk3 1/1 Running 0 32s 172.16.47.2 k8s-node-2 nginx-deployment2-3516246156-h0ncj 1/1 Running 0 32s 172.16.73.3 k8s-node-1 nginx-deployment2-3516246156-r73n7 1/1 Running 0 32s 172.16.73.2 k8s-node-1 [root@k8s-master k8s]# curl -I 172.16.47.3 HTTP/1.1 200 OK Server: nginx/1.15.12 Date: Thu, 22 Aug 2019 08:32:20 GMT Content-Type: text/html Content-Length: 612 Last-Modified: Tue, 16 Apr 2019 13:08:19 GMT Connection: keep-alive ETag: "5cb5d3c3-264" Accept-Ranges: bytes [root@k8s-master k8s]#
3.3 手动扩容缩容
#kubectl scale deployment DeploymentName --replicas=NUM [root@k8s-master ~]# kubectl get deployment nginx-deployment2 NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE nginx-deployment2 5 5 5 5 16m [root@k8s-master ~]# kubectl scale deployment nginx-deployment2 --replicas=8 deployment "nginx-deployment2" scaled [root@k8s-master ~]# kubectl get deployment nginx-deployment2 NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE nginx-deployment2 8 8 8 8 17m [root@k8s-master ~]# kubectl get pods -o wide NAME READY STATUS RESTARTS AGE IP NODE nginx-deployment2-3766659668-2dl98 1/1 Running 0 9m 172.16.47.4 k8s-node-2 nginx-deployment2-3766659668-57p0w 1/1 Running 0 9m 172.16.73.5 k8s-node-1 nginx-deployment2-3766659668-c1lx6 1/1 Running 0 9m 172.16.47.3 k8s-node-2 nginx-deployment2-3766659668-f9rc6 1/1 Running 0 9m 172.16.47.5 k8s-node-2 nginx-deployment2-3766659668-l1pbv 1/1 Running 0 19s 172.16.73.2 k8s-node-1 nginx-deployment2-3766659668-n54wc 1/1 Running 0 19s 172.16.47.2 k8s-node-2 nginx-deployment2-3766659668-pdz98 1/1 Running 0 9m 172.16.73.4 k8s-node-1 nginx-deployment2-3766659668-shlp9 1/1 Running 0 19s 172.16.73.3 k8s-node-1 [root@k8s-master ~]# [root@k8s-master ~]# kubectl scale deployment nginx-deployment2 --replicas=3 deployment "nginx-deployment2" scaled [root@k8s-master ~]# kubectl get deployment nginx-deployment2 NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE nginx-deployment2 3 3 3 3 17m [root@k8s-master ~]# kubectl get pods -o wide NAME READY STATUS RESTARTS AGE IP NODE nginx-deployment2-3766659668-57p0w 1/1 Running 0 10m 172.16.73.5 k8s-node-1 nginx-deployment2-3766659668-f9rc6 1/1 Running 0 10m 172.16.47.5 k8s-node-2 nginx-deployment2-3766659668-pdz98 1/1 Running 0 10m 172.16.73.4 k8s-node-1 [root@k8s-master ~]#
3.4 升级pod镜像版本
#查看pod镜像版本
[root@k8s-master k8s]# kubectl get rs -o wide NAME DESIRED CURRENT READY AGE CONTAINER(S) IMAGE(S) SELECTOR nginx-deployment2-3516246156 5 5 5 5m nginx-deployment2 192.168.0.136:5000/nginx:1.15 pod-template-hash=3516246156,run=nginx-deployment2 [root@k8s-master k8s]#
#升级镜像
[root@k8s-master k8s]# kubectl set image deploy nginx-deployment2 nginx-deployment2=192.168.0.136:5000/nginx:latest deployment "nginx-deployment2" image updated
#查看升级结果
[root@k8s-master k8s]# kubectl get rs -o wide NAME DESIRED CURRENT READY AGE CONTAINER(S) IMAGE(S) SELECTOR nginx-deployment2-3516246156 3 3 3 7m nginx-deployment2 192.168.0.136:5000/nginx:1.15 pod-template-hash=3516246156,run=nginx-deployment2 nginx-deployment2-3766659668 3 3 1 2s nginx-deployment2 192.168.0.136:5000/nginx:latest pod-template-hash=3766659668,run=nginx-deployment2 [root@k8s-master k8s]# kubectl get rs -o wide NAME DESIRED CURRENT READY AGE CONTAINER(S) IMAGE(S) SELECTOR nginx-deployment2-3516246156 0 0 0 7m nginx-deployment2 192.168.0.136:5000/nginx:1.15 pod-template-hash=3516246156,run=nginx-deployment2 nginx-deployment2-3766659668 5 5 5 10s nginx-deployment2 192.168.0.136:5000/nginx:latest pod-template-hash=3766659668,run=nginx-deployment2
#升级版本测试
[root@k8s-master k8s]# kubectl get pods -o wide NAME READY STATUS RESTARTS AGE IP NODE nginx-deployment2-3766659668-2dl98 1/1 Running 0 18s 172.16.47.4 k8s-node-2 nginx-deployment2-3766659668-57p0w 1/1 Running 0 21s 172.16.73.5 k8s-node-1 nginx-deployment2-3766659668-c1lx6 1/1 Running 0 19s 172.16.47.3 k8s-node-2 nginx-deployment2-3766659668-f9rc6 1/1 Running 0 22s 172.16.47.5 k8s-node-2 nginx-deployment2-3766659668-pdz98 1/1 Running 0 22s 172.16.73.4 k8s-node-1 [root@k8s-master k8s]# curl -I 172.16.73.5 HTTP/1.1 200 OK Server: nginx/1.17.3 Date: Thu, 22 Aug 2019 08:39:34 GMT Content-Type: text/html Content-Length: 612 Last-Modified: Tue, 13 Aug 2019 08:50:00 GMT Connection: keep-alive ETag: "5d5279b8-264" Accept-Ranges: bytes [root@k8s-master k8s]#
#查看升级状态
[root@k8s-master ~]# kubectl rollout status deployment nginx-deployment2 deployment "nginx-deployment2" successfully rolled out
#查看升级历史
[root@k8s-master ~]# kubectl rollout history deployment nginx-deployment2 deployments "nginx-deployment2" REVISION CHANGE-CAUSE 1 kubectl run nginx-deployment2 --image=192.168.0.136:5000/nginx:1.15 --replicas=5 --record 2 kubectl set image deploy nginx-deployment2 nginx-deployment2=192.168.0.136:5000/nginx:latest [root@k8s-master ~]#
#查看历史版本详情
[root@k8s-master ~]# kubectl rollout history deployment nginx-deployment2 --revision=2 deployments "nginx-deployment2" with revision #2 Labels: pod-template-hash=3766659668 run=nginx-deployment2 Annotations: kubernetes.io/change-cause=kubectl set image deploy nginx-deployment2 nginx-deployment2=192.168.0.136:5000/nginx:latest Containers: nginx-deployment2: Image: 192.168.0.136:5000/nginx:latest Port: Volume Mounts: <none> Environment Variables: <none> No volumes.
3.5 回滚pod镜像版本
#查看历史版本
[root@k8s-master ~]# kubectl rollout history deployment nginx-deployment2 deployments "nginx-deployment2" REVISION CHANGE-CAUSE 3 kubectl set image deploy nginx-deployment2 nginx-deployment2=192.168.0.136:5000/nginx:1.15 4 kubectl set image deploy nginx-deployment2 nginx-deployment2=192.168.0.136:5000/nginx:latest [root@k8s-master ~]# kubectl get rs -o wide NAME DESIRED CURRENT READY AGE CONTAINER(S) IMAGE(S) SELECTOR nginx-deployment2-3516246156 0 0 0 22m nginx-deployment2 192.168.0.136:5000/nginx:1.15 pod-template-hash=3516246156,run=nginx-deployment2 nginx-deployment2-3766659668 3 3 3 15m nginx-deployment2 192.168.0.136:5000/nginx:latest pod-template-hash=3766659668,run=nginx-deployment2 [root@k8s-master ~]# kubectl get pods -o wide NAME READY STATUS RESTARTS AGE IP NODE nginx-deployment2-3766659668-1ff9k 1/1 Running 0 45s 172.16.73.3 k8s-node-1 nginx-deployment2-3766659668-7v64h 1/1 Running 0 45s 172.16.47.3 k8s-node-2 nginx-deployment2-3766659668-ftvxg 1/1 Running 0 43s 172.16.47.4 k8s-node-2 [root@k8s-master ~]# curl -I 172.16.73.3 HTTP/1.1 200 OK Server: nginx/1.17.3 Date: Thu, 22 Aug 2019 08:54:46 GMT Content-Type: text/html Content-Length: 612 Last-Modified: Tue, 13 Aug 2019 08:50:00 GMT Connection: keep-alive ETag: "5d5279b8-264" Accept-Ranges: bytes [root@k8s-master ~]#
#执行回滚
[root@k8s-master ~]# kubectl rollout undo deployment nginx-deployment2 --to-revision=3 deployment "nginx-deployment2" rolled back [root@k8s-master ~]#
#检查回滚结果
[root@k8s-master ~]# kubectl get deployment nginx-deployment2 NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE nginx-deployment2 3 3 3 3 25m [root@k8s-master ~]# kubectl get rs -o wide NAME DESIRED CURRENT READY AGE CONTAINER(S) IMAGE(S) SELECTOR nginx-deployment2-3516246156 3 3 3 26m nginx-deployment2 192.168.0.136:5000/nginx:1.15 pod-template-hash=3516246156,run=nginx-deployment2 nginx-deployment2-3766659668 0 0 0 18m nginx-deployment2 192.168.0.136:5000/nginx:latest pod-template-hash=3766659668,run=nginx-deployment2
#回滚版本测试
[root@k8s-master ~]# kubectl get pods -o wide NAME READY STATUS RESTARTS AGE IP NODE nginx-deployment2-3516246156-0x490 1/1 Running 0 51s 172.16.47.2 k8s-node-2 nginx-deployment2-3516246156-827rs 1/1 Running 0 49s 172.16.73.4 k8s-node-1 nginx-deployment2-3516246156-k09ch 1/1 Running 0 51s 172.16.73.2 k8s-node-1 [root@k8s-master ~]# curl -I 172.16.73.4 HTTP/1.1 200 OK Server: nginx/1.15.12 Date: Thu, 22 Aug 2019 08:58:08 GMT Content-Type: text/html Content-Length: 612 Last-Modified: Tue, 16 Apr 2019 13:08:19 GMT Connection: keep-alive ETag: "5cb5d3c3-264" Accept-Ranges: bytes [root@k8s-master ~]#
4 生产建议--record
--record参数记录执行的操作步骤和相关信息生产建议采用这种方式创建deployment
kubectl run nginx --image=192.168.0.136:5000/nginx:1.15 --replicas=5 --record