<p>I am trying to set up a load test for an endpoint. This is what I have followed so far:</p>
<h2><code>Dockerfile</code></h2>
<pre><code>FROM python:3.8

# Add the external tasks directory into /tasks
WORKDIR /src

ADD requirements.txt .
RUN pip install --no-cache-dir --upgrade locust==2.10.1

ADD run.sh .
ADD load_test.py .
ADD load_test.conf .

# Expose the required Locust ports
EXPOSE 5557 5558 8089

# Set script to be executable
RUN chmod 755 run.sh

# Start Locust using LOCUS_OPTS environment variable
CMD [&quot;bash&quot;, &quot;run.sh&quot;]

# Modified from:
# https://github.com/scrollocks/locust-loadtesting/blob/master/locust/docker/Dockerfile
</code></pre>
<h2><code>run.sh</code></h2>
<pre class="lang-bash prettyprint-override"><code>#!/bin/bash
LOCUST=&quot;locust&quot;
LOCUS_OPTS=&quot;--config=load_test.conf&quot;
LOCUST_MODE=${LOCUST_MODE:-standalone}

if [[ &quot;$LOCUST_MODE&quot; = &quot;master&quot; ]]; then
    LOCUS_OPTS=&quot;$LOCUS_OPTS --master&quot;
elif [[ &quot;$LOCUST_MODE&quot; = &quot;worker&quot; ]]; then
    LOCUS_OPTS=&quot;$LOCUS_OPTS --worker --master-host=$LOCUST_MASTER_HOST&quot;
fi

echo &quot;${LOCUST} ${LOCUS_OPTS}&quot;

$LOCUST $LOCUS_OPTS

# Copied from
# https://github.com/scrollocks/locust-loadtesting/blob/master/locust/docker/locust/run.sh
</code></pre>
<p>This is how I have written the load test locust script:</p>
<pre class="lang-py prettyprint-override"><code>import json

from locust import HttpUser, constant, task


class CategorizationUser(HttpUser):
    wait_time = constant(1)

    @task
    def predict(self):
        payload = json.dumps(
            {
                &quot;text&quot;: &quot;Face and Body Paint washable Rubies Halloween item 91#385&quot;
            }
        )
        _ = self.client.post(&quot;/predict&quot;, data=payload)
</code></pre>
<p>I am invoking that with a configuration:</p>
<pre><code>locustfile = load_test.py
headless = false
users = 1000
spawn-rate = 1
run-time = 5m
host = IP
html = locust_report.html
</code></pre>
<p>So, after building and pushing the Docker image and creating a k8s cluster on GKE, I am deploying it. This is how the <code>deployment.yaml</code> looks like:</p>
<pre class="lang-yaml prettyprint-override"><code># Copied from 
# https://github.com/scrollocks/locust-loadtesting/blob/master/locust/kubernetes/templates/deployment.yaml

---
kind: Deployment
apiVersion: apps/v1
metadata:
  namespace: default
  name: locust-master-deployment
  labels:
    name: locust
    role: master
spec:
  replicas: 1
  selector:
    matchLabels:
      name: locust
      role: master
  template:
    metadata:
      labels:
        name: locust
        role: master
    spec:
      containers:
        - name: locust
          image: gcr.io/PROJECT_ID/IMAGE_URI
          imagePullPolicy: Always
          env:
            - name: LOCUST_MODE
              value: master
            - name: LOCUST_LOG_LEVEL
              value: DEBUG
          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

---
kind: Deployment
apiVersion: apps/v1
metadata:
  namespace: default
  name: locust-worker-deployment
  labels:
    name: locust
    role: worker
spec:
  replicas: 2
  selector:
    matchLabels:
      name: locust
      role: worker
  template:
    metadata:
      labels:
        name: locust
        role: worker
    spec:
      containers:
        - name: locust
          image: gcr.io/PROJECT_ID/IMAGE_URI
          imagePullPolicy: Always
          env:
            - name: LOCUST_MODE
              value: worker
            - name: LOCUST_MASTER
              value: locust-master-service
            - name: LOCUST_LOG_LEVEL
              value: DEBUG
</code></pre>
<p>After deployment, I am exposing the required ports like so:</p>
<pre class="lang-bash prettyprint-override"><code>kubectl expose pod locust-master-deployment-f9d4c4f59-8q6wk \
    --type NodePort \
    --port 5558 \
    --target-port 5558 \
    --name locust-5558

kubectl expose pod locust-master-deployment-f9d4c4f59-8q6wk \
    --type NodePort \
    --port 5557 \
    --target-port 5557 \
    --name locust-5557

kubectl expose pod locust-master-deployment-f9d4c4f59-8q6wk \
    --type LoadBalancer \
    --port 80 \
    --target-port 8089 \
    --name locust-web
</code></pre>
<p>The cluster and the nodes provision successfully. But the moment I hit the IP of <code>locust-web</code>, I am getting:</p>
<p><a href="https://i.stack.imgur.com/Othog.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Othog.png" alt="enter image description here" /></a></p>
<p>Any suggestions on how to resolve the bug?</p>

S. P
<p>Probably the environment variables set by k8s is colliding with locust’s (LOCUST_WEB_PORT specifically). Change your setup so that no containers are named ”locust”.</p>
<p>See <a href="https://github.com/locustio/locust/issues/1226" rel="nofollow noreferrer">https://github.com/locustio/locust/issues/1226</a> for a similar issue.</p>
