目录
????????Pod水平自动扩容器的实现是一个控制回路,由控制器管理的--horizontal-pod-autoscaler-sync-period?参数指定周期(默认值为 15 秒)通过Status.PodSelector来查询pods的状态,获取pod的CPU使用率,然后,通过现有的pod的CPU使用率的平均值跟目标使用率进行比较,并且在扩容时,还要遵循预先这顶的副本数显示:MinReplicas
#有三个版本
[root@k8s-master-1 cfg]# kubectl api-versions |grep autoscal
autoscaling/v1 #只通过CPU为参考,来改变pod副本数
autoscaling/v2beta1 #支持通过CPU、内存、连接数以及用户自定义
的资源指标数据为参考
autoscaling/v2beta2 #同上
kubectl explain hpa ##默认查询到的是autoscaling/v1版本
kubectl explain hpa --api-version=autoscaling/v2beta1
##如果使用其他版本,可以使用--api-version指明版本
1、创建HorizontalPodAutoscaler.yaml
[root@k8s-master-1 test]# vim HorizontalPodAutoscaler.yaml
apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
name: hpa-demo
spec:
maxReplicas: 10
minReplicas: 1
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: hpa-demo
targetCPUUtilizationPercentage: 10
#或者使用命令:
[root@k8s-master-1 test]# kubectl autoscale deployment hpa-demo --cpu-percent=10 --min=1 --max=10
2、创建Deployment.如果要想让HPA生效,对应的Pod资源必须添加requests资源声明
[root@k8s-master-1 test]# vim deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: hpa-demo
spec:
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.15.3
ports:
- containerPort: 80
resources:
requests:
memory: 50Mi
cpu: 50m
1、HPA无法计算副本计数:无法获取资源cpu的指标:没有从资源指标API返回指标
解决:由于我前期添加了聚合 API,没有重启kube-controller-manager.service、kube-scheduler.service,所以一直报错,在这就是在HorizontalPodAutoscaler.yaml文件中spec.scaleTargetRef.apiVersion: apps/v1,忘记加s了。这里要和Deployment的apiVersion 一致。
[root@k8s-master-1 test]# kubectl get hpa
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
hpa-demo Deployment/hpa-demo 0%/10% 1 10 1 11m
[root@k8s-master-1 test]# kubectl describe hpa
Name: hpa-demo
Namespace: default
Labels: <none>
Annotations: <none>
CreationTimestamp: Fri, 05 May 2023 13:55:49 +0800
Reference: Deployment/hpa-demo
Metrics: ( current / target )
resource cpu on pods (as a percentage of request): 0% (0) / 10%
Min replicas: 1
Max replicas: 10
Deployment pods: 1 current / 1 desired
Conditions:
Type Status Reason Message
---- ------ ------ -------
AbleToScale True ScaleDownStabilized recent recommendations were higher than current one, applying the highest recent recommendation
ScalingActive True ValidMetricFound the HPA was able to successfully calculate a replica count from cpu resource utilization (percentage of request)
ScalingLimited False DesiredWithinRange the desired count is within the acceptable range
压力测试
暴露端口给service
#暴露端口给service
[root@k8s-master-1 test]# kubectl expose deployment hpa-demo --port=80 --target-port=80^C
[root@k8s-master-1 test]#
[root@k8s-master-1 test]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
hpa-demo ClusterIP 10.0.0.56 <none> 80/TCP 21m
kubernetes ClusterIP 10.0.0.1 <none> 443/TCP 15d
[root@k8s-master-1 test]#
[root@k8s-master-1 test]# kubectl exec -it busybox -- sh
/ #
/ # while true; do wget -q -O- 10.0.0.56; done
#在查看hpa 数量在增加
[root@k8s-master-1 ~]#
[root@k8s-master-1 ~]# kubectl get hpa
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
hpa-demo Deployment/hpa-demo 316%/10% 1 10 1 30m
[root@k8s-master-1 ~]# kubectl get deploy
NAME READY UP-TO-DATE AVAILABLE AGE
hpa-demo 8/8 8 8 13m
web 1/1 1 1 11d
[root@k8s-master-1 ~]#
[root@k8s-master-1 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
busybox 1/1 Running 0 94s
hpa-demo-f45447f69-59zsv 1/1 Running 0 13m
hpa-demo-f45447f69-5nd8k 1/1 Running 0 22s
hpa-demo-f45447f69-9whvx 1/1 Running 0 7s
hpa-demo-f45447f69-pv7gb 1/1 Running 0 22s
hpa-demo-f45447f69-qwcrp 1/1 Running 0 7s
hpa-demo-f45447f69-tk4x9 1/1 Running 0 7s
hpa-demo-f45447f69-wvrt8 1/1 Running 0 22s
hpa-demo-f45447f69-xnrcd 1/1 Running 0 7s
web-96d5df5c8-vmxgr 1/1 Running 0 5h51m
#接下来我们ctrl+c取消压力测试,过1分钟,甚至更久就看到cpu和pod数量都回去了
[root@k8s-master-1 test]# kubectl get pod
NAME READY STATUS RESTARTS AGE
hpa-demo-f45447f69-59zsv 1/1 Running 0 19m
web-96d5df5c8-vmxgr 1/1 Running 0 5h57m
[root@k8s-master-1 test]#
[root@k8s-master-1 test]# kubectl get deploy
NAME READY UP-TO-DATE AVAILABLE AGE
hpa-demo 1/1 1 1 19m
web 1/1 1 1 11d
[root@k8s-master-1 test]#
[root@k8s-master-1 test]# kubectl get hpa
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
hpa-demo Deployment/hpa-demo 0%/10% 1 10 1 36m
????????创建deployment
apiVersion: apps/v1
kind: Deployment
metadata:
name: hpa-demo
spec:
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.15.3
ports:
- containerPort: 80
resources:
requests:
memory: 25Mi
cpu: 0.01
limits:
memory: 60Mi
cpu: 0.05
[root@k8s-master-1 test]# kubectl apply -f deployment.yaml
deployment.apps/hpa-demo created
[root@k8s-master-1 test]# vim HorizontalPodAutoscaler.yaml
apiVersion: autoscaling/v2beta1 # v2beta1版本
kind: HorizontalPodAutoscaler
metadata:
name: hpa-demo
spec:
maxReplicas: 10
minReplicas: 1
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: hpa-demo
metrics:
- type: Resource
resource:
name: memory
targetAverageUtilization: 50 # 50%内存利用
[root@k8s-master-1 test]# kubectl apply -f HorizontalPodAutoscaler.yaml
horizontalpodautoscaler.autoscaling/hpa-demo created
[root@k8s-master-1 test]# kubectl get hpa
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
hpa-demo Deployment/hpa-demo 5%/50% 1 10 1 47s