使用kubernetes 進行升級的時候並不需要停止業務,kubectl 支持滾動升級的方式,每次更新一個pod,而不是同時刪除整個服務。
准備實驗素材
apiVersion: apps/v1
kind: Deployment
metadata:
name: hello-nginx
spec:
selector:
matchLabels:
name: nginx
replicas: 3
template:
metadata:
labels:
name: nginx
spec:
containers:
- name: hello-nginx
image: nginx:1.7.9
imagePullPolicy: IfNotPresent
ports:
- containerPort: 80
kubectl apply -f hello-nginx.yml --record
更新鏡像的語法
可以不用使用yml配置文件, 直接替換鏡像版本
# kubectl set image deployment <deploymentName> <containerName>=<image>
kubectl --kubeconfig config-lego-test set image deployment/hello-nginx hello-nginx=nginx:1.9.2
我們可以通過設置docker:lastTag的方式, 回滾到上一個鏡像. 但k8s本身也支持版本記錄和回滾.
查看發布歷史
[root@localhost .kube]# kubectl --kubeconfig config-my rollout history deployment hello-nginx
deployment.extensions/hello-nginx
REVISION CHANGE-CAUSE
1 kubectl apply --kubeconfig=config-lego-test --filename=hello-nginx.yml --record=true
2 kubectl apply --kubeconfig=config-lego-test --filename=hello-nginx.yml --record=true
3 kubectl apply --kubeconfig=config-lego-test --filename=hello-nginx.yml --record=true
4 kubectl apply --kubeconfig=config-lego-test --filename=hello-nginx.yml --record=true
[root@localhost .kube]# kubectl --kubeconfig config-my rollout history deployment hello-nginx --revision=4
deployment.extensions/hello-nginx with revision #4
Pod Template:
Labels: app=hello-nginx
pod-template-hash=6d5c95fcc5
Annotations: kubernetes.io/change-cause: kubectl apply --kubeconfig=config-lego-test --filename=hello-nginx.yml --record=true
Containers:
hello-nginx:
Image: nginx:1.9.2
Port: 8080/TCP
Host Port: 0/TCP
Requests:
cpu: 250m
memory: 512Mi
Environment: <none>
Mounts: <none>
Volumes: <none>
回滾到上一個版本
比如, 剛才查看了最近的幾條發布歷史記錄, 現在回滾到上一個版本, 即revision=3.
[root@localhost .kube]# kubectl --kubeconfig config-lego-test rollout undo deployment hello-nginx
deployment.extensions/hello-nginx rolled back
[root@localhost .kube]#
[root@localhost .kube]# kubectl --kubeconfig config-lego-test rollout history deployment hello-nginx
deployment.extensions/hello-nginx
REVISION CHANGE-CAUSE
1 kubectl apply --kubeconfig=config-lego-test --filename=hello-nginx.yml --record=true
2 kubectl apply --kubeconfig=config-lego-test --filename=hello-nginx.yml --record=true
4 kubectl apply --kubeconfig=config-lego-test --filename=hello-nginx.yml --record=true
5 kubectl apply --kubeconfig=config-lego-test --filename=hello-nginx.yml --record=true
可以發現, revision=3沒了, 多個了一個revison=5. describe一下, 可以看到, 確實是第3個版本的鏡像.
回滾指定版本
除了上面直接回滾到上一次, 也可以指定具體某個版本. 比如回滾到revision=1.
kubectl rollout undo deployment/hello-nginx --to-revision=1
ps: deployment/hello-nginx 表示 deployment hello-nginx
查看回滾狀態
[root@localhost .kube]# kubectl --kubeconfig config-lego-test rollout status deployment hello-nginx
deployment "hello-nginx" successfully rolled out