Save file to Kubernetes pod during deployment

9/17/2018

I am trying to add a file to a pod's disk during initialization of the pod but without luck. Below is my deployment file which I use to deploy the pod. The file gets downloaded to the persistent volume, but the pod doesn't get into ready state. After a few seconds, the pods fail and get rebuilt. Which kicks off the whole process again.

Any help would be appreciated.

apiVersion: apps/v1beta2
kind: Deployment
metadata:
  name: mapserver
spec:
  selector:
    matchLabels:
      app: mapserver
  template:
    metadata:
      labels:
        app: mapserver
    spec:
      volumes:
      - name: storage
        persistentVolumeClaim:
          claimName: mapserver-pv-claim
      containers:
      - name: maptiles
        image: klokantech/tileserver-gl
        command: ["/bin/sh"]
        args:
        - -c
        - |
           echo "[INFO] Startingcontainer"; if [ $(DOWNLOAD_MBTILES) = "true" ]; then
             echo "[INFO] Download MBTILES_PLANET_URL";
             rm /data/*
             cd /data/
             curl -k -sSL -X GET -u user:ww $(MBTILES_PLANET_URL) -O
             echo "[INFO] Download finished";
           fi;
         env:
         - name: MBTILES_PLANET_URL
           value: 'https://abc-dev/nexus/repository/xyz-raw/2017-07-03_europe_netherlands.mbtiles'
         - name: DOWNLOAD_MBTILES
           value: 'true'
        livenessProbe:
          failureThreshold: 120
          httpGet:
            path: /health
            port: 80
            scheme: HTTP
          initialDelaySeconds: 10
          periodSeconds: 30
          successThreshold: 1
          timeoutSeconds: 5
        ports:
        - containerPort: 80
          name: http
          protocol: TCP
        readinessProbe:
          failureThreshold: 120
          httpGet:
            path: /health
            port: 80
            scheme: HTTP
          initialDelaySeconds: 10
          periodSeconds: 30
          successThreshold: 1
          timeoutSeconds: 5
        resources:
          limits:
            cpu: 300m
            memory: 3Gi
          requests:
            cpu: 100m
            memory: 1Gi
        volumeMounts:
        - mountPath: "/data"
          name: storage
-- Martijn
deployment
kubernetes
pod

1 Answer

9/17/2018

am trying to add a file to a pod's disk during initialization of the pod but without luck.

In that case you might want to use InitContainers instead.

Judging from your manifest, your main command gets executed (copies the file and then exits) terminating the container (and accompanying pod) in the process. Deployment then restarts the exited pod and cycle repeats. If you use InitContainers instead (with the same definition and same PV as you are doing now for main container) you should then prepopulate data using InitContaienrs that runs to completion and then continue to use it in your normal container (that should have non-exiting main process as its command/entry point).

Note: if you don't want to use InitContainers or just as a quick test, you could append a regular non-exiting command after your copy statement, and also, check if you need to start container with tty, depending on your use case and ways to keep container up and running.

-- Const
Source: StackOverflow