Config Map
Config Map
In Kubernetes, a ConfigMap is an API object that allows you to store configuration data in key-value pairs. It is
used to decouple configuration artifacts from container image content, making your applications easier to
manage and more flexible.
Key Features:
1. Decoupling configuration: ConfigMaps allow you to keep configuration separate from code, which makes
managing different environments (e.g., dev, test, prod) easier.
2. Multiple usage methods: You can inject ConfigMap data into your application in different ways:
As environment variables.
As command-line arguments.
Stores non-sensitive data like configuration Stores sensitive data like passwords, API keys,
settings (e.g., API URLs, log levels). and certificates.
Data is stored in plain text (not encrypted). Data is base64-encoded and can be encrypted
at rest.
Used for general configuration information.
Access to Secrets is more secure with RBAC
and encryption options.
Key Differences:
ConfigMaps: For non-sensitive data, stored in plain text.
Secrets: For sensitive data, stored securely with encryption options and stricter access control.
apiVersion: v1 apiVersion: v1
kind: ConfigMap kind: Secret
metadata: metadata:
name: my-config name: my-secret
data: type: Opaque
app-config: "dev" data:
log-level: "debug" username: YWRtaW4= # base64 encoded string for "admin
password: MWYyZDFlMmU2N2Rm # base64 encoded string for
apiVersion: v1
kind: ConfigMap
metadata:
name: my-config
Config Map 1
data:
db-port: "3306"
2. Create a ConfigMap by applying “kubectl apply -f configmap.yml” then now configmap is being create.
We can check it by kubectl describe cm my-config
3. Then we need to create pods along with referring and getting the environment variable value
apiVersion: apps/v1
kind: Deployment
metadata:
name: sample-python-app
labels:
app: sample-python-app
spec:
replicas: 2
selector:
matchLabels:
app: sample-python-app
template:
metadata:
labels:
app: sample-python-app
spec:
containers:
- name: python-app
image: rohit5683/python-demo-app:v1
env:
- name: DB-PORT
valueFrom:
configMapKeyRef:
name: my-config
key: db-port
ports:
- containerPort: 8000
env | grep DB
DB-PORT=3306
apiVersion: v1
kind: Secret
Config Map 2
metadata:
name: my-secret
type: Opaque
data:
username: bXl1c2Vy
password: bXlwYXNzd29yZA==
a. Accessing Secret as Environment Variables: You can inject the Secret values as environment
variables in the pod.
Example pod.yaml :
apiVersion: apps/v1
kind: Deployment
metadata:
name: sample-python-app
labels:
app: sample-python-app
spec:
replicas: 2
selector:
matchLabels:
app: sample-python-app
template:
metadata:
labels:
app: sample-python-app
spec:
containers:
- name: python-app
image: rohit5683/python-demo-app:v1
env:
- name: SECRET_USERNAME
valueFrom:
secretKeyRef:
name: my-secret
key: username
- name: SECRET_PASSWORD
valueFrom:
secretKeyRef:
name: my-secret
key: password
- containerPort: 8000
Config Map 3
b. Mounting Secret as a Volume: You can also mount the Secret as a volume so that its data is available
as files in the container.
Example pod.yaml :
apiVersion: apps/v1
kind: Deployment
metadata:
name: sample-python-app
labels:
app: sample-python-app
spec:
replicas: 2
selector:
matchLabels:
app: sample-python-app
template:
metadata:
labels:
app: sample-python-app
spec:
containers:
- name: python-app
image: rohit5683/python-demo-app:v1
volumeMounts:
- name: secret-volume
mountPath: /etc/secret
volumes:
- name: secret-volume
secret:
secretName: my-secret
- containerPort: 8000
If mounted as a volume:
Config Map 4