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:
Review the configmap:
kubectl create configmap backend-config --from-literal=serverAddress=171.24.21.41
kubectl get configmaps backend-config -o yaml
apiVersion: v1data:
key1: serverAddress=171.24.21.41
kind: ConfigMap
metadata:
creationTimestamp: 2020-05-15T16:03:31Z
metadata:
creationTimestamp: 2020-05-15T16:03:31Z
name: backend-config
namespace: my-namespace
namespace: my-namespace
resourceVersion: "4637"
...
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