Supply Nginx Configuration Using ConfigMap¶
Now, let's see how you can use ConfigMap to supply configuration to applications. In this example, we will use ConfigMap to provide configuration to the nginx application.
Nginx Deployment Without ConfigMap¶
First, let's explore the default behaviour of nginx application.
By default, Nginx serves responses from a virtual server that listens on port 80 and serves the default index.html file located at /usr/share/nginx/html.
Let's test this out!
1. Create nginx deployment manifest¶
2. Apply the manifest to create the nginx deployment¶
3. Verify deployment and pods¶
4. View default 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
80by default - Nginx serves the default
index.htmlpage at/usr/share/nginx/html
Get response from nginx server:
What if you want nginx to serve response from port 8080 instead of port 80?
ConfigMap can help. Let's try it out!
Step 1: Create a ConfigMap That Stores Nginx Configuration¶
First, let's create a ConfigMap that stores nginx configuration as follows:
Observe the following:
- The nginx server listens on port
8080 - Nginx will serve the default
index.htmlpage at/usr/share/nginx/html
Apply the manifest to create ConfigMap:
Step 2: Verify ConfigMap¶
Step 3: Update Nginx Deployment to Use Configuration From ConfigMap¶
Let's update the nginx deployment to use nginx configuration from the ConfigMap we created in the previous step.
The updated deployment should look like the following:
Apply the manifest to update 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>
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 now listens on port
8080 - Nginx serves the default
index.htmlpage 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: