<p>I'm working on creating a distributed locust service for benchmarking and REST API testing in the platform. Architecture is as follows:</p>
<ol>
<li>First pod running a docker image with master flag for controlling the whole process</li>
<li>A collection of pods running a docker image with worker flag which will make the work (which can vary depending on the requeriments)</li>
</ol>
<p>Deployment and Service files are:</p>
<p><strong>01-locust-master.yaml</strong></p>
<pre><code>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: &quot;/locust-tasks/locustfiles/the_file.py&quot;
            - name: LOCUST_TARGET_HOST
              value: &quot;the_endpoint&quot;
            - name: LOCUST_USERS
              value: !!integerEnv 300
            - name: LOCUST_SPAWN_RATE
              value: !!integerEnv 100
            - name: LOCUST_TEST_TIME
              value: &quot;5m&quot;
            - name: LOCUST_OUTPUT_DIR
              value: &quot;/locust-tasks/locust-output&quot;
            - name: LOCUST_TEST_API_TOKEN
              value: &quot;some_api_topken&quot;
            - name: LOCUST_S3_OUTPUT_BUCKET
              value: &quot;s3-bucket&quot;
          ports:
            - containerPort: 8089
            - containerPort: 5557
            - containerPort: 5558
          resources:
            limits:
              cpu: 2000m
              memory: 2048Mi
</code></pre>
<p><strong>02-locust-worker.yaml</strong></p>
<pre><code>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: &quot;/locust-tasks/locustfiles/the_file.py&quot;
            - name: LOCUST_TARGET_HOST
              value: &quot;the_endpoint&quot;
            - name: LOCUST_TEST_API_TOKEN
              value: &quot;the_api_token&quot;
            - name: LOCUST_S3_OUTPUT_BUCKET
              value: &quot;s3_bucket&quot;
          resources: 
            limits:
              cpu: 1500m
              memory: 850Mi
            requests:
              cpu: 1200m
              memory: 768Mi
</code></pre>
<p><em><strong>DockerFile</strong></em></p>
<pre><code>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 [&quot;/docker-entrypoint.sh&quot;]

EXPOSE 5557 5558 8089
</code></pre>
<p><em><strong>docker-entrypoint.sh</strong></em></p>
<pre><code>#!/bin/bash -x

LOCUST_MODE=${LOCUST_MODE:=&quot;standalone&quot;}
LOCUST_MASTER=${LOCUST_MASTER:=&quot;&quot;}
LOCUST_LOCUSTFILE_PATH=${LOCUST_LOCUSTFILE_PATH:=&quot;/locust-tasks/locustfiles/the_file.py&quot;}
LOCUST_TARGET_HOST=${LOCUST_TARGET_HOST:=&quot;the_endpoint&quot;}
LOCUST_OUTPUT_DIR=${LOCUST_OUTPUT_DIR:=&quot;/locust-tasks/locust-output&quot;}
LOCUST_TEST_API_TOKEN=${LOCUST_TEST_API_TOKEN:=&quot;the_token&quot;}
LOCUST_S3_OUTPUT_BUCKET=${LOCUST_S3_OUTPUT_BUCKET:=&quot;s3_bucket&quot;}

cd /locust-tasks

if [[ ! -e $LOCUST_OUTPUT_DIR ]]; then
    mkdir $LOCUST_OUTPUT_DIR
elif [[ ! -d $LOCUST_OUTPUT_DIR ]]; then
    echo &quot;$LOCUST_OUTPUT_DIR already exists but is not a directory&quot; 1&gt;&amp;2
fi

LOCUST_PATH=&quot;/usr/local/bin/locust&quot;
LOCUST_FLAGS=&quot;-f $LOCUST_LOCUSTFILE_PATH --host=$LOCUST_TARGET_HOST --csv=$LOCUST_OUTPUT_DIR/locust-${LOCUST_MODE}&quot;

if [[ &quot;$LOCUST_MODE&quot; = &quot;master&quot; ]]; then
   LOCUST_FLAGS=&quot;$LOCUST_FLAGS --master --headless -u $LOCUST_USERS -r $LOCUST_SPAWN_RATE -t $LOCUST_TEST_TIME&quot;
elif [[ &quot;$LOCUST_MODE&quot; = &quot;worker&quot; ]]; then
    LOCUST_FLAGS=&quot;$LOCUST_FLAGS --worker --master-host=$LOCUST_MASTER_NODE_HOST&quot;
fi

auth_token=$LOCUST_TEST_API_TOKEN $LOCUST_PATH $LOCUST_FLAGS

# Copy test output files to S3
today=$(date +&quot;%Y/%m/%d&quot;)
S3_OUTPUT_DIR=&quot;s3://${LOCUST_S3_OUTPUT_BUCKET}/${today}/${HOSTNAME}&quot;

echo &quot;Copying locust output files from [$LOCUST_OUTPUT_DIR] to S3 [$S3_OUTPUT_DIR]&quot;

aws s3 cp --recursive $LOCUST_OUTPUT_DIR $S3_OUTPUT_DIR

retVal=$?
if [ $retVal -ne 0 ]; then
    echo &quot;Something went wrong, exit code is ${retVal}&quot;
fi

exit $retVal
</code></pre>
<p>So my requeriment / idea is to run the script above and after that delete the whole thing. But instead of that, I'm getting and endless pods restarting:</p>
<pre><code>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
</code></pre>
<p>How can I delete both deployments after the shellscript ends?</p>

George Fandango
<p>I think you are looking for <a href="https://kubernetes.io/docs/concepts/workloads/controllers/job/" rel="nofollow noreferrer">Jobs</a>.</p>
<blockquote>
<p>As pods successfully complete, the Job tracks the successful
completions. When a specified number of successful completions is
reached, the task (ie, Job) is complete. Deleting a Job will clean up
the Pods it created.</p>
</blockquote>
<p>You can use ttl mechanism for cleaning up the finished job by specifying the <code>.spec.ttlSecondsAfterFinished</code> field of the Job</p>
<p><a href="https://kubernetes.io/docs/concepts/workloads/controllers/job/#ttl-mechanism-for-finished-jobs" rel="nofollow noreferrer">https://kubernetes.io/docs/concepts/workloads/controllers/job/#ttl-mechanism-for-finished-jobs</a></p>
