ConfigMaps and Secrets
ConfigMap
A ConfigMap is an API object used to store non-credential data in key-value pairs.
Kubernetes pods can use the created ConfigMaps as a:
- Configuration files
- Environment variable
- Command-line argument
A ConfigMap allows you to decouple environment-specific configuration from your container images, so that your applications are easily portable.
Importantly, ConfigMaps are not suitalbe for storing a confidental data. They don't provide any kind of encryption, and all the data in them are visible to anyone who has access to the file.
Define a ConfigMap
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
|
In a ConfigMap, the required information can store in the data
field. We can store values as two ways:
- As individual key-value pair properties
- In a granular format where they are fragments of a configuration format. (File Like Keys)
Utilizing ConfigMaps in Pod
There are four ways that you can use a ConfigMap to configure a container inside a Pod:
- Inside a container command and args
- Environment variables for a containers
- Add a file in read-only volumne, for application to read
- Write code inside the Pod that uses the K8s API to read a ConfigMap
Here's an example Pod that that uses values from the above ConfigMap:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
|
For this example, defining a volume and mounting it inside the demo container as /config
creates three files, /config/game.properties
, /config/user-interface.properties
and prometheus.yaml
, even though there are four keys in the ConfigMap.
This is because the Pod definition specifies an items
array in the volumes
section.
If you omit the items
array entirely, every key in the ConfigMap becomes a file with the same name as the key, and you get 5 files
Pods use ConfigMaps through environment variables and configMap volumes
Pass ConfigMap entries to a pod as files in a volume
Pass a ConfigMap entry as a command-line argument
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
Secrets
A Secret is an object that contains a small amount of sensitive data such as a password, a token, or a key. Secrets are similar to ConfigMaps but are specifically intended to hold confidential data.
Define a Secrets
1 2 3 4 5 6 7 |
|
Note:
- Secrets are not encrypted. Only encoded (base64).
- Secrets are not encrypted in etcd
- Anyone able to create pods/ deployments in the same namespace can access the secrets
In order to safely use Secrets, take at least the following steps:
- Enable Encryption at Rest for Secrets. (encrypt the data in etcd)
- Enable or configure RBAC rules with least-privilege access to Secrets.
- Restrict Secret access to specific containers.
- Consider using external Secret store providers.
Uses for Secrets
There are three main ways for a Pod to use a Secret:
- As files in a volume mounted on one or more of its containers.
- As container environment variable.
- By the kubelet when pulling images for the Pod.
1 2 3 |
|
1 2 3 4 5 6 |
|
1 2 3 4 |
|