在前面的课程中,我们已经可以实现通过手工执行kubectl scale命令实现Pod扩容或缩容,但是这显然不符合Kubernetes的定位目标–自动化、智能化。 Kubernetes期望可以实现通过监测Pod的使用情况,实现pod数量的自动调整,于是就产生了Horizontal Pod Autoscaler(HPA)这种控制器。
HPA可以获取每个Pod利用率,然后和HPA中定义的指标进行对比,同时计算出需要伸缩的具体值,最后实现Pod的数量的调整。其实HPA与之前的Deployment一样,也属于一种Kubernetes资源对象,它通过追踪分析RC控制的所有目标Pod的负载变化情况,来确定是否需要针对性地调整目标Pod的副本数,这是HPA的实现原理。
接下来,我们来做一个实验
1,安装metrics-server
metrics-server可以用来收集集群中的资源使用情况
安装git
yum install git -y
获取metrics-server, 注意使用的版本
git clone -b v0.3.6 https://github.com/kubernetes-incubator/metrics-server
修改deployment, 注意修改的是镜像和初始化参数
cd /root/metrics-server/deploy/1.8+/
vim metrics-server-deployment.yaml
添加/修改下面选项
hostNetwork: true
image: registry.cn-hangzhou.aliyuncs.com/google_containers/metrics-server-amd64:v0.3.6
args:
- --kubelet-insecure-tls
- --kubelet-preferred-address-types=InternalIP,Hostname,InternalDNS,ExternalDNS,ExternalIP
'''添加后文件部分内容如下
spec:
hostNetwork: true
serviceAccountName: metrics-server
volumes:
# mount in tmp so we can safely use from-scratch images and/or read-only containers
- name: tmp-dir
emptyDir: {}
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
volumeMounts:
- name: tmp-dir
mountPath: /tmp
'''
安装metrics-server,这里apply /root/metrics-server/deploy/1.8+/ 里所有的配置文件
kubectl apply -f ./
# 查看metrics-server 的pod的运行情况
kubectl get pod -n kube-system
metrics-server-6b976979db-2xwbj 1/1 Running 0 90s
# 使用kubectl top 资源名 查看资源使用情况,采集信息需要一定时间
kubectl top node
NAME CPU(cores) CPU% MEMORY(bytes) MEMORY%
master 83m 4% 1027Mi 59%
node1 25m 1% 671Mi 38%
node2 28m 1% 648Mi 37%
kubectl top pod -n kube-system
NAME CPU(cores) MEMORY(bytes)
coredns-6955765f44-7ptsb 3m 9Mi
coredns-6955765f44-vcwr5 3m 8Mi
etcd-master 14m 145Mi
metrics-server-6b976979db-dpwrm 1m 11Mi
至此,metrics-server安装完成
2,部署deployment,servie,HPA
deploy的template的spec里,必须设置resources的limits和requests(貌似设置一个就行),
否则 kubectl get hpa -n dev查看当前cpu使用状况时,TARGETS显示unknown/3%
cat >pod-test.yaml <<EOF
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deploy
namespace: dev
spec:
strategy: # 策略
type: RollingUpdate # 滚动更新策略
replicas: 1
selector:
matchLabels:
app: nginx-pod
template:
metadata:
labels:
app: nginx-pod
spec:
containers:
- name: nginx
image: nginx:1.17.1
resources: # 资源配额
limits: # 限制资源(上限)
cpu: "1" # CPU限制,单位是core数
requests: # 请求资源(下限)
cpu: "100m" # CPU限制,单位是core数
#如果不设置, kubectl get hpa -n dev的TARGETS显示unknown/3%
---
apiVersion: v1
kind: Service
metadata:
name: nginx-svc
namespace: dev
spec:
type: NodePort
ports:
- port: 80
targetPort: 80
nodePort: 30000
selector:
app: nginx-pod #根据此标签选择器,选择pod进行expose
---
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-deploy
EOF
写入配置文件后apply即可
3,测试
'''
用3个shell分别监控hpa,deploy,pod
'''
kubectl get hpa -n dev -w
kubectl get deploy -n dev -w
kubectl get pod -n dev -w
'''开始测试,用python运行,url里ip和port都是自己设置的'''
import requests
for i in range(10000):
requests.get('http://192.168.10.100:30000')
kubectl get hpa -n dev -w
'''
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
pc-hpa Deployment/nginx-deploy 0%/3% 1 10 1 7m32s
pc-hpa Deployment/nginx-deploy 1%/3% 1 10 1 8m20s
pc-hpa Deployment/nginx-deploy 54%/3% 1 10 1 9m21s
pc-hpa Deployment/nginx-deploy 54%/3% 1 10 4 9m36s
pc-hpa Deployment/nginx-deploy 54%/3% 1 10 8 9m51s
pc-hpa Deployment/nginx-deploy 54%/3% 1 10 10 10m
pc-hpa Deployment/nginx-deploy 0%/3% 1 10 10 10m
pc-hpa Deployment/nginx-deploy 0%/3% 1 10 10 15m
pc-hpa Deployment/nginx-deploy 0%/3% 1 10 1 15m
'''
kubectl get deploy -n dev -w
'''
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-deploy 1/1 1 1 7m23s
nginx-deploy 1/4 1 1 9m21s
nginx-deploy 1/4 4 1 9m21s
nginx-deploy 2/4 4 2 9m22s
nginx-deploy 3/4 4 3 9m23s
nginx-deploy 4/4 4 4 9m23s
nginx-deploy 4/8 4 4 9m36s
nginx-deploy 4/8 8 4 9m36s
nginx-deploy 5/8 8 5 9m37s
nginx-deploy 6/8 8 6 9m38s
nginx-deploy 7/8 8 7 9m38s
nginx-deploy 8/8 8 8 9m38s
nginx-deploy 8/10 8 8 9m51s
nginx-deploy 8/10 10 8 9m51s
nginx-deploy 9/10 10 9 9m52s
nginx-deploy 10/10 10 10 9m53s
nginx-deploy 10/1 10 10 15m
nginx-deploy 1/1 1 1 15m
'''
kubectl get pod -n dev -w
'''
NAME READY STATUS RESTARTS AGE
nginx-deploy-547dd76d58-j8lm5 1/1 Running 0 7m26s
nginx-deploy-547dd76d58-wf5gj 0/1 Pending 0 0s
nginx-deploy-547dd76d58-wf5gj 0/1 Pending 0 0s
nginx-deploy-547dd76d58-zd5xr 0/1 Pending 0 0s
nginx-deploy-547dd76d58-cdmqq 0/1 Pending 0 0s
nginx-deploy-547dd76d58-cdmqq 0/1 Pending 0 0s
nginx-deploy-547dd76d58-zd5xr 0/1 Pending 0 0s
nginx-deploy-547dd76d58-wf5gj 0/1 ContainerCreating 0 0s
nginx-deploy-547dd76d58-zd5xr 0/1 ContainerCreating 0 0s
nginx-deploy-547dd76d58-cdmqq 0/1 ContainerCreating 0 0s
nginx-deploy-547dd76d58-zd5xr 1/1 Running 0 1s
nginx-deploy-547dd76d58-wf5gj 1/1 Running 0 2s
nginx-deploy-547dd76d58-cdmqq 1/1 Running 0 2s
nginx-deploy-547dd76d58-mk5rx 0/1 Pending 0 0s
nginx-deploy-547dd76d58-mk5rx 0/1 Pending 0 0s
nginx-deploy-547dd76d58-99kx6 0/1 Pending 0 0s
nginx-deploy-547dd76d58-fl99x 0/1 Pending 0 0s
nginx-deploy-547dd76d58-99kx6 0/1 Pending 0 0s
nginx-deploy-547dd76d58-mk5rx 0/1 ContainerCreating 0 0s
nginx-deploy-547dd76d58-v5kvk 0/1 Pending 0 0s
nginx-deploy-547dd76d58-fl99x 0/1 Pending 0 0s
nginx-deploy-547dd76d58-v5kvk 0/1 Pending 0 0s
nginx-deploy-547dd76d58-fl99x 0/1 ContainerCreating 0 0s
nginx-deploy-547dd76d58-99kx6 0/1 ContainerCreating 0 0s
nginx-deploy-547dd76d58-v5kvk 0/1 ContainerCreating 0 0s
nginx-deploy-547dd76d58-99kx6 1/1 Running 0 1s
nginx-deploy-547dd76d58-fl99x 1/1 Running 0 2s
nginx-deploy-547dd76d58-mk5rx 1/1 Running 0 2s
nginx-deploy-547dd76d58-v5kvk 1/1 Running 0 2s
nginx-deploy-547dd76d58-zw2l2 0/1 Pending 0 0s
nginx-deploy-547dd76d58-zw2l2 0/1 Pending 0 0s
nginx-deploy-547dd76d58-qjhqg 0/1 Pending 0 0s
nginx-deploy-547dd76d58-qjhqg 0/1 Pending 0 0s
nginx-deploy-547dd76d58-zw2l2 0/1 ContainerCreating 0 0s
nginx-deploy-547dd76d58-qjhqg 0/1 ContainerCreating 0 0s
nginx-deploy-547dd76d58-zw2l2 1/1 Running 0 1s
nginx-deploy-547dd76d58-qjhqg 1/1 Running 0 2s
nginx-deploy-547dd76d58-zd5xr 1/1 Terminating 0 5m49s
******
nginx-deploy-547dd76d58-v5kvk 0/1 Terminating 0 5m45s
nginx-deploy-547dd76d58-cdmqq 0/1 Terminating 0 6m
nginx-deploy-547dd76d58-cdmqq 0/1 Terminating 0 6m
'''
可以发现发送大量请求后,pod数自动增加至最大(10),停止发送后,一定时间后减少到最小(1)