成功后
kubectl rollout restart deployment/foo
kubectl rollout undo deployment/foo
或
kubectl rollout undo deployment/foo --to-revision=x
不起作用。我的意思是,pod被新的替换,并且创建了一个新的版本,可以通过
检查。kubectl rollout history deployment foo
但是当我调用服务时,回滚没有效果
我还试图删除imagePullPolicy: Always
,猜测它总是在回滚中拉,没有成功,因为可能一件事与另一件事无关。
编辑:测试很简单,我改变了http api的健康检查路由,在json中返回不同的东西,它没有。
编辑:
可能是一个打字错误,但不是:我正在执行... undo deployment/foo ...
,现在尝试... undo deployment foo ...
。它也给了我deployment.apps/foo rolled back
,但没有改变在生活系统。
更多测试:我再次更改了我的api路由,以测试如果我对每个先前的版本逐一执行rollout undo会发生什么。我申请了最后10次修改,什么也没有。
为了能够回滚到以前的版本,不要忘记附加——record参数到kubectl命令,例如:
kubectl apply -f DEPLOYMENT.yaml --record
那么你应该能够看到你知道的历史:
kubectl rollout history deployment DEPLOYMENT_NAME
你的回滚将正常工作
kubectl rollout undo deployment DEPLOYMENT_NAME --to-revision=CHOOSEN_REVISION_NUMBER
小例子:
考虑我的nginx部署清单"nginx-test.yaml;:
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 1
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
让我们创建它:
❯ kubectl apply -f nginx-test.yaml --record
Flag --record has been deprecated, --record will be removed in the future
deployment.apps/nginx-deployment created
让我们检查这个部署的映像,如预期的那样来自清单:
❯ k get pod nginx-deployment-74d589986c-k9whj -o yaml | grep image:
- image: nginx
image: docker.io/library/nginx:latest
现在让我们将这个部署的映像修改为"nginx:1.21":
#"nginx=" correspond to the name of the container inside the pod create by the deployment.
❯ kubectl set image deploy nginx-deployment nginx=nginx:1.21.6
deployment.apps/nginx-deployment image updated
我们可以选择检查rollout状态:
❯ kubectl rollout status deployment nginx-deployment
deployment "nginx-deployment" successfully rolled out
我们可以使用以下命令查看rollout历史:
❯ kubectl rollout history deploy nginx-deployment
deployment.apps/nginx-deployment
REVISION CHANGE-CAUSE
1 kubectl apply --filename=nginx-test.yaml --record=true
2 kubectl apply --filename=nginx-test.yaml --record=true
让我们检查这个部署的映像,如预期的那样:
❯ k get pod nginx-deployment-66dcfc79b5-4pk7w -o yaml | grep image:
- image: nginx:1.21.6
image: docker.io/library/nginx:1.21.6
哦,不,我不喜欢这个图像!可以回滚:
❯ kubectl rollout undo deployment nginx-deployment --to-revision=1
deployment.apps/nginx-deployment rolled back
创建:
> kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
pod/nginx-deployment-66dcfc79b5-4pk7w 1/1 Running 0 3m41s 10.244.3.4 so-cluster-1-worker3 <none> <none>
pod/nginx-deployment-74d589986c-m2htr 0/1 ContainerCreating 0 13s <none> so-cluster-1-worker2 <none> <none>
几秒钟后:
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
pod/nginx-deployment-74d589986c-m2htr 1/1 Running 0 23s 10.244.4.10 so-cluster-1-worker2 <none> <none>
你可以看到它起作用了:
❯ k get pod nginx-deployment-74d589986c-m2htr -o yaml | grep image:
- image: nginx
image: docker.io/library/nginx:latest
让我们重新检查历史记录:
❯ kubectl rollout history deploy nginx-deployment
deployment.apps/nginx-deployment
REVISION CHANGE-CAUSE
1 kubectl apply --filename=nginx-test.yaml --record=true
2 kubectl apply --filename=nginx-test.yaml --record=true
您可以使用"kubernetes.io/change-cause"更改rollout历史的change-cause;注释:
❯ kubectl annotate deploy nginx-deployment kubernetes.io/change-cause="update image from 1.21.6 to latest" --reco
rd
Flag --record has been deprecated, --record will be removed in the future
deployment.apps/nginx-deployment annotated
让我们重新检查历史记录:
❯ kubectl rollout history deploy nginx-deployment
deployment.apps/nginx-deployment
REVISION CHANGE-CAUSE
2 kubectl apply --filename=nginx-test.yaml --record=true
3 update image from 1.21.6 to latest
让我们描述一下部署:
❯ kubectl describe deploy nginx-deploy
Name: nginx-deployment
Namespace: so-tests
CreationTimestamp: Fri, 06 May 2022 00:56:09 -0300
Labels: app=nginx
Annotations: deployment.kubernetes.io/revision: 3
kubernetes.io/change-cause: update image from latest to latest
...
希望这对你有所帮助,我猜。
我也遇到过类似的情况,这更多的是我自己的错误,而不是我的K8s清单的配置问题。
在我的Docker镜像创建工作流程中,我忘记更新Docker镜像的版本。我有一个GitHub动作工作流,将图像推送到DockerHub。未更新映像版本,用应用程序中的最新更改覆盖当前映像。
然后我的kubectl rollout undo
命令正在拉取正确的图像,但是该图像在应用程序中具有最近的更改。换句话说,图像1.1与1.0相同。执行undo命令对应用程序状态没有影响。
愚蠢的错误,但这是我的经验,以防帮助别人。