I need to copy a file inside my pod during the time of creation. I don’t want to use ConfigMap
and Secrets
. I am trying to create a volumeMounts
and copy the source file using the kubectl cp
command—my manifest looks like this.
apiVersion: v1
kind: Pod
metadata:
name: copy
labels:
app: hello
spec:
containers:
- name: init-myservice
image: bitnami/kubectl
command: ['kubectl','cp','./test.json','init-myservice:./data']
volumeMounts:
- name: my-storage
mountPath: data
- name: init-myservices
image: nginx
volumeMounts:
- name: my-storage
mountPath: data
volumes:
- name: my-storage
emptyDir: {}
But I am getting a CrashLoopBackOff
error. Any help or suggestion is highly appreciated.
2
Answers
it’s not possible.
let me explain : you need to think of it like two different
machine
. here your local machine is the one where the file exist and you want to copy it in another machine withcp
. but it’s not possible. and this is what you are trying to do here. you are trying to copy file from your machine topod
‘s machine.here you can do one thing just create your own docker image for
init-container
. and copy the file you want to store before building the docker image. then you can copy that file inshared volume
where you want to store the file.I do agree with an answer provided by H.R. Emon, it explains why you can’t just run
kubectl cp
inside of the container. I do also think there are some resources that could be added to show you how you can tackle this particular setup.For this particular use case it is recommended to use an
initContainer
.You could use the example from the official Kubernetes documentation (assuming that downloading your
test.json
is feasible):You can also modify above example to your specific needs.
Also, referring to your particular example, there are some things that you will need to be aware of:
kubectl
inside of aPod
you will need to have required permissions to access the Kubernetes API. You can do it by usingserviceAccount
with some permissions. More can be found in this links:bitnami/kubectl
container will run intoCrashLoopBackOff
errors because of the fact that you’re passing a single command that will run to completion. After thatPod
would report statusCompleted
and it would be restarted due to this fact resulting in before mentionedCrashLoopBackOff
. To avoid that you would need to useinitContainer
.Additional resources: