Kubernetes的Deployment可以轻松地进行滚动更新,但是如果出现了问题,可能需要回滚到之前的版本。Deployment提供了回滚功能,可以让我们轻松地回滚到之前的版本。
回滚Deployment
Deployment的回滚功能使用kubectl rollout undo
命令来实现,我们可以使用以下命令回滚到上一次成功的版本:
kubectl rollout undo deployment/nginx-deployment
这将回滚到上一次成功的版本,如果没有成功的版本,则回滚到当前版本之前的版本。
我们还可以使用以下命令回滚到特定的版本:
代码语言:javascript复制kubectl rollout undo deployment/nginx-deployment --to-revision=2
这将回滚到指定的版本,版本号为2。
回滚操作会在后台进行,我们可以使用以下命令来查看回滚进度:
代码语言:javascript复制kubectl rollout status deployment/nginx-deployment
这会输出类似于以下内容的信息:
代码语言:javascript复制Waiting for deployment "nginx-deployment" rollout to finish: 1 out of 3 new replicas have been updated...
Waiting for deployment "nginx-deployment" rollout to finish: 1 out of 3 new replicas have been updated...
Waiting for deployment "nginx-deployment" rollout to finish: 1 out of 3 new replicas have been updated...
Waiting for deployment "nginx-deployment" rollout to finish: 2 out of 3 new replicas have been updated...
Waiting for deployment "nginx-deployment" rollout to finish: 2 out of 3 new replicas have been updated...
Waiting for deployment "nginx-deployment" rollout to finish: 2 out of 3 new replicas have been updated...
deployment "nginx-deployment" successfully rolled out
这表示回滚正在进行中,需要等待一段时间才能完成。
当回滚操作完成后,我们可以使用以下命令查看Deployment的更新历史记录:
代码语言:javascript复制kubectl rollout history deployment/nginx-deployment
这将输出类似于以下内容的信息:
代码语言:javascript复制deployment.apps/nginx-deployment
REVISION CHANGE-CAUSE
2 kubectl set image deployment/nginx-deployment nginx=nginx:1.17 --record=true
3 kubectl set image deployment/nginx-deployment nginx=nginx:1.16 --record=true
我们可以看到Deployment已经回滚到了第2个版本,更新历史记录中显示了回滚操作。