Horizontal Pod Autoscaling(Pod水平自动伸缩),简称HPA。HAP通过监控分析RC或者Deployment控制的所有Pod的负载变化情况来确定是否需要调整Pod的副本数量,这是HPA最基本的原理。
当你创建了HPA后,HPA会从metrics-server或者用户自定义的监控获取每一个一个Pod利用率或原始值的平均值,然后和HPA中定义的指标进行对比,同时计算出需要伸缩的具体值并进行相应的操作
参考:https://cloud.tencent.com/developer/article/1492864
当前1.15.2的k8s放弃了heapster,而使用metrics-server实现HPA
1、准备hpa的yaml文件
下载:https://github.com/kubernetes-incubator/metrics-server
进入yaml目录: cd metrics-server/deploy/1.8+/
编辑metrics-server-deployment.yaml
修改其中的
image: 192.168.100.89:80/metrics-server-amd64:v0.3.3
imagePullPolicy: IfNotPresent
command:
- /metrics-server
- --metric-resolution=30s
- --kubelet-preferred-address-types=InternalIP,Hostname,InternalDNS,ExternalDNS,ExternalIP
- --kubelet-insecure-tls
image修改后主要作用是拉取自己的镜像
iamgePullPolicy修改镜像拉取策略
command添加命令和相关参数:
- /metrics-server选择目录
- –metric-resolution=30s 从 kubelet 采集数据的周期 30s
- –kubelet-preferred-address-types优先使用 InternalIP 来访问 kubelet,这样可以避免节点名称没有 DNS 解析记录时,通过节点名称调用节点 kubelet API 失败的情况(未配置时默认的情况)
- –kubelet-insecure-tls不验证客户端证书
---
apiVersion: v1
kind: ServiceAccount
metadata:
name: metrics-server
namespace: kube-system
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: metrics-server
namespace: kube-system
labels:
k8s-app: metrics-server
spec:
selector:
matchLabels:
k8s-app: metrics-server
template:
metadata:
name: metrics-server
labels:
k8s-app: metrics-server
spec:
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: 192.168.100.89:80/metrics-server-amd64:v0.3.3
imagePullPolicy: IfNotPresent
command:
- /metrics-server
- --metric-resolution=30s
- --kubelet-preferred-address-types=InternalIP,Hostname,InternalDNS,ExternalDNS,ExternalIP
- --kubelet-insecure-tls
volumeMounts:
- name: tmp-dir
mountPath: /tmp
部署metrics-server:
kubectl apply -f .
2、创建一个deployment测试 kube-hpa-demo.yaml
加 resources,cpu这部分,否则后面查看hpa的时候,初始化是unknow
---
apiVersion: extensions/v1beta1
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: kube-hpa-demo
spec:
selector:
matchLabels:
app: hpa-demo
revisionHistoryLimit: 10
template:
metadata:
labels:
app: hpa-demo
spec:
containers:
- name: kube-hpa-demo
image: 172.16.10.190:8008/helloworld:0.0.8
resources:
requests:
cpu: 100m
ports:
- containerPort: 8080
imagePullSecrets:
- name: myregistrykey6
minReadySeconds: 10
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 1
maxUnavailable: 1
apply create -f kube-heapster-demo.yaml
3、使用自动扩容命令
kubectl autoscale deployment kube-hpa-demo --cpu-percent=10 --min=2 --max=4
4、查看
[root@k8s-node1 ~]# kubectl get hpa
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
kube-hpa-demo Deployment/kube-hpa-demo 1%/30% 2 4 2 9m57s
[root@k8s-node1 ~]# kubectl get deployment kube-hpa-demo
NAME READY UP-TO-DATE AVAILABLE AGE
kube-hpa-demo 2/2 2 2 21m
[root@k8s-node1 ~]# kubectl describe hpa kube-hpa-demo
Name: kube-hpa-demo
Namespace: default
Labels: <none>
Annotations: <none>
CreationTimestamp: Fri, 30 Aug 2019 10:28:29 +0800
Reference: Deployment/kube-hpa-demo
Metrics: ( current / target )
resource cpu on pods (as a percentage of request): 1% (1m) / 30%
Min replicas: 2
Max replicas: 4
Deployment pods: 2 current / 2 desired
Conditions:
Type Status Reason Message
---- ------ ------ -------
AbleToScale True ReadyForNewScale recommended size matches current size
ScalingActive True ValidMetricFound the HPA was able to successfully calculate a replica count from cpu resource utilization (percentage of request)
ScalingLimited True TooFewReplicas the desired replica count is more than the maximum replica count
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal SuccessfulRescale 6m6s horizontal-pod-autoscaler New size: 2; reason: All metrics bel
5、删除扩容
[root@k8s-node1 k8s]# kubectl delete hpa kube-hpa-demo
horizontalpodautoscaler.autoscaling "kube-hpa-demo" deleted