1.部署修仙业务,要求副本数量为1;
2.使用ab测试工具对修仙业务进行压力测试;
3.请实现Pod水平伸缩,当cpu压力达到95%或者内存达到90%时自动进行扩容pod副本数量(上限数量10)。
1.编写资源清单
[root@master231 horizontalpodautoscalers]# cat 03-deploy-hpa-xiuxian.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: deploy-xiuxian
spec:
replicas: 1
selector:
matchLabels:
apps: xiuxian
template:
metadata:
labels:
apps: xiuxian
spec:
containers:
- name: c1
image: registry.cn-hangzhou.aliyuncs.com/yinzhengjie-k8s/apps:v1
resources:
requests:
memory: 100Mi
cpu: 100m
---
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: hpa-memory
spec:
minReplicas: 2
maxReplicas: 10
metrics:
- type: Resource
resource:
name: memory
target:
type: Utilization
averageUtilization: 90
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: deploy-xiuxian
---
apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
name: hpa-cpu
spec:
maxReplicas: 10
minReplicas: 2
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: deploy-xiuxian
targetCPUUtilizationPercentage: 95
---
apiVersion: v1
kind: Service
metadata:
name: svc-xiuxian
spec:
type: LoadBalancer
selector:
apps: xiuxian
ports:
- port: 80
[root@master231 horizontalpodautoscalers]#
2.创建资源
[root@master231 horizontalpodautoscalers]# kubectl apply -f 03-deploy-hpa-xiuxian.yaml
deployment.apps/deploy-xiuxian created
horizontalpodautoscaler.autoscaling/hpa-memory created
horizontalpodautoscaler.autoscaling/hpa-cpu created
service/svc-xiuxian created
[root@master231 horizontalpodautoscalers]#
3.查看创建的资源
[root@master231 horizontalpodautoscalers]# kubectl get hpa,svc,deploy,po -o wide
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
horizontalpodautoscaler.autoscaling/hpa-cpu Deployment/deploy-xiuxian 0%/95% 2 10 2 46s
horizontalpodautoscaler.autoscaling/hpa-memory Deployment/deploy-xiuxian 3%/90% 2 10 2 46s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
service/kubernetes ClusterIP 10.200.0.1 <none> 443/TCP 2d4h <none>
service/svc-xiuxian LoadBalancer 10.200.71.3 10.0.0.151 80:14241/TCP 46s apps=xiuxian
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
deployment.apps/deploy-xiuxian 2/2 2 2 46s c1 registry.cn-hangzhou.aliyuncs.com/yinzhengjie-k8s/apps:v1 apps=xiuxian
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
pod/deploy-xiuxian-5c66679644-b46sl 1/1 Running 0 46s 10.100.2.211 worker233 <none> <none>
pod/deploy-xiuxian-5c66679644-fjp7h 1/1 Running 0 31s 10.100.1.171 worker232 <none> <none>
[root@master231 horizontalpodautoscalers]#
4.压力测试
[root@harbor250.oldboyedu.com ~]# apt -y install apache2-utils
温馨提示:
我们的k8s集群可能扛不住100w的请求量,最终报错如下,多重复执行几次即可,看到pod扩容效果即可。
[root@harbor250.oldboyedu.com ~]# ab -n 1000000 -c 100 http://10.0.0.151/
This is ApacheBench, Version 2.3 <$Revision: 1879490 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking 10.0.0.151 (be patient)
Completed 100000 requests
apr_socket_recv: Connection reset by peer (104) # 如果报错该信息,说明服务端扛不住啦!
Total of 138386 requests completed
[root@harbor250.oldboyedu.com ~]#
5.再次查看pod数量
如上图所示。
6.删除资源
[root@master231 horizontalpodautoscalers]# kubectl delete -f 03-deploy-hpa-xiuxian.yaml
deployment.apps "deploy-xiuxian" deleted
horizontalpodautoscaler.autoscaling "hpa-memory" deleted
horizontalpodautoscaler.autoscaling "hpa-cpu" deleted
service "svc-xiuxian" deleted
[root@master231 horizontalpodautoscalers]#