Configuring a Deployment to Use a ConfigMap

Objective

After completing this lesson, you will be able to configure a Deployment to use a ConfigMap

Configure a Deployment to Use a ConfigMap

Business Scenario

You want to deploy an application to SAP BTP, Kyma runtime, which needs to be configured with some environment variables. You want to store these environment variables in a ConfigMap and inject them into your application.

Live Environment

In this exercise, you will perform the following steps:

  1. Create a ConfigMap.
  2. Deploy an application with the ConfigMap and verify the result.

Prerequisites

  • You have successfully created a SAP BTP, Kyma runtime instance in the SAP BTP subaccount.
  • You have configured kubectl to work with your SAP BTP, Kyma runtime instance.

Result

In this exercise, you have created a ConfigMap and deployed an application that uses the ConfigMap. Furthermore, you have verified that the application is using the ConfigMap correctly.

Task 1: Create a ConfigMap

Steps

  1. Create a ConfigMap.

    1. Create a new file called config-map.yaml to specify your first name and a greeting message:

      Code Snippet
      1234567
      apiVersion: v1 kind: ConfigMap metadata: name: name-greeting data: GREETING: "Hello" FIRSTNAME: "Kyma"
    2. Apply the ConfigMap via kubectl.

      Code Snippet
      1
      kubectl apply -f config-map.yaml
    3. Check if the ConfigMap has been created:

      Code Snippet
      1
      kubectl get configmaps

Task 2: Deploy an application with the ConfigMap and verify the result

Steps

  1. Deploy an application with the ConfigMap.

    1. Create a new file called deployment.yaml to specify your deployment:

      YAML
      12345678910111213141516171819202122
      apiVersion: apps/v1 kind: Deployment metadata: name: test-configmap spec: replicas: 1 selector: matchLabels: app: test-configmap template: metadata: labels: app: test-configmap spec: containers: - name: test-configmap image: bash envFrom: - configMapRef: name: name-greeting command: ["echo"] args: ["$(GREETING) $(FIRSTNAME)!"]
    2. Apply the deployment via kubectl.

      Code Snippet
      1
      kubectl apply -f deployment.yaml
    3. Get the Pod name.

      Code Snippet
      1
      kubectl get pods -l app=test-configmap
    4. Get the output of the Pod.

      Code Snippet
      1
      kubectl logs <pod-name>

      Alternatively, you could also retrieve the logs via the label selector:

      Code Snippet
      1
      kubectl logs -l app=test-configmap

    5. Delete the deployment

      Code Snippet
      1
      kubectl delete deployment test-configmap

Result

In this exercise, you have created a ConfigMap and deployed an application that uses the ConfigMap. Furthermore, you have verified that the application is using the ConfigMap correctly.

Further Reading

Log in to track your progress & complete quizzes