Problem: While deploying our docker image in Kubernetes we need a way for our python program to read a configuration json file.
Solution: There is one feature called config maps in Kubernetes, let us deep dive into the nitty gritty of defining and using the config maps in our Kubernetes pod.
Steps to inject a config file:
First we need to create an yaml file.
apiVersion: v1
kind: ConfigMap
metadata:
name: json-configmap
namespace: default
data:
config_develop.json: |
{
"current_env" : "gcp-development-minikube",
"Directory_Type" : "folder",
"Input_Environment_Type" : "gcs",
"Input_Environment_Location" : "raw-data",
"Output_Environment_Type" : "bigquery",
"Output_Environment_Location" : "test_ds",
"report_location": "reports"
}
config_prod.json: |
{
"current_env" : "gcp-Prod",
"Directory_Type" : "folder",
"Input_Environment_Type" : "gcs",
"Input_Environment_Location" : "raw-data",
"Output_Environment_Type" : "bigquery",
"Output_Environment_Location" : "data_ds",
"report_location": "reports"
}
Here the kind should be ConfigMap only because there are different kinds we can use in an yaml file for kubernetes like deployment, pod etc...
The namespace is your kubernetes namespace, this will be generally default, unless you created a specific namespace.
Keypoints to note here, yaml files are particular about formatting and postion, so please be careful while editing it. The pipe(|) symbol is important. Inside your curly braces the json should be a valid json.
Now our json-configmap.yaml is ready, next we need to create a deployment file.
apiVersion: apps/v1
kind: Deployment
metadata:
name: python-script
spec:
replicas: 1
selector:
matchLabels:
app: python-script
template:
metadata:
labels:
app: python-script
spec:
volumes:
# `name` here must match the name
# specified in the volume mount
- name: json-configmap-volume
#
configMap:
# `name` here must match the name
# specified in the ConfigMap's YAML
name: json-configmap
containers:
- name: python-script
image: python-app:latest
volumeMounts:
- mountPath: /etc/config
name: json-configmap-volume
resources:
requests:
cpu: 100m
memory: 1Gi
Here we are taking the json from config file and creating a json file and mounting it to our host machine in kubernetes.
Key points to take care of here:
Under spec/volumes the name should be <the name you have given in the config map metadata>-volume followed by hypen volume.
Under spec/volumes/configMap the name should be the name you have given in the config map.
Under spec/containers/volumeMounts the mountPath will the path you want in your host system and name will be the one you have given in spec/volumes.
Now we need to create the config map in kubernetes, use the following command:
kubectl create -f ~/Work/config-json.yaml
After the -f you need to specify the path where you defined your config-json.yaml file
You can delete the config map with the following command.
kubectl delete -f ~/Work/config-json.yaml
kubectl create -f ~/Work//deployment.yaml
That's it, now your config files config_develop.json and config_prod.json are in your kubernetes host machine under the directory /etc/config with filenames as config_develop.json and config_prod.json
Thanks to my collegue Rambabu Kola for helping me in creating and understanding kubernetes config maps. This is a bit mystifying concept, but once you get a hang of it, its damn easy, so please feel free to ask questions if you face any difficulty, in the comments section.