k8s(十二)— HPA
HPA官网[root@foundation50 k8s]# scp hpa-example.tar server1:将下载好的镜像拷贝到server1上[root@server1 ~]# docker load -i hpa-example.tar导入镜像[root@server1 ~]# docker tag mirrorgooglecontainers/hpa-example:latest r
·
HPA官网
从最基本的角度来看,Pod 水平自动扩缩控制器根据当前指标和期望指标来计算扩缩比例。
期望副本数 = ceil[当前副本数 * (当前指标 / 期望指标)]
例如,如果当前指标值为 200m,而期望值为 100m,则副本数将加倍, 因为 200.0 / 100.0 == 2.0 如果当前值为 50m,则副本数将减半, 因为 50.0 / 100.0 == 0.5。如果比率足够接近 1.0(在全局可配置的容差范围内,默认为 0.1), 则控制平面会跳过扩缩操作。
如果 HorizontalPodAutoscaler 指定的是 targetAverageValue 或 targetAverageUtilization, 那么将会把指定 Pod 度量值的平均值做为 currentMetricValue。
1 单项cpu指标扩容缩容
[root@foundation50 k8s]# scp hpa-example.tar server1: 将下载好的镜像拷贝到server1上
[root@server1 ~]# docker load -i hpa-example.tar 导入镜像
[root@server1 ~]# docker tag mirrorgooglecontainers/hpa-example:latest reg.westos.org/library/hpa-example:latest 改标签
[root@server1 ~]# docker push reg.westos.org/library/hpa-example:latest 上传镜像到仓库
[root@server2 ~]# mkdir hpa
[root@server2 ~]# cd hpa/
[root@server2 hpa]# vim hpa.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: php-apache
spec:
selector:
matchLabels:
run: php-apache
replicas: 1
template:
metadata:
labels:
run: php-apache
spec:
containers:
- name: php-apache
image: hpa-example
ports:
- containerPort: 80
resources:
limits:
cpu: 500m
requests:
cpu: 200m
---
apiVersion: v1
kind: Service
metadata:
name: php-apache
labels:
run: php-apache
spec:
ports:
- port: 80
selector:
run: php-apache
[root@server2 hpa]# kubectl apply -f hpa.yaml 创建
deployment.apps/php-apache created
service/php-apache created
[root@server2 ~]# kubectl get svc 查看svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 9d
php-apache ClusterIP 10.102.198.154 <none> 80/TCP 3m47s 创建成功
[root@server2 ~]# kubectl describe svc php-apache
[root@server2 ~]# kubectl top pod 查看pod所占用资源
NAME CPU(cores) MEMORY(bytes)
php-apache-594d676555-x7fnp 1m 5Mi
[root@server2 ~]# kubectl autoscale deployment php-apache --cpu-percent=50 --min=1 --max=10 cpu期望值到达50%,也就是cpu超过0.1m,启动新的pod,pod数范围为1-10
horizontalpodautoscaler.autoscaling/php-apache autoscaled
[root@server2 ~]# kubectl get hpa
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
php-apache Deployment/php-apache 0%/50% 1 10 1 14m cpu目标是50% ,现在只有1个副本
压测:
[root@server2 ~]# kubectl run demo -it --rm --image=busybox --restart=Never -- /bin/sh -c "while sleep 0.01; do wget -q -O- http://php-apache; done"
[root@server2 ~]# kubectl top pod 查看pod资源,pod不断拉伸
NAME CPU(cores) MEMORY(bytes)
demo 12m 0Mi
php-apache-594d676555-2vxdh 92m 8Mi
php-apache-594d676555-dxz7z 106m 8Mi
php-apache-594d676555-jmttp 99m 8Mi
php-apache-594d676555-lc7dm 96m 8Mi
php-apache-594d676555-nrq9t 90m 8Mi
php-apache-594d676555-rklfk 93m 8Mi
[root@server2 ~]# kubectl get hpa
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
php-apache Deployment/php-apache 43%/50% 1 10 7 12h cpu达到43%,扩容了8个pod
停止压测
[root@server2 ~]# kubectl top pod 查看pod资源,cpu资源已经降下来了
NAME CPU(cores) MEMORY(bytes)
php-apache-594d676555-2vxdh 1m 8Mi
php-apache-594d676555-dxz7z 1m 8Mi
php-apache-594d676555-jmttp 1m 8Mi
php-apache-594d676555-lc7dm 1m 8Mi
php-apache-594d676555-nrq9t 1m 8Mi
php-apache-594d676555-rklfk 1m 8Mi
php-apache-594d676555-z8mjh 1m 8Mi
[root@server2 ~]# kubectl top pod 再次查看pod资源,pod已经缩容了,变成了1个
NAME CPU(cores) MEMORY(bytes)
php-apache-594d676555-jmttp 1m 8Mi
2 基于多项指标的自动扩缩
[root@server2 hpa]# vim hpa2.yaml
apiVersion: autoscaling/v2beta2
kind: HorizontalPodAutoscaler
metadata:
name: php-apache
spec:
maxReplicas: 10
minReplicas: 1
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: php-apache
metrics:
- type: Resource
resource:
name: cpu
target:
averageUtilization: 60 目标值60%
type: Utilization
- type: Resource
resource:
name: memory
target:
averageValue: 50Mi 内存平均值50Mi
type: AverageValue
[root@server2 hpa]# kubectl apply -f hpa2.yaml 创建
[root@server2 hpa]# kubectl get hpa 查看hpa
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
php-example Deployment/php-example <unknown>/50Mi, <unknown>/60% 1 10 0 9m24s
[root@server2 hpa]# kubectl delete hpa php-apache 删除之前的
horizontalpodautoscaler.autoscaling "php-apache" deleted
更多推荐
已为社区贡献9条内容
所有评论(0)