1.创建待测试的Deployment。 echo " --- # Deployment apiVersion: apps/v1 kind: Deployment metadata: name: rollback-test labels: app: rollback-test spec: replicas: 1 selector: matchLabels: app: rollback-test template: metadata: labels: app: rollback-test spec: containers: - name: rollback-test image: registry.k8s.io/ingress-nginx/e2e-test-echo@sha256:6fc5aa2994c86575975bb20a5203651207029a0d28e3f491d8a127d08baadab4 ports: - containerPort: 80 env: - name: NODE_NAME valueFrom: fieldRef: fieldPath: spec.nodeName - name: POD_NAME valueFrom: fieldRef: fieldPath: metadata.name - name: POD_NAMESPACE valueFrom: fieldRef: fieldPath: metadata.namespace - name: POD_IP valueFrom: fieldRef: fieldPath: status.podIP - name: VERSION value: "v1" " | kubectl apply -f - 2.获取POD的IP,并执行curl请求,查看响应结果。
#获取POD的IP kubectl get pod -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES rollback-test-56cd4bc4bc-m4vm4 1/1 Running 0 14s 10.43.0.12 192.168.1.20 <none> <none> #执行curl语句 curl 10.43.0.12|grep --regexp="\"VERSION\":\"\w\+\"" -o3.更改版本(VERSION)环境变量,并重复执行步骤2。
"VERSION":"v1"
kubectl patch deployment rollback-test -p '{"spec":{"template":{"spec":{"containers":[{"name":"rollback-test", "env":[{"name": "VERSION", "value": "v2"}] }] }}}}' kubectl get pod -o wide [root@VM-16-12-tlinux ~]# kubectl get pod -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES rollback-test-5fc7d8dbcb-67c2c 1/1 Running 0 23s 10.43.0.13 192.168.1.20 <none> <none>4.重复步骤3,依次将版本设置为v3、v4、v5。
curl 10.43.0.13|grep --regexp="\"VERSION\":\"\w\+\"" -o
"VERSION":"v2"
kubectl patch deployment rollback-test -p '{"spec":{"template":{"spec":{"containers":[{"name":"rollback-test", "env":[{"name": "VERSION", "value": "v3"}] }] }}}}' deployment.apps/rollback-test patched kubectl get pod -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES rollback-test-79f9f664b5-2v9dw 1/1 Running 0 80s 10.43.0.14 192.168.1.20 <none> <none> curl 10.43.0.13|grep --regexp="\"VERSION\":\"\w\+\"" -o "VERSION":"v3"kubectl patch deployment rollback-test -p '{"spec":{"template":{"spec":{"containers":[{"name":"rollback-test", "env":[{"name": "VERSION", "value": "v4"}] }] }}}}' deployment.apps/rollback-test patched kubectl get pod -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES rollback-test-58d697cd74-vdklk 1/1 Running 0 15s 10.43.0.15 192.168.1.20 <none> <none> curl 10.43.0.15|grep --regexp="\"VERSION\":\"\w\+\"" -o "VERSION":"v4"
kubectl patch deployment rollback-test -p '{"spec":{"template":{"spec":{"containers":[{"name":"rollback-test", "env":[{"name": "VERSION", "value": "v5"}] }] }}}}' deployment.apps/rollback-test patched kubectl get pod -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES rollback-test-79445d67bc-n6hv6 1/1 Running 0 6s 10.43.0.16 192.168.1.20 <none> <none> curl 10.43.0.16|grep --regexp="\"VERSION\":\"\w\+\"" -o "VERSION":"v5" 5.查看历史版本并恢复到上一个版本。
kubectl rollout history deployment rollback-test deployment.apps/rollback-test REVISION CHANGE-CAUSE 1 <none> 2 <none> 3 <none> 4 <none> 5 <none> kubectl rollout undo deployment rollback-test deployment.apps/rollback-test rolled back kubectl get pod -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES rollback-test-58d697cd74-xg45g 1/1 Running 0 6s 10.43.0.17 192.168.1.20 <none> <none> curl 10.43.0.17|grep --regexp="\"VERSION\":\"\w\+\"" -o "VERSION":"v4" kubectl rollout history deployment rollback-test deployment.apps/rollback-test REVISION CHANGE-CAUSE 1 <none> 2 <none> 3 <none> 5 <none> 6 <none>6.恢复到指定版本。
kubectl rollout undo deployment rollback-test --to-revision 2 deployment.apps/rollback-test rolled back kubectl get pod -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES rollback-test-5fc7d8dbcb-5j95x 1/1 Running 0 7s 10.43.0.18 192.168.1.20 <none> <none> curl 10.43.0.18|grep --regexp="\"VERSION\":\"\w\+\"" -o "VERSION":"v2"标签:kubectl,回滚,rollback,name,kubernetes,VERSION,版本,deployment,test From: https://www.cnblogs.com/brightdays/p/18380805