In this blog post I am sharing some of the useful Kubernetes aliases that I use on daily basis to make my job a little bit easier.
When working with a complex Kubernetes cluster where you are dealing with multiple namespaces, it could become quite cumbersome to switching between different namespaces. Following alias helps me to that quickly:
alias kn='kubectl config set-context --current --namespace'
kn dev # Switch to dev namespace
kn prod # Switch to prod namespace
As you develop Kubernetes YAML definition files, you may need a quick way to test if the configuration that you are building will yield desired results or not. For example, you create a network policy to ensure that only API pod can call the database pod. How would you test this configuration?
This is where following alias comes in handy:
alias kt='kubectl run test-pod --image=busybox -it --rm --restart=Never --'
kt command quickly spins up a pod with busybox image allowing you to quickly run any commands required for testing your configurations. And thanks to
--rm parameter, this pod will automatically get deleted as soon as you are done with your testing.
kt /bin/sh # Open a shell inside the test-pod
kt wget -O- http://pod1.dev.svc.cluster.local # Test if the pod is able to reach pod1 in the dev namespace
kt wget -O- www.google.com # Check if the pod is able to reach the internet
Often times you need to generate YAML for various Kubernetes objects like deployments, Pods, Services etc. using the
--dry-run=client -o yaml parameters. Instead of typing these parameters every time, you can use the following handy shortcut:
export dr='--dry-run=client -o yaml'
kubectl run nginx-pod --image=nginx $dr # Generate YAML definition for a Pod
kubectl create deployment nginx-deploy --image=nginx --replicas=2 $dr # Generate YAML definition for a Deployment
The following shortcut is particularly a time saver when you are testing different configurations and need to quickly delete pods:
export now='--force --grace-period 0'
kubectl delete pod nginx $now # Force delete a Pod without any grace period
Following are few other aliases that I use on daily basis:
alias kd='kubectl describe'
alias kg='kubectl get'