首发:https://mp.weixin.qq.com/s/eWwT1xBvnqvmHMXTVZ7KUA
公众号:程序员架构进阶
直到现在,都还停留在动态扩容的门口,本篇将开始正式尝试动态扩容实现。kubectl 本身提供了 scale 和 autoscale 命令用于扩容和自动扩容,下面就重点介绍这两种方式。
kubectl scale 命令可以支持动态扩容。可以通过 kubectl scale rc 针对 replication controller 进行扩容;也可以针对 deployment,通过--current-replicas=1 --replicas=3 参数调整副本数量。为了示例,需要先部署一个应用,这里使用 nginx。
创建文件:nginx-namespace.yaml
apiVersion: v1 #类型为Namespace
kind: Namespace #类型为Namespace
metadata:
name: ns-test #命名空间名称
labels:
name: label-test #pod标签
创建文件:nginx-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
namespace: ns-test
name: nginx-deployment
spec:
selector:
matchLabels:
app: nginx
replicas: 2
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:alpine
ports:
- containerPort: 80
flamingskys$ kubectl create -f nginx-namespace.yaml
namespace/ns-test created
flamingskys$ kubectl get namespace
NAME STATUS AGE
default Active 2d
kube-node-lease Active 2d
kube-public Active 2d
kube-system Active 2d
ns-test Active 5s
flamingskys$ kubectl create -f nginx-deployment.yaml
deployment.apps/nginx-deployment created
flamingskys$ kubectl get deployment -n ns-test
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-deployment 2/2 2 2 4m36s
针对 nginx-deployment 进行扩容,当前副本数量 2,扩容到 3 个副本:
flamingskys$ kubectl scale -n ns-test --current-replicas=2 --replicas=3 deployment/nginx-deployment
deployment.apps/nginx-deployment scaled
需要注意的是,--current-replicas=2 参数一定注意,与当前副本数相同,否则会报错:
flamingskys$ kubectl scale -n ns-test --current-replicas=1 --replicas=3 deployment/nginx-deployment
error: Expected replicas to be 1, was 2
autoscale 命令用于自动扩展确认,跟 scale 不同的是前者还是需要手动执行,而 autoscale 则会根据负载进行调解。而这条命令则可以对 Deployment/ReplicaSet/RC 进行设定,通过最小值和最大值的指定进行设定。
kubectl create deployment web --image=nginx:1.14 -o yaml --dry-run=client >deploy-web.yaml
内容如下:
apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
app: web
name: web
spec:
replicas: 1
selector:
matchLabels:
app: web
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
app: web
spec:
containers:
- image: nginx:1.14
name: nginx
resources: {}
status: {}
修改 deploy-web.yaml 文件,增加 pod 限制。修改 resources 内容:
apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
app: web
name: web
spec:
replicas: 1
selector:
matchLabels:
app: web
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
app: web
spec:
containers:
- image: nginx:1.14
name: nginx
resources:
limits:
cpu: 200m
memory: 200Mi
requests:
cpu: 200m
memory: 200Mi
status: {}
flamingskys$ kubectl create -f deploy-web.yaml
deployment.apps/web created
kubectl autoscale deployment web --min=2 --max=10 -o yaml --dry-run=client > hpa-web.yaml
内容:
apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
creationTimestamp: null
name: web
spec:
maxReplicas: 10
minReplicas: 2
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: web
status:
currentReplicas: 0
desiredReplicas: 0
在 scaleTargetRef 后,增加 targetCPUUtilizationPercentage: 40
apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
creationTimestamp: null
name: web
spec:
maxReplicas: 10
minReplicas: 2
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: web
targetCPUUtilizationPercentage: 40
status:
currentReplicas: 0
desiredReplicas: 0
autoscale flamingskys$ kubectl create -f hpa-web.yaml
horizontalpodautoscaler.autoscaling/web created
查看信息,可见 web 已经扩容成 2 个(注意:各环境不同,本地测试时,会扩容,但可能有延迟且时间较长。测试环境最多有1分钟左右):
autoscale flamingskys$ kubectl get pods
NAME READY STATUS RESTARTS AGE
web-5bb6fd4c98-fqpwc 1/1 Running 0 26s
web-5bb6fd4c98-wks5t 1/1 Running 0 6m25s
测试使用 apache 的 ab 工具进行压力测试,并观察在指定压力下扩容效果:
创建 service-web.yaml 文件,内容如下。注意,port 我们使用了非 80 端口,目的是避免与本地已启动的其他 80 端口应用冲突。
apiVersion: v1
kind: Service
metadata:
creationTimestamp: null
labels:
app: web
name: web
spec:
ports:
- name: web
port: 8099
protocol: TCP
targetPort: 80
selector:
app: web
type: NodePort
status:
loadBalancer: {}
flamingskys$ kubectl create -f service-web.yaml
service/web created
查看刚刚创建的资源:
autoscale flamingskys$ kubectl get pod,deploy,svc,hpa -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
pod/web-5bb6fd4c98-fqpwc 1/1 Running 0 29m 10.1.0.15 docker-desktop <none> <none>
pod/web-5bb6fd4c98-wks5t 1/1 Running 0 35m 10.1.0.14 docker-desktop <none> <none>
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
deployment.apps/web 2/2 2 2 35m nginx nginx:1.14 app=web
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 2d2h <none>
service/web NodePort 10.111.71.90 <none> 80:32183/TCP 25s app=web
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
horizontalpodautoscaler.autoscaling/web Deployment/web <unknown>/40% 2 10 2 30m
查看 service 详情:
autoscale flamingskys$ kubectl get services
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 2d4h
web NodePort 10.106.46.111 <none> 8099:31856/TCP 5s
重点在 PORT(S)列,web 使用的是 8099:31856/TCP 端口,所以本地访问路径为:http://localhost:31856/,看到 nginx 的 welcome 输出后表示一切正常。
命令说明:
ab -n 20000 -c 1000 "http://jd.com/"
其中参数-n:请求数;-c:并发数
本地压测命令:
ab -n 200000 -c 20 "http://localhost:30029/"
执行过程中可以通过 kubectl logs web-569c7c8cb6-pw25d -f 查看 pod 日志,观察多个 pod 的请求情况。
在压测过程中,通过 top 命令查看实时 cpu 和内存使用情况,以及 pod 扩容进度。至此,两种 kubectl 提供的扩容和自动扩容方法介绍完毕。接下来的文章,将探索对接 prometheus 的自定义指标感知及触发自动扩容方法。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。