Deploying Kubernetes with ConfigMaps.

Abidoye Joshua mayowa
9 min readMay 12, 2023

Hello, Everyone; welcome to my blog post for Week 18 in the LevelUpInTech Devops Program. This week's project is based on another Kubernetes-based project scenario.

Project scenario:

This week's project could be used by a company that must host multiple websites or web applications using a single IP address and port. This could be a cost-effective solution for smaller businesses that cannot afford to have separate servers for each website or application.

For example, let's say that a small e-commerce business wants to launch two separate websites: one for selling clothes and accessories and another for selling home decor items. Instead of purchasing two separate servers, the business could use this project to host both websites on a single server using two separate deployments.

The first deployment could contain two pods running the nginx image with a custom index.html page that displays "This is Deployment One" acting as the clothing and accessories website. The second deployment could contain two pods running the nginx image with a custom index.html page that displays "This is Deployment Two", acting as the home decor website.

Customers can access both websites using the same IP address and port number by using a service that points to both deployments. This will make it easier for customers to remember the website address and also make it easier for the business to manage the websites.

The curl command can validate that both websites are accessible using the same IP address and port number. This will ensure that customers can access both websites without any issues and that the business is providing a reliable service.

Objective:

Spin up two deployments. One deployment contains 2 pods running the nginx image. Include a ConfigMap that points to a custom index.html page that contains the line "This is Deployment One". The other deployment contains 2 pods running the nginx image. Include a ConfigMap that points to a custom index.html page that contains the line "This is Deployment Two".

Create a service that points to both deployments. You should be able to access both deployments using the same ip address and port number.

Use the curl command to validate that you eventually see the index.html pages from both Deployment 1 and Deployment 2.

Example: curl service_ip_address:service_port

Prerequisites:

  • Basic Linux commands
  • Basic knowledge of Docker CLI commands
  • Basic knowledge of Kubernetes
  • Docker Desktop installed
  • Minikube installed in CLI
  • Vs code editor

In this project, we will create a single node cluster using Minikube, create config maps with custom HTML, create two NGINX deployments with four replicas, and create a load balancer service so the deployments can reach an external IP address. Let's get started!

Setting Up A Kubernetes Environment

In order for us to create a cluster, firstly, we need to set up our environment, which entails installing Docker Desktop and Minikube.

If you don't have this already installed on your system, follow this tutorial below:

Install Docker Desktop

Install Docker Desktop on Linux

Install Docker Desktop on Mac

Install Docker Desktop on Windows

Enable Kubernetes:

Enable Kubernetes on Docker Desktop

Install Minikube:

Install minikube on Linux, Mac, or Windows

Step 1: Start a Single-Node Cluster with Minikube

Once you have your environment set up, use the command below to create a single-node cluster using minikube:

minikube start

This will take a few minutes to create, so you can listen to your favourite song on youtube and come back.

To confirm the node that was created, use the following command:

kubectl get nodes

You can also see your cluster in your Docker Desktop dashboard.

Step 2: Create a Deployment with 2 pods Running Nginx.

Based on Docker knowledge, to deploy pods on a Kubernetes cluster, you need to create a YAML file. To get started creating the file, change into the directory you want to work in and either Vim, Vi, or Nano to make a new file or use your favorite code editor. I will be using VS Code.

Firstly, we will create the NGINX deployment file, and we can give it the name deployment1.yml. In the YAML below, you can see that the kind is Deployment. We've named the deployment "nginx1" and are creating 2 replicas or pods. The template defines our containers and tells Kubernetes which image to run, on which port, and which volume to mount our custom NGINX html file. We are referencing the name of the config map that we will create next.

# Deployment with 2 pods running NGINX image
# Include a ConfigMap that points to a custom index.html page that contains
# the line "This is Deployment One"

apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx1
spec:
selector:
matchLabels:
app: nginx
replicas: 2
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
volumeMounts:
- name: nginx-index-file-1
mountPath: /usr/share/nginx/html
volumes:
- name: nginx-index-file-1
configMap:
name: configmap1-html
items:
- key: index.html
path: index.html

Step 3:Include a ConfigMap that Points to a Custom index.html Page.

To meet the objectives above, we want our first NGINX landing page to have a custom greeting of "This is Deployment One" and our second NGINX landing page to have a custom greeting of "This is Deployment Two". We can do this using a ConfigMap.

