Kubernetes - Scale Your App

Running Multiple Instances of Your App

使用kubectl扩展app

Scaling

Scaling是透过改变Deployment的Replicas的数量来实现的

透过Scaling,Deployment将会确保新的Pods会被调度再有可用资源的Node上,且将Pod所需的数量增加到新的要求数目
k8s支援pods的autoscaling,但这部分不会在此解说,但必须知道的是有可能透过autoscaling会将Deployment的所有pods全数终止的情形发生

运行多个app时需要一个方法将流量分配,而service具有一个集成的load-blacer,将会把流量分配给对外开放的Deployment中的pods,且会持续的监控这些pods的endpoints,以确保能够将流量输送到这些pods中

而一旦你有了多个app运行,你将能够使用滚动式更新
这是个重要的概念,将会决定service的部署策略

Interactive Tutorial - Scaling Your App

Step 1: Scaling a deployment

首先列出deployments list kubectl get deployments 接着会显示几项资讯

NAME lists the names of the Deployments in the cluster.READY shows the ratio of CURRENT/DESIRED replicasUP-TO-DATE displays the number of replicas that have been updated to achieve the desired state.AVAILABLE displays how many replicas of the application are available to your users.AGE displays the amount of time that the application has been running.

接着列出透过这个deployment所创建的ReplicaSet kubectl get rs ,而ReplicaSet的格式为 [DEPLOYMENT-NAME]-[RANDOM-STRING] 随机字串的部分是使用pod-template-hash作为seed产生的,列出的ReplicaSet资讯有两个重要的部分

DESIRED displays the desired number of replicas of the application, which you define when you create the Deployment. This is the desired state.CURRENT displays how many replicas are currently running.

再来利用 kubectl scale 指令scale Deployment变成四个ReplicaSet kubectl scale deployments/kubernetes-bootcamp --replicas=4

再次取得deployments list观察变化 kubectl get deployments

取得pods的资讯 kubectl get pods -o wide

观察deployment得资讯 kubectl describe deployments/kubernetes-bootcamp

Step 2: Load Balancing

首先检查Service是否在对流量进行负载平衡,然后利用describe找出对外裸露的IP & Ports kubectl describe services/kubernetes-bootcamp

将NodePort导出为环境变数 export NODE_PORT=$(kubectl get services/kubernetes-bootcamp -o go-template='{{(index .spec.ports 0).nodePort}}') echo NODE_PORT=$NODE_PORT

接着利用curl多呼叫几次API,会发现透过不同的pod response curl $(minikube ip):$NODE_PORT

Step 3: Scale Down

将Service缩减为2个replicas kubectl scale deployments/kubernetes-bootcamp --replicas=2

取得deployments列表确认改动成功 kubectl get deployments

确认pods的数量 kubectl get pods -o wide

参考

https://kubernetes.io/docs/tutorials/kubernetes-basics/scale/

关于作者: 网站小编

码农网专注IT技术教程资源分享平台,学习资源下载网站,58码农网包含计算机技术、网站程序源码下载、编程技术论坛、互联网资源下载等产品服务,提供原创、优质、完整内容的专业码农交流分享平台。

热门文章