24、k8s-pod的控制器-第三种-HPA(Horizontal Pod Autoscaler)-自动调整pod的数量
监测pod的使用情况来做调整
概念:HPA可以获取每个pod的利用率、然后和HPA中定义的指标(如cpu、内存等使用情况)进行对比、同时计算出需要伸缩的具体值、最后实现pod数量的调整、其实HPA与之前的Deployment
控制器一样、也属于一种kubernetes资源对象、它通过追踪分析目标pod的负载变化情况、来确定是否需要针对性的调整目标pod的副本数量
1、安装部署metrics-server
metrics-server可以用来收集集群中的资源使用情况
1、安装git:yum install -y git
2、从github上克隆服务:
·git clone -b v0.3.6 https://github.com/kubernetes-sigs/metrics-server.git #如果无法克隆就直接去git上下载
·直接网页下载:https://github.com/kubernetes-sigs/metrics-server/tree/v0.3.6 #再上传解压
·这里注意使用得版本:0.3.6是包含了deploy目录的 、0.4以上就没有这个目录了
·且注意下载的0.3.6解压后会报错、直接解压再压缩里面的文件夹即可、再上传到服务器解压
3、修改deployment.yaml的参数、注意修改的是镜像和初始化参数:
·进入解压后的文件里:cd metrics-server/deploy/1.8+/
·编辑yaml文件:vim metrics-server-deployment.yaml
-------------------------------------------------------
Ⅰ、#添加hostNetwork: true
spec: #注意是在template下的spec添加
hostNetwork: true
serviceAccountName: metrics-server
Ⅱ、修改镜像仓库下载的地址:image:
containers:
- name: metrics-server
image: registry.cn-hangzhou.aliyuncs.com/google_containers/metrics-server-amd64:v0.3.6 #修改为阿里云仓库
Ⅲ、添加启动参数srgs:在containers下添加
containers:
- name: metrics-server
image: registry.cn-hangzhou.aliyuncs.com/google_containers/metrics-server-amd64:v0.3.6
imagePullPolicy: Always
args:
- --kubelet-insecure-tls
- --kubelet-preferred-address-types=InternalIP,Hostname,InternalDNS,ExternalDNS,ExternalIP
--------------------------------------------------------------------------------------------------------
4、安装部署metrics-server、去到该目录下metrics-server/deploy/1.8+/执行所有的yaml文件:
·执行yaml文件:[root@master-1 1.8+]# kubectl apply -f ./ #这就安装了metrics-server了、运行在pod下的服务
·查看生产的pod情况、默认在kube-system空间下生成:kubectl get pods -nkube-system
----------------------------------------------------------------------------------
NAME READY STATUS RESTARTS AGE
coredns-6955765f44-7rtrd 1/1 Running 15 25d
coredns-6955765f44-gvx48 1/1 Running 15 25d
etcd-master-1 1/1 Running 17 25d
kube-apiserver-master-1 1/1 Running 18 25d
kube-controller-manager-master-1 1/1 Running 25 25d
kube-flannel-ds-amd64-7b4gl 1/1 Running 1 17h
kube-flannel-ds-amd64-ws2xl 1/1 Running 20 25d
kube-proxy-777s9 1/1 Running 17 25d
kube-proxy-xgfdl 1/1 Running 13 25d
kube-scheduler-master-1 1/1 Running 25 25d
metrics-server-6b976979db-m7488 1/1 Running 0 2m3s #这个就是生成的pod 、上面其它都是系统自带的
---------------------------------------------------------------------------------------------------------------------------
5、查看node运行占用资源情况:kubectl top node
---------------------------------------------------
NAME CPU(cores) CPU% MEMORY(bytes) MEMORY%
master-1 159m 3% 1000Mi 58%
node-1 44m 1% 337Mi 19%
--------------------------------------------------------
5、查看pod的资源占用情况:kubectl top pods -nkube-system
图为vim metrics-server-deployment.yaml 文件修改情况
2、准备deployment和service
1、创建deployment:
·kubectl run nginx --image=nginx:1.17.1 --requests=cpu=100m -ndev
·--requests=cpu=100m #cpu的限制、一定要加
·查看deployment和生成的pod: kubectl get deploy,pod -ndev
----------------------------------------------------------------------
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/nginx 1/1 1 1 5m34s #这里的nginx是deployment的名字
NAME READY STATUS RESTARTS AGE
pod/nginx-778cb5fb7b-f4wbg 1/1 Running 0 5m34s #nginx-778cb5fb7b-f4wbg是pod的名字
---------------------------------------------------------------------------------------------------------
2、创建server暴露给外界访问:
·kubectl expose deployment nginx --type=NodePort --port=80 -ndev #--type=NodePort 生成一个给外界访问的端口
·查看server:kubectl get deploy,pod,service -ndev
--------------------------------------------------------------------
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/nginx 1/1 1 1 10m
NAME READY STATUS RESTARTS AGE
pod/nginx-778cb5fb7b-f4wbg 1/1 Running 0 10m
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/nginx NodePort 10.105.35.10 <none> 80:30114/TCP 89s #这里的30114端口是给外界访问的:ip:30114
-------------------------------------------------------------------------------------------------------------------------------
3、部署HPA
1、创建HPA的yaml文件:
---------------------------------------------------------
apiVersion: autoscaling/v1 #注意这里的版本
kind: HorizontalPodAutoscaler
metadata:
name: pc-hpa
namespace: dev
spec:
minReplicas: 1 #最小pod数量
maxReplicas: 10 #最大pod数量
targetCPUUtilizationPercentage: 3 #cpu使用率指标
scaleTargetRef: #指定要控制nginx的信息
apiVersion: apps/v1
kind: Deployment
name: nginx
----------------------------------------------------------------------
2、执行文件创建HPA:kubectl create -f pc-hpa.yaml
3、查看HPA:kubectl get hpa -ndev
-------------------------------------------------------------
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
pc-hpa Deployment/nginx 0%/3% 1 10 1 45s
#TARGETS 0%/3% 0是正在使用的cpu率、3是指定的阈值
----------------------------------------------------------------------------
4、再开三个窗口实时观看deployment、pod和hpa的情况:
·第一个窗口:kubectl get deploy -ndev -w
·第二个窗口: kubectl get pods -ndev -w
·第三个窗口: kubectl get hpa -ndev -w
5、使用postman工具向nginx服务发送访问请求:
·http://192.168.177.160:30114/ #测试正常
·向网址发送10000个请求观察hpa的cpu变化和pod、deployment的变化