- It stores data as key-value pair.
- It helps us to separate application environment specific configurations from the code.
- Each Pod uses ConfigMap values as environment variables, volumes or command-line arguments.
- Key name must contain alphanumeric, dash (-), underscore (_) and dot(.) only
- It is not suitable for storing confidential data.
Create ConfigMap
There are multiple ways to create K8 ConfigMap object- from YAML file
apiVersion: v1
kind: ConfigMap
metadata:
name: configmap-db
data:
db_host: "mydb.rds.amazonaws.com"
db_port: "3306"
db_name: "testuser"
Here, we should specify key-value pair as data property in the K8 ConfigMap object
Save above yaml content as "configmapdb.yaml" and run following kubectl command
// create configmap object from yaml file
$ kubectl apply -f configmapdb.yaml
configmap/configmap-db created
// display all configmap objects
$ kubectl get configmap
NAME DATA AGE
configmap-db 3 2m
// view configmap object details using describe command
$ kubectl describe configmap configmap-db
// view configmap object details as yaml file
$ kubectl get configmap configmap-db -o yaml
2. from literals
In this approach, we pass key-value pair as part of kubectl command directly.
In the below example, we are setting Pod container individual environment variables from “configmap-db” object
// create configmap object from literals
// syntax
kubectl create configmap <configmap-name> --from-literal=<key_name>=<key_value>
$ kubectl create configmap configmap-ui --from-literal=app_name=mytestapp --from-literal=app_theme=bluetheme
configmap/configmap-ui created
// display all configmap objects
$ kubectl get configmap
NAME DATA AGE
configmap-db 3 134m
configmap-ui 2 19s
// view configmap object details using describe command
$ kubectl describe configmap configmap-ui
// view configmap object details as yaml file
$ kubectl get configmap configmap-ui -o yaml
3. from an env-file
In this approach, we create an env file with list of environment variables and values
db_host=mydb.rds.amazonaws.com
db_port=3306
db_name=testuser
save above content as "configmap-db-env.txt" or "configmap-db-env.properties" and run following kubectl command
// create configmap object from env file
$ kubectl create configmap configmap-db-env --from-env-file=configmap-db-env.txt
configmap-db-env created
// display all configmap objects
$ kubectl get configmap
NAME DATA AGE
configmap-db 3 153m
configmap-db-env 3 6s
configmap-ui 2 18m
// view configmap object details using describe command
$ kubectl describe configmap configmap-db-env
// view configmap object details as yaml file
$ kubectl get configmap configmap-db-env -o yaml
Passing ConfigMap values to Pod
1. passing ConfigMap values to container environment variables
apiVersion: v1
kind: Pod
metadata:
name: configmap-pod
spec:
containers:
- name: busybox
image: k8s.gcr.io/busybox
command: ["/bin/sh", "-c", "env"]
env:
- name: DATABASE_PORT
value: "3306"
- name: DATABASE_HOST
valueFrom:
configMapKeyRef:
name: configmap-db
key: db_host
- name: DATABASE_NAME
valueFrom:
configMapKeyRef:
name: configmap-db
key: db_name
save above yaml content as "configmap-pod-valuefrom.yaml" and run following kubectl command
// create busybox Pod
$ kubectl apply -f configmap-pod-valuefrom.yaml
pod/configmap-pod created
// check environment values from busybox Pod
$ kubectl logs pod/configmap-pod | grep DATABASE
DATABASE_PORT=3306
DATABASE_NAME=testuser
DATABASE_HOST=mydb.rds.amazonaws.com
2. passing complete ConfigMap Object as environment variables to the container
apiVersion: v1
kind: Pod
metadata:
name: configmap-pod-env
spec:
containers:
- name: busybox
image: k8s.gcr.io/busybox
command: ["/bin/sh", "-c", "env"]
envFrom:
- configMapRef:
name: configmap-db
save above yaml content as "configmap-pod-envfrom.yaml" and run following kubectl command
// create busybox Pod
$ kubectl apply -f configmap-pod-envfrom.yaml
pod/configmap-pod-env created
// check environment values from busybox Pod
$ kubectl logs pod/configmap-pod-env | grep db
db_port=3306
db_name=testuser
db_host=mydb.rds.amazonaws.com
3. passing ConfigMap defined environment variables as container command arguments
apiVersion: v1
kind: Pod
metadata:
name: configmap-pod-command
spec:
containers:
- name: busybox
image: k8s.gcr.io/busybox
command:
[
"/bin/echo",
"dbhostname: $(DATABASE_HOST) and dbport: $(DATABASE_PORT)",
]
env:
- name: DATABASE_HOST
valueFrom:
configMapKeyRef:
name: configmap-db
key: db_host
- name: DATABASE_PORT
valueFrom:
configMapKeyRef:
name: configmap-db
key: db_port
save above yaml content as "configmap-pod-command.yaml" and run following kubectl command
// create busybox Pod
$ kubectl apply -f configmap-pod-command.yaml
pod/configmap-pod-command created
// check logs from busybox Pod
$ kubectl logs pod/configmap-pod-command
dbhostname: mydb.rds.amazonaws.com and dbport: 3306
4. Attaching ConfigMap as container volume
- we are first creating volumes by setting configMap name property as "configmap-db"
- we are accessing this volume by setting volumeMounts property
apiVersion: v1
kind: Pod
metadata:
name: configmap-pod-volume
spec:
containers:
- name: busybox
image: k8s.gcr.io/busybox
command: ["/bin/sh", "-c", "ls /etc/configmap/"]
volumeMounts:
- name: configmap-volume
mountPath: /etc/configmap
volumes:
- name: configmap-volume
configMap:
name: configmap-db
save above yaml content as "configmap-pod-volume.yaml" and run following kubectl command
// create busybox Pod
$ kubectl apply -f configmap-pod-volume.yaml
pod/configmap-pod-volume created
// check logs from busybox Pod
$ kubectl logs pod/configmap-pod-volume
db_host
db_name
db_port
Kubernetes for Developers Journey.
- Kubernetes for Developers #25: PersistentVolume and PersistentVolumeClaim in-detail
- Kubernetes for Developers #24: Kubernetes Volume hostPath in-detail
- Kubernetes for Developers #23: Kubernetes Volume emptyDir in-detail
- Kubernetes for Developers #22: Access to Multiple Clusters or Namespaces using kubectl and kubeconfig
- Kubernetes for Developers #21: Kubernetes Namespace in-detail
- Kubernetes for Developers #20: Create Automated Tasks using Jobs and CronJobs
- Kubernetes for Developers #19: Manage app credentials using Kubernetes Secrets
- Kubernetes for Developers #18: Manage app settings using Kubernetes ConfigMap
- Kubernetes for Developers #17: Expose service using Kubernetes Ingress
- Kubernetes for Developers #16: Kubernetes Service Types - ClusterIP, NodePort, LoadBalancer and ExternalName
- Kubernetes for Developers #15: Kubernetes Service YAML manifest in-detail
- Kubernetes for Developers #14: Kubernetes Deployment YAML manifest in-detail
- Kubernetes for Developers #13: Effective way of using K8 Readiness Probe
- Kubernetes for Developers #12: Effective way of using K8 Liveness Probe
- Kubernetes for Developers #11: Pod Organization using Labels
- Kubernetes for Developers #10: Kubernetes Pod YAML manifest in-detail
- Kubernetes for Developers #9: Kubernetes Pod Lifecycle
- Kubernetes for Developers #8: Kubernetes Object Name, Labels, Selectors and Namespace
- Kubernetes for Developers #7: Imperative vs. Declarative Kubernetes Objects
- Kubernetes for Developers #6: Kubernetes Objects
- Kubernetes for Developers #5: Kubernetes Web UI Dashboard
- Kubernetes for Developers #4: Enable kubectl bash autocompletion
- Kubernetes for Developers #3: kubectl CLI
- Kubernetes for Developers #2: Kubernetes for Local Development
- Kubernetes for Developers #1: Kubernetes Architecture and Features
Happy Coding :)
No comments:
Post a Comment