In this guide, I will show you how to change the default index.html in Kubernetes Nginx deployment using an index.html file from the configmap.
In this guide, we will do the following task.
- Create a config map with an index.html file as data
- Add the index.html configmap to the Nginx deployment as volume
If you are preparing for Kubernetes certification, I recommend you check out the Linux Foundation Coupon page to get discounts on CKAD, CKS, KCNA & CKA certifications.
How To Change Nginx index.html in Kubernetes?
Follow the below steps to change the default index.html file served by Nginx in a Kubernetes cluster using a ConfigMap.
Step 1: Create an HTML file
So the first step you need to take is to create the custom index.html file that you want to serve using the Nginx. You can simply create a simple HTML file and save it as an index.html.
Step 2: Create a ConfigMap
Now, create a configmap in Kubernetes that stores the index.html custom file. Now, use the following command to create a configmap or define a YAML file.
Here is an example code to create a configmap from a YAML file:
apiVersion: v1 kind: ConfigMap metadata: name: nginx-config data: index.html: | <!DOCTYPE html> <html> <head> <title>Custom Nginx Page</title> </head> <body> <h1>Welcome to my custom Nginx page!</h1> </body> </html>
Now, save this YAML file & apply it using the following kubectl command:
kubectl apply -f nginx-configmap.yaml
Step 3: Create a Kubernetes Deployment
Now, to run the Nginx container with the custom index.html file, you will need a Kubernetes Deployment. Here is a simple example:
apiVersion: apps/v1 kind: Deployment metadata: name: nginx-deployment spec: replicas: 1 selector: matchLabels: app: nginx template: metadata: labels: app: nginx spec: containers: - name: nginx image: nginx:latest ports: - containerPort: 80 volumeMounts: - name: nginx-config-volume mountPath: /usr/share/nginx/html volumes: - name: nginx-config-volume configMap: name: nginx-config
Just save this YAML file and apply it using the following kubectl command:
kubectl apply -f nginx-deployment.yaml
Step 4: Verify the Deployment
When you apply the Deployment, Kubernetes will create a Pod running Nginx with the custom index.html file. Now you can verify that the Pod is running using the following command:
kubectl get pods
Step 5: Expose the Nginx Service (Optional)
To access the Nginx service externally, you need to create a Kubernetes service and expose it using a NodePort, LoadBalancer, or an Ingress controller.
Example of NodePort service is as follows:
apiVersion: v1 kind: Service metadata: name: nginx-service spec: selector: app: nginx ports: - protocol: TCP port: 80 targetPort: 80 type: NodePort
Now, you need to save this YAML file and apply it using the following kubectl command:
kubectl apply -f nginx-service.yaml
If you have followed the steps correctly, you should have an Nginx Deployment serving your custom index.html file in your Kubernetes cluster. To access it, you must use the Service IP address or NodePort.
Read the Kubernetes tutorials for beginners guide and improve your skills.
I hope this guide helped you to change the Nginx index.html in Kubernetes with configmap.
If you face any error or do not understand any part of this guide, please let me know in the comment section, and I will try to answer them to my best.
Frequently Asked Questions
How to serve an HTML file with nginx?
The configuration file for Nginx should be opened. This is usually found in /etc/nginx/nginx.conf or /etc/nginx/sites-available/default. In the server block, add a location block for the path where you want the HTML files to be served.
What is the maximum size of ConfigMap in Kubernetes?
A ConfigMap’s data storage capacity is limited to 1 MiB. If you need to store settings that exceed this limit, then you need to consider mounting a disk or using a separate database or file service.
How do I refresh a config file?
To refresh a config file, select the loaded configurations that you want to reload and click Reload. Only configurations with the status Loaded can be reloaded. To refresh Configs, click Refresh. The information for all of the configurations in the table is displayed again.
What is ConfigMap used for in Kubernetes?
ConfigMaps are a Kubernetes technique for inserting configuration data into application pods. To keep containerized applications portable, the ConfigMap idea allows you to separate configuration artifacts from image content.