Deploying Spring Boot with A Dynamic application.properties Location to Openshift
I want to create a simple spring boot app, and deploy it to Openshift 4.2. It supposed to be a straigh forward task, but the problem is that it is required to externalize all configuration to a configmaps or secret so no need to recompile the whole app in case of configuration change.
There are several approach of externalizing configuraton to configmaps, one way is put it as a string literal, include on your pod and call on application via environment variables, or deploy the whole configuration file and mount it on your Openshift pod. The last approach is the one that we will be doing now today.
First lets start with deploying our properties to Openshift as configmaps,
oc create cm myapp-api-configmap --from-file=D:\source\my-app\src\main\resources\application.properties
We can check and validate the result,
oc get cm oc describe cm myapp-api-configmap
After that, we can mount corresponding configmap to a specific folder on our Pod, on below example modification is done on DeploymentConfig.yaml and mounting application.properties to /deployments/config folder.
kind: DeploymentConfig apiVersion: apps.openshift.io/v1 metadata: ........ spec: volumes: - name: myapp-api-configmap-volume configMap: name: myapp-api-configmap defaultMode: 420 containers: - name: myapp-api image: >- image-registry.openshift-image-registry.svc:5000/openshift/myapp@sha256:1127..... ports: - containerPort: 8778 protocol: TCP - containerPort: 8080 protocol: TCP - containerPort: 8443 protocol: TCP resources: {} volumeMounts: - name: myapp-api-configmap-volume mountPath: /deployments/config terminationMessagePath: /dev/termination-log terminationMessagePolicy: File imagePullPolicy: Always restartPolicy: Always terminationGracePeriodSeconds: 30 dnsPolicy: ClusterFirst securityContext: {} schedulerName: default-scheduler
A modification is also needed on my Dockerfile, pointing a new path for my properties file by using “spring.config.location” parameter,
FROM registry.access.redhat.com/openjdk/openjdk-11-rhel7 USER jboss RUN mkdir -p /deployments/image && chown -R jboss:0 /deployments EXPOSE 8080 COPY target/application-1.0.jar /deployments/application.jar CMD ["java", "-jar", "/deployments/application.jar", "--spring.config.location=file:///deployments/config/application.properties"]
Build, deploy,and see that application is now taking configuration from external configuration file.
No Comments