kubernetes deployment升级和回滚

时间:2023-12-16 13:21:08

1.创建deployment pod

kubectl run mynginx --image=docker.io/nginx:1.13 --replicas= --record

准备svc文件

apiVersion: v1
kind: Service
metadata:
name: nginxsvc
spec:
type: NodePort
ports:
- port:
nodePort:
selector:
run: mynginx #修改为需要关联pod的lable参数,'kubectl describe deploy/mynginx'查看

kubernetes deployment升级和回滚

创建nginx-svc

kubectl create -f nginx-svc.yaml
[root@master01 ~]# kubectl get all -o wide
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
deploy/mynginx 14m NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
svc/kubernetes 10.254.0.1 <none> /TCP 1d <none>
svc/nginxsvc 10.254.198.39 <nodes> :/TCP 9m run=mynginx NAME DESIRED CURRENT READY AGE CONTAINER(S) IMAGE(S) SELECTOR
rs/mynginx- 14m mynginx docker.io/nginx:1.13 pod-template-hash=,run=mynginx NAME READY STATUS RESTARTS AGE IP NODE
po/mynginx--0q0cg / Running 4m 172.16.43.4 192.168.29.203
po/mynginx--7g99g / Running 4m 172.16.22.3 192.168.29.202
po/mynginx--bzjn2 / Running 4m 172.16.43.3 192.168.29.203
po/mynginx--g9dd1 / Running 4m 172.16.22.4 192.168.29.202

2.升级mynginx,nginx:1.13 > nginx:1.15

kubectl set image deploy/mynginx mynginx=docker.io/nginx:1.15  #升级
[root@master01 ~]# kubectl get all -o wide
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
deploy/mynginx 35m NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
svc/kubernetes 10.254.0.1 <none> /TCP 1d <none>
svc/nginxsvc 10.254.198.39 <nodes> :/TCP 30m run=mynginx NAME DESIRED CURRENT READY AGE CONTAINER(S) IMAGE(S) SELECTOR
rs/mynginx- 35m mynginx docker.io/nginx:1.13 pod-template-hash=,run=mynginx
rs/mynginx- 2m mynginx docker.io/nginx:1.15 pod-template-hash=,run=mynginx NAME READY STATUS RESTARTS AGE IP NODE
po/mynginx--7h3hc / Running 2m 172.16.43.5 192.168.29.203
po/mynginx--cp080 / Running 2m 172.16.22.2 192.168.29.202
po/mynginx--k6kkb / Running 2m 172.16.43.4 192.168.29.203
po/mynginx--xg6cr / Running 2m 172.16.43.3 192.168.29.203

3.回滚升级

kubectl rollout history deploy/mynginx  #查看操作历史
[root@master01 ~]# kubectl rollout history deploy/mynginx
deployments "mynginx"
REVISION CHANGE-CAUSE
kubectl run mynginx --image=docker.io/nginx:1.13 --replicas= --record
kubectl set image deploy/mynginx mynginx=docker.io/nginx:1.15
kubectl rollout undo deploy/mynginx --to-revision=1  #回滚
[root@master01 ~]# kubectl rollout undo deploy/mynginx --to-revision=
deployment "mynginx" rolled back
[root@master01 ~]# kubectl get all -o wide
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
deploy/mynginx 39m NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
svc/kubernetes 10.254.0.1 <none> /TCP 1d <none>
svc/nginxsvc 10.254.198.39 <nodes> :/TCP 35m run=mynginx NAME DESIRED CURRENT READY AGE CONTAINER(S) IMAGE(S) SELECTOR
rs/mynginx- 39m mynginx docker.io/nginx:1.13 pod-template-hash=,run=mynginx
rs/mynginx- 6m mynginx docker.io/nginx:1.15 pod-template-hash=,run=mynginx NAME READY STATUS RESTARTS AGE IP NODE
po/mynginx--4z1f5 / Running 51s 172.16.43.3 192.168.29.203
po/mynginx--f1qzz / Running 52s 172.16.22.4 192.168.29.202
po/mynginx--ks19b / Running 52s 172.16.22.3 192.168.29.202
po/mynginx--xqxj6 / Running 50s 172.16.43.6 192.168.29.203
[root@master01 ~]# kubectl rollout history deploy/mynginx
deployments "mynginx"
REVISION CHANGE-CAUSE
kubectl set image deploy/mynginx mynginx=docker.io/nginx:1.15
kubectl run mynginx --image=docker.io/nginx:1.13 --replicas= --record