Kubernetes/Openshift: how to use envFile to read from a filesystem file

6/21/2018

I've configured two container into a pod.

The first of them is engaged to create a file like:

SPRING_DATASOURCE_USERNAME: username
SPRING_DATASOURCE_PASSWORD: password

I want that the second container reads from this location in order to initialize its env variables.

I'm using envFrom but I don't quite figure out how to use it.

This is my spec:

metadata:
  annotations:
    configmap.fabric8.io/update-on-change: ${project.artifactId}
  labels:
    name: wsec
  name: wsec
spec:
  replicas: 1
  selector:
    name: wsec
    version: ${project.version}
    provider: fabric8
  template:
    metadata:
      labels:
        name: wsec
        version: ${project.version}
        provider: fabric8
    spec:
      containers:
      - name: sidekick
        image: quay.io/ukhomeofficedigital/vault-sidekick:latest
        args:
        - -cn=secret:openshift/postgresql:env=USERNAME
        env:
        - name: VAULT_ADDR
          value: "https://vault.vault-sidekick.svc:8200"
        - name: VAULT_TOKEN
          value: "34f8e679-3fbd-77b4-5de9-68b99217cc02"
        volumeMounts:      
        - name: sidekick-backend-volume
          mountPath: /etc/secrets
          readOnly: false

      - name: wsec
        image: ${docker.image}
        env:
        - name: SPRING_APPLICATION_JSON
          valueFrom:
           configMapKeyRef:
             name: wsec-configmap
             key: SPRING_APPLICATION_JSON
          envFrom:
            ???
      volumes:
      - name: sidekick-backend-volume
        emptyDir: {}
-- Jordi
kubernetes
openshift

1 Answer

6/21/2018

It looks like you are packaging the environment variables with the first container and read then in the second container. If that's the case, you can use initContainers.

Create a volume mapped to an emptyDir. Mount that on the initContainer(propertiesContainer) and the main container(springBootAppContainer) as a volumeMount. This directory is now visible to both containers.

    image: properties/container/path
    command: [bash, -c]
    args: ["cp -r /location/in/properties/container /propsdir"]
    volumeMounts:
    - name: propsDir
      mountPath: /propsdir

This will put the properties in /propsdir. When the main container starts, it can read properties from /propsdir

-- Anand
Source: StackOverflow