The data is not being shared across containers

12/28/2020

I am trying to create two containers within a pod with one container being an init container. The job of the init container is to download a jar and make it available for the app container. I am able to create everything and the logs look good but when i check, i do not see the jar in my app container. Below is my deployment yaml:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: web-service-test
  labels:
    app: web-service-test
spec:
  replicas: 1
  selector:
    matchLabels:
      app: web-service-test
  template:
    metadata:
      labels:
        app: web-service-test
    spec:
      volumes:
      - name: shared-data
        emptyDir: {}
      containers:
      - name: web-service-test
        image: some image
        ports:
        - containerPort: 8081
        volumeMounts:
        - name: shared-data
          mountPath: /tmp/jar
      initContainers:
      - name: init-container
        image: busybox
        volumeMounts:
        - name: shared-data
          mountPath: /jdbc-jar
        command:
        - wget
        - "https://repo1.maven.org/maven2/com/oracle/ojdbc/ojdbc8/19.3.0.0/ojdbc8-19.3.0.0.jar"
-- daze-hash
kubernetes

2 Answers

12/29/2020

Add following block of code to your init container section:

command: ["/bin/sh","-c"]
args: ["wget -O /jdbc-jar/ojdbc8-19.3.0.0.jar https://repo1.maven.org/maven2/com/oracle/ojdbc/ojdbc8/19.3.0.0/ojdbc8-19.3.0.0.jar"]

The command ["/bin/sh", "-c"] says "run a shell, and execute the following instructions". The args are then passed as commands to the shell. In shell scripting a semicolon separates commands. In the wget command I have added the -O flag to download the jar from the specified url and save it as /jdbc-jar/ojdbc8-19.3.0.0.jar . To check if jar is persistent in container. Simply execute command:

$ kubectl exec -it web-service-test -- /bin/bash

Then go to folder /jdbc-jar ( $ cd jdbc-jar ) and list files in it ($ ls -al). You should see your jar there.

See examples: commands-in-containers, initcontainers-running.

-- Malgorzata
Source: StackOverflow

12/29/2020

You need to save jar in the /jdbc-jar folder

try updating your yaml to following

command: ["/bin/sh"]
args: ["-c", "wget -O /pod-data/ojdbc8-19.3.0.0.jar https://repo1.maven.org/maven2/com/oracle/ojdbc/ojdbc8/19.3.0.0/ojdbc8-19.3.0.0.jar"] 
-- Abhijit Gaikwad
Source: StackOverflow