To try out this example, ensure you have access to a Kubernetes cluster by choosing one of the options from INSTALL.
The sample [random-generator] REST service can be configured using environment variables and an application.properties
configuration file. In this example, we’ll provide this configuration via a ConfigMap.
First, create the ConfigMap with the following command:
kubectl apply -f https://k8spatterns.io/ConfigurationResource/configmap.yml
Next, the following Pod declaration will use this ConfigMap as environment variables and mount the application.properties
entry as random-generator.properties
in the /config/app
directory.
For additional explanations, please review the comments at pod.yml.
Create the Pod with this command:
kubectl create -f https://k8spatterns.io/ConfigurationResource/pod.yml
As in previous examples, we’ll expose a nodePort service for accessing our service:
kubectl apply -f https://k8spatterns.io/ConfigurationResource/service.yml
Now, access the service, assuming your cluster node has an external IP address that can be directly contacted from your desktop.
The /info
endpoint returns the configuration data picked up by the random-generator Pod:
minikube service random-generator --url > /tmp/random-url.txt &
This command starts a tunnel in the background and writes down the access URLs for the exposed ports. Store these URLs in a text file and use the following command to access the service:
curl -s $(cat /tmp/random-url.txt)/info | jq .
To filter the environment variables that have been bulk-exposed with a CONFIG_
prefix in an envFrom
section, use:
curl -s $(cat /tmp/random-url.txt)/info | \
jq '.env | with_entries(select(.key | startswith("CONFIG_")))'
Now let’s check whether the permissions of the mounted random-generator.properties
file has been set properly to 0400
; use the following command:
kubectl exec random-generator -- ls -l /config/app/random-generator.properties
Finally, to demonstrate that the ConfigMap is indeed immutable, attempt to patch it with the following command:
kubectl patch configmap random-generator-config --type merge -p '{"data":{"newKey":"newValue"}}'
You should receive an error indicating that the ConfigMap cannot be updated since it is marked as immutable.