How to simulate traffic in Kubernetes cluster?

This post will show the most straightforward yet extremely powerful way to simulate traffic load in your environment, including the Kubernetes cluster. We will be using fortio for traffic generation for desired duration and requests per second. To see fortio in action, let’s see an example:
we assume our application is an Nginx pod and will send the desired number of queries per second to the nginx pod.

#Create Nginx pod

kubectl run nginx --image nginx
pod/nginx created
#expose the pod via a service of any type, for simplicity I used NodePort

kubectl expose  pod nginx  --name nginx --port 80 --type NodePort
service/nginx exposed
#validate that the pod is up and service is created

kubectl get all -l run=nginx
pod/nginx   2/2     Running   0          82s

NAME            TYPE           CLUSTER-IP      EXTERNAL-IP       PORT(S)        AGE
nginx           NodePort   <none>            80:31308/TCP   24s

Using the following YAML, create a traffic generator POD.

apiVersion: v1
kind: Pod
  name:  traffic-generator
    app: traffic-generator
  - name: fortio
    image: fortio/fortio
    args: ["load", "-t", "0", "-qps", "30", "nginx.default.svc"]

In the above manifest file’s args:

"-t 0" means run the traffic generation perpetually; alternatively, you can set it to some value like 30m for 30 minutes if you want it to run under Kubernetes Jobs(anything not perpetual).
"-qps 30" means the number of queries per second. You can set whatever you like.

"nginx.default.svc" is the target against traffic is generated.

With the help of Istio Addons(Kiali), I can visualize the traffic rate. (optional for this activity)


You can find significant details about fortio on its GitHub page linked here.
I have only demoed the -t and -qps flags. However, fortio is a great tool and could be used in many other ways. Consider checking the flags and documentation.

Note: You can download the binary and use it for traffic generation even out of the Kubernetes environment.

Leave a Comment

Your email address will not be published.

Scroll to Top