# An introduction to Kubernetes Secrets and ConfigMaps
Kubernetes has two types of objects that can inject configuration data into a container when it starts up: Secrets and ConfigMaps. Secrets and ConfigMaps behave similarly in Kubernetes, both in how they are created and because they can be exposed inside a container as mounted files or volumes or environment variables.
# To understand more how configmap and secrets are important let’s consider the following scenario:
You have to run a postgres docker image on your host, you explore the documentation of this docker image provided in DockerHub and you find that you the PostgreSQL image uses several environment variables and there is a mandatory variable called POSTGRES_PASSWORD
must be defined by running this following command:
1 | $ docker run --name my-postgres -e POSTGRES_PASSWORD=mypassword -d postgres |
# But how we can use this environment variables and how we can manage them in kubernetes ?
We can centralize the variables environment in two types of objects and import these variables in the manifest of a pod, replicasets, or deployments
# What is a ConfigMaps
In Kubernetes, a ConfigMap is nothing more than a key/value pair. A ConfigMap store’s non-confidential data, meaning no passwords or API keys. Pods can consume ConfigMaps as environment variables, command-line arguments, or as configuration files in a volume.
A ConfigMap allows you to decouple environment-specific configuration from your container images, so that your applications are easily portable.
# Example of confimap manifest that store the database name & username:
1 | apiVersion: v1 |
# Create the ConfigMaps Object:
1 | justk8s@justk8s-master:~$ kubectl apply -f first-configmap.yaml |
# List the ConfigMap Objects:
1 | justk8s@justk8s-master:~$ kubectl get configmap |
# What is a Secrets:
Secrets are a Kubernetes object intended for storing a small amount of sensitive data. It is worth noting that Secrets are stored base64-encoded within Kubernetes, so they are not wildly secure.
Secrets are similar to ConfigMaps but are specifically intended to hold confidential data.
# Example of confimap manifest that store the database password:
we must encode the value that we will stored in the Secrets
1 | justk8s@justk8s-master:~$ echo -n "mohamed" | base64 |
Now we can use the base64 cipher in the Secret manifest
1 | apiVersion: v1 |
# Create the ConfigMaps Object:
1 | justk8s@justk8s-master:~$ kubectl apply -f first-secret.yaml |
# List the ConfigMap Objects:
1 | justk8s@justk8s-master:~$ kubectl get secrets |
# How to use ConfigMaps and Secrets values in a Pod
we can use the values from ConfigMaps
and Secrets
in the pod manifests in the env
propriety of the container by using the valueFrom
field that can import values from configMap and Secrets
1 | apiVersion: v1 |
# Create a PostgreSQL Pod that uses values from ConfigMaps and Secrets
# 1- Create the Pod manifest:
1 | apiVersion: v1 |
# 2- Create the Pod:
1 | justk8s@justk8s-master:~$ kubectl apply -f postgres.yaml |
# 3- List The Created Pod:
1 | justk8s@justk8s-master:~$ kubectl get pods |
# 4- Test the Database created with variables of ConfigMap and Secrets:
We can open a bash session on the pod and open the database mohamed
with the psql
command provided by the postgreSQL
1 | justk8s@justk8s-master:~$ kubectl exec --stdin --tty postgresql -- /bin/bash |
# References:
Get a Shell to a Running Container
ConfigMaps
Secrets
PostgreSQL Docker