资源的自动申缩,有了资源的使用指标数据后,根据设定的指标比如当cpu使用率达到50%就开始自动扩展
创建一个hpa资源(针对核心资源的监控)
#使用命令的方式创建kubectl autoscale deploy nginx-hpa --min=1 --max=8 --cpu-percent=50#使用资源清单格式创建(yaml的文件)cat nginx-autoscale-hpa.yamlapiVersion: autoscaling/v1kind: HorizontalPodAutoscalermetadata: name: nginx-hpaspec: maxReplicas: 8 minReplicas: 1 scaleTargetRef: apiVersion: extensions/v1beta1 kind: Deployment name: nginx-hpa targetCPUUtilizationPercentage: 50
创建一个deploy和svc资源
cat nginx-hpa.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-hpa
spec:
replicas: 1
selector:
matchLabels:
app: nginx-hpa
release: canary
template:
metadata:
labels:
app: nginx-hpa
release: canary
spec:
containers:
- name: nginx-hpa
image: ikubernetes/myapp:v3
ports:
- name: http
containerPort: 80
resources:
limits:
cpu: 50m
memory: 256Mi
requests:
cpu: 50m
memory: 256Mi
---
apiVersion: v1
kind: Service
metadata:
name: nginx-hpa-svc
namespace: default
spec:
selector:
app: nginx-hpa
release: canary
type: NodePort
ports:
- port: 80
targetPort: 80
protocol: TCP
对上述svc做ab压测
yum install http-tools -y
ab -c 50 -n 50000 http://10.111.229.8/index.html
32329此端口为svc映射物理机的端口
创建一个针对cpu和内存监控的hpa
cat nginx-autoscale-hpa-v2.yaml
apiVersion: autoscaling/v2beta1
kind: HorizontalPodAutoscaler
metadata:
name: nginx-hpa-v2
spec:
maxReplicas: 8
minReplicas: 1
scaleTargetRef:
apiVersion: extensions/v1beta1
kind: Deployment
name: nginx-hpa
metrics:
- type: Resource
resource:
name: cpu
targetAverageUtilization: 55
- type: Resource
resource:
name: memory
targetAverageValue: 50Mi