Kubectl exec command to write contents to a file in the pod - kubernetes

I am trying below command
kubectl exec -it sss-pod-four echo "hi" >> /mnt/sss/testnew.txt
But it throws error
-bash: /mnt/sss/testnew.txt: No such file or directory
What is the best way to achieve this

Found a similar question here and below command works now
kubectl exec -it sss-pod-four -- bash -c "echo hi > /mnt/sss/testnew.txt"

Related

K8S How to add quotes when on parameters provided for `kubectl exec -c`

I am using K8S
I want to calculate a string that that is a result of kubectl exec -it ... -c
after the -c option there is a string.
How can I pass a string with double quotes inside.
The following example doesn't work properly.
x="$(kubectl exec -it mysql-pod -- /bin/sh -c \"mysql -uroot -p12345
-e 'show databases'\" 2>/dev/null)"
echo $x
Thanks.
when only a command needs to be executed on a pod , -it option is not required as it stands for attaching an interactive teminal
when mysql is itself an executable command , no need to use /bin/sh -c
no need to encapsulate whole command in " "
So try following
x=$(kubectl exec mysql-pod -- mysql -uroot -p12345 -e 'show databases ;' 2>/dev/null)
echo $x

Why does kubectl cp command terminates with exit code 126?

I am trying to copy files from the pod to local using following command:
kubectl cp /namespace/pod_name:/path/in/pod /path/in/local
But the command terminates with exit code 126 and copy doesn't take place.
Similarly while trying from local to pod using following command:
kubectl cp /path/in/local /namespace/pod_name:/path/in/pod
It throws the following error:
OCI runtime exec failed: exec failed: container_linux.go:367: starting container process caused: exec: "tar": executable file not found in $PATH: unknown
Please help through this.
kubectl cp is actually a very small wrapper around kubectl exec whatever tar c | tar x. A side effect of this is that you need a working tar executable in the target container, which you do not appear to have.
In general kubectl cp is best avoided, it's usually only good for weird debugging stuff.
kubectl cp requires the tar to be present in your container, as the help says:
!!!Important Note!!!
Requires that the 'tar' binary is present in your container
image. If 'tar' is not present, 'kubectl cp' will fail.
Make sure your container contains the tar binary in its $PATH
An alternative way to copy a file from local filesystem into a container:
cat [local file path] | kubectl exec -i -n [namespace] [pod] -c [container] "--" sh -c "cat > [remote file path]"
Useful command to copy the file from pod to local
kubectl exec -n <namespace> <pod> -- cat <filename with path> > <filename>
For me the cat worked like this:
cat <file name> | kubectl exec -i <pod-id> -- sh -c "cat > <filename>"
Example:
cat file.json | kubectl exec -i server-77b7976cc7-x25s8 -- sh -c "cat > /tmp/file.json"
Didn't need to specify namespace since I run the command from a specific project, and since we have one container, didn't need to specify it

kubectl exec: Permission denied

Try to append some new entries to /etc/hosts in pods, but failed:
$ ips=$(cat ips.txt); kubectl exec -u root myspark-master-5d6656bd84-5zf2h echo "$ips" >> /etc/hosts
-sh: /etc/hosts: Permission denied
How to fix this?
Thanks
UPDATE
$ ips=$(cat ips.txt); kubectl exec myspark-worker-5976b685b4-8bcbl -- sh -c "echo $ips >> /etc/hosts"
sh: 2: 10.233.88.5: not found
sh: 3: 10.233.96.2: not found
sh: 4: 10.233.86.5: not found
10.233.88.4 myspark-master-5d6656bd84-dxhxc
command terminated with exit code 127
I think you mean to write to the file inside the container, but bash is parsing that on your workstation and try to apply the redirect locally. Use kubectl exec ... -- sh -c “...” instead.
There is indeed a parsing problem because $ips contain new lines.
Try with
$ ips=$(cat ips.txt); kubectl exec myspark-worker-5976b685b4-8bcbl -- sh -c "echo \"$ips\" >> /etc/hosts"

running kubectl create command from inside a shell script

