Kubernetes ConfigMap and Secret
Introduction
This article will guide you on how to use ConfigMap and Secret to pass environment variables. As you know, during software development, you need to deploy on different environments such as development, staging, and production. We aim to use the same codebase across these environments but sometimes need to pass different environment variables for specific configurations. Kubernetes (K8s) supports ConfigMap and Secret to address this need.
If you've used Docker before, configuring environment variables in K8s will feel similar.
- ConfigMap is an object that stores data in key-value pairs, intended for non-sensitive information (i.e., values that can be displayed when viewing the configuration of K8s resources). Supported data types include number, boolean, and string (it can store a string of characters or the contents of a text file).
- Secret has several types, with type=Opaque being the most commonly used. It works similarly to ConfigMap but is used for sensitive information such as database connections, passwords, API keys, and other secret data.
Prepare Docker Image
You can use the following code block to build a NodeJS TypeScript application for this article:
Note: You need to create a `data` folder to mount the volume when starting the Pod.
Next, build your Docker image and push it to Google Container Registry or Docker Hub. You can also use an existing Docker image with the same functionality.
Practice K8s
To start creating resources, you first need to set up a cluster. You can use Google Kubernetes Engine (GKE) or a local K8s setup with Kind.
After that, create a `deployment.yml` file with the following content:
Explanation:
- ConfigMap: This holds information defined as key-value pairs in the `data` field.
- Secret: This is defined similarly to ConfigMap. Just remember:
- The `stringData` field takes raw data.
- The `data` field requires base64 encoded values. When you get resource info, the results are displayed in base64.
- I've already provided a guide on creating Deployment and Service here.
- For Deployment, you'll need to define the following:
- volumes: Similar to defining volumes in Docker. Just make sure to correctly map the `name` field to the ConfigMap and Secret you created earlier.
- volumeMounts: This is used to mount the volumes into the Deployment created from the Docker image. Here, I'm mounting two files, `data.txt` and `secret.txt`, into the `data` folder within the Docker image.
- env: Maps values from ConfigMap and Secret to create environment variables.
Apply to create resource:
Note: I've defined all resources in a single file for simplicity, but in practice, you should separate each resource into individual YAML files for better management.
Checking resource provisioning
View the information for the created ConfigMap and Secret. Note that the Secret will display its value in base64 encoded format.
Application results after deployment
See you in the next articles!
Comments
Post a Comment