K8s教程之扩缩容和灰度发布
[root@master ~]# kubectl scale --replicas=5 deployment myapp
deployment.extensions/myapp scaled
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
client 1/1 Running 0 48m
myapp-848b5b879b-6kqp2 1/1 Running 0 14m
myapp-848b5b879b-f99n4 1/1 Running 0 36s
myapp-848b5b879b-gcx2r 1/1 Running 0 36s
myapp-848b5b879b-hd5l4 1/1 Running 0 36s
myapp-848b5b879b-vg8sm 1/1 Running 0 14m
nginx-deploy-5b595999-xgh9r 1/1 Running 0 36m
[root@master ~]# kubectl scale --replicas=3 deployment myapp
deployment.extensions/myapp scaled
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
client 1/1 Running 0 48m
myapp-848b5b879b-6kqp2 1/1 Running 0 14m
myapp-848b5b879b-f99n4 1/1 Running 0 49s
myapp-848b5b879b-hd5l4 0/1 Terminating 0 49s
myapp-848b5b879b-vg8sm 1/1 Running 0 14m
nginx-deploy-5b595999-xgh9r 1/1 Running 0 37m
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
client 1/1 Running 0 49m
myapp-848b5b879b-6kqp2 1/1 Running 0 15m
myapp-848b5b879b-f99n4 1/1 Running 0 1m
myapp-848b5b879b-vg8sm 1/1 Running 0 15m
nginx-deploy-5b595999-xgh9r 1/1 Running 0 37m
实现命令: kubectl set image (-f FILENAME | TYPE NAME) CONTAINER_NAME_1=CONTAINER_IMAGE_1 ... CONTAINER_NAME_N=CONTAINER_IMAGE_N
[options]
[root@master ~]# kubectl describe pod myapp-848b5b879b-6kqp2
Name: myapp-848b5b879b-6kqp2
Namespace: default
Priority: 0
PriorityClassName: <none>
Node: node1/192.168.100.238
Start Time: Wed, 26 Sep 2018 16:34:16 +0800
Labels: pod-template-hash=4046164356
run=myapp
Annotations: <none>
Status: Running
IP: 10.244.1.4
Controlled By: ReplicaSet/myapp-848b5b879b
Containers:
myapp:
Container ID: docker://01867440a77bfe62a4ccfce559104852d79a5885505a78370ae2c1bfe2f08f4c
Image: ikubernetes/myapp:v1
Image ID: docker-pullable://ikubernetes/myapp@sha256:9c3dc30b5219788b2b8a4b065f548b922a34479577befb54b03330999d30d513
Port: <none>
Host Port: <none>
State: Running
找到容器名
[root@master ~]# kubectl set image deployment myapp myapp=ikubernetes/myapp:v2
deployment.extensions/myapp image updated
[root@master ~]# kubectl rollout status deployment myapp
Waiting for deployment "myapp" rollout to finish: 1 old replicas are pending termination...
Waiting for deployment "myapp" rollout to finish: 1 old replicas are pending termination...
deployment "myapp" successfully rolled out
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>
Hello MyApp | Version: v2 | <a href="hostname.html">Pod Name</a>
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>
Hello MyApp | Version: v2 | <a href="hostname.html">Pod Name</a>
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>
Hello MyApp | Version: v2 | <a href="hostname.html">Pod Name</a>
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>
Hello MyApp | Version: v2 | <a href="hostname.html">Pod Name</a>
- 知道滚动升级完成,则所有的版本都改为v2
- 查看pods名称则会发现pods名称都改变了
未升级前
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
client 1/1 Running 0 49m
myapp-848b5b879b-6kqp2 1/1 Running 0 15m
myapp-848b5b879b-f99n4 1/1 Running 0 1m
myapp-848b5b879b-vg8sm 1/1 Running 0 15m
nginx-deploy-5b595999-xgh9r 1/1 Running 0 37m
升级后
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
client 1/1 Running 0 56m
myapp-74c94dcb8c-btnq2 1/1 Running 0 4m
myapp-74c94dcb8c-fxxpr 1/1 Running 0 4m
myapp-74c94dcb8c-gv2bz 1/1 Running 0 3m
nginx-deploy-5b595999-xgh9r 1/1 Running 0 45m
kubectl rollout undo (TYPE NAME | TYPE/NAME) [flags] [options]
若未指定版本,则回滚至上一次更新的状态
[root@master ~]# kubectl rollout undo deployment myapp
deployment.extensions/myapp
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
client 1/1 Running 0 1h
myapp-74c94dcb8c-btnq2 1/1 Running 0 8m
myapp-74c94dcb8c-fxxpr 1/1 Running 0 8m
myapp-74c94dcb8c-gv2bz 1/1 Running 0 8m
myapp-848b5b879b-zjzf9 0/1 ContainerCreating 0 8s
nginx-deploy-5b595999-xgh9r 1/1 Running 0 49m
Hello MyApp | Version: v2 | <a href="hostname.html">Pod Name</a>
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>
Hello MyApp | Version: v2 | <a href="hostname.html">Pod Name</a>
Hello MyApp | Version: v2 | <a href="hostname.html">Pod Name</a>
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>
- 因为生产环境中肯定不会去简单的run一个服务出来,这样做不到完全定制,所以这里只是简单去讨论k8s的一些特性
- k8s还支持自动扩缩容,这部分需要监控系统,基于资源使用率进行弹性扩展,这里先不去研究