kubernetes - Configmap - 删除现有文件

kubernetes - Configmap - Existing files are removed

当使用 ConfigMap 作为挂载卷时,pod 挂载位置内的所有现有文件和文件夹都将被删除。

ConfigMap 是使用以下命令创建的:

$ kubectl create configmap tomcat-configmap --from-file=./config/tomcat-config.xml

部署 yaml 文件:

kind: Service
apiVersion: v1
metadata:
  name: tomcat-svc
spec:
  selector:
    app: tomcat-container
  ports:
  - protocol: TCP
    port: 83
    targetPort: 8080
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: tomcat-deployment
spec:
  selector:
    matchLabels:
      app: tomcat-container
  replicas: 1
  template:
    metadata:
      labels:
        app: tomcat-container
    spec:
      containers:
      - name: tomcat
        image: bitnami/tomcat:latest
        ports:
        - containerPort: 8080
        volumeMounts:
        - name: tomcat-configmap-volume
          mountPath: /usr/local/tomcat/webapps/examples/WEB-INF/classes/com/test/app-conf.xml
          subPath: app-conf.xml
      volumes:
        - name: tomcat-configmap-volume
          configMap:
            name: tomcat-configmap

我尝试了不同的配置但没有成功:(

任何建议都会很有帮助

我正在为 windows 使用 docker 提供的 kubernetes :

Client Version: version.Info{Major:"1", Minor:"10", GitVersion:"v1.10.3", GitCommit:"2bba0127d85d5a46ab4b778548be28623b32d0b0", GitTreeState:"clean", BuildDate:"2018-05-21T09:17:39Z", GoVersion:"go1.9.3", Compiler:"gc", Platform:"windows/amd64"}

Server Version: version.Info{Major:"1", Minor:"10", GitVersion:"v1.10.3", GitCommit:"2bba0127d85d5a46ab4b778548be28623b32d0b0", GitTreeState:"clean", BuildDate:"2018-05-21T09:05:37Z", GoVersion:"go1.9.3", Compiler:"gc", Platform:"linux/amd64"}

这里的问题是您使用的是 subPath,它本来是一个目录,您认为 app-conf.xml 应该创建为一个文件。

你真正想要的是:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: tomcat-deployment
spec:
  selector:
    matchLabels:
      app: tomcat-container
  replicas: 1
  template:
    metadata:
      labels:
        app: tomcat-container
    spec:
      containers:
      - name: tomcat
        image: bitnami/tomcat:latest
        ports:
        - containerPort: 8080
        volumeMounts:
        - name: tomcat-configmap-volume
          mountPath: /usr/local/tomcat/webapps/examples/WEB-INF/classes/com/test
      volumes:
        - name: tomcat-configmap-volume
          configMap:
            name: tomcat-configmap

并将您的文件命名为 app-conf.xml,这样当您创建 ConfigMap 时,它会在您的 ConfigMap 本身中分配 dataapp-conf.xml

$ kubectl create configmap tomcat-configmap --from-file=./app-conf.xml

那么您的 ConfigMap 将如下所示:

$ kubectl describe cm tomcat-configmap
Name:         tomcat-configmap
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
app-conf.xml:
----
<xml>
...
...
</xml>

Events:  <none>
kind: Service
apiVersion: v1
metadata:
  name: tomcat-svc
spec:
  selector:
  app: tomcat-container
  ports:
  - protocol: TCP
    port: 8080
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: tomcat-deployment
spec:
  selector:
    matchLabels:
      app: tomcat-container
  replicas: 1
  template:
    metadata:
      labels:
        app: tomcat-container
    spec:
  containers:
  - name: tomcat
    image: bitnami/tomcat:latest
    ports:
    - containerPort: 8080
    volumeMounts:
    - name: tomcat-configmap-volume
      mountPath: /usr/local/tomcat/webapps/examples/WEB-INF/classes/com/test
      #subPath: app-conf.xml
  volumes:
    - name: tomcat-configmap-volume
      configMap:
        name: tomcat-configmap

Rico 上面提供的答案应该有效,无论如何我在下面提供实施结果:

注释掉子路径,当你想覆盖现有的配置文件时,子路径非常有用。此外,您的 tomcat 路径是您正在使用的图像中的 /opt/bitnami/tomcat ,并且您正在将文件挂载到 /usr/local/tomcat/ 中自定义动态创建的位置,不确定您的意思正在覆盖内容

你做对了,你只是错过了音量中的关键参数。 当你试图用另一个文件替换一个文件时,我的意思是你正在使用 subPath 只替换一个文件,在你的情况下文件是“app-conf。xml”但是你'没有指定 configMap 中的哪个键将替换此文件。 每个 configMap 键都被视为一个独立的配置文件

kind: Service
apiVersion: v1
metadata:
  name: tomcat-svc
spec:
  selector:
    app: tomcat-container
  ports:
  - protocol: TCP
    port: 83
    targetPort: 8080
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: tomcat-deployment
spec:
  selector:
    matchLabels:
      app: tomcat-container
  replicas: 1
  template:
    metadata:
      labels:
        app: tomcat-container
    spec:
      containers:
      - name: tomcat
        image: bitnami/tomcat:latest
        ports:
        - containerPort: 8080
        volumeMounts:
        - name: tomcat-configmap-volume
          mountPath: /usr/local/tomcat/webapps/examples/WEB-INF/classes/com/test/app-conf.xml
          subPath: app-conf.xml
      volumes:
        - name: tomcat-configmap-volume
          configMap:
            name: tomcat-configmap
            key: <enter the key here>