如何将jar文件安装到tomcat容器

时间:2019-05-28 08:31:21

标签: kubernetes

我的项目中有一个文件夹,其中包含1个属性文件和1个jar文件(db-driver)文件。

我需要将这两个文件都复制到pod上的/ usr / local / tomcat / lib目录中。我不确定如何在kubernetes yaml文件中实现此目标。以下是我的yaml文件,我正在其中尝试使用configMap来实现此目的,但是pod创建失败,并显示错误“ configmap引用了不存在的配置密钥:app.properties”

目标/usr/local/tomcat/lib已经具有其他jar文件,因此我试图使用configMap而不覆盖整个目录,而仅添加2个特定于我的应用程序的文件。

apiVersion: apps/v1
kind: Deployment
metadata:
  name: tomcatdeployment
  labels:
    app:  tomcat
spec:
  replicas: 1
  selector:
    matchLabels:
      app:  tomcat
  template:
    metadata:
      labels:
        app:  tomcat
    spec:
      containers:
        - name: tomcat
          image: tomcat:latest
          imagePullPolicy: IfNotPresent
          volumeMounts:
          - name: appvolume
            mountPath: /usr/local/data
          - name: config
            mountPath: /usr/local/tomcat/lib
            subPath: ./configuration
          ports:
            - name: http
              containerPort: 8080
              protocol: TCP
      volumes:
        - name: appvolume
        - name: config
          configMap:
            name: config-map
            items:
              - key: app.properties
                path: app.properties
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: config-map
data:
  key:  app.properties

当前目录结构...

.
├── configuration
│   ├── app.properties
│   └── mysql-connector-java-5.1.21.jar
├── deployment.yaml
└── service.yaml

请分享您有关如何实现此目标的宝贵反馈意见。

致谢。

2 个答案:

答案 0 :(得分:0)

出现此错误是正常的,因为在此卷声明中您提到了键:app.properties,否则在configmap键中提到了:app.properties,所以这里的键是key,值是app.properties,因此您必须在卷中声明更改:

volumes:
        - name: appvolume
        - name: config
          configMap:
            name: config-map
            items:
              - key: app.properties
                path: app.properties

至:

   volumes:
            - name: appvolume
            - name: config
              configMap:
                name: config-map
                items:
                  - key: key
                    path: app.properties

有关更多信息,请参见:add-configmap-data-to-a-volume

答案 1 :(得分:0)

请尝试以下操作:

kubectl create configmap config-map --from-file = app.properties --from-file = mysql-connector-java-5.1.21.jar

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: tomcatdeployment
      labels:
        app:  tomcat
    spec:
      replicas: 1
      selector:
        matchLabels:
          app:  tomcat
      template:
        metadata:
          labels:
            app:  tomcat
        spec:
          containers:
            - name: tomcat
              image: tomcat:latest
              imagePullPolicy: IfNotPresent
              volumeMounts:
              - name: config
                mountPath: /usr/local/tomcat/lib/conf
              ports:
                - name: http
                  containerPort: 8080
                  protocol: TCP
          volumes:
            - name: config
              configMap:
                name: config-map

apiVersion: apps/v1
kind: Deployment
metadata:
  name: tomcatdeployment
  labels:
    app:  tomcat
spec:
  replicas: 1
  selector:
    matchLabels:
      app:  tomcat
  template:
    metadata:
      labels:
        app:  tomcat
    spec:
      containers:
        - name: tomcat3
          image: tomcat:latest
          imagePullPolicy: IfNotPresent
          volumeMounts:
          - name: config
            mountPath: /usr/local/tomcat/lib/app.properties
            subPath: app.properties
          - name: config
            mountPath: /usr/local/tomcat/lib/mysql-connector-java-5.1.21.jar
            subPath: mysql-connector-java-5.1.21.jar
          ports:
            - name: http
              containerPort: 8080
              protocol: TCP
      volumes:
        - name: config
          configMap:
            name: config-map
            items:
            - key: app.properties
              path: app.properties 
            - key: mysql-connector-java-5.1.21.jar
              path: mysql-connector-java-5.1.21.jar