1

当使用 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

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

任何建议都会非常有帮助

我正在使用随 docker for windows 提供的 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"}
4

2 回答 2

1

这里的问题是您正在使用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 本身的data键:app-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>
于 2018-10-29T18:26:38.610 回答
1
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/ 中的自定义动态创建位置安装文件,不确定您的意思是覆盖内容

截屏

于 2018-12-10T04:56:08.620 回答