Configuring Applications with ConfigMaps and Secrets

Tutorial 4 of 5

Introduction

In this tutorial, we aim to guide you on how to configure your applications using ConfigMaps and Secrets in Kubernetes. The ultimate goal is to help you understand how to separate configuration data from application code, making your applications more flexible and secure.

By the end of this tutorial, you'll learn:

  • How to create and use ConfigMaps
  • How to create and use Secrets
  • How to use ConfigMaps and Secrets in your applications

Prerequisites:

  • Basic understanding of Kubernetes
  • A Kubernetes Cluster with kubectl command-line tool installed

Step-by-Step Guide

ConfigMaps and Secrets are Kubernetes API objects that allow you to store configuration for your applications separately from the application code.

ConfigMaps allow you to decouple configuration details from image content to keep containerized applications portable. They store non-confidential data in key-value pairs.

Secrets are used to save sensitive data, such as passwords, OAuth tokens, and ssh keys. Unlike ConfigMaps, Kubernetes offers some additional protection to Secret data.

Creating a ConfigMap

You can create ConfigMaps using the kubectl command-line interface or a ConfigMap manifest file (YAML or JSON).

kubectl create configmap example-config --from-literal=key1=value1 --from-literal=key2=value2

This command creates a new ConfigMap named example-config with the key-value pairs key1=value1 and key2=value2.

Creating a Secret

Similar to ConfigMaps, you can create Secrets using the kubectl command-line interface or a Secret manifest file.

kubectl create secret generic example-secret --from-literal=username=myuser --from-literal=password=mypassword

This command creates a new Secret named example-secret with the key-value pairs username=myuser and password=mypassword.

Code Examples

Let's now look at how to use these ConfigMaps and Secrets in your applications.

Using ConfigMaps

Here's an example of a Pod that uses values from example-config to configure a container:

apiVersion: v1
kind: Pod
metadata:
  name: mypod
spec:
  containers:
    - name: mycontainer
      image: myimage
      env:
        - name: KEY1
          valueFrom:
            configMapKeyRef:
              name: example-config
              key: key1

Using Secrets

Here's an example of a Pod that uses values from example-secret to configure a container:

apiVersion: v1
kind: Pod
metadata:
  name: mypod
spec:
  containers:
    - name: mycontainer
      image: myimage
      env:
        - name: USERNAME
          valueFrom:
            secretKeyRef:
              name: example-secret
              key: username

Summary

In this tutorial, we learned how to create and use ConfigMaps and Secrets in Kubernetes. We also looked at how to decouple configuration details from the application code, which leads to more flexible and secure applications.

To continue your learning journey, consider exploring more about Kubernetes, such as Persistent Volumes, Policies, and Kubernetes API resources.

Practice Exercises

  1. Create a ConfigMap with three different key-value pairs and use them in a Pod.
  2. Create a Secret with a username and password, and use these credentials in a Pod.
  3. Create a Pod that uses both ConfigMaps and Secrets.

Solutions:

  1. Refer to the "Creating a ConfigMap" section for creating ConfigMaps. The usage of these ConfigMaps in a Pod is explained in the "Using ConfigMaps" section.
  2. Refer to the "Creating a Secret" section for creating Secrets. The usage of these Secrets in a Pod is explained in the "Using Secrets" section.
  3. The "Code Examples" section provides examples of how to use both ConfigMaps and Secrets in a Pod.

Remember, practice is key to mastering any concept. Happy Learning!