In Kubernetes, managing application configurations can be efficiently handled using ConfigMaps. A ConfigMap is an API object used to store non-confidential data in key-value pairs. Pods can consume these ConfigMaps as environment variables, command-line arguments, or as configuration files in a volume.
ConfigMaps provide a way to separate configuration from code, making it easier to manage and update application settings without needing to rebuild container images. This tutorial will guide you through the basics of using ConfigMaps and then delve into some advanced concepts.
A ConfigMap can be created in two ways: imperatively using kubectl or declaratively using a YAML file.
You can create a ConfigMap from literal values:
ConfigMaps can be used in Pods in several ways, including as environment variables or mounted as files.
You can inject the ConfigMap data into a Pod's environment variables:
1apiVersion: v12kind: Pod3metadata:4name: my-pod5spec:6containers:7- name: my-container8image: nginx9env:10- name: KEY111valueFrom:12configMapKeyRef:13name: my-config14key: key115- name: KEY216valueFrom:17configMapKeyRef:18name: my-config19key: key2
You can also mount the ConfigMap as files in a volume:
1apiVersion: v12kind: Pod3metadata:4name: my-pod5spec:6containers:7- name: my-container8image: nginx9volumeMounts:10- name: config-volume11mountPath: /etc/config12volumes:13- name: config-volume14configMap:15name: my-config
Let's create a simple Nginx Pod that uses a ConfigMap for its configuration:
1apiVersion: v12kind: ConfigMap3metadata:4name: nginx-config5data:6default.conf: |7server {8listen 80;9server_name localhost;1011location / {12root /usr/share/nginx/html;13index index.html index.htm;14}15}
Now, you can access the Nginx server at http://localhost:8080.
Let's create a simple application that reads configuration from a file:
1apiVersion: v12kind: ConfigMap3metadata:4name: app-config5data:6config.json: |7{8"name": "My App",9"version": "1.0"10}
{
"name": "My App",
"version": "1.0"
}After mastering ConfigMaps, you might want to explore how to handle sensitive data using Secrets in Kubernetes. Secrets provide a way to manage sensitive information like passwords and tokens securely.
Stay tuned for more tutorials on advanced Kubernetes concepts!
Info