Bash commands connected using pipe(|) are not executed correctly in kubectl exec.
Example:
kubectl exec -ti <POD> -- pg_dump -h localhost -t my_table | psql -d <TARGET_DB> -h localhost
Here I want to run the pg_dump command first and redirect the output to the psql command as input using pipe(|).
But kubectl takes commands only until pipe character, pg_dump in my case and psql has been run on host machine instead of inside the <POD>.
The solution is to wrap all commands using double quote(") and run these commands using bash -c.
Fixed example:
kubectl exec -ti <POD> -- bash -c "pg_dump -h localhost -t my_table | psql -d <TARGET_DB> -h localhost"
This is not an issue with
kubectl. It is how the shell works: it first opens the left side of the pipeto produce the input to the right side of the pipe
You need
bash -cbecause you want the pipe to run completely inside the pod.