What is ConfigMap in Kubernetes: Definition and Usage
ConfigMap in Kubernetes is a way to store configuration data separately from application code. It lets you keep settings like environment variables or config files outside containers, so you can change them without rebuilding images.How It Works
Think of a ConfigMap as a simple container for configuration data that your applications need to run. Instead of hardcoding settings inside your app or container image, you store them in a ConfigMap. Kubernetes then makes this data available to your app as environment variables or files.
This separation is like keeping your recipe (app code) and your ingredients list (config) in different places. If you want to change the ingredients, you don’t rewrite the recipe; you just update the list. Similarly, updating a ConfigMap lets you change app settings without rebuilding or redeploying containers.
Example
This example shows a ConfigMap that stores two settings: a database host and a port. The app can then use these values as environment variables.
apiVersion: v1
kind: ConfigMap
metadata:
name: example-config
data:
DB_HOST: database.example.com
DB_PORT: "5432"When to Use
Use ConfigMap when you want to keep configuration separate from your container images. This is helpful when you need to:
- Change settings without rebuilding containers
- Share configuration across multiple pods
- Keep sensitive data out of code (though for secrets, use
Secretinstead)
For example, you might store API endpoints, feature flags, or database connection info in a ConfigMap. This makes your apps more flexible and easier to manage.
Key Points
- ConfigMap stores non-sensitive configuration data.
- It separates config from container images for easier updates.
- Config data can be injected as environment variables or files.
- Use
Secretfor sensitive information like passwords.