ConfigMaps: Kubernetes (k8s)
ConfigMaps in Kubernetes allow you to decouple configuration files from the container image. This allows you to change configuration without having to rebuild or redeploy the container image.
A ConfigMap can be created in Kubernetes in a variety of ways.
By creating a YAML file
By creating a YAML file with the configuration data and using the kubectl create
command to create the ConfigMap.
Here is an example of a ConfigMap defined in a YAML file:
apiVersion: v1
kind: ConfigMap
metadata:
name: example-config
data:
example.property: value
This ConfigMap can be created by running the following command:
kubectl create -f example-config.yaml
By using the kubectl command
By using the kubectl create configmap
command and specifying the configuration data on the command line.
kubectl create configmap example-config --from-literal=example.property=value
You can also use the --from-file
option to create a ConfigMap from one or more files.
Once you have created a ConfigMap, you can reference it from a pod or deployment by using a volume and a volume mount. The following is an example of a pod that references a ConfigMap:
apiVersion: v1
kind: Pod
metadata:
name: example-pod
spec:
containers:
- name: example
image: nginx
volumeMounts:
- name: config
mountPath: /etc/example
volumes:
- name: config
configMap:
name: example-config
In this example, the ConfigMap named “example-config” is mounted to the path “/etc/example” in the container. Now the configMap available inside pod container and the application running inside can use that configuration
You can also use ConfigMaps to set environment variables in a container. The following is an example of a pod that sets environment variables from a ConfigMap:
apiVersion: v1
kind: Pod
metadata:
name: example-pod
spec:
containers:
- name: example
image: nginx
envFrom:
- configMapRef:
name: example-config
In this example, the pod’s container has all the key-value pairs in the example-config ConfigMap as environment variables.
You can also reference ConfigMap keys in the command and args sections of a pod spec. This allows you to use the ConfigMap data to configure the command and arguments that the container runs with.
Useful kubectl
Commands
kubectl create configmap
: This command creates a new ConfigMap. You can specify the configuration data using the --from-literal
or --from-file
options.
kubectl create configmap my-config --from-literal=key1=value1 --from-literal=key2=value2
kubectl create configmap my-config-file --from-file=path/to/configfile.txt
kubectl get configmap
: This command retrieves information about one or more ConfigMaps.
kubectl get configmap my-config
kubectl describe configmap
: This command provides detailed information about a specific ConfigMap.
kubectl describe configmap my-config
kubectl update configmap
: This command updates an existing ConfigMap. You can use the --from-literal
or --from-file
options to update the configuration data.
kubectl update configmap my-config --from-literal=key1=new-value1
kubectl delete configmap
: This command deletes one or more ConfigMaps.
kubectl delete configmap my-config
kubectl edit configmap
: This command opens an editor to edit the ConfigMap in place.
kubectl edit configmap my-config
ConfigMaps
are a powerful way to manage configuration data in Kubernetes. They allow you to decouple configuration files from container images, making it easier to update and manage configuration changes.