Horizontal Pod Autoscaler 可以通过监控CPU利用率 动态调整Pod的数量 达到动态扩容和缩容的目的。
Before you begin
例子运行在Kubernetes cluster and kubectl, version 1.2 or later。 metrics-server 需要被部署到集群 因为 Horizontal Pod Autoscaler 使用 API to 收集资源利用率。部署指南metrics-server。如果想指定多种度量策略,可以升级Kubernetes cluster and kubectl 到 1.6 以上版本。
Run & expose php-apache server
为了演示Horizontal Pod Autoscaler 我们将使用基于php-apache 镜像的自定义docker镜像 dockerfile
FROM php:5-apache
ADD index.php /var/www/html/index.php
RUN chmod a+rx index.php
PHP进行了消耗CPU资源的计算
<?php $x = 0.0001; for ($i = 0; $i <= 1000000; $i++) { $x += sqrt($x); } echo "OK!"; ?>
首先运行部署镜像 并且通过以下配置将服务暴露出去
application/php-apache.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: k8s.gcr.io/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
运行以下命令:
kubectl apply -f https://k8s.io/examples/application/php-apache.yaml
deployment.apps/php-apache created
service/php-apache created
Create Horizontal Pod Autoscaler
Increase load
Stop load