Home > Enterprise >  How to remotely create a file in a Kubernetes container?
How to remotely create a file in a Kubernetes container?

Time:12-12

I have a pod named nginx which has two containers named nginx and busybox. To create a file inside the busybox container, I have to do:

$ kubectl exec -it nginx -c busybox -- bin/sh
# echo sometext > /temp/testfile
# exit

I want to simplify it into a one-liner, so I try:

$ kubectl exec -it nginx -c busybox -- echo sometext > /temp/testfile

However, it gives me this error:

bash: /temp/testfile: No such file or directory

Putting echo sometext > /temp/testfile between double quotes yields this error:

OCI runtime exec failed: exec failed: container_linux.go:380: starting container process caused: exec: "echo sometext > /temp/testfile": stat echo sometext > /temp/testfile: no such file or directory: unknown

Why is it not working? Is there any way to make such one-liner?

CodePudding user response:

Try this

kubectl exec -it nginx -c busybox -- sh -c "echo sometext > /temp/testfile"
  • Related