In this section, we’ll use Kubernetes 1.2’s new feature, ConfigMap, to configure the application. You can store multiple string-based configuration files inside of a single ConfigMap configuration. In our example, we’ll store Vert.x’s config.json into a ConfigMap entry.
First, update the kubernetes-lab/hellworld-service/config.json with a new configuration value:
[vagrant@rhel-cdk kubernetes]$ cd ~/kubernetes-lab/hellworld-service/
[vagrant@rhel-cdk kubernetes]$ vi config.json
Here is a suggestion for you:
{
"GREETING": "Hello {name} from ConfigMap"
}
Next, create a ConfigMap entry with this file:
[vagrant@rhel-cdk hellworld-service]$ oc create configmap greeting-config --from-file=config.json
configmap "greeting-config" created
Let’s take a look inside the newly created entry:
[vagrant@rhel-cdk hellworld-service]$ oc edit configmap greeting-config
# HINT: Type ':wq' to exit from vi
You’ll see that the config.json is now part of the YAML file:
apiVersion: v1
data:
config.json: |
{
"GREETING": "Hello {name} from ConfigMap"
}
kind: ConfigMap
…
You can, of course, edit this ConfigMap in the editor too. If you do, edit only the value for the greeting variable.
There are several ways to access the values in this ConfigMap:
-
Mount the entries (in our case, config.json) as a file.
-
Access from the Kubernetes API (we won’t cover this today).
Let’s see how we can mount the configurations as files under a specific directory, e.g., /etc/config/config.json.
First, edit the Helloworld Service Deployment:
[vagrant@rhel-cdk hellworld-service]$ oc edit dc/helloworld-service-vertx
In the editor, add volumes and volume mounts (important - indentation matters!):
apiVersion: v1
kind: DeploymentConfig
…
spec:
…
template:
…
spec:
volumes:
- name: config-volume
configMap:
name: greeting-config
containers:
- image: rafabene/microservices-helloworld-vertx:1.0
volumeMounts:
- name: config-volume
mountPath: /etc/config
…
This will make the configuration file available as the file /etc/config/config.json. Let’s verify by going into the pod itself (remember how to do this by using kubectl exec? You can also use oc exec, but now you will learn another option: oc rsh):
First, find the pod name:
[vagrant@rhel-cdk hellworld-service]$ oc get pods
NAME READY STATUS RESTARTS AGE
…
helloworld-service-vertx-7-e1o5u 1/1 Running 0 54s
helloworld-service-vertx-7-sgosp 1/1 Running 0 40s
…
Then, run a shell inside the pod, and see what’s in /etc/config:
[vagrant@rhel-cdk hellworld-service]$ oc rsh helloworld-service-vertx-7-?????
sh-4.2$ ls /etc/config/
config.json
sh-4.2$ cat /etc/config/config.json
{
"GREETING": "Hello {name} from ConfigMap"
}
sh-4.2$ exit
exit
Note
|
Don’t forget to exit out of the pod environment! |
Great, the file is there, but the application needs to be configured to reference to the file. Vert.x can reference to an external configuration with the command line argument:
-conf /etc/config/config.json
Recall how you were able to configure the command line arguments in the previous steps. You know the drill. Edit the Helloworld Service Deployment Config and add the arguments:
apiVersion: v1
kind: DeploymentConfig
…
spec:
…
template:
…
spec:
…
containers:
- image: rafabene/microservices-helloworld-vertx:…
args:
- -conf
- /etc/config/config.json
…
Check the application to see it is using the new greeting string.
Last, but not least, you can also specify simple key/value pairs in ConfigMap, and then expose them directly as environmental variables too. See the ConfigMap guide for more examples.