How To Change Nginx index.html in Kubernetes With Configmap
- Last Updated On: August 29, 2022
- By: Aswin Vijayan
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.
- Create a config map with
index.html
file as data - 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
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 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
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.
- 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
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
metadata:
name: nginx-service
namespace: default
spec:
selector:
app: nginx
type: NodePort
ports:
- 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.
Conclusion
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.
Aswin Vijayan
Other Interesting Blogs
[40% OFF] Linux Foundation LFCA, LFCS & LFCT Exam Voucher Codes
Linux Foundation has announced up to a $284 discount on its Linux certification programs Linux Foundation Certified IT Associate (LFCA) and Linux
Linux Foundation Coupon for October 2024
Hi Techies, I wanted to let you know about a pretty sweet deal with the Linux Foundation Coupon that is running now.
CKA Certification Study Guide (Certified Kubernetes Administrator)
This comprehensive CKA certification exam study guide covers all the important aspects of the Certified Kubernetes Administrator exam and useful resources. Passing
1 thought on “How To Change Nginx index.html in Kubernetes With Configmap”
Thanks! This was helpful.