Horizontal Pod Autoscaling(Pod水平自動(dòng)伸縮),簡(jiǎn)稱HPA。HAP通過監(jiān)控分析RC或者Deployment控制的所有Pod的負(fù)載變化情況來確定是否需要調(diào)整Pod的副本數(shù)量,這是HPA最基本的原理。
HPA在kubernetes集群中被設(shè)計(jì)成一個(gè)controller,HPA Controller默認(rèn)30s輪詢一次(可通過kube-controller-manager的標(biāo)志--horizontal-pod-autoscaler-sync-period進(jìn)行設(shè)置),查詢指定的資源(RC或者Deployment)中Pod的資源使用率,并且與創(chuàng)建時(shí)設(shè)定的值和指標(biāo)做對(duì)比,從而實(shí)現(xiàn)自動(dòng)伸縮的功能。
HPA可以從兩個(gè)地方獲取數(shù)據(jù):
1、Heapster:僅支持CPU使用率
2、自定義監(jiān)控
成都創(chuàng)新互聯(lián)堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:網(wǎng)站制作、網(wǎng)站建設(shè)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時(shí)代的都安網(wǎng)站設(shè)計(jì)、移動(dòng)媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!
安裝配置heapster
https://github.com/kubernetes-retired/heapster/tree/v1.4.2/deploy/kube-config/influxdb
$ docker pull docker.io/zhaoqing/heapster-amd64:1.4.2
$ docker pull docker.io/pupudaye/heapster-influxdb-amd64:v1.3.3
1、安裝heapster數(shù)據(jù)庫influxdb
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: monitoring-influxdb
namespace: kube-system
spec:
replicas: 1
template:
metadata:
labels:
task: monitoring
k8s-app: influxdb
spec:
containers:
- name: influxdb
image: docker.io/pupudaye/heapster-influxdb-amd64:v1.3.3
volumeMounts:
- mountPath: /data
name: influxdb-storage
volumes:
- name: influxdb-storage
emptyDir: {}
---
apiVersion: v1
kind: Service
metadata:
labels:
task: monitoring
kubernetes.io/cluster-service: 'true'
kubernetes.io/name: monitoring-influxdb
name: monitoring-influxdb
namespace: kube-system
spec:
ports:
- port: 8086
targetPort: 8086
selector:
k8s-app: influxdb
2、安裝heapster
---
apiVersion: v1
kind: ServiceAccount
metadata:
name: heapster
namespace: kube-system
---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
name: dongyali-sa2-clusterrolebinding
subjects:
- kind: ServiceAccount
name: heapster
namespace: kube-system
roleRef:
kind: ClusterRole
name: cluster-admin
apiGroup: rbac.authorization.k8s.io
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: heapster
namespace: kube-system
spec:
replicas: 1
template:
metadata:
labels:
task: monitoring
k8s-app: heapster
spec:
serviceAccountName: heapster
containers:
- name: heapster
image: docker.io/zhaoqing/heapster-amd64:1.4.2
imagePullPolicy: IfNotPresent
command:
- /heapster
- --source=kubernetes:https://kubernetes.default
- --sink=influxdb:http://monitoring-influxdb.kube-system.svc:8086
---
apiVersion: v1
kind: Service
metadata:
labels:
task: monitoring
kubernetes.io/cluster-service: 'true'
kubernetes.io/name: Heapster
name: heapster
namespace: kube-system
spec:
ports:
- port: 80
targetPort: 8082
selector:
k8s-app: heapster
3、修改kube-controller-manager.yaml
$ cd /etc/kubernetes/manifests/
$ vim kube-controller-manager.yaml
spec:
containers:
- command:
- kube-controller-manager
- --horizontal-pod-autoscaler-use-rest-clients=false #增加
修改后自動(dòng)生效
4、創(chuàng)建應(yīng)用pod用戶測(cè)試hpa
apiVersion: apps/v1beta1
kind: Deployment
metadata:
name: hpa-nginx-deploy
labels:
app: nginx-demo
spec:
revisionHistoryLimit: 15
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
resources:
requests:
cpu: 100m
ports:
- containerPort: 80
5、針對(duì)上述pod創(chuàng)建hpa
$ kubectl autoscale deployment hpa-nginx-deploy --cpu-percent=10 --min=1 --max=10
設(shè)置最小的 pod 副本數(shù)為1,最大為10。HPA會(huì)根據(jù)設(shè)定的 cpu使用率(10%)動(dòng)態(tài)的增加或者減少pod數(shù)量。
$ kubectl get hpa
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS
hpa-nginx-deploy Deployment/hpa-nginx-deploy 0%/10% 1 10 1
$ kubectl get hpa hpa-nginx-deploy -o yaml
$ kubectl describe hpa hpa-nginx-deploy
6、用壓力測(cè)試查看hpa變化情況
$ kubectl get pod -o wide|grep hpa
hpa-nginx-deploy-7db6976d9b-p7llf 1/1 Running 0 155m 10.244.2.250 server242.example.com
$ kubectl run -i --tty load-generator --image=busybox /bin/sh
/ # while true; do wget -q -O- http://10.244.2.250; done
在另一個(gè)終端查看
$ kubectl get hpa
$ kubectl get deployment hpa-nginx-deploy
$ kubectl delete deployments.apps load-generator