Configuring Apps with Configmaps

KubernetesKubernetesBeginner
Practice Now

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.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL kubernetes(("`Kubernetes`")) -.-> kubernetes/TroubleshootingandDebuggingCommandsGroup(["`Troubleshooting and Debugging Commands`"]) kubernetes(("`Kubernetes`")) -.-> kubernetes/BasicCommandsGroup(["`Basic Commands`"]) kubernetes(("`Kubernetes`")) -.-> kubernetes/AdvancedCommandsGroup(["`Advanced Commands`"]) kubernetes(("`Kubernetes`")) -.-> kubernetes/AdvancedDeploymentGroup(["`Advanced Deployment`"]) kubernetes/TroubleshootingandDebuggingCommandsGroup -.-> kubernetes/exec("`Exec`") kubernetes/BasicCommandsGroup -.-> kubernetes/get("`Get`") kubernetes/AdvancedCommandsGroup -.-> kubernetes/apply("`Apply`") kubernetes/AdvancedDeploymentGroup -.-> kubernetes/rollout("`Rollout`") subgraph Lab Skills kubernetes/exec -.-> lab-9689{{"`Configuring Apps with Configmaps`"}} kubernetes/get -.-> lab-9689{{"`Configuring Apps with Configmaps`"}} kubernetes/apply -.-> lab-9689{{"`Configuring Apps with Configmaps`"}} kubernetes/rollout -.-> lab-9689{{"`Configuring Apps with Configmaps`"}} end

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 sh < pod-name > --

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 sh < pod-name > --

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.

Other Kubernetes Tutorials you may like