Understanding Configuration — Kubernetes
Like many other applications, we will always have the logic of the application and also the configuration. While we can always put the configuration in the code, this is not a good approach. Kubernetes is no different. It’s always better and safer to separate the logic of your Kubernetes application from its configuration.
Configurations are settings or values that might change over the life of an application. Configuration values include things such as authentication credentials, DNS addresses of third-party services and environment-specific settings. A change in any of these values will require a complete redeployment of the entire application.
We can manage these configurations in Kubernetes in two ways.
- Passing values as environment variables.
- Using ConfigMaps and Secrets
What are ConfigMaps?
The ConfigMaps are the primary objects for storing configuration data in Kubernetes.ConfigMaps are key-value pairs that store configuration data.
Creating ConfigMaps
You can create ConfigMaps in two ways
A- The imperative way which uses kubectl create configmap
B- The declarative way by using a manifest file
A: The Imperative way of creating ConfigMaps
- By the use of literal values. This command allows you to pass your key and values from the command line. You can pass in a single or multiple key-value pairs
kubectl create configmap nameofyourconfigmap --from-literal=db=test
The YAML file of the configmap created above look like this
2. By use of a single file with environment variables
kubectl create configmap nameofyourconfigmap --from-env-file=config.env
if you had a file called testenv.properties with the following key/values
You would the create a configmap using the following command
The YAML of the created configmap would then look like this:
3. By use of a file or directory
kubectl create configmap nameofyourconfigmap --from-file=config.txt
The above command means that Kubernetes will look for a file called config.txt and will package the key and value items as config maps.
Example:
i. Create a config.txt file with two values key1=value1 and key2=value2
ii. Create a configmap named testconfigmap and read data from that file.
iii. Our configmap has been successfully been created from the file.
iv. Let’s now view the YAML file created by the above command.
You can also create config maps from multiple files through the following command
kubectl create configmap configmapname --from-file=filename1 --from-file=filename2
Let’s view the YAML file create from the two files above
B: The declarative way of creating ConfigMaps.
You can also create a ConfigMap by using a manifest file
apiVersion: v1
data:
db:test
username:peterpan
kind:ConfigMap
metadata:
name:db-config
Consuming ConfigMap
Creating a configMap is the first step. The second step is to consume your data. You can supply data to your application in two major ways.
i. The first way is by mounting the key/value pairs as volumes in your pod. When you use ConfigMap in Pod as volume, each key becomes a file in the mounted directory.
apiVersion: v1
kind: Pod
metadata:
name:backend
spec:
containers:
- image:nginx
name:backend
volumeMounts:
- name: config-volume
mountPath: /etc/config
volumes:
- name: config-volume
configMap:
name: db-config [name of your configmap]
Let’s navigate to the our volume path and see if our data is there.
$ kubectl exec -it backend -- /bin/sh
# ls /etc/config
db
username
# cat /etc/config/db
test
ii. The second way is by injecting the ConfigMap environment variables to your pod. This is used and convenient if the ConfigMap reflects the desired syntax. Example:
apiVersion: v1
kind: Pod
metadata:
name:backend
spec:
containers:
- image:nginx
name:backend
envFrom:
- configMapRef:
name:db-config[name of your configmap]
Next, we are going to look at Secrets which are Kubernetes objects mostly used with sensitive config data such as passwords or API keys.