Distributed Load Testing Using Kubernetes - GSP182

Distributed Load Testing Using Kubernetes - GSP182

Overview

In this lab you will learn how to use Kubernetes Engine to deploy a distributed load testing framework. The framework uses multiple containers to create load testing traffic for a simple REST-based API. Although this solution tests a simple web application, the same pattern can be used to create more complex load testing scenarios such as gaming or Internet-of-Things (IoT) applications. This solution discusses the general architecture of a container-based load testing framework.

System under test

For this lab the system under test is a small web application deployed to Google App Engine. The application exposes basic REST-style endpoints to capture incoming HTTP POST requests (incoming data is not persisted).

Example workloads

The application that you'll deploy is modeled after the backend service component found in many Internet-of-Things (IoT) deployments. Devices first register with the service and then begin reporting metrics or sensor readings, while also periodically re-registering with the service.

Common backend service component interaction looks like this:

A diagram depicting the interaction between the client and the application

To model this interaction, you'll use Locust, a distributed, Python-based load testing tool that is capable of distributing requests across multiple target paths. For example, Locust can distribute requests to the /login and /metrics target paths.

The workload is based on the interaction described above and is modeled as a set of Tasks in Locust. To approximate real-world clients, each Locust task is weighted. For example, registration happens once per thousand total client requests.

Container-based computing

  • The Locust container image is a Docker image that contains the Locust software.

  • A container cluster consists of at least one cluster master and multiple worker machines called nodes. These master and node machines run the Kubernetes cluster orchestration system. For more information about clusters, see the Kubernetes Engine documentation

  • A pod is one or more containers deployed together on one host, and the smallest compute unit that can be defined, deployed, and managed. Some pods contain only a single container. For example, in this lab, each of the Locust containers runs in its own pod.

  • A Deployment controller provides declarative updates for Pods and ReplicaSets. This lab has two deployments: one for locust-master and other for locust-worker.

Services

A particular pod can disappear for a variety of reasons, including node failure or intentional node disruption for updates or maintenance. This means that the IP address of a pod does not provide a reliable interface for that pod. A more reliable approach would use an abstract representation of that interface that never changes, even if the underlying pod disappears and is replaced by a new pod with a different IP address. A Kubernetes Engine service provides this type of abstract interface by defining a logical set of pods and a policy for accessing them.

In this lab there are several services that represent pods or sets of pods. For example, there is a service for the DNS server pod, another service for the Locust master pod, and a service that represents all 10 Locust worker pods.

The following diagram shows the contents of the master and worker nodes:

Contents of the master and worker nodes

What you'll learn

  • Create a system under test i.e. a small web application deployed to Google App Engine.

  • Use Kubernetes Engine to deploy a distributed load testing framework.

  • Create load testing traffic for a simple REST-based API.

Prerequisites

  • Familiarity with App Engine and Kubernetes Engine Google Cloud services.

  • Familiarity with standard Linux text editors such as Vim, Emacs or Nano.

Setup and requirements

Before you click the Start Lab button

Read these instructions. Labs are timed and you cannot pause them. The timer, which starts when you click Start Lab, shows how long Google Cloud resources will be made available to you.

This hands-on lab lets you do the lab activities yourself in a real cloud environment, not in a simulation or demo environment. It does so by giving you new, temporary credentials that you use to sign in and access Google Cloud for the duration of the lab.

To complete this lab, you need:

  • Access to a standard internet browser (Chrome browser recommended).

Note: Use an Incognito or private browser window to run this lab. This prevents any conflicts between your personal account and the Student account, which may cause extra charges incurred to your personal account.

  • Time to complete the lab---remember, once you start, you cannot pause a lab.

Note: If you already have your own personal Google Cloud account or project, do not use it for this lab to avoid extra charges to your account.

How to start your lab and sign in to the Google Cloud console

  1. Click the Start Lab button. If you need to pay for the lab, a pop-up opens for you to select your payment method. On the left is the Lab Details panel with the following:

    • The Open Google Cloud console button

    • Time remaining

    • The temporary credentials that you must use for this lab

    • Other information, if needed, to step through this lab

  2. Click Open Google Cloud console (or right-click and select Open Link in Incognito Window if you are running the Chrome browser).

    The lab spins up resources, and then opens another tab that shows the Sign in page.

    Tip: Arrange the tabs in separate windows, side-by-side.

    Note: If you see the Choose an account dialog, click Use Another Account.

  3. If necessary, copy the Username below and paste it into the Sign in dialog.

     student-01-3f72056b6745@qwiklabs.net
    

    You can also find the Username in the Lab Details panel.

  4. Click Next.

  5. Copy the Password below and paste it into the Welcome dialog.

     mNEFJPL4Y6xL
    

    You can also find the Password in the Lab Details panel.

  6. Click Next.

    Important: You must use the credentials the lab provides you. Do not use your Google Cloud account credentials.

    Note: Using your own Google Cloud account for this lab may incur extra charges.

  7. Click through the subsequent pages:

    • Accept the terms and conditions.

    • Do not add recovery options or two-factor authentication (because this is a temporary account).

    • Do not sign up for free trials.

