参考链接https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale-walkthrough/
Horizontal Pod Autoscaler automatically scales the number of pods in a replication controller, deployment or replica set based on observed CPU utilization (or, with beta support, on some other, application-provided metrics).
首先构建一个基础镜像用来进行开方计算,会消耗大量的cpu资源
准备index.php
[root@Ac-private-1 hpa]# cat index.php
<?php
$x = 0.0001;
for ($i = 0; $i <= 1000000; $i++) {
$x += sqrt($x);
}
echo "OK!";
?>
准备apache基础镜像dockerfile文件
[root@Ac-private-1 hpa]# cat Dockerfile
FROM php:5-apache
ADD index.php /var/www/html/index.php
RUN chmod a+rx index.php
构建镜像
启动容器 限制cpu=200m 开放80端口
kubectl run php-apache --image=chenwenkai123456/hpa-example --requests=cpu=200m --expose --port=80
创建hpa策略 cpu利用率阈值50% 副本数量在1--10之间
kubectl autoscale deployment php-apache --cpu-percent=50 --min=1 --max=10
kubectl get hpa
kubectl get deployment php-apache
查看当前hpa情况
[root@Ac-private-1 hpa]# kubectl get hpa
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
php-apache Deployment/php-apache <unknown>/50% 1 10 0 7s
[root@Ac-private-1 hpa]# kubectl get deployment php-apache
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
php-apache 1 1 1 1 5m40s
使用v1 CPU利用率来自动控制
可以直接使用docker hub镜像
docker pull pilchard/hpa-example
使用yaml方式来部署php服务
[root@Ac-private-1 hpa]# cat php-svc.yaml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: hpa-ds
spec:
replicas: 1
template:
metadata:
labels:
app: hpa-ds
spec:
containers:
- name: hps-ds
image: pilchard/hpa-example
ports:
- containerPort: 80
resources:
limits:
cpu: 0.2
memory: 64Mi
---
apiVersion: v1
kind: Service
metadata:
name: hpa-svc
labels:
app: hpa-ds
spec:
ports:
- port: 80
targetPort: 80
nodePort: 30964
type: NodePort
selector:
app: hpa-ds
定义HPA对象
[root@Ac-private-1 hpa]# cat hpa.yaml
apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
name: my-app-hpa
namespace: default
spec:
scaleTargetRef:
apiVersion: extensions/v1beta1
kind: Deployment
name: hpa-ds
minReplicas: 1
maxReplicas: 10
targetCPUUtilizationPercentage: 50
访问php service暴露的NodePort
while true; do wget -q -O- http://192.168.1.16:30964; done
观察HPA的策略变化
初始时cpu利用率 集群数量1
[root@Ac-private-1 ~]# kubectl get hpa
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
my-app-hpa Deployment/hpa-ds 0%/50% 1 10 1 25s
并发访问cpu以及集群数量的变化
[root@Ac-private-1 ~]# kubectl get hpa
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
my-app-hpa Deployment/hpa-ds 55%/50% 1 10 2 5m49s
[root@Ac-private-1 ~]# kubectl get hpa
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
my-app-hpa Deployment/hpa-ds 32%/50% 1 10 3 6m43s
[root@Ac-private-1 ~]# kubectl get hpa
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
my-app-hpa Deployment/hpa-ds 59%/50% 1 10 4 7m39s
停止访问时
[root@Ac-private-1 ~]# kubectl get hpa
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
my-app-hpa Deployment/hpa-ds 0%/50% 1 10 4 12m
[root@Ac-private-1 ~]# kubectl get hpa
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
my-app-hpa Deployment/hpa-ds 0%/50% 1 10 1 15m
可知当并发访问增加时,HPA通过不断调整集群数量使得集群cpu利用率维持在50%左右。
使用v2 更多指标以及自定义指标来控制
获取v2版本的yaml文件
kubectl get hpa.v2beta2.autoscaling -o yaml > /tmp/hpa-v2.yaml
v1版本cpu利用率
apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
name: my-app-hpa
namespace: default
spec:
scaleTargetRef:
apiVersion: extensions/v1beta1
kind: Deployment
name: hpa-ds
minReplicas: 1
maxReplicas: 10
targetCPUUtilizationPercentage: 50
v2版本自定义指标
apiVersion: autoscaling/v2beta2
kind: HorizontalPodAutoscaler
metadata:
name: php-apache
namespace: default
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: php-apache
minReplicas: 1
maxReplicas: 10
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 50
添加metrics标签 type=Resource name=cpu type=Utilization
type可以选择Pods以及Object对象
apiVersion: autoscaling/v2beta1
kind: HorizontalPodAutoscaler
metadata:
name: php-apache
namespace: default
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: php-apache
minReplicas: 1
maxReplicas: 10
metrics:
- type: Resource
resource:
name: cpu
target:
kind: AverageUtilization
averageUtilization: 50
- type: Pods
pods:
metric:
name: packets-per-second
targetAverageValue: 1k
- type: Object
object:
metric:
name: requests-per-second
describedObject:
apiVersion: extensions/v1beta1
kind: Ingress
name: main-route
target:
kind: Value
value: 10k
Then, your HorizontalPodAutoscaler would attempt to ensure that each pod was consuming roughly 50% of its requested CPU, serving 1000 packets per second, and that all pods behind the main-route Ingress were serving a total of 10000 requests per second
查看日志
[root@Ac-private-1 ~]# kubectl describe hpa php-apache
Name: php-apache
Namespace: default
Labels: <none>
Annotations: <none>
CreationTimestamp: Tue, 18 Dec 2018 15:48:43 +0800
Reference: Deployment/hpa-ds
Metrics: ( current / target )
resource cpu on pods (as a percentage of request): 0% (1m) / 50%
Min replicas: 1
Max replicas: 10
Deployment pods: 1 current / 1 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 increasing faster than the maximum scale rate
Events: <none>