Change Nginx index.html in Kubernetes With Configmap

How To Change Nginx index.html in Kubernetes With Configmap

In this guide, you will learn how to change the default index.html in Kubernetes Nginx deployment with an index.html file from the configmap.

Here is what we are going to do.

  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 take a look at the following configmap, under the data section, we are creating are adding a file index.html with custom HTML content.

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

Now let’s have a look at the deployment config which uses the index.html config map.

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 is what we are doing 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

Now create the deployment.

kubectl apply -f nginx.yaml

Now if you check the Nginx deployment will have the custom index.html file you have added through the configmap.

Validate the Change With an Nginx Service

If you want to validate the change from the UI, you need to add an Nginx service to the deployment.

We will create a NodePort service to access the Nginx deployment from any Kubernetes node IP on port 32000.

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 are explicitly mentioning the nodeport as 32000.

Create the service.

kubectl apply -f nginx-service.yaml

Now you will be able to access the Nginx deployment on the Node port 32000 which displays the custom index.html file created via configmap.


In this guide, you have learned to effectively use Kubernetes configmap to replace the Nginx index page.

Configmap is an important topic in Kubernetes certification.

If you are preparing for Kubernetes certification, check out the Linux Foundation Coupon page to get discounts on CKA, CKAD, CKS, and KCNA certifications.

Change Nginx index.html in Kubernetes With Configmap

Other Interesting Blogs

1 thought on “How To Change Nginx index.html in Kubernetes With Configmap”

Leave a Comment

Share via
Copy link
Powered by Social Snap