如何在Pod内放置配置文件?

时间:2019-01-09 16:34:27

标签: kubernetes openshift

例如,我想在其中放置一个应用程序配置文件:

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

我从文件创建一个ConfigMap,然后将其放置在如下卷中:

/opt/persistentData/

这个想法是在以后运行一个类似以下内容的脚本:

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

但是可能是任何需要执行操作的startup.sh脚本。

如何运行此命令/脚本? (在Tomcat启动之前Pod初始化期间)。

2 个答案:

答案 0 :(得分:1)

如何在实际需要的位置安装ConfigMap而不是进行复制?

更新:

提到的@ccshih初始化容器应该可以,但是也可以尝试其他选择:

  1. 使用Docker配方构建一个自定义映像,以替代基本映像。下面的示例获取一个java + tomcat7 openshift图像,向应用程序类路径添加了一个额外的文件夹,因此您可以将ConfigMap挂载到/ mnt / config而不覆盖任何内容,从而使两个文件夹均可用。

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. 通过修改映像或通过DeploymentConfig挂钩来更改应用程序的ENTRYPOINT,请参阅:https://docs.okd.io/latest/dev_guide/deployments/deployment_strategies.html#pod-based-lifecycle-hook 使用钩子后,只需记住在完成所有自定义内容后调用原始入口点或启动脚本即可。

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

答案 1 :(得分:1)

如果可以,我会首先尝试。

  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

如果没有,请添加init container来复制文件。

spec:
  initContainers:
  - name: copy-config
    image: busybox
    command: ['sh', '-c', '/bin/cp /opt/persistentData/my_config_file.xml  /opt/webserver/my_application/config/']
相关问题