简体   繁体   English

Kubernetes 卷装载 ConfigMap 到 pod 内的文件,而不覆盖所有其他文件

[英]Kubernetes Volume Mount ConfigMap to File within the pod without overwriting all other files

I have the following ConfigMap in my Kubernetes cluster that contains the web.config for my application there is a different one per environment so I would like to volume mount the ConfigMap to web.config in the pod.我的 Kubernetes 集群中有以下 ConfigMap,其中包含适用于我的应用程序的web.config ,每个环境都有一个不同的配置,因此我想在 89DZconfig 中将 ConfigMap 卷挂载到web.config

ConfigMap :配置图

kind: ConfigMap 
apiVersion: v1 
metadata:
  name: stars-website-data-config
  namespace: stars-website
data:
  config: |-
    <?xml version="1.0"?>
    <configuration>
      ...
    </configuration>

Deployment :部署

kind: Deployment
apiVersion: apps/v1
metadata:
  name: stars-website-data
  namespace: stars-website
spec:
  template:
    spec:
      containers:
        - name: stars-website-data
          ...
          volumeMounts:
            - name: config
              mountPath: C:\STARS.Website.Data
      volumes:
        - name: config
          configMap:
            name: stars-website-data-config
            items:
            - key: config
              path: web.config

This seems to do what I want it to do but it replaces all the other files and folder in that directory.这似乎做了我想做的事,但它替换了该目录中的所有其他文件和文件夹。

PS C:\STARS.Website.Data> ls 


    Directory: C:\STARS.Website.Data 


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----        5/14/2021  10:55 AM                ..2021_05_14_09_55_42.624339347
d----l        5/14/2021  10:55 AM                ..data
-a---l        5/14/2021  10:55 AM              0 web.config


PS C:\STARS.Website.Data> cat .\web.config
<?xml version="1.0"?>
<configuration>
        <configSections>

If I try to use the subPath value like:如果我尝试使用subPath值,例如:

kind: Deployment
apiVersion: apps/v1
metadata:
  name: stars-website-data
  namespace: stars-website
spec:
  template:
    spec:
      containers:
        - name: stars-website-data
          ...
          volumeMounts:
            - name: config
              mountPath: C:\STARS.Website.Data\web.config
              subPath: web.config
      volumes:
        - name: config
          configMap:
            name: stars-website-data-config
            items:
            - key: config
              path: web.config

I am getting the following error:我收到以下错误:

Error: Error response from daemon: invalid volume specification: 'c:\var\lib\kubelet\pods\ddfe6b07-4bff-42fe-ad3b-a02add00fbbf\volumes\kubernetes.io~configmap\config\web.config:C:\STARS.Website.Data\web.config:ro': invalid mount config for type "bind": source path must be a directory

When configMap is mounted as volume then it will delete all the files which were previously present in the mount path, this is expected behaviour.当 configMap 作为卷挂载时,它将删除以前存在于挂载路径中的所有文件,这是预期的行为。 Please refer caution section in this link .请参阅此链接中的注意部分。

With respective to your subpath configuration, there is a limitation in bind mounting files directly for windows based container, please refer this discussion .对于您的子路径配置,直接为基于 windows 的容器绑定安装文件存在限制,请参阅此讨论 The preferred approach is directory mounting for windows based container, you can create separate empty directory and you can mount your configmap to that directory which might be quick and straight forward solution.首选方法是基于 windows 的容器的目录挂载,您可以创建单独的空目录,您可以将 configmap 挂载到该目录,这可能是快速直接的解决方案。

This link has got workaround solution which might be useful for you.链接有可能对您有用的解决方法。

There is one major disadvantage of using either subpath configuration or workaround solution given in the above link is whenever you update your configmap it would not automatically update the project key.使用上述链接中给出的子路径配置或解决方案的一个主要缺点是,每当您更新 configmap 时,它都不会自动更新项目密钥。

Thanks,谢谢,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM