这篇文章主要介绍了kubernetes中如何实现分布式负载测试Locust,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。
一: 前言
本文介绍如何在Kubernetes集群中对一个应用进行分布式测试,sample-webapp是一个简单的web测试应用。测试工具使用Locust.
二:Locust 介绍
Locust是一个用于可扩展的,分布式的,性能测试的,开源的,用Python编写框架/工具.
在Locust测试框架中,测试场景是采用纯Python脚本进行描述的。对于最常见的HTTP(S)协议的系统,Locust采用Python的requests库作为客户端,使得脚本编写大大简化,富有表现力的同时且极具美感。而对于其它协议类型的系统,Locust也提供了接口,只要我们能采用Python编写对应的请求客户端,就能方便地采用Locust实现压力测试。从这个角度来说,Locust可以用于压测任意类型的系统。
在模拟有效并发方面,Locust的优势在于其摒弃了进程和线程,完全基于事件驱动,使用gevent提供的非阻塞IO和coroutine来实现网络层的并发请求,因此即使是单台压力机也能产生数千并发请求数;再加上对分布式运行的支持,理论上来说,Locust能在使用较少压力机的前提下支持极高并发数的测试。
Locust脚本示例:
-
from locust import HttpLocust, TaskSet, task
-
class WebsiteTasks(TaskSet):
-
def on_start(self):
-
self.client.post("/login", {
-
"username": "test",
-
"password": "123456"
-
})
-
@task(2)
-
def index(self):
-
self.client.get("/")
-
@task(1)
-
def about(self):
-
self.client.get("/about/")
-
class WebsiteUser(HttpLocust):
-
task_set = WebsiteTasks
-
host = "http://debugtalk.com"
-
min_wait = 1000
-
max_wait = 5000
在这个示例中,定义了针对http://debugtalk.com网站的测试场景:先模拟用户登录系统,然后随机地访问首页(/)和关于页面(/about/),请求比例为2:1;并且,在测试过程中,两次请求的间隔时间为1~5秒间的随机值。
三:部署测试WEB应用
sample-webapp-controller.yaml
-
kind: ReplicationController
-
apiVersion: v1
-
metadata:
-
name: sample-webapp
-
namespace: kube-system
-
labels:
-
name: sample-webapp
-
spec:
-
selector:
-
name: sample-webapp
-
replicas: 1
-
template:
-
metadata:
-
labels:
-
name: sample-webapp
-
spec:
-
containers:
-
– name: sample-webapp
-
image: index.tenxcloud.com/jimmy/k8s-sample-webapp:latest
-
ports:
-
– containerPort: 8000
sample-webapp-service.yaml
-
kind: Service
-
apiVersion: v1
-
metadata:
-
name: sample-webapp
-
namespace: kube-system
-
labels:
-
name: sample-webapp
-
spec:
-
ports:
-
– port: 8000
-
selector:
-
name: sample-webapp
kubectl create -f sample-webapp-controller.yaml
kubectl create -f sample-webapp-service.yaml
四:部署Locust
locust-master-controller.yaml
-
kind: ReplicationController
-
apiVersion: v1
-
metadata:
-
name: locust-master
-
namespace: kube-system
-
labels:
-
name: locust
-
role: master
-
spec:
-
replicas: 1
-
selector:
-
name: locust
-
role: master
-
template:
-
metadata:
-
labels:
-
name: locust
-
role: master
-
spec:
-
containers:
-
– name: locust
-
image: index.tenxcloud.com/jimmy/locust-tasks:latest
-
env:
-
– name: LOCUST_MODE
-
value: master
-
– name: TARGET_HOST
-
value: http://sample-webapp:8000
-
ports:
-
– name: loc-master-web
-
containerPort: 8089
-
protocol: TCP
-
– name: loc-master-p1
-
containerPort: 5557
-
protocol: TCP
-
– name: loc-master-p2
-
containerPort: 5558
-
protocol: TCP
locust-master-service.yaml
-
kind: Service
-
apiVersion: v1
-
metadata:
-
name: locust-master
-
namespace: kube-system
-
labels:
-
name: locust
-
role: master
-
spec:
-
ports:
-
– port: 8089
-
targetPort: loc-master-web
-
protocol: TCP
-
name: loc-master-web
-
– port: 5557
-
targetPort: loc-master-p1
-
protocol: TCP
-
name: loc-master-p1
-
– port: 5558
-
targetPort: loc-master-p2
-
protocol: TCP
-
name: loc-master-p2
-
selector:
-
name: locust
-
role: master
locust-worker-controller.yaml
-
kind: ReplicationController
-
apiVersion: v1
-
metadata:
-
name: locust-worker
-
namespace: kube-system
-
labels:
-
name: locust
-
role: worker
-
spec:
-
replicas: 1
-
selector:
-
name: locust
-
role: worker
-
template:
-
metadata:
-
labels:
-
name: locust
-
role: worker
-
spec:
-
containers:
-
– name: locust
-
image: index.tenxcloud.com/jimmy/locust-tasks:latest
-
env:
-
– name: LOCUST_MODE
-
value: worker
-
– name: LOCUST_MASTER
-
value: locust-master
-
– name: TARGET_HOST
-
value: http://sample-webapp:8000
kubectl create -f locust-master-controller.yaml
kubectl create -f locust-master-service.yaml
kubectl create -f locust-worker-controller.yaml
五:配置Traefik
-
– host: locust.donkey
-
http:
-
paths:
-
– path: /
-
backend:
-
serviceName: locust-master
-
servicePort: 8089
kubectl replace -f ingress.yaml
六:执行测试
访问http://locust.donkey/ 设置测试参数,进行测试
感谢你能够认真阅读完这篇文章,希望小编分享的“kubernetes中如何实现分布式负载测试Locust”这篇文章对大家有帮助,同时也希望大家多多支持云搜网,关注云搜网行业资讯频道,更多相关知识等着你来学习!