The Complete Beginner's Guide to Setting Up Kubectl Aliases with Kuberc
If you’re working with Kubernetes, you’ve probably found yourself typing long commands like kubectl get pods
or kubectl logs my-pod --follow --tail=50
over and over again. It gets tedious quickly, right?
Good news! Starting with Kubernetes version 1.33, there’s now a built-in way to create shortcuts (called “aliases”) for these repetitive commands. It’s called kuberc, and in this guide, I’ll walk you through everything you need to know to set it up and start saving time.
What You’ll Learn
By the end of this tutorial, you’ll know how to:
- Install the right version of kubectl
- Enable the kuberc feature on your computer
- Create your own command shortcuts
- Use and test these shortcuts
- Troubleshoot common issues
Why Should You Care About Aliases?
Think of aliases like speed dial for your phone. Instead of typing out a full phone number every time, you just press one button.
Similarly, instead of typing:
1 | kubectl get pods --namespace=production --output=yaml |
You could just type:
1 | kgp |
Much easier, right? This saves time, reduces typos, and makes your work more efficient.
What’s New About Kuberc?
The Old Way (Before Kubernetes 1.33)
Previously, if you wanted shortcuts for kubectl commands, you had to manually add them to your shell configuration files (like .bashrc
or .zshrc
). This meant mixing Kubernetes shortcuts with all your other shell settings, making things messy and hard to manage.
The New Way (Kubernetes 1.33+)
Now, kubectl has its own dedicated configuration file called kuberc
. It’s a clean, organized YAML file specifically for kubectl aliases. This keeps your shortcuts separate from other configurations and makes them easier to manage and share with your team.
Step 1: Check and Upgrade Your Kubectl Version
First things first - you need kubectl version 1.33 or higher for this to work.
Check Your Current Version
Open your terminal and run:
1 | kubectl version --client |
You should see something like:
1 | Client Version: v1.33.1 |
Look at the number after “Client Version: v” - if it’s 1.33 or higher, you’re good to go! If it’s lower (like v1.28 or v1.30), you need to upgrade.
How to Upgrade Kubectl
Visit the official kubectl installation documentation and follow the instructions for your operating system:
- Linux users: Follow the Linux installation guide
- Mac users: You can use Homebrew with
brew upgrade kubectl
- Windows users: Follow the Windows installation guide
After upgrading, check the version again to confirm it worked.
Step 2: Enable the Kuberc Feature
Even though you now have the right version, the kuberc feature isn’t turned on automatically. You need to enable it manually.
Turn On Kuberc
Run this command in your terminal:
1 | export KUBECTL_KUBERC=true |
This tells kubectl: “Hey, I want to use the kuberc file for my aliases!”
Make It Permanent
Important: The command above only works for your current terminal session. If you close the terminal and open a new one, you’ll need to run it again.
To make it permanent, add this line to your shell configuration file:
For Bash users (most Linux distributions):
1 | echo 'export KUBECTL_KUBERC=true' >> ~/.bashrc |
For Zsh users (default on newer Macs):
1 | echo 'export KUBECTL_KUBERC=true' >> ~/.zshrc |
After adding this, close and reopen your terminal, or run source ~/.bashrc
(or source ~/.zshrc
for Zsh) to apply the changes immediately.
Step 3: Understanding the Kuberc File Location
Default Location
By default, kubectl looks for your kuberc file at:
1 | ~/.kube/kuberc |
Let me break this down:
~
means your home directory (like/home/username
on Linux or/Users/username
on Mac).kube
is a hidden folder where kubectl stores its configurationkuberc
is the file where you’ll define your aliases
Using a Custom Location (Optional)
If you want to use a different location for your kuberc file, you can! Just use the --kuberc
flag whenever you run kubectl commands:
1 | kubectl --kuberc /path/to/my/custom/kuberc get pods |
Why would you use this? Maybe you have different aliases for different projects or environments (development, staging, production). You could create separate kuberc files for each and switch between them easily.
Step 4: Create Your Kuberc File
Now let’s create the actual configuration file where you’ll define your shortcuts.
Create the File
Run this command:
1 | vim ~/.kube/kuberc |
Don’t like vim? You can use any text editor you prefer:
1 | nano ~/.kube/kuberc |
Step 5: Understanding the Kuberc Configuration Structure
The kuberc file is written in YAML format (the same format used for Kubernetes configuration files). Here’s what a basic kuberc file looks like:
1 | apiVersion: kubectl.config.k8s.io/v1beta1 |
Breaking Down the Structure
Line 1: apiVersion: kubectl.config.k8s.io/v1beta1
- This tells kubectl which version of the kuberc format you’re using
- Since this is still a beta feature, it uses “v1beta1”
Line 2: kind: Preference
- This indicates that this file contains user preferences for kubectl
- Think of it like a label that says “this is a preferences file”
Line 3: aliases:
- This is where your actual shortcuts are defined
- Everything under this is a list of aliases you want to create
The Alias Definition:
name
: The shortcut you’ll type (likekgp
)command
: The kubectl command to run (likeget
)appendArgs
: Additional arguments or options (likepods
)
When you type kubectl kgp
, it automatically expands to kubectl get pods
.
Step 6: Creating Your First Aliases
Let’s create some useful aliases! Add this configuration to your kuberc file:
1 | apiVersion: kubectl.config.k8s.io/v1beta1 |
What Each Alias Does
kgp
- Quickly view all pods in the current namespace- Instead of:
kubectl get pods
- You type:
kubectl kgp
- Instead of:
klogs
- View live logs from a pod, showing the last 50 lines- Instead of:
kubectl logs my-pod --follow --tail=50
- You type:
kubectl klogs my-pod
- Instead of:
crns
- Create a namespace called “dev-project-01”- Instead of:
kubectl create namespace dev-project-01
- You type:
kubectl crns
- Instead of:
kgd
- View all deployments- Instead of:
kubectl get deployments
- You type:
kubectl kgd
- Instead of:
kgs
- View all services- Instead of:
kubectl get services
- You type:
kubectl kgs
- Instead of:
Step 7: Advanced Feature - Overrides
Overrides let you set default flags for certain commands. This is super useful for commands you always want to run with specific options.
What Are Overrides?
Imagine you always want to see pod information in YAML format, or you always want a confirmation before deleting something. Overrides let you set these defaults automatically.
Example Override Configuration
Add this to your kuberc file:
1 | apiVersion: kubectl.config.k8s.io/v1beta1 |
Understanding Each Override
1. Get Command Override (output: yaml
):
- When you run
kubectl get pods
, it automatically adds-o yaml
- You’ll see full YAML output instead of the simple table format
- Great for when you need detailed information
2. Delete Command Override (interactive: true
):
- Before deleting anything, kubectl will ask: “Are you sure?”
- Prevents accidental deletions (we’ve all been there!)
- You can confirm or cancel before anything is deleted
3. Apply Command Override (server-side: true
):
- Tells kubectl to process changes on the server side
- More efficient for large configurations
- Reduces network traffic
4. Exec Command Override (it
flag):
- Automatically adds
-it
when you run exec commands -i
= interactive (keeps stdin open)-t
= allocates a terminal- Essential for running shell commands in containers
Step 8: Testing Your Aliases
Now that you’ve set everything up, let’s test it!
Test 1: View Pods
1 | kubectl kgp |
If you set up the override for YAML output, you should see pod information in YAML format.
Test 2: Create a Namespace
1 | kubectl crns |
This should create a namespace called “dev-project-01”.
Test 3: Delete with Confirmation
1 | kubectl delete ns dev-project-01 |
If you set up the delete override, you should see a confirmation prompt asking if you’re sure you want to delete.
Troubleshooting
If your aliases aren’t working:
Check that kuberc is enabled:
1
echo $KUBECTL_KUBERC
Should output:
true
Check kubectl version:
1
kubectl version --client
Should be 1.33 or higher
Check if the kuberc file exists:
1
ls -la ~/.kube/kuberc
Should show the file
Check for YAML syntax errors:
- Make sure indentation is correct (use spaces, not tabs)
- Make sure there are no typos in
apiVersion
orkind
Step 9: Pro Tips and Best Practices
Combine with Shell Aliases
You can make things even shorter by adding a shell alias for kubectl itself:
1 | echo 'alias k=kubectl' >> ~/.bashrc |
Now instead of kubectl kgp
, you can just type k kgp
!
Use Tab Completion
Kubectl supports tab completion, and it works with kuberc aliases too! After typing a few letters, press Tab to autocomplete.
To enable completion (if not already enabled):
For Bash:
1 | kubectl completion bash >> ~/.bashrc |
For Zsh:
1 | kubectl completion zsh >> ~/.zshrc |
Version Control Your Kuberc
Since kuberc is just a file, you can:
- Back it up to GitHub
- Share it with your team
- Keep different versions for different projects
Temporarily Disable Kuberc
If you need to temporarily turn off kuberc:
1 | export KUBERC=off |
Or if using the feature gate:
1 | export KUBECTL_KUBERC=false |
Common Aliases You Might Want
Here are some popular aliases used by Kubernetes professionals:
1 | aliases: |
Quick Reference
Enable kuberc:
1 | export KUBECTL_KUBERC=true |
Kuberc file location:
1 | ~/.kube/kuberc |
Basic kuberc structure:
1 | apiVersion: kubectl.config.k8s.io/v1beta1 |
Check if it’s working:
1 | kubectl <your-alias> |
Conclusion
Congratulations! You now know how to:
- Set up kubectl aliases using the new kuberc feature
- Create custom shortcuts for your most-used commands
- Use overrides to set default behaviors
- Troubleshoot common issues
Using kuberc will save you time and make working with Kubernetes much more enjoyable. Start with a few basic aliases and gradually add more as you discover which commands you use most often.