I am trying to call kubectl create -n $test_namespace -f <absolute-path-to-tea-rc-file>.yaml from a shell script.
#!/bin/bash
current_dir=$(pwd)
echo "current_dir is $current_dir"
parentdir="$(dirname "$current_dir")"
echo $parentdir
kubectl create -n $test_namespace -f $parentdir/deployment/common/tea-rc.yaml
It gives error as below:
Bhagyashrees-MacBook-Pro:execution_controller bhagyashree$ sh test.sh
current_dir is /Users/bhagyashree/Documents/FDrepo/armada-ingress-ctl-test/execution_controller
/Users/bhagyashree/Documents/FDrepo/armada-ingress-ctl-test
error: unknown command "/Users/bhagyashree/Documents/FDrepo/armada-ingress-ctl-test/deployment/common/tea-rc.yaml"
See 'kubectl create -h' for help and examples.
the same command works when it is executed from a terminal.
kubectl create -n testnamespace -f /Users/bhagyashree/Documents/FDrepo/armada-ingress-ctl-test/deployment/common/tea-rc.yaml
What I am i missing here?
I think it's because the variable $test_namespace has not been set.
If you change the last line to echo "kubectl create -n $test_namespace -f $parentdir/deployment/common/tea-rc.yaml" you'll see what it's trying to run, it will look like kubctl create -n -f /path/to/dir/deployment/common/tea-rc.yaml. The Namespace can not be left blank.
You could also try adding a line like echo $test_namespace to check.

Executing multiple commands( or from a shell script) in a kubernetes pod

I'm writing a shell script which needs to login into the pod and execute a series of commands in a kubernetes pod.
Below is my sample_script.sh:
kubectl exec octavia-api-worker-pod-test -c octavia-api bash
unset http_proxy https_proxy
mv /usr/local/etc/octavia/octavia.conf /usr/local/etc/octavia/octavia.conf-orig
/usr/local/bin/octavia-db-manage --config-file /usr/local/etc/octavia/octavia.conf upgrade head
After running this script, I'm not getting any output.
Any help will be greatly appreciated
Are you running all these commands as a single line command? First of all, there's no ; or && between those commands. So if you paste it as a multi-line script to your terminal, likely it will get executed locally.
Second, to tell bash to execute something, you need: bash -c "command".
Try running this:
$ kubectl exec POD_NAME -- bash -c "date && echo 1"
Wed Apr 19 19:29:25 UTC 2017
1
You can make it multiline like this:
$ kubectl exec POD_NAME -- bash -c "date && \
echo 1 && \
echo 2"
The following should work
kubectl -it exec podname -- bash -c "ls && ls"
bin dev etc home proc root run sys tmp usr var bin
dev etc home proc root run sys tmp usr var
If above command doesn't work then try too replace bash with one of the following /bin/bash, sh or /bin/sh
-t
can solve your task
For example, I run here few cmd:
kubectl get pods |grep nginx|cut -f1 -d\ |\
while read pod; \
do echo "$pod writing:";\
kubectl exec -t $pod -- bash -c \
"dd if=/dev/zero of=/feeds/test.bin bs=260K count=4 2>&1|\
grep copi |cut -d, -f4; \
a=$SECONDS; echo -ne 'reading:'; cat /feeds/test.bin >/dev/null ; \
let a=SECONDS-a ; \
echo $a sec"
done
p.s. your example will be:
kubectl exec -t octavia-api-worker-pod-test -c octavia-api -- bash -c "unset http_proxy https_proxy ; mv /usr/local/etc/octavia/octavia.conf /usr/local/etc/octavia/octavia.conf-orig ; /usr/local/bin/octavia-db-manage --config-file /usr/local/etc/octavia/octavia.conf ; upgrade ; head"
Posting here because google search still brings you to this post...
I'd like to throw out using a HEREDOC as an additional possibility.
kubectl exec -i --tty-false PODNAME -- bash << EOF
echo "insert all your commands here."
echo "this subprocess will even pickup any variables you have in"
echo "the shell script that is calling this"
EOF