本文主要是介绍k8s学习(九) 使用metrics-server 进行hpa扩容,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
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-servernamespace: kube-system
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:name: metrics-servernamespace: kube-systemlabels:k8s-app: metrics-server
spec:selector:matchLabels:k8s-app: metrics-servertemplate:metadata:name: metrics-serverlabels:k8s-app: metrics-serverspec:serviceAccountName: metrics-servervolumes:# mount in tmp so we can safely use from-scratch images and/or read-only containers- name: tmp-diremptyDir: {}containers:- name: metrics-serverimage: 192.168.100.89:80/metrics-server-amd64:v0.3.3imagePullPolicy: IfNotPresentcommand:- /metrics-server- --metric-resolution=30s- --kubelet-preferred-address-types=InternalIP,Hostname,InternalDNS,ExternalDNS,ExternalIP- --kubelet-insecure-tlsvolumeMounts:- name: tmp-dirmountPath: /tmp
部署metrics-server:
kubectl apply -f .
2、创建一个deployment测试 kube-hpa-demo.yaml
加 resources,cpu这部分,否则后面查看hpa的时候,初始化是unknow
---apiVersion: extensions/v1beta1---apiVersion: extensions/v1beta1kind: Deploymentmetadata:name: kube-hpa-demospec:selector:matchLabels:app: hpa-demorevisionHistoryLimit: 10 template:metadata:labels:app: hpa-demospec:containers:- name: kube-hpa-demoimage: 172.16.10.190:8008/helloworld:0.0.8resources:requests:cpu: 100mports:- containerPort: 8080imagePullSecrets:- name: myregistrykey6minReadySeconds: 10strategy:type: RollingUpdaterollingUpdate:maxSurge: 1maxUnavailable: 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 sizeScalingActive 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
这篇关于k8s学习(九) 使用metrics-server 进行hpa扩容的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!