测试开发技术网站
博客
设计
设计
开发
Python
测试
unittest
运维
Linux基础应用
CI/CD
CI/CD
数据库
数据库
云计算
云计算
云原生
云原生
爬虫
爬虫
数据分析
数据分析
人工智能
人工智能
登录
注册
Kubernetes----HPA控制器实现动态弹性扩缩容
收藏本文
作者:redrose2100 类别: 日期:2022-05-23 14:00:13 阅读:1018 次 消耗积分:0 分
# 一、HPA控制器简介 HPA(Horizontal Pod Autoscaler)控制器可以获取pod利用率,然后和HPA中定义的指标进行对比,同时计算出需要伸缩的具体值,最后实现pod的数量的调整,其实HPA与之前的Deployment一样,也属于一种Kubernetes对象,它通过追踪分析目标pod的负载变化情况,类确定是否需要针对性地调整目标pod的副本数 ![](/static/upload/20220523_140008.png) # 二、HPA控制器环境准备 ## 2.1 安装metric-server (1)参照 [Git----安装(CentOS)](https://blog.csdn.net/redrose2100/article/details/121007604) 首先安装git工具 (2)下载metric-server ```bash git clone -b v0.3.6 https://github.com/kubernetes-incubator/metrics-server ``` (3)配置yaml文件 ```bash cd metrics-server/deploy/1.8+/ vi metrics-server-deployment.yaml ``` 然后按照下图的位置编辑文件,内容如下: ```bash hostNetwork: true image: registry.cn-hangzhou.aliyuncs.com/google_containers/metrics-server-amd64:v0.3.6 - --kubelet-insecure-tls - --kubelet-preferred-address-types=InternalIP,Hostname,InternalNDS,ExternalDNS,ExternalIP ``` ![在这里插入图片描述](https://img-blog.csdnimg.cn/d126a310dc8a405aab40e4d87a56c2d4.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAcmVkcm9zZTIxMDA=,size_20,color_FFFFFF,t_70,g_se,x_16) (4)使用如下命令部署 ```bash [root@master 1.8+]# kubectl apply -f ./ clusterrole.rbac.authorization.k8s.io/system:aggregated-metrics-reader created Warning: rbac.authorization.k8s.io/v1beta1 ClusterRoleBinding is deprecated in v1.17+, unavailable in v1.22+; use rbac.authorization.k8s.io/v1 ClusterRoleBinding clusterrolebinding.rbac.authorization.k8s.io/metrics-server:system:auth-delegator created Warning: rbac.authorization.k8s.io/v1beta1 RoleBinding is deprecated in v1.17+, unavailable in v1.22+; use rbac.authorization.k8s.io/v1 RoleBinding rolebinding.rbac.authorization.k8s.io/metrics-server-auth-reader created Warning: apiregistration.k8s.io/v1beta1 APIService is deprecated in v1.19+, unavailable in v1.22+; use apiregistration.k8s.io/v1 APIService apiservice.apiregistration.k8s.io/v1beta1.metrics.k8s.io created serviceaccount/metrics-server created deployment.apps/metrics-server created service/metrics-server created clusterrole.rbac.authorization.k8s.io/system:metrics-server created clusterrolebinding.rbac.authorization.k8s.io/system:metrics-server created [root@master 1.8+]# ``` 查询如下,有metrics-server-669dfc56ff-v6drv 即表示已经部署成功了 ```bash [root@master 1.8+]# kubectl get pod -n kube-system NAME READY STATUS RESTARTS AGE coredns-558bd4d5db-7vbmq 1/1 Running 0 12d coredns-558bd4d5db-sps22 1/1 Running 0 12d etcd-master 1/1 Running 0 12d kube-apiserver-master 1/1 Running 0 12d kube-controller-manager-master 1/1 Running 0 12d kube-flannel-ds-cd9qk 1/1 Running 0 12d kube-flannel-ds-gg4jq 1/1 Running 0 12d kube-flannel-ds-n76xj 1/1 Running 0 12d kube-proxy-g4j5g 1/1 Running 0 12d kube-proxy-h27ms 1/1 Running 0 12d kube-proxy-tqzjl 1/1 Running 0 12d kube-scheduler-master 1/1 Running 0 12d metrics-server-669dfc56ff-v6drv 1/1 Running 0 56s [root@master 1.8+]# ``` (5)此时通过如下命令,可以查询到节点的资源使用情况 ```bash [root@master 1.8+]# kubectl top node W0327 11:53:58.289701 9379 top_node.go:119] Using json format to get metrics. Next release will switch to protocol-buffers, switch early by passing --use-protocol-buffers flag NAME CPU(cores) CPU% MEMORY(bytes) MEMORY% master 71m 0% 1268Mi 3% node1 24m 0% 687Mi 2% node2 23m 0% 721Mi 2% [root@master 1.8+]# ``` (6)通过如下命令,可以查看到pod的资源使用情况 ```bash [root@master 1.8+]# kubectl top pod -n kube-system W0327 11:55:35.833234 10195 top_pod.go:140] Using json format to get metrics. Next release will switch to protocol-buffers, switch early by passing --use-protocol-buffers flag NAME CPU(cores) MEMORY(bytes) coredns-558bd4d5db-7vbmq 1m 13Mi coredns-558bd4d5db-sps22 1m 13Mi etcd-master 9m 26Mi kube-apiserver-master 22m 297Mi kube-controller-manager-master 6m 53Mi kube-flannel-ds-cd9qk 2m 15Mi kube-flannel-ds-gg4jq 2m 15Mi kube-flannel-ds-n76xj 1m 16Mi kube-proxy-g4j5g 1m 19Mi kube-proxy-h27ms 1m 19Mi kube-proxy-tqzjl 1m 19Mi kube-scheduler-master 2m 21Mi metrics-server-669dfc56ff-v6drv 1m 15Mi [root@master 1.8+]# ``` ## 2.2 创建deployment和service 编辑service_deployment.yaml文件,内容如下: ```yaml apiVersion: v1 kind: Namespace metadata: name: dev --- apiVersion: apps/v1 kind: Deployment metadata: name: deploy-nginx namespace: dev spec: replicas: 1 selector: matchLabels: run: nginx template: metadata: labels: run: nginx spec: containers: - name: nginx image: nginx:1.17.1 ports: - containerPort: 80 protocol: TCP --- apiVersion: v1 kind: Service metadata: name: service-nginx namespace: dev spec: ports: - port: 80 protocol: TCP targetPort: 80 nodePort: 30030 selector: run: nginx type: NodePort ``` 使用如下命令创建 ```bash [root@master pod_controller]# kubectl apply -f service_deployment.yaml namespace/dev created deployment.apps/deploy-nginx created service/service-nginx created [root@master pod_controller]# ``` 查看创建的资源如下: ```bash [root@master pod_controller]# kubectl get deployment,pod,service -n dev NAME READY UP-TO-DATE AVAILABLE AGE deployment.apps/deploy-nginx 3/3 3 3 5m6s NAME READY STATUS RESTARTS AGE pod/deploy-nginx-66ffc897cf-jhc9w 1/1 Running 0 5m6s NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE service/service-nginx NodePort 10.106.128.172
80:30030/TCP 5m6s [root@master pod_controller]# ``` ## 2.3 部署HPA 编辑pc_hpa.yaml文件,内容如下:注意这里把cpu使用率设置为1%是为了测试用。在实际部署中需要根据具体情况去定 ```yaml apiVersion: autoscaling/v1 kind: HorizontalPodAutoscaler metadata: name: pc-hpa namespace: dev spec: minReplicas: 1 maxReplicas: 10 targetCPUUtilizationPercentage: 1 scaleTargetRef: apiVersion: apps/v1 kind: Deployment name: deploy-nginx ``` 然后使用如下命令创建 ```bash [root@master pod_controller]# kubectl apply -f pc_hpa.yaml horizontalpodautoscaler.autoscaling/pc-hpa created [root@master pod_controller]# ``` 查看hpa如下 ```bash [root@master pod_controller]# kubectl get hpa -n dev NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE pc-hpa Deployment/deploy-nginx
/1% 1 10 1 5m18s [root@master pod_controller]# ``` ## 2.4 压力测试观测HPA控制器 开三个窗口分别对pod,deployment,hpa进行监视分别使用如下命令 ```bash kubectl get pod -n dev -w kubectl get deploy -n dev -w kubectl get hpa -n dev -w ``` 编写测试脚本 test.sh,用于发送get请求 ```bash #! /bin/bash for((i=1;i<1000000;i++)); do curl http://192.168.16.40:30030; done ``` 执行测试脚本,然后观察pod数量变化 如下为deploy的观测结果: ```bash [root@master ~]# kubectl get deploy -n dev -w NAME READY UP-TO-DATE AVAILABLE AGE deploy-nginx 1/1 1 1 82m deploy-nginx 1/3 1 1 84m deploy-nginx 1/3 1 1 84m deploy-nginx 1/3 1 1 84m deploy-nginx 1/3 3 1 84m deploy-nginx 2/3 3 2 84m deploy-nginx 3/3 3 3 84m ``` 如下为HPA的观测结果 ```bash [root@master ~]# kubectl get hpa -n dev -w NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE pc-hpa Deployment/deploy-nginx 0%/1% 1 10 1 63m pc-hpa Deployment/deploy-nginx 3%/1% 1 10 1 64m pc-hpa Deployment/deploy-nginx 3%/1% 1 10 3 64m pc-hpa Deployment/deploy-nginx 1%/1% 1 10 3 65m pc-hpa Deployment/deploy-nginx 1%/1% 1 10 3 66m ``` 可以看到,HPA确实可以做到动态扩缩容
始终坚持开源开放共享精神,同时感谢您的充电鼓励和支持!
版权所有,转载本站文章请注明出处:redrose2100, http://blog.redrose2100.com/article/275
上一篇:
Kubernetes----Deployment控制器实现版本金丝雀发布
下一篇:
Kubernetes----DaemonSet控制器
搜索
个人成就
出版书籍
《Pytest企业级应用实战》
测试开发技术全栈公众号
测试开发技术全栈公众号
DevOps技术交流微信群
加微信邀请进群
常用网站链接
开源软件洞察
云原生技术栈全景图
Python语言官方文档
Golang官方文档
Docker官方文档
Jenkins中文用户手册
Scrapy官方文档
VUE官方文档
Harbor官方文档
openQA官方文档
云原生开源社区
开源中国
Kubernetes中文文档
Markdown语法官方教程
Kubernetes中文社区
Kubersphere官方文档
BootStrap中文网站
JavaScript中文网
NumPy官方文档
Pandas官方文档
GitLink确实开源网站
数据库排名网站
编程语言排名网站
SEO综合查询网站
数学加减法练习自动生成网站
Kickstart Generator
文章分类
最新文章
最多阅读
特别推荐
×
Close
登录
注册
找回密码
登录邮箱:
登录密码:
图片验证码:
注册邮箱:
注册密码:
邮箱验证码:
发送邮件
注册邮箱:
新的密码:
邮箱验证码:
发送邮件