如何在不清除文件夹的情况下挂载具有ConfigMap的键/值的内容的文件?

如何解决如何在不清除文件夹的情况下挂载具有ConfigMap的键/值的内容的文件?

我创建了一个Deploymentconfig.yml来部署应用程序,并希望挂载内容存储在ConfigMap中的文件。 装入时,将替换装入的文件夹中的文件。 不过,目标是仅添加/删除/覆盖该文件夹中的特定文件。

spec:
    containers:
          volumeMounts:
          - mountPath: /path/toaSubPath/
            name: somename

在这个deploymentconfig中有可能吗? 如果是这样,我该怎么办?

解决方法

是的,我正在使用它来安装默认配置。只需使用subPath和subPath中的文件名即可。在下面的示例中找到它的魅力

spec:
  selector:
     matchLabels:
       name: some_name
  template:
    spec:
      containers:
      - args:
        - bash
        - entrypoint.sh
        image: xyz
        imagePullPolicy: IfNotPresent
        name: some_name
        volumeMounts:
        - mountPath: /full/path/to/be/mounted/default.json
          name: config
          subPath: default.json
      volumes:
      - configMap:
          defaultMode: 420
          name: config
,

您可以使用“ subPath”将其挂载为在ConfigMap中配置的文件,如下所示。 该演示演示了如何仅挂载配置为ConfigMap的test.txt文件为“ /etc/test.txt”。

// Create test pod and deploymentconfig.
$ oc run test --image registry.redhat.io/rhel7 -- tail -f /dev/null
deploymentconfig.apps.openshift.io/test created

// Create test.txt
$ cat <<EOF > test.txt
Test file
EOF

// Create testmap ConfigMap using above test.txt file.
$ oc create configmap testmap --from-file=test.txt

// Modify volumes and volumeMounts for mounting only test.txt file to "/etc/test.txt".
$ oc edit dc/test
:
containers:
- name: test
  :
  volumeMounts:
  - mountPath: /etc/test.txt
    name: testtxt
    subPath: test.txt
:
terminationGracePeriodSeconds: 30
volumes:
- configMap:
    defaultMode: 420
    items:
    - key: test.txt
      path: test.txt
    name: testmap
  name: testtxt
:

// You can verify test.txt after redeploying test pod after modification.
$ oc rsh dc/test cat /etc/test.txt
Test file

// you can also verify test.txt file is only mounted to /etc directory as one specified file by subPath,not all directory.
$ oc rsh dc/test ls -l /etc/
total 888
:
drwxr-xr-x.  4 root root          151 Aug  3 09:13 systemd
drwxr-xr-x.  2 root root            6 Aug 15  2017 terminfo
-rw-r--r--.  1 root 1000110000     10 Aug 14 16:02 test.txt
:
drwxr-xr-x.  1 root root            6 Aug  3 09:37 yum.repos.d
,

在应用所需的配置之前,这是默认的nginx配置 / usr / share / nginx / html

root@ng:/usr/share/nginx/html# ls -la
total 16
drwxr-xr-x 2 root root 4096 Aug 14 00:36 .
drwxr-xr-x 3 root root 4096 Aug 14 00:36 ..
-rw-r--r-- 1 root root  494 Aug 11 14:50 50x.html
-rw-r--r-- 1 root root  612 Aug 11 14:50 index.html

示例自定义配置

wget https://kubernetes.io/examples/configmap/game.properties 
wget https://kubernetes.io/examples/configmap/ui.properties

kubectl create configmap game --from-file=game.properties --from-file=ui.properties

apiVersion: v1
kind: Pod
metadata:
  name: my
spec:
    containers:
    - name: nginx
      image: nginx
      volumeMounts:
      - mountPath: /usr/share/nginx/html/index.html
        name: data
        subPath: game                    # make a reference to existing CM Key
      - mountPath: /usr/share/nginx/html/ui.properties.txt
        name: data
        subPath: ui.properties.txt        # make a reference to existing CM Key
    volumes:
    - name: data
      configMap:
        name: game
        items:
        - key: game.properties
          path: game
        - key: ui.properties
          path: ui.properties.txt

pod部署后 kubectl apply -f <your_pod_yaml>

root@my:/usr/share/nginx/html# ls -la
total 24
drwxr-xr-x 1 root root 4096 Aug 17 12:26 .
drwxr-xr-x 1 root root 4096 Aug 14 00:36 ..
-rw-r--r-- 1 root root  494 Aug 11 14:50 50x.html
-rw-r--r-- 1 root root  157 Aug 17 12:26 index.html
-rw-r--r-- 1 root root   83 Aug 17 12:26 ui.properties.txt

验证index.html

root@my:/usr/share/nginx/html# curl localhost

enemies=aliens
lives=3
enemies.cheat=true
enemies.cheat.level=noGoodRotten
secret.code.passphrase=UUDDLRLRBABAS
secret.code.allowed=true
secret.code.lives=30

验证ui.properties.txt

root@my:/usr/share/nginx/html# cat ui.properties.txt 
color.good=purple
color.bad=yellow
allow.textmode=true
how.nice.to.look=fairlyNice

从ConfigMap中获取不同的文件时,请确保您引用的是正确的密钥

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-