After a few moments, the Google Cloud console opens in this tab.

Note: To view a menu with a list of Google Cloud products and services, click the Navigation menu at the top-left.

Navigation menu icon

Activate Cloud Shell

Cloud Shell is a virtual machine that is loaded with development tools. It offers a persistent 5GB home directory and runs on the Google Cloud. Cloud Shell provides command-line access to your Google Cloud resources.

  1. Click Activate Cloud Shell

    Activate Cloud Shell icon

    at the top of the Google Cloud console.

When you are connected, you are already authenticated, and the project is set to your Project_ID, qwiklabs-gcp-00-2da02550dcc0. The output contains a line that declares the Project_ID for this session:

Your Cloud Platform project in this session is set to qwiklabs-gcp-00-2da02550dcc0

gcloud is the command-line tool for Google Cloud. It comes pre-installed on Cloud Shell and supports tab-completion.

  1. (Optional) You can list the active account name with this command:
gcloud auth list
  1. Click Authorize.

Output:

ACTIVE: *
ACCOUNT: student-01-3f72056b6745@qwiklabs.net

To set the active account, run:
    $ gcloud config set account `ACCOUNT`
  1. (Optional) You can list the project ID with this command:
gcloud config list project

Output:

[core]
project = qwiklabs-gcp-00-2da02550dcc0

Note: For full documentation of gcloud, in Google Cloud, refer to the gcloud CLI overview guide.

Task 1. Set project and zone

  • Define environment variables for the project id, region and zone you want to use for the lab.
PROJECT=$(gcloud config get-value project)
REGION=us-east1
ZONE=us-east1-c
CLUSTER=gke-load-test
TARGET=${PROJECT}.appspot.com
gcloud config set compute/region $REGION
gcloud config set compute/zone $ZONE

Task 2. Get the sample code and build a Docker image for the application

  1. Get the source code from the repository by running:
gsutil -m cp -r gs://spls/gsp182/distributed-load-testing-using-kubernetes .
  1. Move into the directory:
cd distributed-load-testing-using-kubernetes/sample-webapp/
  1. Update the python version in app.yaml file:
sed -i "s/python37/python39/g" app.yaml
  1. Move back to the directory distributed-load-testing-using-kubernetes.
cd ..
  1. Build docker image and store it in container registry:
gcloud builds submit --tag gcr.io/$PROJECT/locust-tasks:latest docker-image/.

Example Output:

ID                                    CREATE_TIME                DURATION  SOURCE                                                                                                   IMAGES                                                       STATUS
47f1b8f7-0b81-492c-aa3f-19b2b32e515d  xxxxxxx  51S       gs://project_id_cloudbuild/source/1554261539.12-a7945015d56748e796c55f17b448e368.tgz  gcr.io/project_id/locust-tasks (+1 more)  SUCCESS

Click Check my progress to verify the objective.

Get the sample code and build a Docker image for the application

Check my progress

Task 3. Deploy web application

The sample-webapp folder contains a simple Google App Engine Python application as the "system under test".

  • To deploy the application to your project use the gcloud app deploy command:
gcloud app deploy sample-webapp/app.yaml

After running the command, you'll be prompted with the following.

Please choose the region where you want your App Engine application located:

From the list of regions, you can choose us-east1, since we selected us-east1 as the region for this project. For example, to choose us-central enter "10" as the input for the prompt.

Note: You will need the URL of the deployed sample web application when deploying the locust-master and locust-worker deployments which is already stored in TARGET variable.

Click Check my progress to verify the objective.

Deploy Web Application

Check my progress

Task 4. Deploy Kubernetes cluster

gcloud container clusters create $CLUSTER \
  --zone $ZONE \
  --num-nodes=5

Example output:

NAME: gke-load-test  
LOCATION:  us-east1-c  
MASTER_VERSION: 1.11.7-gke.12   
MASTER_IP: 34.66.156.246  
MACHINE_TYPE: e2-medium
NODE_VERSION: 1.11.7-gke.12  
NUM_NODES: 5          
STATUS: RUNNING

Click Check my progress to verify the objective.

Deploy Kubernetes cluster

Check my progress

Task 5. Load testing master

The first component of the deployment is the Locust master, which is the entry point for executing the load testing tasks described above. The Locust master is deployed with a single replica because we need only one master.

The configuration for the master deployment specifies several elements, including the ports that need to be exposed by the container (8089 for web interface, 5557 and 5558 for communicating with workers). This information is later used to configure the Locust workers.

The following snippet contains the configuration for the ports:

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

