您好,登錄后才能下訂單哦!
這篇文章主要介紹了kubernetes中如何實(shí)現(xiàn)分布式負(fù)載測(cè)試Locust,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
一: 前言
本文介紹如何在Kubernetes集群中對(duì)一個(gè)應(yīng)用進(jìn)行分布式測(cè)試,sample-webapp是一個(gè)簡(jiǎn)單的web測(cè)試應(yīng)用。測(cè)試工具使用Locust.
二:Locust 介紹
Locust是一個(gè)用于可擴(kuò)展的,分布式的,性能測(cè)試的,開(kāi)源的,用Python編寫框架/工具.
在Locust測(cè)試框架中,測(cè)試場(chǎng)景是采用純Python腳本進(jìn)行描述的。對(duì)于最常見(jiàn)的HTTP(S)協(xié)議的系統(tǒng),Locust采用Python的requests庫(kù)作為客戶端,使得腳本編寫大大簡(jiǎn)化,富有表現(xiàn)力的同時(shí)且極具美感。而對(duì)于其它協(xié)議類型的系統(tǒng),Locust也提供了接口,只要我們能采用Python編寫對(duì)應(yīng)的請(qǐng)求客戶端,就能方便地采用Locust實(shí)現(xiàn)壓力測(cè)試。從這個(gè)角度來(lái)說(shuō),Locust可以用于壓測(cè)任意類型的系統(tǒng)。
在模擬有效并發(fā)方面,Locust的優(yōu)勢(shì)在于其摒棄了進(jìn)程和線程,完全基于事件驅(qū)動(dòng),使用gevent提供的非阻塞IO和coroutine來(lái)實(shí)現(xiàn)網(wǎng)絡(luò)層的并發(fā)請(qǐng)求,因此即使是單臺(tái)壓力機(jī)也能產(chǎn)生數(shù)千并發(fā)請(qǐng)求數(shù);再加上對(duì)分布式運(yùn)行的支持,理論上來(lái)說(shuō),Locust能在使用較少壓力機(jī)的前提下支持極高并發(fā)數(shù)的測(cè)試。
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
在這個(gè)示例中,定義了針對(duì)http://debugtalk.com網(wǎng)站的測(cè)試場(chǎng)景:先模擬用戶登錄系統(tǒng),然后隨機(jī)地訪問(wèn)首頁(yè)(/)和關(guān)于頁(yè)面(/about/),請(qǐng)求比例為2:1;并且,在測(cè)試過(guò)程中,兩次請(qǐng)求的間隔時(shí)間為1~5秒間的隨機(jī)值。
三:部署測(cè)試WEB應(yīng)用
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
六:執(zhí)行測(cè)試
訪問(wèn)http://locust.donkey/ 設(shè)置測(cè)試參數(shù),進(jìn)行測(cè)試
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“kubernetes中如何實(shí)現(xiàn)分布式負(fù)載測(cè)試Locust”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來(lái)學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。