For trying out this example you need to have access to a Kubernetes cluster, e.g. by choosing one of the options from INSTALL.
Our sample [random-generator] REST service can be configured with environment variables and an application.properties
configuration file.
We will now provide this configuration via a ConfigMap.
For this, let’s create the ConfigMap with:
kubectl create -f https://k8spatterns.io/ConfigurationResource/configmap.yml
The following Pod declaration will now put up this ConfigMap for being used as environment variables and mounted as files in a directory /config
.
Please check out the comments in pod.yml which provide some more explanations.
Let’s create that Pod with:
kubectl create -f https://k8spatterns.io/ConfigurationResource/pod.yml
Like in the other examples we expose a nodePort service for accessing our service:
kubectl create -f https://k8spatterns.io/ConfigurationResource/service.yml
Now let’s access the service where we again assume that your cluster node’s have an external IP address which can be directly contacted from your desktop.
The /info
endpoint will return the configuration data that has been picked up by the random-generator Pod:
port=$(kubectl get svc random-generator -o jsonpath={.spec.ports[0].nodePort})
# Use Minikube's IP or any external accessible node IP from your cluster
ip=$(minikube ip)
curl -s http://$ip:$port/info | jq .
In order to filter on the environment variables which have been bulk-exposed with a CONFIG_
prefix in an envFrom
section, use:
# Check also for env variables which has been set via envFrom
curl -s http://$ip:$port/info | \
jq '.env | with_entries(select(.key | startswith("CONFIG_")))'