How To Change Nginx index.html in Kubernetes With Configmap
- Last Updated on: June 22, 2021
- By: scriptcrunch
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 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.
- 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 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.
Other Interesting Blogs
How to Install AWS CLI? Step by Step Configuration Guide
In this step-by-step guide, you will learn how to install AWS CLI and configure the CLI to connect to AWS and execute
Python for DevOps: Python Scripting Tutorial for Beginners
One of the common questions aspiring DevOps engineers ask is, do we need python for DevOps? In this blog, we will explain
How To Run a Python Script – Beginners Guide
In this post, we will walk through the basic concepts, different ways, and environment setup that is required to run a python