如果我们人工监控pods,人工进行调整副本那么这个工作量无疑是巨大的,但kubernetes已经有了相应的机制来应对了。
创新互联成都企业网站建设服务,提供成都网站设计、网站建设、外贸网站建设网站开发,网站定制,建网站,网站搭建,网站设计,响应式网站开发,网页设计师打造企业风格网站,提供周到的售前咨询和贴心的售后服务。欢迎咨询做网站需要多少钱:18982081108###HPA全称Horizontal Pod Autoscaler控制器工作流程(V1版本)
更详细的介绍参考官方文档Horizontal Pod Autoscaler
流程
--horizontal-pod-autoscaler-downscale-delay
--horizontal-pod-autoscaler-upscale-delay
###配置HPA实现应用横向扩展
cat hpa-deployment.ymal
apiVersion: apps/v1
kind: Deployment
metadata:
name: php-apache
labels:
app: hpa-test
spec:
replicas: 1
selector:
matchLabels:
name: php-apache
app: hpa-test
template:
metadata:
labels:
name: php-apache
app: hpa-test
spec:
containers:
2. 配置service: php-apache-svc
cat hpa-svc.yaml
apiVersion: v1
kind: Service
metadata:
name: php-apache-svc
labels:
app: hpa-test
spec:
selector:
name: php-apache
app: hpa-test
ports:
3. 配置hpa:php-apache-hpa
cat hpa-hpa.yaml
apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
name: php-apache-hpa
labels:
app: hpa-test
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: php-apache
minReplicas: 1
maxReplicas: 10
targetCPUUtilizationPercentage: 50
4. 启动deployment,service,hpa,并验证
deployment.apps/php-apache configured
service/php-apache-svc unchanged
horizontalpodautoscaler.autoscaling/php-apache-hpa unchanged
NAME READY STATUS RESTARTS AGE
pod/php-apache-6b9f498dc4-vwlfr 1/1 Running 0 3h24m
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 7d20h
service/php-apache-svc ClusterIP 10.104.34.168 <none> 80/TCP 3h24m
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/php-apache 1/1 1 1 3h24m
NAME DESIRED CURRENT READY AGE
replicaset.apps/php-apache-6b9f498dc4 1 1 1 3h24m
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
horizontalpodautoscaler.autoscaling/php-apache-hpa Deployment/php-apache 20%/50% 1 10 1 3h24m
###压力测试,观察HPA效果
>1.生成一个压测客户端,持续压力测试
kubectl run --generator=run-pod/v1 -i --tty load-generator --image=busybox /bin/sh
OK!OK!OK!OK!OK!OK!OK!OK!OK!OK!OK!OK!OK!
>2.压测一下,观察结果
kubectl get hpa
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
php-apache-hpa Deployment/php-apache 800%/50% 1 10 1 27m
kubectl get hpa
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
php-apache-hpa Deployment/php-apache 1000%/50% 1 10 2 27m
kubectl get hpa
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
php-apache-hpa Deployment/php-apache 1000%/50% 1 10 4 27m
kubectl get hpa
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
php-apache-hpa Deployment/php-apache 1000%/50% 1 10 8 27m
kubectl get hpa
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
php-apache-hpa Deployment/php-apache 120%/50% 1 10 10 27m
kubectl get deployment php-apache
NAME READY UP-TO-DATE AVAILABLE AGE
php-apache 10/10 10 10 28m
#####结论:随着压力测试进行,deployment下pod的CPU使用率增加,超过HPA设定的百分比50%,之后逐次翻倍扩容replicaset。达到上限停止扩容。根据replicaset设置的request QoS逐渐稳定资源的使用率。
>3.停止压测
while true; do wget -q -O- http://php-apache-svc.default.svc.cluster.local; done
OK!OK!OK!OK!OK!OK!OK!OK!OK!OK!OK!OK!OK!OK!wget: can't connect to remote host (10.104.63.73): Connection refused
OK!OK!OK!OK!OK!OK!........OK!OK!OK! ^C
/ # exit
/ # Session ended, resume using 'kubectl attach load-generator -c load-generator -i -t' command when the pod is running
#####CPU使用率恢复到最初值20%,controller会周期观测,逐次缩容到最小值。
kubectl get hpa
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
php-apache-hpa Deployment/php-apache 20%/50% 1 10 10 36m
#等待几分钟之后(默认5分钟),原因:
kubectl get hpa
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
php-apache-hpa Deployment/php-apache 20%/50% 1 10 4 41m
#再次等待几分钟后(默认5分钟)
kubectl get hpa
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
php-apache-hpa Deployment/php-apache 20%/50% 1 10 2 46m
#再次等待几分钟后(默认5分钟),稳定在最小副本数量
kubectl get hpa
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
php-apache-hpa Deployment/php-apache 20%/50% 1 10 1 53m
###其他
以上测试验证了HPA功能,使用的API版本是autoscaling/v1。通过kubectl api-versions可以查看到存在3个版本。v1版本只支持CPU,v2beta2版本支持多metrics(CPU,memory)以及自定义metrics。基于autoscaling/v2beta2的hpa yaml文件写法
apiVersion: autoscaling/v2beta2
kind: HorizontalPodAutoscaler
metadata:
name: php-apache-hpa
labels:
app: hpa-test
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: php-apache
minReplicas: 1
maxReplicas: 10
metrics:
另外有需要云服务器可以了解下创新互联cdcxhl.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
分享文章:Kubernetes的弹性伸缩用法-创新互联
文章起源:https://www.cdcxhl.com/article40/iooeo.html
成都网站建设公司_创新互联,为您提供全网营销推广、网站建设、网站策划、用户体验、电子商务、服务器托管
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联