ConfigMaps are API objects that can store unencrypted data in key-value pairs. Pods (a set of containers) can reference ConfigMaps environment variables, CLI arguments, or config files in a volume.

We want to keep our source code (in this case, our custom index.html file) separate from our deployments. To do this, we will create a ConfigMap in a separate file. Create a new file in the same directory as your Deployment file, and name it configmap1-html.yml . Under data, our key is index.html , and our value is the actual HTML we want. You can see the custom greeting of "Welcome" and "This is Deployment One".

apiVersion: v1
kind: ConfigMap
metadata:
name: html-configmap1
data:
index.html: |
<html>
<h1>Welcome</h1>
</br>
<h1>This is Deployment One</h1>
</html

Ensure that your reference to the config map in your Deployment file is the same as the name in your ConfigMap metadata and that the key in your Deployment file is the same as your data in your ConfigMap file.

Since we now have our deployment1.yml file and configmap1-html.yml file, we need to repeat the above steps for Deployment 2. The files will be very similar, but we'll change the name of the deployment and create a separate config map file for Deployment 2.

Attached below is the second deployment file. Note the name of the deployment has changed to "nginx2", and the name of the volume mounts and configMap have changed slightly.

# Deployment with 2 pods running NGINX image
# Include a ConfigMap that points to a custom index.html page that contains the
# line "This is Deployment Two"

apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx2
spec:
selector:
matchLabels:
app: nginx
replicas: 2
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
volumeMounts:
- name: nginx-index-file-2
mountPath: /usr/share/nginx/html/
volumes:
- name: nginx-index-file-2
configMap:
name: configmap2-html
items:
- key: index.html
path: index.html

Attached below is the second ConfigMap:

apiVersion: v1
kind: ConfigMap
metadata:
name: html-configmap2
namespace: default
data:
index.html: |
<html>
<h1>Welcome</h1>
</br>
<h1>This is Deployment Two</h1>
</html

At this point, you should have four YAML files: deployment1.yml, deployment2.yml, configmap1-html.yml, and configmap2-html.yml.

Once all of these are created, move on to the next section.

Step 4: Create Two ConfigMaps

We have created our two config map files, but now we need to apply them to our Kubernetes cluster. In the CLI, use the following command:

kubectl apply -f configmap1-html.yml 

Repeat this step for the second config file:

kubectl apply -f configmap2-html.yml

Step 5: Deploy the Pods

Earlier, we created our deployment YAML files for Deployment 1 and Deployment 2 but didn't actually apply or deploy those files; thus, we haven't yet created our pods. To deploy the pods (two replicas for Deployment 1 and two replicas for Deployment 2), use the following command for both of our deployment files:

kubectl apply -f <deployment_file_name.yml>

To confirm our pods were created, use the following command:

kubectl get pods -o wide

Step 6: Create a Service that Points to Both Deployment 1 and Deployment 2

A Kubernetes service allows you to expose a network application to make it available on the internet and serves as a consistent endpoint that allows external communication. For this project, we will use a load balancer to help our pods communicate outside our cluster.

If you do kubectl get all you will see that a ClusterIP service has already been created by default.

Next, Create a new file in the same directory as your others, and name it loadbalancer.yml.

Below is the YAML for the loadbalancer.yml file:

apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
type: LoadBalancer
selector:
app: nginx
ports:
- protocol: TCP
port: 80
targetPort: 80

Now let's deploy our service file.

kubectl apply -f <service_file.yml>

Do kubectl get all to see all of the running pods and services.

Step 7: Access Both Deployments Using Curl

minikube tunnel
kubectl get all

You should now see an external IP for your load balancer.

Run the following command using the IP address of the Service and include Port 80, where each Nginx web server pod is listening on

curl  <external_IP>:<listening_port>

Run the same command again (or a few times), and you should eventually see both the HTML for deployments 1 and 2.

Step 8: The next step is to Cleanup.

To delete these pods and services, we can delete the cluster using the following command:

minikube delete

Use the command below to confirm the cluster was deleted:

minikube profile list

Suppose you've got this far; thanks for reading! I hope it was worthwhile for you.

Thank you so much for following along with me on another tutorial. This tutorial strengthened basic Kubernetes concepts like creating a cluster and launching deployments. Keep watching my page for new tutorials as I learn about containerization.

--

--

Abidoye Joshua mayowa

DevOps Engineer. I'm interested in collaborating with anyone interested in cloud engineer or cloud DevOPs.