Friday, 15 May 2020

Understanding ConfigMap resources on Kubernetes

An Introduction to Kubernetes for Data Scientists - ML in Production

ConfigMap resources are similar to Secrets, but they are suitable to hold no sensitive information.
As the name suggest, they provide a way to inject configurations into containers in order to set those configuration data separately from the application code.
For example, you can use ConfigMaps to store information about server addresses to connect to.

Example.

1) Create a configmap, to store a server url:

kubectl create configmap backend-config --from-literal=serverAddress=171.24.21.41


Review the configmap:

kubectl get configmaps backend-config -o yaml
apiVersion: v1
data:
  key1: serverAddress=171.24.21.41 
kind: ConfigMap
metadata:
  creationTimestamp: 2020-05-15T16:03:31Z 
  name: backend-config
  namespace: my-namespace 
  resourceVersion: "4637" 
  ...



2) To use the ConfigMap inside a pod, write a definition like pod-definition.yaml:

apiVersion: v1
kind: Pod
metadata:
  name: mypod
  namespace: my-namespace
spec:
  containers:
    - name: myapp
      image: myapp:0.1.0
      env:
        - name: SERVER_ADDRESS
          valueFrom:
            configMapKeyRef:
              name: backend-config
              key: serverAddress
  restartPolicy: Always


3) Create the pod:

kubectl apply -f pod-definition.yaml


4) Now you can read the SERVER_ADDRESS configuration value inside you pod as an environment variable.


No comments:

Post a Comment