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.
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 with cp
. but it's not possible. and this is what you are trying to do here. you are trying to copy file from your machine to pod
'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 in shared 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
.
initContainers
- specialized containers that run before app containers in a Pod. Init containers can contain utilities or setup scripts not present in an app image.Kubernetes.io: Docs: Concepts: Workloads: Pods: Init-containers
You could use the example from the official Kubernetes documentation (assuming that downloading your test.json
is feasible):
apiVersion: v1 kind: Pod metadata: name: init-demo spec: containers: - name: nginx image: nginx ports: - containerPort: 80 volumeMounts: - name: workdir mountPath: /usr/share/nginx/html # These containers are run during pod initialization initContainers: - name: install image: busybox command: - wget - "-O" - "/work-dir/index.html" - http://info.cern.ch volumeMounts: - name: workdir mountPath: "/work-dir" dnsPolicy: Default volumes: - name: workdir emptyDir: {}
-- Kubernetes.io: Docs: Tasks: Configure Pod Initalization: Create a pod that has an initContainer
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 a Pod
you will need to have required permissions to access the Kubernetes API. You can do it by using serviceAccount
with some permissions. More can be found in this links: bitnami/kubectl
container will run into CrashLoopBackOff
errors because of the fact that you're passing a single command that will run to completion. After that Pod
would report status Completed
and it would be restarted due to this fact resulting in before mentioned CrashLoopBackOff
. To avoid that you would need to use initContainer
. Additional resources:
A side note!
I also do consider including the reason why
Secrets
andConfigMaps
cannot be used to be important in this particular setup.