Simple service for testing Kubernetes configurations

Martin Hodges
5 min readJan 16, 2024

--

On a number of occasions, I have needed to use a simple web server to test out some Kubernetes configuration. I thought I would capture how I do this in case you need something similar.

Test setup

When testing Kubernetes services, I need to be able to create a quick, disposable service that will serve a simple page or line of text.

In these test scenarios I sometimes need a single service, a service with multiple replicas or a mixture of both. This means the service I create should be able to identify itself and I should be able to deploy the configuration I want.

Using NGINX

For my test service I decided to use NGINX rather than a bespoke application as it is widely understood, used and available. It is also quick to set up.

We will create a Kubernetes ConfigMap, Deployment and Service. As manifest files are in YAML, we can actually add these together in a single file. To help you understand what I am doing, I will show you the three sections separately but create them in the single file:

hello-world1.yml

Part 1 — ConfigMap

---
apiVersion: v1
kind: ConfigMap
metadata:
name: nginx-config-1
namespace: default
data:
index.html: |
<html>
<h2>Hello world 1!!</h2>
</html>

This defines the static text that will be returned by the service (In this case a line of HTML saying Hello world 1!!). You will want to change the name of the ConfigMap to suit your test service implementation and instance.

Part 2 — Deployment

---
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment-1
spec:
replicas: 2
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
testService: service-1
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
volumeMounts:
- name: nginx-index-config
mountPath: /usr/share/nginx/html
volumes:
- name: nginx-index-config
configMap:
name: nginx-config-1

There are a few things to note here. The line of three dashes (---) indicates that this is a new YAML document within the file. Kubernetes will treat this as if it is a separate file.

The name of the deployment (nginx-deployment-1) should be changed based on your test requirements (eg: nginx-deployment-2, 3, 4 etc). You should also change the testService label as this will link the service tot eh deployment.

The configMap.name field must match the name in the first part. A volume is created from the ConfigMap and then this is mounted into the file system for NGINX to use as a static content file.

Part 3 — Service

---
apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
selector:
app: nginx
testService: service-1
type: NodePort
ports:
- name: http
port: 80
targetPort: 80
nodePort: 30081

This creates a NodePort that exposes our service on a given port on each of our Kubernetes nodes. This means you can access you service via the IP address of any of your nodes.

To ensure that the service is wired up correctly, ensure that the selector.testService value matches your deployment above. You will also need to ensure that the nodePort is unique for each set of test services you create, eg: 20081, 30082, 3, 4 etc.

You should now have a file that looks like this:

hello-world1.yml

---
apiVersion: v1
kind: ConfigMap
metadata:
name: nginx-config-1
namespace: default
data:
index.html: |
<html>
<h2>Hello world 1!!</h2>
</html>
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment-1
spec:
replicas: 2
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
testService: service1
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
volumeMounts:
- name: nginx-index-config
mountPath: /usr/share/nginx/html
volumes:
- name: nginx-index-config
configMap:
name: nginx-config-1
---
apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
selector:
app: nginx
testService: service-1
type: NodePort
ports:
- name: http
port: 80
targetPort: 80
nodePort: 30081

Creating your test service

You can now create your test service by running this from wherever you run kubectl:

kubectl create -f hello-world1.yml 

Note that I have deployed everything to the default namespace as this makes it easier to work with. This is acceptable as these are going to be short-lived and will be deleted after testing is finished.

You can now check the resources with:

kubectl get all

You should see your deployment, pods (2 in the case above) and your service.

You can now go to the IP address of any of your nodes and enter the following URL into your browser (remembering to replace the < > fields with your own values:

http://<node IP address>:30081

And you should be greeted with the message defined in your ConfigMap.

You can now create as many replicas and as many copies with their own message as you need for your test purposes.

Command line testing

Sometimes, when testing Kubernetes, you need to run a test from within a Pod. The easiest way to do that is with this command:

kubectl run -i --tty --rm debug --image=busybox --restart=Never -- sh

This will start a small Linux Pod based on busybox. It will start into the command line and there you can run your tests.

If busybox does not do everything you need, you can also try an ubuntu image with /bin/bash instead of sh, which is bigger but has more features.

Once you have finished, when you log out, the Pod will be deleted automatically.

Helm

I have not mentioned Helm. Helm is useful in that it allows you to parameterise the configuration files required by Kubernetes. In this way you can create a single Helm chart that you can then use as many times as you like.

For this article I chose to avoid Helm so that you can follow what is happening. A future article may extend this with a Helm version of this test service.

Summary

This was a quick article to help you design and create simple web services that can be used to test your deployments.

We looked at how we can use a single manifest YAML file to create a static configuration, an NGINX deployment and a service. We then deployed that manifest and accessed the service through our web browser.

If you found this article of interest, please give me a clap as that helps me identify what people find useful and what future articles I should write. If you have any suggestions, please add them in the comments section.

--

--