先运行几个pods.
代码语言:javascript复制apiVersion: apps/v1
kind: Deployment
metadata:
name: dp-tomcat
namespace: learning
spec:
revisionHistoryLimit: 15
replicas: 1
selector:
matchLabels:
app: dp-tomcat
matchExpressions:
- key: app
operator: In
values: [dp-tomcat]
template:
metadata:
labels:
app: dp-tomcat
spec:
nodeSelector:
kubernetes.io/hostname: dev-k8s-05.xsl.link
containers:
- name: dp-tomcat
image: tomcat:9.0
resources:
limits:
memory: "1024Mi"
cpu: "500m"
ports:
- containerPort: 8080
name: web
上面的yaml说明一下:
- 运行了一个副本数为3的deployment
- 暴露出一个名为nginxsvc的service
- 指定了`revisionHistoryLimit`表示保留历史版本的个数
- 因为要通过资源负载模拟HPA功能的场景,必须依赖template中对pod做资源限制
- 集群环境需要安装metrics-server,方便观察集群资源负载
➜ 00-template.resources.yaml git:(master) ✗ (☸ kubernetes-admin@kubernetes:default) k get pods -n learning
NAME READY STATUS RESTARTS AGE
abcontainer-58c67cfb87-g4j8q 1/1 Running 0 25d
dp-tomcat-5b4465b6bf-82llc 1/1 Running 0 125m
dp-tomcat-5b4465b6bf-zsttg 1/1 Running 0 126m
nginx-deployment-8558b4659-6dkmr 1/1 Running 0 17s
nginx-deployment-8558b4659-v8fzf 1/1 Running 0 17s
nginx-deployment-8558b4659-w5tlq 1/1 Running 0 8m56s
定义一个HPA资源对象
代码语言:javascript复制apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
name: dp-tomcat
namespace: learning
spec:
minReplicas: 1
maxReplicas: 10
scaleTargetRef:
kind: Deployment
name: dp-tomcat
apiVersion: apps/v1
targetCPUUtilizationPercentage: 10
运行一个客户端程序
代码语言:javascript复制apiVersion: apps/v1
kind: Deployment
metadata:
name: abcontainer
namespace: abcontainer
spec:
selector:
matchLabels:
app: abcontainer
template:
metadata:
labels:
app: abcontainer
spec:
containers:
- name: abcontainer
image: httpd
ports:
- containerPort: 80
进入到abcontainer通过ab增加负载
代码语言:javascript复制k exec abcontainer-58c67cfb87-g4j8q -n learning -it -- bash
压力测试
代码语言:javascript复制ab -c 5000 -n 2000000 http://tomcat-svc:8080/
查看HPA资源变化
代码语言:javascript复制NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
dp-tomcat Deployment/dp-tomcat 0%/10% 1 10 1 16m
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
dp-tomcat Deployment/dp-tomcat 98%/10% 1 10 1 18m
# 中断ab测试
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
dp-tomcat Deployment/dp-tomcat 36%/10% 1 10 10 19m
# pods的变化
NAME READY STATUS RESTARTS AGE
abcontainer-58c67cfb87-g4j8q 1/1 Running 0 25d
dp-tomcat-96b7b9f6f-54v84 0/1 Pending 0 13s
dp-tomcat-96b7b9f6f-55hsf 0/1 ContainerCreating 0 13s
dp-tomcat-96b7b9f6f-btbzr 0/1 ContainerCreating 0 13s
dp-tomcat-96b7b9f6f-dpppj 0/1 ContainerCreating 0 13s
dp-tomcat-96b7b9f6f-k4jqq 1/1 Running 0 12m
dp-tomcat-96b7b9f6f-mxd4f 1/1 Running 0 28s
dp-tomcat-96b7b9f6f-tjpj6 1/1 Running 0 28s
dp-tomcat-96b7b9f6f-zztqm 1/1 Running 0 28s
默认情况下,当流量下降,5分钟后pod的数量会慢慢恢复到replicas的值