Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

k8s: copy file to container after pod's deployment

I'm using a Helm chart to deploy an app in the Kubernetes. After deployment, I want to copy a file from the chart repository to the container.

Currently I am doing this manually:

kubectl cp custom-samples.json che-8467596d54-7c2hg:/data/templates

But I want to make this step a part of the deployment that will be performed automatically. Note that I took a look at post-install hooks but I'm not sure it's a good solution.

[UPD] I created this init container:

 - name: add-custom-samples
        image: alpine:3.5
        command: ["sh", "-c", "cd /data/templates; touch custom.json;"]
        volumeMounts: [{
              "mountPath": "/data",
              "name": "che-data-volume"
        }]

But the file custom.json is missing in the mounted volume.

like image 700
kostiamol Avatar asked Jan 28 '23 10:01

kostiamol


1 Answers

You can include your file in the Helm chart. You'd generally include that in a Kubernetes ConfigMap object, which can then be mounted in a Pod as a volume.

You need to move the file to somewhere in the Helm chart directory; say it's in charts/mychart/files/custom-samples.json. You can create a ConfigMap in, say, charts/mychart/templates/configmap.yaml that would look like

apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ .Release.Name }}-configmap
data:
  custom-samples.json" |-
{{ .Files.Get "custom-samples.json" | indent 4 }}

Then in your Deployment's Pod spec, you'd reference this:

apiVersion: v1
kind: Deployment
spec:
  template:
    spec:
      volumes:
        - name: config
          configMap:
            name: {{ .Release.Name }}-configmap
      containers:
        - name: ...
          volumeMounts:
            - name: config
              mountPath: /data/templates

Note that this approach causes the file to be stored as a Kubernetes object, and there are somewhat modest size limits; something that looks like a text file and is sized in kilobytes should be fine. Also, if there are other files in the /data/templates directory, this approach will cause them to be hidden in favor of whatever's in the ConfigMap.

like image 200
David Maze Avatar answered Jan 31 '23 08:01

David Maze