Horizontal Pod Autoscaling(Pod水平自动伸缩),简称HPA。HAP通过监控分析RC或者Deployment控制的所有Pod的负载变化情况来确定是否需要调整Pod的副本数量,这是HPA最基本的原理。
HPA在kubernetes集群中被设计成一个controller,HPA Controller默认30s轮询一次(可通过kube-controller-manager的标志--horizontal-pod-autoscaler-sync-period进行设置),查询指定的资源(RC或者Deployment)中Pod的资源使用率,并且与创建时设定的值和指标做对比,从而实现自动伸缩的功能。
HPA可以从两个地方获取数据:
1、Heapster:仅支持CPU使用率
2、自定义监控
安装配置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数据库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 #增加
修改后自动生效
4、创建应用pod用户测试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、针对上述pod创建hpa
$ kubectl autoscale deployment hpa-nginx-deploy --cpu-percent=10 --min=1 --max=10
设置最小的 pod 副本数为1,大为10。HPA会根据设定的 cpu使用率(10%)动态的增加或者减少pod数量。
$ 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、用压力测试查看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
在另一个终端查看
$ kubectl get hpa
$ kubectl get deployment hpa-nginx-deploy
$ kubectl delete deployments.apps load-generator
另外有需要云服务器可以了解下创新互联cdcxhl.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
本文标题:用heapster实现HPA-创新互联
链接分享:https://www.cdcxhl.com/article6/cshdig.html
成都网站建设公司_创新互联,为您提供静态网站、定制网站、ChatGPT、网站改版、网站导航、网站营销
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联