kubernetes学习(一) Scale应用 & 滚动更新
一、Scale应用
默认请款下应用只会运行一个副本,可通过kubectl get deployments 查看副本数。
~$ kubectl get deployments NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE blogtest 1 1 1 1 3d kubernetes-bootcamp 1 1 1 1 3d
1、使用scale增加pod数量
执行如下命令,可将副本数增加到3个。
~$ kubectl scale deployments/blogtest --replicas=3 deployment.extensions "blogtest" scaled
通过kubectl get pods 可以看到当前的Pod增加到了三个。
~$ kubectl get deployment NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE blogtest 3 3 3 1 3d kubernetes-bootcamp 1 1 1 1 3d ~$ kubectl get pods NAME READY STATUS RESTARTS AGE blogtest-7f9798c5b5-drvl8 0/1 ContainerCreating 0 8s blogtest-7f9798c5b5-gwkm9 1/1 Running 0 3d blogtest-7f9798c5b5-snppq 0/1 ContainerCreating 0 8s kubernetes-bootcamp-56cdd766d-5nj9n 1/1 Running 0 3d
每次请求发送到不同的Pod,3个副本轮询处理请求,这样就可以实现负载均衡了。
2、使用scale减少Pod数量
~$ kubectl scale deployments/blogtest --replicas=2 deployment.extensions "blogtest" scaled
二、滚动更新
1、滚动更新
当前使用版本为v1,使用以下命令将其升级到v2.
~$ curl galaxy-k8s-test-master-02:17812 Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-56cdd766d-5nj9n | v=1 ~$ kubectl set image deployments/kubernetes-bootcamp kubernetes-bootcamp=jocatalin/kubernetes-bootcamp:v2deployment.apps "kubernetes-bootcamp" image updated ~$ curl galaxy-k8s-test-master-02:17812 Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-7799cbcb86-7dnzp | v=2
滚动更新过程:v1的Pod被逐个删除,同时启动了新的v2 Pod。
2、回退更新
如果要回退到v1版本,执行kubectl rollout undo 命令。
~$ kubectl get pods NAME READY STATUS RESTARTS AGE blogtest-7f9798c5b5-gwkm9 1/1 Running 0 3d blogtest-7f9798c5b5-snppq 1/1 Running 0 48m kubernetes-bootcamp-56cdd766d-7skbz 1/1 Terminating 0 1m kubernetes-bootcamp-7799cbcb86-bpnpp 1/1 Running 0 22s ~$ curl galaxy-k8s-test-master-02:17812 Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-7799cbcb86-bpnpp | v=2 ~$ kubectl rollout undo deployments/kubernetes-bootcamp deployment.apps "kubernetes-bootcamp" ~$ kubectl get pods NAME READY STATUS RESTARTS AGE blogtest-7f9798c5b5-gwkm9 1/1 Running 0 3d blogtest-7f9798c5b5-snppq 1/1 Running 0 49m kubernetes-bootcamp-56cdd766d-jjzqm 1/1 Running 0 13s kubernetes-bootcamp-7799cbcb86-bpnpp 1/1 Terminating 0 57s ~$ curl galaxy-k8s-test-master-02:17812 Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-56cdd766d-jjzqm | v=1