Kubernetes Deployment that uses a custom index.html file on an Nginx Pod using a ConfigMap

Tim Krassowski
4 min readDec 11, 2022

--

Ft. George, Scotland

For my Week 18 project, I will walk us through how to set up a custom index.html web page on a Kubernetes Pod using a ConfigMap that will be using the Nginx web server. We will print out the custom index.html message to the Command line and a web browser to prove that it works.

Objective:

— Create a deployment that has 5 nginx pods;

— Create a ConfigMap that points to a custom index.html file that contains the line “ This web page is housed on a Pod running Nginx”.

— Ensure you can access the service from the internet.

Pre-requisites:

— A CLI: I’ll be using my A Cloud Guru account’s server.

— I’ve created a Control Plane Node and two worker nodes on my A Cloud Guru account and am using the built-in CLI to access them.

  1. Let’s create our Deployment:

We’ll use the following command to create the deployment with 5 containers (Replicas), and open port 80 to allow internet traffic.

Create the deployment with the image set to nginx, replicas to 5 and port to 80.

2. Verify the pods and deployment were successful:

Use the following commands to verify that the Pods and Deployment were created successfully:

Week 18 pods created successfully!
Week 18 Deployment created successfully!

3. Add a NodePort and expose the deployment to the internet.

4. Test the deployment:

Well use the IP address obtained from the following command to see if our web server can access the nginx web server:

kubectl get services -o wide

When done, you should get the Nginx test page shown below. Congratulations! The webserver was installed correctly!

Nginx test page successfully installed!

5. Create the ConfigMap:

Here we will use the following script to create a ConfigMap that will point to our custom web page. For our project, we’re going to point out that the web page is being stored on a Kubernetes Pod running Nginx.

We’ll start with using a text editor to create the file:

vi index-html-configmap.yml

6. Go back and edit the original deployment YAML file to point it to the configMap:

Here we need to do vi nginx-wk18.yml and insert the following information to point the deployment to the config map and the outside port. This information is located in the volumeMount and volumes section:

Note the index-html-configmap seciton at the bottom.

Run the following command to create the deployment. We’ll use the apply command to apply the changes:

Apply deployment changes.

7. Create the configMap deployment by deploying the service:

Apply changes to the ConfigMap services.

8. Run kubectl get svc to get the deployment service IP address.

Copy it down afterwards, too(FYI, my service name is different because I forgot to change it in the above YAML file):

9. Lastly, test the IP address using the curl command from the CLI:

Success!!

Also, just to check for internet connectivity, we’ll connect in the web browser to our Control plane servers’ Public IP and add the 30080 port to the end:

If you get the above html message, congratulations! We’ve successfully created a deployment as well as a configMap that directed us to a customized html file!

Thank you for following along this tutorial with me. If you think it’s great, please clap for it. If you think it needs work, please let me know so I can improve!

--

--