k8s(5)-拓展服务

时间:2021-10-09 19:39:12

   在之前我们创建了一个部署,然后通过服务公开它。部署只创建了一个Pod来运行我们的应用程序。当流量增加时,我们需要扩展应用程序以满足用户需求。

通过更改部署中的副本数来完成扩展。

k8s(5)-拓展服务k8s(5)-拓展服务

1. 拓展部署

这里将拓展副本到4个

[root@master ~]# kubectl scale deployments/kubernetes-bootcamp --replicas=4
deployment.extensions/kubernetes-bootcamp scaled
[root@master ~]# kubectl get deployment
NAME READY UP-TO-DATE AVAILABLE AGE
kubernetes-bootcamp 4/4 4 4 5h11m

 

2. 查看副本是否都运行
[root@master ~]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
kubernetes-bootcamp-7f56b875dc-6zbjt 1/1 Running 0 5h12m 10.244.0.15 master <none> <none>
kubernetes-bootcamp-7f56b875dc-cp7cq 1/1 Running 0 47s 10.244.0.16 master <none> <none>
kubernetes-bootcamp-7f56b875dc-n4wkn 1/1 Running 0 47s 10.244.0.17 master <none> <none>
kubernetes-bootcamp-7f56b875dc-pmvck 1/1 Running 0 47s 10.244.0.18 master <none> <none>

 

3. 我们现在有4个Pod了,可以通过describe查看event变化
[root@master ~]# kubectl describe deployments/kubernetes-bootcamp
Name: kubernetes-bootcamp
.....
.....
Type Status Reason
---- ------ ------
Progressing True NewReplicaSetAvailable
Available True MinimumReplicasAvailable
OldReplicaSets: <none>
NewReplicaSet: kubernetes-bootcamp-7f56b875dc (4/4 replicas created)
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 4m28s deployment-controller Scaled up replica set kubernetes-bootcamp-7f56b875dc to 4

  

4. 访问应用查看是否有负载均衡的效果
[root@master ~]# curl http://192.168.0.10:31238
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-7f56b875dc-6zbjt | v=1
[root@master ~]# curl http://192.168.0.10:31238
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-7f56b875dc-cp7cq | v=1
[root@master ~]# curl http://192.168.0.10:31238
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-7f56b875dc-pmvck | v=1
[root@master ~]# curl http://192.168.0.10:31238
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-7f56b875dc-n4wkn | v=1
[root@master ~]#