Task 6. Deploy locust-master

  1. Replace [TARGET_HOST] and [PROJECT_ID] in locust-master-controller.yaml and locust-worker-controller.yaml with the deployed endpoint and project-id respectively.
sed -i -e "s/\[TARGET_HOST\]/$TARGET/g" kubernetes-config/locust-master-controller.yaml
sed -i -e "s/\[TARGET_HOST\]/$TARGET/g" kubernetes-config/locust-worker-controller.yaml
sed -i -e "s/\[PROJECT_ID\]/$PROJECT/g" kubernetes-config/locust-master-controller.yaml
sed -i -e "s/\[PROJECT_ID\]/$PROJECT/g" kubernetes-config/locust-worker-controller.yaml
  1. Deploy Locust master:
kubectl apply -f kubernetes-config/locust-master-controller.yaml
  1. To confirm that the locust-master pod is created, run the following command:
kubectl get pods -l app=locust-master
  1. Next, deploy the locust-master-service:
kubectl apply -f kubernetes-config/locust-master-service.yaml

This step will expose the pod with an internal DNS name (locust-master) and ports 8089, 5557, and 5558. As part of this step, the type: LoadBalancer directive in locust-master-service.yaml will tell Google Kubernetes Engine to create a Compute Engine forwarding-rule from a publicly available IP address to the locust-master pod.

  1. To view the newly created forwarding-rule, execute the following:
kubectl get svc locust-master

Example output:

NAME            TYPE           CLUSTER-IP     EXTERNAL-IP      PORT(S)                                        AGE
locust-master   LoadBalancer   10.59.244.88   35.222.161.198   8089:30865/TCP,5557:30707/TCP,5558:31327/TCP   1m

Click Check my progress to verify the objective.

Load testing master

Check my progress

Task 7. Load testing workers

The next component of the deployment includes the Locust workers, which execute the load testing tasks described above. The Locust workers are deployed by a single deployment that creates multiple pods. The pods are spread out across the Kubernetes cluster. Each pod uses environment variables to control important configuration information such as the hostname of the system under test and the hostname of the Locust master.

After the Locust workers are deployed, you can return to the Locust master web interface and see that the number of slaves corresponds to the number of deployed workers.

The following snippet contains the deployment configuration for the name, labels, and number of replicas:

apiVersion: "apps/v1"
kind: "Deployment"
metadata:
  name: locust-worker
  labels:
    name: locust-worker
spec:
  replicas: 5
  selector:
    matchLabels:
      app: locust-worker
  template:
    metadata:
      labels:
        app: locust-worker
    spec:
...

Deploy locust-worker

  1. Now deploy locust-worker-controller:
kubectl apply -f kubernetes-config/locust-worker-controller.yaml
  1. The locust-worker-controller is set to deploy 5 locust-worker pods. To confirm they were deployed, run the following:
kubectl get pods -l app=locust-worker

Scaling up the number of simulated users will require an increase in the number of Locust worker pods. To increase the number of pods deployed by the deployment, Kubernetes offers the ability to resize deployments without redeploying them.

  1. The following command scales the pool of Locust worker pods to 20:
kubectl scale deployment/locust-worker --replicas=20
  1. To confirm that pods have launched and are ready, get the list of locust-worker pods:
kubectl get pods -l app=locust-worker

The following diagram shows the relationship between the Locust master and the Locust workers:

The flow from the Locust master to the Locust worker to the application

Click Check my progress to verify the objective.

Load testing workers

Check my progress

Task 8. Execute tests

  1. To execute the Locust tests, get the external IP address by following command:
EXTERNAL_IP=$(kubectl get svc locust-master -o yaml | grep ip: | awk -F": " '{print $NF}')
echo http://$EXTERNAL_IP:8089
  1. Click the link and navigate to Locust master web interface.

The Locust master web interface enables you to execute the load testing tasks against the system under test.

934dc685f86ood1f.png

  1. To begin, specify the total number of users to simulate and a rate at which each user should be spawned.

  2. Next, click Start swarming to begin the simulation. For example you can specify Number of users to simulate as 300 and Hatch rate as 10.

  3. Click Start swarming.

As time progresses and users are spawned, statistics aggregate for simulation metrics, such as the number of requests and requests per second.

  1. To stop the simulation, click Stop and the test will terminate. The complete results can be downloaded into a spreadsheet.

Solution of Lab

💡
This script and guide are provided for educational purposes to help you understand lab services and boost your career. Before using the script, please review it to familiarize yourself with Google Cloud services. Be sure to follow Qwiklabs terms of service. The goal is to enhance your learning experience, not to bypass it.
curl -LO raw.githubusercontent.com/Techcps/GSP-Short-Trick/master/Distributed%20Load%20Testing%20Using%20Kubernetes/techcps182.sh
sudo chmod +x techcps182.sh
./techcps182.sh