1. 当前环境
kubernetes v1.17
metrics-server v0.3.6
要实现hpa,metrics-server 需要部署到集群中, 它可以通过 resource metrics API 对外提供度量数据,Horizontal Pod Autoscaler 正是根据此 API 来获取度量数据。
2. 部署metrics-server
k8s部署参考网站 https://github.com/kubernetes-sigs/metrics-server/tree/master/deploy/kubernetes
- metrics-server不能使用,报错不能解析node节点的主机名
- --kubelet-preferred-address-types=InternalIP,Hostname,InternalDNS,ExternalDNS,ExternalIP
- metrics-server报错,x509,证书是非信任的
- --kubelet-insecure-tls
加在 args 的参数里面,args 片段如下
args:
- --cert-dir=/tmp
- --secure-port=4443
- --kubelet-preferred-address-types=InternalIP,Hostname,InternalDNS,ExternalDNS,ExternalIP
- --kubelet-insecure-tls
3. 部署一个应用与hpa
hpa 全拼 horizontal pod autoscaler,可以实现pod根据条件实现水平扩展,比如cpu、内存、访问量等。
测试一个根据cpu的水平扩展,cpu限制的片段,限制cpu为0.02,100mCPU就是100 miliCPU,等价于0.1CPU
resources:
limits:
cpu: "20m"
requests:
cpu: "20m"
部署一个hpa
apiVersion: autoscaling/v2beta2
kind: HorizontalPodAutoscaler
metadata:
name: spring-boot-demo-deployment
namespace: default
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: spring-boot-demo-deployment
minReplicas: 1
maxReplicas: 2
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 15
status:
conditions: null
observedGeneration: 1
currentReplicas: 1
desiredReplicas: 1
currentMetrics:
- type: Resource
resource:
name: cpu
current:
averageUtilization: 0
averageValue: 0
当cpu利用率超过15%,pod会水平扩展成2个,当cpu降下来后pod又会收缩成1个。
当pod每秒超过服务1000个数据包请求时,触发扩展,样本如下。
- type: Pods
pods:
metric:
name: packets-per-second
targetAverageValue: 1k
参考网站 https://kubernetes.io/zh/docs/tasks/run-application/horizontal-pod-autoscale-walkthrough/