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.
- Create a config map with
index.html
file as data - Add the
index.html
configmap to the Nginx deployment as volume.
Table of Contents
ToggleKubernetes 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
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
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
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’s what we’re up to in the deployment.
- Creating a volume named
nginx-index-file
mapping the configmap. - Under
volumeMounts
, we are adding the configmap that contains the index.html file to the/usr/share/nginx/html/
location to replace the Nginx defaultindex.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
metadata:
name: nginx-service
namespace: default
spec:
selector:
app: nginx
type: NodePort
ports:
- 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.