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.
You could use ksync or OpenShift, which has the oc rsync
command built in available.
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.
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.