Exercise 4 - ConfigMaps¶
We want to change the index.html
served by the Apache HTTP Server in order to personalize our website.
There are multiple approaches to achieve this:
- change the container image by adding the file during the build to the image (out of scope for this workshop)
- use a
ConfigMap
to inject the file to the container in thePod
ConfigMap
provides a mechanisms to inject containers with configuration data (entire files or single values) while keeping the containers unchanged.
Check out the OpenShift or Kubernetes Documentation for more information about ConfigMaps
.
Note
For each exercise you can choose and only have to do either the 'Web Console' or the 'Command Line Interface (CLI)' section in oder to complete the exercise.
Create a ConfigMap
¶
- Switch to the 'Administrator' view using the dropdown menu at .
- Navigate to 'Workloads' → 'Config Maps' .
- Switch to the your project using the dropdown menu at . In each of the exercise make sure that you are in your project context before you are making any changes.
- Press 'Create Config Map' .
-
Create a
ConfigMap
with theindex.html
using the content below:(replace<!DOCTYPE html> <html> <head> <title>John Doe</title> </head> <body> <h1>John Doe</h1> <p> Today I learned how to deploy my own website in the cloud using an Apache HTTP Server (httpd) running on OpenShift. </p> </body> </html>
John Doe
with your actual name - if you are John Doe you can skip this)Try to create the
ConfigMap
definition file on your own using the template at and the additional schema informationSolution:
apiVersion: v1 kind: ConfigMap metadata: name: httpd data: index.html: |- <!DOCTYPE html> <html> <head> <title>John Doe</title> </head> <body> <h1>John Doe</h1> <p> Today I learned how to deploy my own website in the cloud using an Apache HTTP Server (httpd) running on OpenShift. </p> </body> </html>
-
Press 'Create' .
Mount a ConfigMap
¶
- Navigate to 'Workloads' → 'Deployments' .
- Open the 'Deployment Details' by clicking on the 'Name' .
- Switch to the 'YAML' tab .
- Add the highlighted parts to the
Deployment
defintion file in the editor :apiVersion: apps/v1 kind: Deployment ... spec: ... template: metadata: creationTimestamp: null labels: app: httpd spec: volumes: - name: html configMap: name: <configmap name> containers: - image: ubi9/httpd-24:latest volumeMounts: - name: html mountPath: /var/www/html ...
- Press 'Save' .
- Reload your web site using the URL of the
Route
created in Exercise 3.
Important
At the beginning of the exercise make sure that you are in your project context.
oc project <project name>
Create a ConfigMap
¶
- Create a
index.html
file using thevi
editor with the following content:(replace<!DOCTYPE html> <html> <head> <title>John Doe</title> </head> <body> <h1>John Doe</h1> <p> Today I learned how to deploy my own website in the cloud using an Apache HTTP Server (httpd) running on OpenShift. </p> </body> </html>
John Doe
with your actual name - if you are John Doe you can skip this) -
Create a
ConfigMap
with theindex.html
.Try to figure out how to create a new configmap named
httpd
based on a file usingoc create configmap -h
Solution:
oc create configmap httpd --from-file=index.html
Inspect a ConfigMap
¶
- Inspect the created
ConfigMap
usingoc get
andoc describe
.oc get configmaps
oc describe configmap <configmap name>
- Also check the
ConfigMap
in the Web Console.
Mount a ConfigMap
¶
- Let's add the
ConfigMap
to thePod
template in theDeployment
. Theoc edit
command allows us to edit existing resources using thevi
editor.oc edit deployment <deployment name>
- Add the highlighted parts to the
Deployment
:apiVersion: apps/v1 kind: Deployment ... spec: ... template: metadata: creationTimestamp: null labels: app: httpd spec: volumes: - name: html configMap: name: <configmap name> containers: - image: ubi9/httpd-24:latest volumeMounts: - name: html mountPath: /var/www/html ...
- Store and close the file.
The configuration change triggers automatically a redeployment of all your
Pods
- wait a couple of seconds until it is done. - Reload your web site using the URL of the
Route
created in Exercise 3.
🎉 Congratulations, you have made it!¶
You have successfully deployed your first personal website in the cloud using OpenShift. 👏