
[root@master ~]# vim myhpa.yaml
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: myweb
spec:
selector:
matchLabels:
app: apache
replicas: 1
template:
metadata:
labels:
app: apache
spec:
containers:
- name: apache
image: 192.168.1.100:5000/myos:httpd
ports:
- containerPort: 80
resources:
requests:
cpu: 200m
restartPolicy: Always
---
apiVersion: v1
kind: Service
metadata:
name: web-service
spec:
ports:
- protocol: TCP
port: 80
targetPort: 80
selector:
app: apache
type: ClusterIP
---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: my-app
annotations:
kubernetes.io/ingress.class: "nginx"
spec:
backend:
serviceName: web-service
servicePort: 80
---
apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
name: myweb
spec:
minReplicas: 1
maxReplicas: 3
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: myweb
targetCPUUtilizationPercentage: 50
[root@master ~]# kubectl apply -f hpa-example.yaml
[root@master ~]# kubectl get hpa
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
myweb Deployment/myweb 0%/50% 1 3 1 15m
当容器的cpu占用超过 50% 的时候,自动扩展一个POD,依次扩展,一直到最大值
如果cpu访问不足 50% 的时候,每 300s 缩减一个 POD 节点,直到最小值时停止
访问测试可以使用镜像内提供的 info.php增加系统负载,从而查看状态信息
访问地址 http://ip.xx.xx.xx/info.php?id=1000000
id 为计算结果集的数量,id 越大,占用内存和CPU越高,设置特别大容易死机
网友评论