1、HPA可以做什么?
- 通过观察pod的cpu、内存使用率或自定义metrics指标进行自动的扩容或缩容pod的数量。
- 通常用于Deployment,不适用于无法扩/缩容的对象,如DaemonSet。
- 控制管理器每隔30s(可以通过-horizontal-pod-autoscaler–sync-period修改)查询metrics的资源使用情况
2、cpu、内存指标监控
- 实现cpu或内存的监控,首先有个前提条件是该对象必须配置了resources.requests.cpu或resources.requests.memory才可以,可以配置当cpu/memory达刻上述配置的百分比后进行扩容或缩
容。
2.1 创建一个deployment的配置文件
apiVersion: apps/v1 # deployment api版本
kind: Deployment # 资源类型为 Deployment
metadata: # 原信息labels: # 标签app: nginx-deploy # 具体的标签信息:app=nginx-deploy key: value 配置形式name: nginx-deploy # deployment的名字namespace: default # 所在的命名空间
spec:replicas: 1 # 期望副本数revisionHistoryLimit: 10 # 进行滚动更新后,保留的历史版本数量selector: # 选择器,用于找到匹配的RSmatchLabels: # 按照标签匹配app: nginx-deploy # 匹配的标签strategy: #更新策略rollingUpdate: # 滚动更新配置maxSurge: 25% # 滚动更新时,更新的个数最多超过多少个期望副本数,或者比例maxUnavailable: 25% # 进行滚动更新时,最大不可用比例更新比例,表示在所有副本数中,最多可以有多少个不更新成功type: RollingUpdate # 更新类型,采用滚动更新template: # pod 模板metadata: # pod模板的元信息labels: # pod模板的标签app: nginx-deploy # pod模板的标签信息spec: # pod 期望信息containers: # pod 的容器信息- image: nginx:1.20 # 镜像信息imagePullPolicy: IfNotPresent # 镜像拉取策略name: nginx # 容器名字restartPolicy: Always # pod的重启策略terminationGracePeriodSeconds: 30 # pod的过期时间
2.2 创建这个deployment资源
[root@k8s-master ~]# kubectl create -f nginx-deploy.yaml
deployment.apps/nginx-deploy created
[root@k8s-master ~]# kubectl get deployments.apps nginx-deploy
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-deploy 1/1 1 1 15s
[root@k8s-master ~]# kubectl get po nginx-deploy-86b7d8c46d-7xwkw
NAME READY STATUS RESTARTS AGE
nginx-deploy-86b7d8c46d-7xwkw 1/1 Running 0 29s
2.3 更新配置文件
刚才的配置文件中不包含resources.requests.cpu和resouces.limits.cpu
2.4 替换deployment
[root@k8s-master ~]# kubectl replace -f nginx-deploy.yaml
deployment.apps/nginx-deploy replaced
2.5 执行hpa命令
kubectl autoscale deploy nginx-deploy --cpu-percent=20 --min=2 --max=5
[root@k8s-master ~]# kubectl autoscale deploy nginx-deploy --cpu-percent=20 --min=2 --max=5
horizontalpodautoscaler.autoscaling/nginx-deploy autoscaled# 本来我们的replace副本是1个的,现在又帮我们创建了一个
[root@k8s-master ~]# kubectl get deployments.apps nginx-deploy
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-deploy 2/2 2 2 15m
2.6 开启指标服务
2.6.1 下载开启指标的yaml配置文件
[root@k8s-master ~]# wget https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml -O metrics-server-components.yaml
2.6.2 修改镜像地址为国内的地址
[root@k8s-master ~]# sed -i 's/registry.k8s.io\/metrics-server/registry.cn-hangzhou.aliyuncs.com\/google_containers/g' metrics-server-components.yaml[root@k8s-master ~]# grep image metrics-server-components.yamlimage: registry.cn-hangzhou.aliyuncs.com/google_containers/metrics-server:v0.7.0imagePullPolicy: IfNotPresent
2.6.3 修改metrics-server-components.yaml配置
修改容器的ts配置,不验证ts,在containers的args参数中增加 --kubelet-insecure-tls 参数
2.6.4 安装这个指标插件
[root@k8s-master ~]# kubectl apply -f metrics-server-components.yaml
serviceaccount/metrics-server created
clusterrole.rbac.authorization.k8s.io/system:aggregated-metrics-reader created
clusterrole.rbac.authorization.k8s.io/system:metrics-server created
rolebinding.rbac.authorization.k8s.io/metrics-server-auth-reader created
clusterrolebinding.rbac.authorization.k8s.io/metrics-server:system:auth-delegator created
clusterrolebinding.rbac.authorization.k8s.io/system:metrics-server created
service/metrics-server created
deployment.apps/metrics-server created
apiservice.apiregistration.k8s.io/v1beta1.metrics.k8s.io created
2.6.5 查看这个pod的资源
[root@k8s-master ~]# kubectl get po --all-namespaces | grep metrics
kube-system metrics-server-7bb86dcf48-hfpb5 1/1 Running 0 99s
2.7 通过kubectl top pod 监控 融资所占用的资源
[root@k8s-master ~]# kubectl top pod
NAME CPU(cores) MEMORY(bytes)
dns-test 0m 0Mi
fluentd-59k8k 2m 57Mi
fluentd-hhtls 3m 57Mi
nginx-deploy-fdd948cf4-fgxjv 0m 1Mi
nginx-deploy-fdd948cf4-r8ktj 0m 1Mi
2.8 测试自动扩缩容
由于刚才创建的deploy资源还未包含service,所以需要创建一个service
2.8.1 创建一个service
# service配置文件如下
apiVersion: v1
kind: Service
metadata:name: nginx-svclabels:app: nginx
spec:selector:app: nginx-deployports:- port: 80targetPort: 80name: webtype: NodePort
[root@k8s-master ~]# kubectl create -f nginx-svc.yml
service/nginx-svc created
[root@k8s-master ~]#
[root@k8s-master ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.1.0.1 <none> 443/TCP 5d
nginx-svc NodePort <none> 80:31231/TCP 6s
[root@k8s-master ~]# curl 10.10.10.100:31231
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>body {width: 35em;margin: 0 auto;font-family: Tahoma, Verdana, Arial, sans-serif;}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p><p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p><p><em>Thank you for using nginx.</em></p>
</body>
</html>
2.8.1 进行压测
- 方法:通过给service发送http请求,实现压测,看我们的hpa是否可以自动扩容
2.8.1.1 在node1上执行请求
2.8.1.2 在node2上执行请求
2.8.1.3 在master上查看hpa的状态
[root@k8s-master ~]# kubectl get hpa
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
nginx-deploy Deployment/nginx-deploy 52%/20% 2 5 3 120m
2.8.1.4 在master上查看pod的状态
我们的hpa设置的副本数量是5个,这时可以看到副本由2个变为了5个副本。可以自动扩容
[root@k8s-master ~]# kubectl top pod
NAME CPU(cores) MEMORY(bytes)
dns-test 0m 0Mi
fluentd-59k8k 5m 57Mi
fluentd-hhtls 7m 57Mi
nginx-deploy-fdd948cf4-fgxjv 39m 2Mi
nginx-deploy-fdd948cf4-r8ktj 38m 2Mi
nginx-deploy-fdd948cf4-td2vn 41m 2Mi
nginx-deploy-fdd948cf4-xckj5 37m 2Mi
nginx-deploy-fdd948cf4-zpvhc 37m 2Mi
2.8.1.5 取消掉两台node节点上的http请求,查看hpa状态
两台node节点的负载取消后,hpa的targets也下降下来,但是replicas的副本数量是5,不是立马就缩减为2的。这个需要一段时间后可以减少为2。
[root@k8s-master ~]# kubectl get hpa
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
nginx-deploy Deployment/nginx-deploy 0%/20% 2 5 5 127m
2.8.1.6 查看pod的状态
两台node节点的负载取消后,pod的负载也降下来了。
[root@k8s-master ~]# kubectl top pod
NAME CPU(cores) MEMORY(bytes)
dns-test 0m 0Mi
fluentd-59k8k 2m 57Mi
fluentd-hhtls 2m 57Mi
nginx-deploy-fdd948cf4-fgxjv 0m 2Mi
nginx-deploy-fdd948cf4-r8ktj 0m 2Mi
nginx-deploy-fdd948cf4-td2vn 0m 2Mi
nginx-deploy-fdd948cf4-xckj5 0m 2Mi
nginx-deploy-fdd948cf4-zpvhc 0m 2Mi
过一段时间后,发现pod的数量减少为了2
[root@k8s-master ~]# kubectl top pod
NAME CPU(cores) MEMORY(bytes)
dns-test 0m 0Mi
fluentd-59k8k 2m 57Mi
fluentd-hhtls 2m 57Mi
nginx-deploy-fdd948cf4-r8ktj 0m 2Mi
nginx-deploy-fdd948cf4-td2vn 0m 2Mi
2.8.1.7 查看hpa的描述信息
3、自定义指标(metrics)
- 控制管理器开启 -horiztal-pod-autoscaler-use-rest-clients
- 控制管理器的-apiserver指向API Server Aggregator
- 在API Server Aggregatore中注册自定义的metrics API