When dealing with
microservices, you need to figure out how the configurations will be applied.
In situations where you want to deploy to multiple environments like stage, dev
and prod, it is not ideal to bake the configs into the application because of the
difference in environments. Ideally you want separate configurations to match
the environment you are deploying to. This is where ConfigMaps comes into play.
It allows you to decouple configuration artifacts from image content. This
allows containerized application to become portable without worrying about
configurations. ConfigMap is similar to Secrets, but provides a way of working
with strings that don’t contain sensitive information. Users and system
components can store configuration data in ConfigMap.
Creating
configmaps
Creating ConfigMaps is
pretty simple and straightforward. You can create it with directories, file or
literal values. Let us see each in action. The rest of the blog post will use a
simple example to demonstrate how to work with a configmap.
From
a directory
To create configmap from a
directory, we must create or have an existing directory with our configs in
there.
mkdir configmap-demo
wget the configs into our
configmap-demo directory.
wget
https://k8s.io/docs/tasks/configure-pod-container/configmap/kubectl/game.properties
-O configmap-demo
wget
https://k8s.io/docs/tasks/configure-pod-container/configmap/kubectl/ui.properties
-O configmap-demo
What we just did was
download test config files into our directory. We can then create a configmap
from our directory that will have all the files we downloaded.
kubectl create configmap
demo-configmap --from-file=configmap-demo
If we describe our
configmap, we will see both files as data entries with their contents.
kubectl describe configmap
demo-configmap
From
a file
Creating from a file is very
similar to creating from directory. All we need to do is pass in the name of
the file to –from-file argument. When creating configmap this way, you can pass
in as many files as you want to the –from-file argument and it will add it to
the configmap.
From
literal value
Creating configmap this way
mean you can specify your configuration directly from command line without
creating any file or directory. For example “kubectl create configmap
special-config –from-literal=special.how=very
–from-literal=special.type=charm”. You can have multiple key-value pairs if
needed.
Using
configmap in a pod
Lets create a test configmap
kubectl create configmap
special-config --from-literal=special.how=very
Then create a pod to use the
configmap as an env variable.
demo-pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: configmap-demo-pod
spec:
containers:
- name: test-container
image: k8s.gcr.io/busybox
command: [ "/bin/sh",
"-c", "env" ]
env:
# Define the environment variable
- name: SPECIAL_LEVEL_KEY
valueFrom:
configMapKeyRef:
# The ConfigMap containing the
value you want to assign to SPECIAL_LEVEL_KEY
name: special-config
# Specify the key associated with
the value
key: special.how
restartPolicy: Never
kubectl create -f
demo-pod.yaml
We can now see that there is
an environment variable set in the pod with our defined configmap value.
kubectl logs
configmap-demo-pod | grep SPECIAL_LEVEL_KEY
No comments:
Post a Comment