Skip to content

Instantly share code, notes, and snippets.

@baybatu
Last active December 14, 2022 11:35
Show Gist options
  • Save baybatu/99bb566e208955b3794c0ac151427f4a to your computer and use it in GitHub Desktop.
Save baybatu/99bb566e208955b3794c0ac151427f4a to your computer and use it in GitHub Desktop.
Piping bash commands in kubectl exec

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"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment