Supply Multiple Nginx Configuration Using ConfigMap¶
In the previous lesson, we learned how to use a ConfigMap to supply nginx configuration file to nginx. More specifically, we used a ConfigMap to modify the default nginx port.
In this example, we will explore how to use a ConfigMap to supply multiple nginx configuration files. Specifically, we will learn how to use a ConfigMap to modify the default nginx port and serve a custom HTML page.
Step 1: Create a ConfigMap That Stores Nginx Configuration¶
Let's create a ConfigMap that stores nginx configuration:
Observe the following:
- The nginx server listens on port
8080 - Nginx will serve the modified
index.htmlpage at/usr/share/nginx/html
Apply the manifest to create ConfigMap:
Step 2: Verify ConfigMap¶
Step 3: Create Nginx Deployment That Uses Configuration from ConfigMap¶
Let's create nginx deployment that uses configuration files from the ConfigMap we created in the previous step:
Observe the following:
- We have created a volume from
nginx-configmapConfigMap - We have mounted
default.conffile fromnginx-configmapto thenginxcontainer at/etc/nginx/conf.d/default.conf - We have mounted
index.htmlfile fromnginx-configmapto thenginxcontainer at/usr/share/nginx/html/index.html
Note
By using subPath to specify the specific file within the ConfigMap volume, we can selectively mount only the necessary configuration files or data that a container needs.
Apply the manifest to create the deployment:
Step 4: Verify Deployment and Pods¶
# List deployments
kubectl get deployments
# List pods
kubectl get pods
# Describe the Pod
kubectl describe pod <pod-name>
Observe the Mounts field of the nginx container. You'll see 2 mounts from my-volume.
Step 5: View Nginx Configuration Files¶
# Start a shell session inside the container
kubectl exec -it <pod-name> -- bash
# View the content of the default.conf file
cat /etc/nginx/conf.d/default.conf
# View the content of the default web pages
cd /usr/share/nginx/html
ls
cat index.html
Observe the following:
- The nginx server listens on port
8080 - Nginx serves the supplied
index.htmlpage mounted at/usr/share/nginx/html
Get response from nginx server:
Clean Up¶
Assuming your folder structure looks like the one below:
Let's delete all the resources we created: