Skip to main content

Tips

Creating a kubectl alias​

To simplify using kubectl, you can create a shortcut command k. Follow these steps:

  1. Create a new script for the alias:

    mkdir -p ~/bin
    vi ~/bin/k

    Add the following lines to ~/bin/k:

    #!/bin/bash
    kubectl "$@"
  2. Make the script executable:

    chmod +x ~/bin/k
  3. Add ~/bin to your PATH (if not already present): Open your shell configuration file (e.g., ~/.zshrc or ~/.bashrc) and add:

    export PATH="$HOME/bin:$PATH"
  4. Reload your shell configuration:

    source ~/.zshrc # or source ~/.bashrc

Now you can use k as a shortcut for kubectl in your terminal.

We don't use alias k=kubectl because it can cause issues with command completion and other features in some shells. The script method avoids these problems while still providing a convenient shortcut. For example, with this method you can run watch k get node.