我正在创建一个分布式蝗虫服务,用于在平台上进行基准测试和REST API测试。架构如下:
- 第一个运行带有master标志的docker镜像的pod,用于控制整个进程
- 运行带有worker标志的docker映像的pod集合,它将使工作(根据需求而变化)
部署和服务文件:
01-locust-master.yaml
apiVersion: v1
kind: Service
metadata:
name: locust-master
labels:
name: locust
spec:
type: LoadBalancer
selector:
name: locust
role: master
ports:
- port: 8089
protocol: TCP
name: master-web
- port: 5557
protocol: TCP
name: master-port1
- port: 5558
protocol: TCP
name: master-port2
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: locust-master
spec:
replicas: 1
template:
selector:
matchLabels:
name: locust
role: master
template:
metadata:
labels:
name: locust
role: master
spec:
containers:
- name: locust
image: locust-image:latest
imagePullPolicy: Always
env:
- name: LOCUST_MODE
value: master
- name: LOCUST_LOCUSTFILE_PATH
value: "/locust-tasks/locustfiles/the_file.py"
- name: LOCUST_TARGET_HOST
value: "the_endpoint"
- name: LOCUST_USERS
value: !!integerEnv 300
- name: LOCUST_SPAWN_RATE
value: !!integerEnv 100
- name: LOCUST_TEST_TIME
value: "5m"
- name: LOCUST_OUTPUT_DIR
value: "/locust-tasks/locust-output"
- name: LOCUST_TEST_API_TOKEN
value: "some_api_topken"
- name: LOCUST_S3_OUTPUT_BUCKET
value: "s3-bucket"
ports:
- containerPort: 8089
- containerPort: 5557
- containerPort: 5558
resources:
limits:
cpu: 2000m
memory: 2048Mi
02-locust-worker.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: locust-worker
spec:
replicas: 3
selector:
matchLabels:
name: locust
template:
metadata:
labels:
name: locust
role: worker
spec:
containers:
- name: locust
image: locust:latest
imagePullPolicy: Always
env:
- name: LOCUST_MODE
value: worker
- name: LOCUST_MASTER_NODE_HOST
value: locust-master
- name: LOCUST_LOCUSTFILE_PATH
value: "/locust-tasks/locustfiles/the_file.py"
- name: LOCUST_TARGET_HOST
value: "the_endpoint"
- name: LOCUST_TEST_API_TOKEN
value: "the_api_token"
- name: LOCUST_S3_OUTPUT_BUCKET
value: "s3_bucket"
resources:
limits:
cpu: 1500m
memory: 850Mi
requests:
cpu: 1200m
memory: 768Mi
DockerFile
FROM python:3.7.3
# Install packages
COPY requirements.txt /tmp/
RUN pip install --upgrade pip
RUN pip install --requirement /tmp/requirements.txt
RUN pip install awscli
# Add locustfiles
COPY common/ /locust-tasks/common/
COPY templates/ /locust-tasks/templates/
COPY locustfiles/ /locust-tasks/locustfiles/
# Set the entrypoint
COPY docker-entrypoint.sh /
RUN chmod +x /docker-entrypoint.sh
ENTRYPOINT ["/docker-entrypoint.sh"]
EXPOSE 5557 5558 8089
docker-entrypoint.sh
#!/bin/bash -x
LOCUST_MODE=${LOCUST_MODE:="standalone"}
LOCUST_MASTER=${LOCUST_MASTER:=""}
LOCUST_LOCUSTFILE_PATH=${LOCUST_LOCUSTFILE_PATH:="/locust-tasks/locustfiles/the_file.py"}
LOCUST_TARGET_HOST=${LOCUST_TARGET_HOST:="the_endpoint"}
LOCUST_OUTPUT_DIR=${LOCUST_OUTPUT_DIR:="/locust-tasks/locust-output"}
LOCUST_TEST_API_TOKEN=${LOCUST_TEST_API_TOKEN:="the_token"}
LOCUST_S3_OUTPUT_BUCKET=${LOCUST_S3_OUTPUT_BUCKET:="s3_bucket"}
cd /locust-tasks
if [[ ! -e $LOCUST_OUTPUT_DIR ]]; then
mkdir $LOCUST_OUTPUT_DIR
elif [[ ! -d $LOCUST_OUTPUT_DIR ]]; then
echo "$LOCUST_OUTPUT_DIR already exists but is not a directory" 1>&2
fi
LOCUST_PATH="/usr/local/bin/locust"
LOCUST_FLAGS="-f $LOCUST_LOCUSTFILE_PATH --host=$LOCUST_TARGET_HOST --csv=$LOCUST_OUTPUT_DIR/locust-${LOCUST_MODE}"
if [[ "$LOCUST_MODE" = "master" ]]; then
LOCUST_FLAGS="$LOCUST_FLAGS --master --headless -u $LOCUST_USERS -r $LOCUST_SPAWN_RATE -t $LOCUST_TEST_TIME"
elif [[ "$LOCUST_MODE" = "worker" ]]; then
LOCUST_FLAGS="$LOCUST_FLAGS --worker --master-host=$LOCUST_MASTER_NODE_HOST"
fi
auth_token=$LOCUST_TEST_API_TOKEN $LOCUST_PATH $LOCUST_FLAGS
# Copy test output files to S3
today=$(date +"%Y/%m/%d")
S3_OUTPUT_DIR="s3://${LOCUST_S3_OUTPUT_BUCKET}/${today}/${HOSTNAME}"
echo "Copying locust output files from [$LOCUST_OUTPUT_DIR] to S3 [$S3_OUTPUT_DIR]"
aws s3 cp --recursive $LOCUST_OUTPUT_DIR $S3_OUTPUT_DIR
retVal=$?
if [ $retVal -ne 0 ]; then
echo "Something went wrong, exit code is ${retVal}"
fi
exit $retVal
所以我的要求/想法是运行上面的脚本,然后删除整个东西。但相反,我得到和无尽的pod重新启动:
NAME READY STATUS RESTARTS AGE
locust-master-69b4547ddf-7fl4d 1/1 Running 4 23m
locust-worker-59b9689857-l5jhw 1/1 Running 4 23m
locust-worker-59b9689857-l5nd2 1/1 Running 4 23m
locust-worker-59b9689857-lwqbb 1/1 Running 4 23m
如何在shellscript结束后删除两个部署?
我想你是在找工作。
当pod成功完成时,作业跟踪成功完成。当指定数量的成功完井时到达时,任务(工作)就完成了。删除作业将进行清理创建的pod
通过指定job的.spec.ttlSecondsAfterFinished
字段,可以使用ttl机制来清理已完成的作业。
https://kubernetes.io/docs/concepts/workloads/controllers/job/ttl-mechanism-for-finished-jobs