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 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
metadata:
  name: index-html-configmap
  namespace: default
data:
  index.html: |
    <html>
    <h1>Welcome</h1>
    </br>
    <h1>Hi! This is a configmap Index file </h1>
    </html

Create the config map

kubectl apply -f index-html-configmap.yaml

Kubernetes Nginx Deployment With index.html Configmap

Now lets 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
metadata:
  name: nginx-deployment
  namespace: default
spec:
  selector:
    matchLabels:
      app: nginx
  replicas: 2 
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:latest
        ports:
        - containerPort: 80
        volumeMounts:
            - name: nginx-index-file
              mountPath: /usr/share/nginx/html/
      volumes:
      - name: nginx-index-file
        configMap:
          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 a Nginx Service

If you want to valide the change from the UI, you need to add a 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
metadata:
  name: nginx-service
  namespace: default
spec:
  selector:
    app: nginx
  type: NodePort
  ports:
    - port: 80
      nodePort: 32000
      targetPort: 80
      

Here we are explicitely 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 Node port 32000 which displays the custom index.html file created via configmap.

Change Nginx index.html in Kubernetes With Configmap

Other Interesting Blogs

Leave a Comment

2 Shares
Share via
Copy link
Powered by Social Snap