---
title: "Deploy and Test a Load Balancer with Nginx | Hostman Docs"
description: "Step-by-step guide on creating an Nginx deployment behind a Kubernetes load balancer and verifying traffic distribution between pods."
---

> For the complete documentation index for AI agents, see [llms.txt](https://hostman.com/llms.txt).

To demonstrate the functionality of a load balancer, we will create two Nginx deployments, each displaying its own HTML page. The load balancer will distribute requests randomly between the pods, showing one of the pages depending on which pod is selected.

## Environment Setup

To simplify management and allow quick removal of all resources associated with the load balancer, we will create a separate namespace. This makes testing and resource cleanup easier while keeping the main cluster clean.

Run the following command to create the namespace:

```shell
kubectl create namespace test-namespace
```

After creation, use this namespace for all subsequent resources, including the load balancer, deployments, and `ConfigMap`. To do this, add the line `namespace: test-namespace` to every manifest related to the example.

## Create a ConfigMap for HTML Pages

We will start by creating a `ConfigMap` to store two HTML pages. Pod 1 will display a page with the heading "Pod 1," and Pod 2 will display a page with the heading "Pod 2." These pages will be connected to Nginx within the pods.

`nginx-pages-configmap.yaml`:

```yml
apiVersion: v1
kind: ConfigMap
metadata:
  name: nginx-pages
  namespace: test-namespace
data:
  index-page1.html: |
    <html>
    <body>
      <h1>Pod 1</h1>
      <p>This is page served by Pod 1.</p>
    </body>
    </html>
  index-page2.html: |
    <html>
    <body>
      <h1>Pod 2</h1>
      <p>This is page served by Pod 2.</p>
    </body>
    </html>
```

Here we create a `ConfigMap` with two HTML files: `index-page1.html` and `index-page2.html`. These files will be mounted into Nginx pods, allowing each pod to display its specific page.

Apply the `ConfigMap`:

```shell
kubectl apply -f nginx-pages-configmap.yaml
```

## Create Nginx Deployments

Next, we create two deployments, each using different HTML pages from the `ConfigMap`. The deployments use the selector `app: nginx`, which the load balancer will use to identify pods participating in traffic distribution.

`nginx-deployment-pod1.yaml`:

```yml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-pod1
  namespace: test-namespace
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:latest
        volumeMounts:
          - name: nginx-pages
            mountPath: /usr/share/nginx/html/index.html
            subPath: index-page1.html
        ports:
          - containerPort: 80
      volumes:
      - name: nginx-pages
        configMap:
          name: nginx-pages
```

This deployment creates a single pod (replica 1) with the Nginx image, which mounts the `index-page1.html` file from the `ConfigMap` into the directory `/usr/share/nginx/html/index.html`. Port 80 is open for accessing the page.

Apply the deployment:

```shell
kubectl apply -f nginx-deployment-pod1.yaml
```

`nginx-deployment-pod2.yaml`:

```yml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-pod2
  namespace: test-namespace
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
        - name: nginx
          image: nginx:latest
          volumeMounts:
            - name: nginx-pages
              mountPath: /usr/share/nginx/html/index.html
              subPath: index-page2.html
          ports:
            - containerPort: 80
      volumes:
        - name: nginx-pages
          configMap:
            name: nginx-pages
```

This deployment also creates an Nginx pod but mounts the `index-page2.html` file, which has different content.

Apply the second deployment:

```shell
kubectl apply -f nginx-deployment-pod2.yaml
```

## Configure the Load Balancer

Now, create a load balancer that will direct requests to pods with the label `app: nginx`.

`nginx-loadbalancer.yaml`:

```yaml
apiVersion: v1
kind: Service
metadata:
  name: nginx-loadbalancer
  namespace: test-namespace
spec:
  selector:
    app: nginx
  ports:
    - port: 80
      targetPort: 80
      appProtocol: k8s.hostman.com/proto-http
  type: LoadBalancer
```

In this `Service`, we specify `type: LoadBalancer`, which creates a load balancer, and `selector: app: nginx`, which directs requests to the Nginx pods from our deployments. Requests to the load balancer are distributed among the pods using the `roundrobin` algorithm, which is the default.

Apply the load balancer:

```shell
kubectl apply -f nginx-loadbalancer.yaml
```

## Verify the Load Balancer

After creating the load balancer, you can find its public IP address in the dashboard or by running the command:

```shell
kubectl get services -n test-namespace
```

Accessing this IP address will display a page served by one of the pods. Each time you refresh the page, traffic may be redirected to different pods, allowing the load balancer to switch the displayed page randomly.

## Deleting Resources After Testing

Once you have verified the load balancer’s functionality, you can delete all the created pods and resources. Use the following commands:

```shell
kubectl delete service nginx-loadbalancer -n test-namespace
kubectl delete deployment nginx-pod1 -n test-namespace
kubectl delete deployment nginx-pod2 -n test-namespace
kubectl delete configmap nginx-pages -n test-namespace
```

These commands will remove the load balancer, pod deployments, and the `ConfigMap` created earlier.

Alternatively, delete the entire namespace:

```shell
kubectl delete namespace test-namespace
```

This method will automatically remove all resources associated with the test environment.
