千家信息网

kubernetes 控制器

发表于:2025-01-31 作者:千家信息网编辑
千家信息网最后更新 2025年01月31日,1.基础apiVersion: apps/v1kind: Deploymentmetadata: name: myapp-deploy namespace: defaultspec: repli
千家信息网最后更新 2025年01月31日kubernetes 控制器

1.基础

apiVersion: apps/v1kind: Deploymentmetadata:  name: myapp-deploy  namespace: defaultspec:  replicas: 2  selector:    matchLabels:      app: myapp      release: dev  template:    metadata:      labels:        app: myapp        release: dev    spec:      containers:      - name: myapp-rongqi        image: ikubernetes/myapp:v1

2.滚动升级

vim de.yaml #修改yaml里的镜像版本

 apiVersion: apps/v1kind: Deploymentmetadata:  name: myapp-deploy  namespace: defaultspec:  replicas: 5  selector:    matchLabels:      app: myapp      release: dev  strategy:    rollingUpdate:    #设置策略为滚动更新,还有一个重建更新,全部一起结束一起新建 不友好这里就不介绍了      maxSurge: 1      #最大可超出预设值1个容器      maxUnavailable: 0      #最大可有0个容器不可用 ,整体表示更新时可同时可有6个容器,必须保证有5个容器可用  template:    metadata:      labels:        app: myapp        release: dev    spec:      containers:      - name: myapp-rongqi        image: ikubernetes/myapp:v1

kubectl apply -f de.yaml #用apply更新配置

[root@cs25 ~]# kubectl get pod  -l app=myapp -wNAME                            READY   STATUS    RESTARTS   AGEmyapp-deploy-576d479bcc-bt7hz   1/1     Running   0          47mmyapp-deploy-576d479bcc-bzqwv   1/1     Running   0          11mmyapp-deploy-576d479bcc-hf6kb   1/1     Running   0          47mmyapp-deploy-576d479bcc-hgb6w   1/1     Running   0          11mmyapp-deploy-576d479bcc-vztqp   1/1     Running   0          11mmyapp-deploy-7f969b64fb-2frdn   0/1     Pending   0          0smyapp-deploy-576d479bcc-vztqp   1/1     Terminating   0          12mmyapp-deploy-7f969b64fb-6lhjw   0/1     Pending       0          0smyapp-deploy-7f969b64fb-2frdn   0/1     Pending       0          0smyapp-deploy-7f969b64fb-6lhjw   0/1     Pending       0          0smyapp-deploy-7f969b64fb-5ms8f   0/1     Pending       0          0smyapp-deploy-7f969b64fb-5ms8f   0/1     Pending       0          0smyapp-deploy-7f969b64fb-6lhjw   0/1     ContainerCreating   0          1smyapp-deploy-7f969b64fb-2frdn   0/1     ContainerCreating   0          1smyapp-deploy-7f969b64fb-5ms8f   0/1     ContainerCreating   0          0smyapp-deploy-7f969b64fb-2frdn   0/1     ContainerCreating   0          35smyapp-deploy-7f969b64fb-5ms8f   0/1     ContainerCreating   0          35smyapp-deploy-7f969b64fb-6lhjw   0/1     ContainerCreating   0          36smyapp-deploy-576d479bcc-vztqp   0/1     Terminating         0          12mmyapp-deploy-576d479bcc-vztqp   0/1     Terminating         0          12mmyapp-deploy-576d479bcc-vztqp   0/1     Terminating         0          12mmyapp-deploy-7f969b64fb-5ms8f   1/1     Running             0          43smyapp-deploy-7f969b64fb-2frdn   1/1     Running             0          47smyapp-deploy-7f969b64fb-6lhjw   1/1     Running             0          51smyapp-deploy-576d479bcc-hgb6w   1/1     Terminating         0          13mmyapp-deploy-576d479bcc-bzqwv   1/1     Terminating         0          13mmyapp-deploy-576d479bcc-bt7hz   1/1     Terminating         0          49mmyapp-deploy-7f969b64fb-gprvk   0/1     Pending             0          0smyapp-deploy-7f969b64fb-fwdbr   0/1     Pending             0          0smyapp-deploy-7f969b64fb-fwdbr   0/1     Pending             0          1smyapp-deploy-7f969b64fb-gprvk   0/1     Pending             0          1smyapp-deploy-7f969b64fb-fwdbr   0/1     ContainerCreating   0          1smyapp-deploy-7f969b64fb-gprvk   0/1     ContainerCreating   0          1smyapp-deploy-576d479bcc-hgb6w   0/1     Terminating         0          13mmyapp-deploy-576d479bcc-bzqwv   0/1     Terminating         0          13mmyapp-deploy-7f969b64fb-fwdbr   0/1     ContainerCreating   0          18smyapp-deploy-7f969b64fb-gprvk   0/1     ContainerCreating   0          19smyapp-deploy-576d479bcc-hgb6w   0/1     Terminating         0          13mmyapp-deploy-576d479bcc-bt7hz   0/1     Terminating         0          49mmyapp-deploy-576d479bcc-bzqwv   0/1     Terminating         0          13mmyapp-deploy-7f969b64fb-gprvk   1/1     Running             0          20smyapp-deploy-7f969b64fb-fwdbr   1/1     Running             0          20s

kubectl get rs -o wide #使用命令查看

可以看到当前镜像版本已经换成了v2 且模板也变了,之前的v1版本还保这方便回滚,yaml文件内容修改一次就会当做一次更新,最后一串数字就是通过yaml文件计算出来的
注意: deployment的pod模板被更改时才会创建新的修订版本,例如更新模板标签或者容器镜像可以触发滚动更新, 其它操作例如扩展副本数将不会触发deployment的更新操作

金丝雀发布

kubectl set image deploy myapp-deploy myapp-rongqi=ikubernetes/myapp:v3 && kubectl rollout pause deploy myapp-deploy
#kubectl set image deploy "控制器Name" "容器name"=ikubernetes/myapp:v3 && kubectl rollout pause deploy "控制器Name"
#原理就是先升级一个容器的版本,执行pause 暂停升级任务,待升级的那个容器经过真实用户测试后无错后解除暂停

kubectl get rs -o wide #查看控制器能看到,有1个v3版,5个v2版本,出现6个的原因是我滚动策略定义了升级可允许存在6个
kubectl rollout resume deploy myapp-deploy
#解除暂停

#解除暂停后能看到,v2版本在一点点的关闭向v3版转移,最终v2全部关闭,v3版5个容器全部正常上线

回滚

 kubectl rollout history deployment/nginx-deployment #查看升级历史记录 kubectl rollout history deployment/nginx-deployment --revision=2 #查看某个升级历史记录的的详细信息kubectl rollout undo deployment/nginx-deployment#回滚到上一个版本kubectl rollout undo deployment/nginx-deployment --to-revision=2#回滚到指定版本
0