How to mount jar file to tomcat container

5/28/2019

I have a folder in my project which contains 1 properties file and 1 jar file(db-driver) file.

I need to copy both of these files to /usr/local/tomcat/lib directory on my pod. I am not sure how to achieve this in kubernetes yaml file. Below is my yaml file where i am trying to achieve this using configMap but pod creation fail with error "configmap references non-existent config key: app.properties"

Target /usr/local/tomcat/lib already has other jar files so i am trying to use configMap to not override entire directory and just add 2 files which are specific to my application.

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

Current Directory structure...

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

Please share your valuable feedback on how to achieve this.

Regards.

-- user3552454
kubernetes

2 Answers

5/28/2019

it's normal to have this error because in this volume declaration you mentioned that key: app.properties otherwise in the configmap key: app.properties so here the key is key and the value is app.properties so you must in the volume declaration change :

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

to :

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

for more you can refer here : add-configmap-data-to-a-volume

-- Semah Mhamdi
Source: StackOverflow

5/30/2019

Please try this:

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

or

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
-- Hanx
Source: StackOverflow