Kubernetes基本概念的学习-Deployment
一、Deployment控制器
Deployment是最常用的K8s负载控制器(Workload Controllers),是K8s的一个抽象概念,用于更高层级对象,部署和管理Pod。
Deployment的主要功能:
管理Pod、即应用程序;
具有上线部署、副本设定、滚动升级、回滚等功能
用于部署无状态的服务,这个最常用的控制器。一般用于管理维护企业内部无状态的微服务。比如confingserver、zuul、springboot。他可以管理多个副本的Pod,实现无缝的迁移,自动扩容、缩容、自动灾难恢复。
用命令创建一个Deployment:
[root@master01 ~]# kubectl create deployment nginx --image=nginx:1.15.2 deployment.apps/nginx created
导出这个deployment对应的yaml文件
[root@master01 ~]# kubectl get deployment -o yaml > nginx-deploy.yaml
1.1、验证滚动升级 ,实现零停机发布,用户无感知
创建新的pod及service
apiVersion: apps/v1 kind: Deployment metadata: name: web spec: replicas: 3 selector: matchLabels: app: nginx template: metadata: labels: app: nginx spec: containers: - name: nginx image: nginx:1.16 --- apiVersion: v1 kind: Service metadata: name: web spec: selector: app: nginx ports: - protocol: TCP port: 80 targetPort: 80 type: NodePort
查看创建的pod及service
[root@master01 ~]# kubectl get pod,service NAME READY STATUS RESTARTS AGE pod/web-6d4cf56db6-jclts 1/1 Running 0 45s pod/web-6d4cf56db6-strfs 1/1 Running 0 45s pod/web-6d4cf56db6-xtrrs 1/1 Running 0 45s NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE service/kubernetes ClusterIP 10.1.0.1 <none> 443/TCP 7d2h service/web NodePort 10.1.129.126 <none> 80:31151/TCP 45s
修改yaml中nginx的版本
spec: containers: - name: nginx image: nginx:1.17
重新执行yaml
[root@master01 ~]# kubectl apply -f dema.yaml deployment.apps/web configured service/web unchanged
观察pod滚动升级
[root@master01 ~]# kubectl get pod NAME READY STATUS RESTARTS AGE web-6d4cf56db6-jclts 1/1 Running 0 41m web-6d4cf56db6-strfs 1/1 Running 0 41m web-6d4cf56db6-xtrrs 1/1 Running 0 41m web-db749865c-dg2dh 0/1 ContainerCreating 0 5s [root@master01 ~]# kubectl get pod NAME READY STATUS RESTARTS AGE web-6d4cf56db6-jclts 1/1 Running 0 41m web-6d4cf56db6-strfs 1/1 Running 0 41m web-6d4cf56db6-xtrrs 1/1 Running 0 41m web-db749865c-dg2dh 0/1 ContainerCreating 0 14s [root@master01 ~]# kubectl get pod NAME READY STATUS RESTARTS AGE web-6d4cf56db6-strfs 0/1 Terminating 0 42m web-6d4cf56db6-xtrrs 1/1 Running 0 42m web-db749865c-dg2dh 1/1 Running 0 54s web-db749865c-k5m5t 0/1 ContainerCreating 0 3s web-db749865c-q2wlt 1/1 Running 0 21s [root@master01 ~]# kubectl get pod NAME READY STATUS RESTARTS AGE web-db749865c-dg2dh 1/1 Running 0 60s web-db749865c-k5m5t 1/1 Running 0 9s web-db749865c-q2wlt 1/1 Running 0 27s
用参数describe查看升级信息
[root@master01 ~]# kubectl describe deployment web
另外一种方法使用kubectl
[root@master01 ~]# kubectl set image deployment web nginx=nginx:1.18 deployment.apps/web image updated
1.2 应用发布失败回滚
查看之前升级过的版本, CHANGE-CAUSE,可以在升级版本的时候将参数--record=true
[root@master01 ~]# kubectl rollout history deployment web deployment.apps/web REVISION CHANGE-CAUSE 1 <none> 3 <none> 4 <none>
使用rollout undo命令回滚
[root@master01 ~]# kubectl rollout undo deployment web
deployment.apps/web rolled back
回滚到指定的版本:
[root@master01 ~]# kubectl rollout undo deployment web --to-revision=5
deployment.apps/web rolled back
1.3 扩容和缩容
修改yaml文件的replicas,改为想要的副本数目;
apiVersion: apps/v1 kind: Deployment metadata: name: web spec: replicas: 3
然后重新apply
[root@master01 ~]# kubectl apply -f dema.yaml deployment.apps/web configured service/web unchanged
查看结果
[root@master01 ~]# kubectl get pod -w NAME READY STATUS RESTARTS AGE web-db749865c-48jds 1/1 Running 1 14h web-db749865c-c279p 1/1 Running 1 14h web-db749865c-gdszd 1/1 Running 1 14h web-db749865c-mh2ml 1/1 Running 0 3m5s
第二种方法是用户命令行
[root@master01 ~]# kubectl scale deployment web --replicas=2 deployment.apps/web scaled
1.4 Deployment更新的停止和启动
停止更新,执行更新命令后不会立即生效,
[root@master01 ~]# kubectl rollout pause deployment web
启动更新,前面执行的所有更新命令一次开始执行生效
[root@master01 ~]# kubectl rollout pause deployment web
1.5应用下线
通过命令删除文件
[root@master01 ~]# kubectl delete -f dema.yaml deployment.apps "web" deleted service "web" deleted
第二种方法是删除deployment
[root@master01 ~]# kubectl delete deployment web deployment.apps "web" deleted