7

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.

1
  • 2
    Copying files into the pod from your local machine isn't a scalable solution. Can the file be published somewhere which is accessible to the pod, such as an object storage service in your preferred cloud provider? In that instance, you would use an init container to copy the file to the relevant location in the pod. Commented Sep 14, 2018 at 11:38

3 Answers 3

6

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.

Sign up to request clarification or add additional context in comments.

3 Comments

That's a problem because I need to add a file, not to substitute whatever is there.
you could use this then have a command that copies or sym-links it to the correct place in the container start?
@Kevlar since it’s routine (for me at least) to cycle Deployments with new container versions, and in any case there are multiple pod replicas with unpredictable names, “and run this manual command after the pod comes up” isn’t really a sustainable answer.
4

kubectl cp (in the form you're using it) takes a file from the host on which it runs. If you are always initiating deployment from that same host, you could (in theory) arrange for the file to be copied - either by having kubectl cp ... in a script that you use to control the deployment or by setting up a watch and performing the copy every time a new container appears in the deployment that needs it.

However, it is probably better to have the pods get that file by themselves (e.g., over http or from github) as part of their startup. If you have control of the container's startup (e.g., you own its code or you can specify the command that it runs), you can do that easily. You could also define an init container that runs before the container that does the real work and have it pull the file into a volume that's shared with the main container.

Comments

0

You could use ksync or OpenShift, which has the oc rsync command built in available.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.