Configuration mgt in Kubernetes 1 (ConfigMap)
Configuration management aims to provide a systematic approach to handling configuration data throughout the application lifecycle, from development to deployment and maintenance. It ensures consistency, reliability, and flexibility by enabling easy management of configurations across different environments, such as development, testing, staging, and production.
Effective configuration management involves several key practices, including:
- Centralized Configuration: Storing configuration data in a centralized location allows easy management and ensures consistency across different instances of the application.
- Versioning and Auditing: Keeping track of configuration changes, maintaining version history, and enabling auditing help in troubleshooting issues and reverting to previous configurations if needed.
- Automation and Deployment Pipelines: Integrating configuration management into the deployment pipelines ensures consistent and reliable application configuration across various environments.
- Security and Access Control: Implementing appropriate security measures, such as encryption and access controls, protects sensitive configuration data and prevents unauthorized access or modifications.
- Testing and Validation: Performing testing and validation of configuration changes help ensure that the application behaves as expected in different environments and with various configuration scenarios.
ConfigMap creation
ConfigMaps in Kubernetes can be created manually using the Kubernetes command-line interface (CLI) or through configuration files written in YAML or JSON format. Let’s explore both approaches:
- Manual Creation: When creating a ConfigMap manually, you can use the
kubectl create configmap
command followed by the desired options and values. Here's an example of the command to create a ConfigMap with key-value pairs:
kubectl create configmap our-configmap --from-literal=key1=value1 --from-literal=key2=value2
In this example, the --from-literal
flag is used to specify key-value pairs directly on the command line.
Additionally, you can create a ConfigMap from a file using the --from-file
flag, like this:
kubectl create configmap our-configmap --from-file=path/to/config/file.txt
This command creates a ConfigMap named our-configmap using the contents of the specified file.
2. Configuration File Creation: ConfigMaps can also be created using configuration files in YAML or JSON format. These files define the ConfigMap’s properties, including the key-value pairs or the contents of configuration files.
Here’s an example YAML file defining a ConfigMap with key-value pairs:
apiVersion: v1
kind: ConfigMap
metadata:
name: my-configmap
data:
key1: value1
key2: value2
You can apply this configuration using the kubectl apply
command:
kubectl apply -f path/to/configmap.yaml
How ConfigMaps can be used to configure applications deployed in Kubernetes
Setting Environment Variables: ConfigMaps can be used to set environment variables for applications. For example, you can define a ConfigMap with key-value pairs for different configuration settings and then inject them as environment variables into the application container. This allows the application to access the configuration values during runtime. Here’s an example:
apiVersion: v1
kind: ConfigMap
metadata:
name: our-configmap
data:
DB_HOST: db.example.com
DB_PORT: "5432"
API_KEY: ABC123
The above ConfigMap can be consumed in deployment as environment variables:
spec:
template:
spec:
containers:
- name: our-app
image: our-image
envFrom:
- configMapRef:
name: our-configmap
The application container will have environment variables DB_HOST
, DB_PORT
, and API_KEY
set with the respective values from the ConfigMap.
Mounting Configuration Files: ConfigMaps can also be used to provide configuration files to applications by mounting them as volumes. This approach allows the application to access the configuration files as regular files within the container. Here’s an example:
apiVersion: v1
kind: ConfigMap
metadata:
name: our-configmap
data:
app-config.yaml: |
apiEndpoint: https://api.example.com
timeout: 5000
debug: false
The ConfigMap above contains a configuration file named app-config.yaml
. To consume it in a deployment, you can mount it as a volume:
spec:
template:
spec:
containers:
- name: our-app
image: our-image
volumeMounts:
- name: config-volume
mountPath: /app/config
volumes:
- name: config-volume
configMap:
name: our-configmap
The ConfigMap’s app-config.yaml
file is mounted at the path /app/config
within the container, allowing the application to access and utilize the configuration file.
Conclusion
In this article, we have covered what ConfigMap is, how it’s applied and used.