|
| 1 | +# kubectl-tmux-exec |
| 2 | + |
| 3 | +A kubectl plugin that uses [Tmux](https://github.com/tmux/tmux) to multiplex commands to pods. |
| 4 | + |
| 5 | +It is to `kubectl exec` as `csshX` or `pssh` is to `ssh`. |
| 6 | + |
| 7 | +Instead of `exec bash` into multiple pod's containers one-at-a-time, like `kubectl exec -it pod{N} /bin/bash`. |
| 8 | + |
| 9 | +You can now use |
| 10 | + |
| 11 | +```sh |
| 12 | +kubectl tmux exec -it -l app=nginx /bin/bash |
| 13 | +``` |
| 14 | + |
| 15 | +# Installation via Homebrew |
| 16 | + |
| 17 | +If you do not have Homebrew installed on your mac, please follow [its installation instruction](https://brew.sh/). |
| 18 | + |
| 19 | +After that, execute the command below. |
| 20 | + |
| 21 | +```sh |
| 22 | +brew install predatorray/brew/kubectl-tmux-exec |
| 23 | +``` |
| 24 | + |
| 25 | +The script should be installed under `/usr/local/bin/kubectl-tmux-exec` by default. Please ensure the `bin` directory is in your `$PATH` environment variable. |
| 26 | + |
| 27 | +# Usage |
| 28 | + |
| 29 | +To execute this script as a [plugin]((https://kubernetes.io/docs/tasks/extend-kubectl/kubectl-plugins/)), a `kubectl` version prior to `1.12.0` is required and the latest version is preferred. But you can execute the script directly like `kubectl-tmux-exec [...ARGS]` if it is not supported. |
| 30 | + |
| 31 | +If it is supported, you can check if the script has been added to kubectl's plugin list by |
| 32 | + |
| 33 | +```sh |
| 34 | +kubectl plugin list |
| 35 | +``` |
| 36 | + |
| 37 | +The output should be like |
| 38 | + |
| 39 | +```txt |
| 40 | +The following compatible plugins are available: |
| 41 | +
|
| 42 | +/usr/local/bin/kubectl-tmux-exec |
| 43 | +``` |
| 44 | + |
| 45 | +If it does not show in the list, check `$PATH` env again. |
| 46 | + |
| 47 | +You can use the command below to get the usage of the script. |
| 48 | + |
| 49 | +```sh |
| 50 | +kubectl tmux exec --help |
| 51 | +``` |
| 52 | + |
| 53 | +Or, execute it directly. |
| 54 | + |
| 55 | +``` |
| 56 | +kubectl-tmux-exec --help |
| 57 | +``` |
| 58 | + |
| 59 | +## Example |
| 60 | + |
| 61 | +The `tmux exec` is similar to `exec`, except that it requires label selectors while `exec` requires a pod name. |
| 62 | + |
| 63 | +To `bash` into all pod containers that share some common labels, `foo=bar` for instance. |
| 64 | + |
| 65 | +```sh |
| 66 | +kubectl tmux exec -it -l foo=bar /bin/bash |
| 67 | +``` |
| 68 | + |
| 69 | +It should be noted that the `-i` / `--stdin` and `-t` / `--tty` options must both be turned on when you are trying to initiate an interactive session. If not, there will not be any errors. Instead, the `tmux` process simply exits because the `exec`-ed command exits due to no inputs. |
| 70 | + |
| 71 | +After you have successfully `bash`-ed into your selected containers, a Tmux window is opened actually, where each pane displays the execution result of each pod's container. Your keyboard inputs will be synchronized to all those panes. |
| 72 | + |
| 73 | +If you are not familar with Tmux, you can have a look at tmux's man page or online tutorials. Or you can see the cheatsheet below, which will be sufficient I think. |
| 74 | + |
| 75 | +## Tmux cheatsheet |
| 76 | + |
| 77 | +All Tmux command starts with a PREFIX. By default the PREFIX is `ctrl+b`. I will use `C-b` below to stand for it. |
| 78 | + |
| 79 | +`C-b d`, detach from the session. After that, the Tmux will be running in the backgroud. You can type `tmux a` to re-attach. |
| 80 | + |
| 81 | +`C-b :setw synchronize-panes off`, turn off synchronizing inputs to all panes. |
| 82 | + |
| 83 | +`C-b :setw synchronize-panes on`, turn on synchronizing inputs to all panes. |
| 84 | + |
| 85 | +`C-b <ARROW-KEY>`, move cursor between panes. |
| 86 | + |
| 87 | +`C-b xy`, close the current pane. |
| 88 | + |
| 89 | +`C-b &y`, close the window including all panes. |
0 commit comments