5分钟带你体验kubernetes集群伸缩功能

作者声明:本篇文章系本人依照真实部署过程原创,未经许可,谢绝转载。

上一篇文章介绍了kubernetes集群扩展功能,这一篇文件带你来 5分钟带你体验kubernetes集群伸缩功能

环境准备

本文中我用到了docker的的镜像文件 28code/httpd:v1.0 ,系作者基于docker的busybox镜像做的一个docker image,主要功能是提供httpd服务并显示版本和pod信息。 启动28code/httpd:v1.0镜像时候会生成文件位于 /var/www/index.html,index.html记录了版本号V1和启动pod时候的pod名称,用来提供展示版本变更和访问时候显示访问的哪个pod的显示效果。

kubernetes 集群伸缩

上次在master执行命令,部署docker镜像文件28code/httpd:v1.0,,镜像提供httpd服务,扩展为5个服务

1
2
3
4
5
6
7
8
9
10
11
12
扩展命令为
# kubectl scale --replicas=5 deployment myapp
deployment.extensions/myapp scaled

查看pod运行情况
# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
myapp-799bbcd6b4-6697m 1/1 Running 0 57s 10.244.1.25 node01.k8s.com <none> <none>
myapp-799bbcd6b4-6tqkk 1/1 Running 0 96m 10.244.2.30 node02.k8s.com <none> <none>
myapp-799bbcd6b4-kfqpw 1/1 Running 0 57s 10.244.1.26 node01.k8s.com <none> <none>
myapp-799bbcd6b4-p4dxz 1/1 Running 0 96m 10.244.1.24 node01.k8s.com <none> <none>
myapp-799bbcd6b4-xxxb9 1/1 Running 0 57s 10.244.2.31 node02.k8s.com <none> <none>

可以看到kubernetes现在有5个pod提供httpd服务。
如果我们现在想要伸缩,只需要3个pod提供httpd服务,只需执行命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# kubectl scale --replicas=3 deployment myapp
deployment.extensions/myapp scaled

查看pod运行情况
# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
myapp-799bbcd6b4-6697m 1/1 Running 0 17m 10.244.1.25 node01.k8s.com <none> <none>
myapp-799bbcd6b4-6tqkk 1/1 Running 0 113m 10.244.2.30 node02.k8s.com <none> <none>
myapp-799bbcd6b4-kfqpw 0/1 Terminating 0 17m 10.244.1.26 node01.k8s.com <none> <none>
myapp-799bbcd6b4-p4dxz 1/1 Running 0 113m 10.244.1.24 node01.k8s.com <none> <none>
myapp-799bbcd6b4-xxxb9 0/1 Terminating 0 17m 10.244.2.31 node02.k8s.com <none> <none>

可以看到kubernetes结束了2个pod,等候几秒再次查看pod运行情况,pod已经变成3个
# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
myapp-799bbcd6b4-6697m 1/1 Running 0 18m 10.244.1.25 node01.k8s.com <none> <none>
myapp-799bbcd6b4-6tqkk 1/1 Running 0 113m 10.244.2.30 node02.k8s.com <none> <none>
myapp-799bbcd6b4-p4dxz 1/1 Running 0 113m 10.244.1.24 node01.k8s.com <none> <none>

在master节点分别访问3个pod节点查看httpd服务信息

1
2
3
4
5
6
7
8
9
# curl 10.244.1.25
V1
myapp-799bbcd6b4-6697m
# curl 10.244.2.30
V1
myapp-799bbcd6b4-6tqkk
# curl 10.244.1.24
V1
myapp-799bbcd6b4-p4dxz

可以看到3个pod节点正常访问(V1代表版本信息,myapp-XXXXXX 是代表pods名称),显示了版本号和pod名称。

此时我们来测试kubernetes LB功能

1
2
3
4
5
6

查看 service
# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 72m
myapp ClusterIP 10.100.210.72 <none> 80/TCP 17m

在master节点执行命令展示LB的效果,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# curl 10.100.210.72
V1
myapp-799bbcd6b4-6697m
# curl 10.100.210.72
V1
myapp-799bbcd6b4-6tqkk
# curl 10.100.210.72
V1
myapp-799bbcd6b4-p4dxz
# curl 10.100.210.72
V1
myapp-799bbcd6b4-6697m
# curl 10.100.210.72
V1
myapp-799bbcd6b4-6tqkk

可以看到访问服务地址10.100.210.72后,分别均匀的访问到3个pod上的httpd服务

以上是体验kubernetes集群伸缩功能整个过程