kubectl exec: Permission denied

12/31/2018

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
-- BAE
kubectl
kubernetes

2 Answers

1/1/2019

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"
-- Quentin Revel
Source: StackOverflow

12/31/2018

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.

-- coderanger
Source: StackOverflow