Copy files into kubernetes pod with deployment.yaml

4/11/2020

I have containerized microservice built with Java. This application uses the default /config-volume directory when it searches for property files. Previously I manually deployed with Dockerfiles, and now I'm looking to automate this process with Kubernetes. The container image starts the microservice immediately so I need to get add properties to the config-volume folder immediately. I accomplished this in Docker with this simple Dockerfile

FROM ########.amazon.ecr.url.us-north-1.amazonaws.com/company/image-name:1.0.0

RUN mkdir /config-volume
COPY path/to/my.properties /config-volume

I'm trying to copy this type of behavior in a kubernetes deployment.yaml but I have found no way to do it.

I've tried performing a kubectl cp command immediately after applying the deployment and it sometimes works but it can results in a race condition which cause the microservice to fail at startup.

(I've redacted unnecessary parts)

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-service
spec:
  replicas: 1
  template:
    spec:
      containers:
      - env:
        image: ########.amazon.ecr.url.us-north-1.amazonaws.com/company/image-name:1.0.0
        name: my-service
        ports:
        - containerPort: 8080
        volumeMounts:
        - mountPath: /config-volume
          name: config-volume
      volumes:
        - name: config-volume
          emptyDir: {}
status: {}

Is there a way to copy files into a volume inside the deployment.yaml?

-- Joe Caruso
docker
kubernetes

1 Answer

4/11/2020

You are trying to emulate a ConfigMap using volumes. Instead, put your configuration into a ConfigMap, and mount that to your deployments. The documentation is there:

https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/

Once you have your configuration as a ConfigMap, mount it using something like this:

...
  containers:
    - name: mycontainer
      volumeMounts:
      - name: config-volume
        mountPath: /config-volume
  volumes:
    - name: config-volume
      configMap:
        name: nameOfConfigMap
-- Burak Serdar
Source: StackOverflow