Kubernetes27--彈性伸縮--HPA實踐
參考連結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
[[email protected] hpa]# cat index.php
<?php
$x = 0.0001;
for ($i = 0; $i <= 1000000; $i++) {
$x += sqrt($x);
}
echo "OK!";
?>
準備apache基礎映象dockerfile檔案
[[email protected] 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情況
[[email protected] hpa]# kubectl get hpa
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
php-apache Deployment/php-apache <unknown>/50% 1 10 0 7s
[[email protected] 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服務
[[email protected] 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物件
[[email protected] 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
[[email protected] ~]# kubectl get hpa
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
my-app-hpa Deployment/hpa-ds 0%/50% 1 10 1 25s
併發訪問cpu以及叢集數量的變化
[[email protected] ~]# kubectl get hpa
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
my-app-hpa Deployment/hpa-ds 55%/50% 1 10 2 5m49s
[[email protected] ~]# kubectl get hpa
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
my-app-hpa Deployment/hpa-ds 32%/50% 1 10 3 6m43s
[[email protected] ~]# kubectl get hpa
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
my-app-hpa Deployment/hpa-ds 59%/50% 1 10 4 7m39s
停止訪問時
[[email protected] ~]# kubectl get hpa
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
my-app-hpa Deployment/hpa-ds 0%/50% 1 10 4 12m
[[email protected] ~]# 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
檢視日誌
[[email protected] ~]# 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>