Introduction
In this lab, you will learn how to use ConfigMaps in Kubernetes to configure your applications. ConfigMaps provide a way to separate configuration data from your application code, making it easy to manage and update your application configuration without modifying the application itself.
Start the Minikube Cluster
Before creating resources, you need a running Kubernetes cluster. Minikube is a lightweight Kubernetes environment that runs on your local machine.
Navigate to your working directory:
Open the terminal and navigate to the default project folder:
cd /home/labex/projectStart Minikube:
Start Minikube to initialize a Kubernetes cluster:
minikube start- This command sets up a single-node Kubernetes cluster on your local machine.
- Minikube may take a few minutes to start depending on your system's performance.
Verify Minikube is running:
Check the status of the Minikube cluster:
minikube status- Look for components like
kubeletandapiserverlisted asRunning. - If the cluster is not running, rerun
minikube start.
- Look for components like
If you encounter issues starting Minikube. Use minikube delete to reset the environment if needed.
Create a Configmap
In this step, you will create a ConfigMap containing the configuration data for your application.
Create a file named configmap.yaml in /home/labex/project/ directory with the following contents:
apiVersion: v1
kind: ConfigMap
metadata:
name: my-config
data:
DATABASE_URL: postgres://user:password@host:port/dbname
This ConfigMap contains a single key-value pair, where the key is DATABASE_URL and the value is a PostgreSQL database connection string.
To create the ConfigMap, run the following command:
kubectl apply -f configmap.yaml
Use the Configmap in Your Application
In this step, you will use the ConfigMap in your application.
Create a file named deployment.yaml in /home/labex/project/ directory with the following contents:
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app
spec:
replicas: 1
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: my-app
image: nginx:latest
env:
- name: DATABASE_URL
valueFrom:
configMapKeyRef:
name: my-config
key: DATABASE_URL
This deployment specifies a single container running your application, which uses the DATABASE_URL environment variable to connect to a PostgreSQL database. The value of DATABASE_URL is obtained from the my-config ConfigMap.
To create the deployment, run the following command:
kubectl apply -f deployment.yaml
Verify the Configuration
In this step, you will verify that the configuration has been applied to your application.
First, find the name of the pod running your application by running the following command:
kubectl get pods -l app=my-app
You should see a single pod running your application. Note the name of the pod.
Next, run the following command to open a shell session in the container running your application:
kubectl exec -it pod-name -- /bin/sh
Replace <pod-name> with the name of the pod that you noted earlier.
Once you are in the shell session, run the following command to print the value of the DATABASE_URL environment variable:
echo $DATABASE_URL
You should see the database connection string that you set in the ConfigMap.
Update the Configmap
In this step, you will update the ConfigMap and see how it affects your application.
Update the configmap.yaml file with a new value for the DATABASE_URL key:
apiVersion: v1
kind: ConfigMap
metadata:
name: my-config
data:
DATABASE_URL: postgres://newuser:newpassword@newhost:newport/newdbname
This updates the DATABASE_URL key to a new value.
To update the ConfigMap, run the following command:
kubectl apply -f configmap.yaml
Verify the Update
In this step, you will verify that the update to the ConfigMap has been applied to your application.
First, find the name of the pod running your application by running the following command:
kubectl get pods -l app=my-app
You should see a single pod running your application. Note the name of the pod.
Next, run the following command to open a shell session in the container running your application:
kubectl exec -it pod-name -- /bin/sh
Replace <pod-name> with the name of the pod that you noted earlier.
Once you are in the shell session, run the following command to print the value of the DATABASE_URL environment variable:
echo $DATABASE_URL
You can see that the configuration did not take effect, it is still the same data as before. You need to restart Deployment with the following command.
kubectl rollout restart deployment my-app
When the reboot is complete, go inside the container again and use the above command to check the configuration.
You should see the updated database connection string.
Summary
In this lab, you learned how to use ConfigMaps in Kubernetes to configure your applications. You created a ConfigMap containing configuration data, used the ConfigMap in your application, and updated the ConfigMap to see how it affects your application. You also learned how to verify that the configuration was applied to your application and how to clean up the resources created in the lab.


