How to place configuration files inside pods?

1/9/2019

For example I want to place an application configuration file inside:

/opt/webserver/my_application/config/my_config_file.xml

I create a ConfigMap from file and then place it in a volume like:

/opt/persistentData/

The idea is to run afterwards an script that does something like:

cp /opt/persistentData/my_config_file.xml  /opt/webserver/my_application/config/

But it could be any startup.sh script that does needed actions.

How do I run this command/script? (during Pod initialization before Tomcat startup).

-- Alexander Meise
kubernetes
openshift

2 Answers

1/11/2019

How about mounting the ConfigMap where you actually want it instead of copying over?

update:

The init container @ccshih mentioned should do, but one can try other options too:

  1. Build a custom image modyfying the base one, using a Docker recipe. The example below takes a java+tomcat7 openshift image, adds an additional folder to the app classpath, so you can mount your ConfigMap to /mnt/config without overwriting anything, keeping both folders available.

.

FROM openshift/webserver31-tomcat7-openshift:1.2-6
# add classpaths to config
RUN sed -i 's/shared.loader=/shared.loader=\/mnt\/config/' 
/opt/webserver/conf/catalina.properties
  1. Change the ENTRYPOINT of the application, either by modifying the image, or by the DeploymentConfig hooks, see: https://docs.okd.io/latest/dev_guide/deployments/deployment_strategies.html#pod-based-lifecycle-hook With the hooks one just needs to remember to call the original entrypoint or launch script after all the custom stuff is done.

.

spec:
  containers:
    -
    name: my-app
    image: 'image'
    command:
      - /bin/sh
    args:
      - '-c'
      - cp /wherever/you/have/your-config.xml /wherever/you/want/it/ && /opt/webserver/bin/launch.sh
-- ptrk
Source: StackOverflow

1/14/2019

I would first try if this works.

  spec:
    containers:
    - volumeMounts:
      - mountPath: /opt/webserver/my_application/config/my_config_file.xml
        name: config
        subPath: my_config_file.xml
    volumes:
    - configMap:
        items:
        - key: KEY_OF_THE_CONFIG
          path: my_config_file.xml
        name: config
      name: YOUR_CONFIGMAP_NAME

If not, add an init container to copy the file.

spec:
  initContainers:
  - name: copy-config
    image: busybox
    command: ['sh', '-c', '/bin/cp /opt/persistentData/my_config_file.xml  /opt/webserver/my_application/config/']
-- ccshih
Source: StackOverflow