How To Change Nginx index.html in Kubernetes with Configmap?

How To Change Nginx index.html in Kubernetes

In this article, I will help you understand how you can change the default index.html in Kubernetes Nginx deployment with an index.html file from the configmap.

Here are the simple steps to follow.

  1. Create a config map with index.html file as data
  2. Add the index.html configmap to the Nginx deployment as volume.

Kubernetes Nginx index.html Configmap

If you look carefully at the following configmap, you’ll notice that we’re adding a file index.html with custom HTML content inside the data section.

Save the manifest as index-html-configmap.yaml

apiVersion: v1
kind: ConfigMap
  name: index-html-configmap
  namespace: default
  index.html: |
    <h1>Hi! This is a configmap Index file </h1>

Create the config map.

kubectl apply -f index-html-configmap.yaml

Kubernetes Nginx Deployment With index.html Configmap

The deployment config which uses the index.html config map, let’s dive deep into this.

Save the following manifest as nginx.yaml

apiVersion: apps/v1
kind: Deployment
  name: nginx-deployment
  namespace: default
      app: nginx
  replicas: 2 
        app: nginx
      - name: nginx
        image: nginx:latest
        - containerPort: 80
            - name: nginx-index-file
              mountPath: /usr/share/nginx/html/
      - name: nginx-index-file
          name: index-html-configmap

Here’s what we’re up to in the deployment.

  1. Creating a volume named nginx-index-file mapping the configmap.
  2. Under volumeMounts, we are adding the configmap that contains the index.html file to the /usr/share/nginx/html/ location to replace the Nginx default index.html

Create the deployment.

kubectl apply -f nginx.yaml

If you check the Nginx deployment, you should see the custom index.html file you created via the configmap.

How To Validate the Change With an Nginx Service?

To validate the change with an Nginx service from the UI you must add a Nginx service to the deployment.

To access the Nginx deployment from any Kubernetes node IP on port 32000, we will establish a NodePort service.

Save the following manifest as nginx-service.yaml

apiVersion: v1
kind: Service
  name: nginx-service
  namespace: default
    app: nginx
  type: NodePort
    - port: 80
      nodePort: 32000
      targetPort: 80

Here, we can see clearly that the node port is 32000.

Now, establish the service.

kubectl apply -f nginx-service.yaml

You can now connect to the Nginx deployment on Node port 32000, which shows the unique index.html file generated by configmap.

Final Thoughts

So now you have mastered the use of Kubernetes config map to successfully replace the Nginx index page.

In order to become certified in Kubernetes, you must understand Configmap.

Also, you need to check out the CKA exam coupon code page to find discounts on the CKA, CKAD, CKS, and KCNA certifications if you’re getting ready for the Kubernetes certification.

Leave a Reply

Your email address will not be published. Required fields are marked *