kubectl Plugin Development Guide
kubectl Plugin Development Guide
A kubectl plugin is the easiest way to extend kubectl with commands tailored to your own needs.
What Is a kubectl Plugin?
A kubectl plugin is a standalone executable file that works as a subcommand of kubectl. With plugins you can simplify complex commands or build custom tools for cluster management.
Key Characteristics
| Item | Description | Note |
|---|---|---|
| Naming convention | Must follow the kubectl-<plugin-name> format | Required |
| File location | A directory listed in the user’s $PATH environment variable | Required |
| How it runs | Invoked as kubectl <plugin-name> | - |
| Language | Any language works as long as the file is executable (Bash, Go, Python, etc.) | Flexibility |
Plugin Command Rules
The relationship between the command a user types in the terminal and the actual file name looks like this.
graph LR
Input["Terminal: kubectl ns"] -- "Search $PATH" --> File["File: kubectl-ns"]
Input2["Terminal: kubectl oidc-login"] -- "Search $PATH" --> File2["File: kubectl-oidc-login"]
subgraph Execution
File -- "Execute" --> Result["Namespace-switching logic"]
File2 -- "Execute" --> Result2["Login flow logic"]
end
Step 1: Building the Simplest Plugin (Bash)
1. Write the script
Create a very simple plugin that prints a greeting.
# Create the /usr/local/bin/kubectl-hello file
cat <<'EOF' > /usr/local/bin/kubectl-hello
#!/bin/bash
echo "Hello! This is a custom kubectl plugin."
EOF2. Make it executable and verify
# Make the file executable
chmod +x /usr/local/bin/kubectl-hello
# List installed plugins
kubectl plugin list3. Usage
kubectl hello
# Output: Hello! This is a custom kubectl plugin.Step 2: A Practical Plugin Example (Switching Namespaces)
This example implements a commonly used namespace-switching tool in Bash.
#!/bin/bash
# File name: kubectl-ns
if [ -z "$1" ]; then
kubectl config view --minify | grep namespace
else
kubectl config set-context --current --namespace="$1"
echo "Context switched to namespace '$1'."
fiKey Takeaways
- Rule: The file name must always start with
kubectl-. - Distribution: Copy the file into a
$PATHdirectory and give it execute permission withchmod +x, and the plugin is installed. - Management: The
kubectl plugin listcommand shows all currently available plugins at a glance. - Recommended tool: To manage more plugins easily, use the plugin manager Krew.
With plugins, you can turn repetitive, complex kubectl options into your own shortcut commands and greatly improve your productivity.