In my last post, I shared my method of switching between different aws
account using oh-my-zsh
.
Often times, many DevOps and cloud administrators also manage multiple Kubernetes clusters. Just like knowing which aws
profile you are using on a terminal, it is important to know which k8s cluster context you are under. This helps to avoid costly mistakes.
Additionally, kubectl
is a long command to type. Many professionals use customized alias
to shorten it.
We can use the right oh-my-zsh
and kube-ps1
plugins to do all of the above.
Assumptions:
zsh
with oh-my-zsh
framework installedkubectl
installed.kube/config
file is populated with Kubernetes clusters you manageaws
eks
clusters, you can auto populate this cluster by running this command:aws eks update-kubeconfig --name eksClusterName
.zshrc
customization
kubectl
and kube-ps1
into the plugins
plugins=(git aws kubectl kube-ps1 globalias)
kgp
then space, it expands to kubectl get pods
..zshrc
. This line adds the Kubernetes cluster name and cluster namespace into the command prompt, so you’ll always know the k8s contextPROMPT='$(kube_ps1)'$PROMPT
Relaunch your terminal. Viola, you will see aws
profile, Kubernetes cluster and namespace you are currently in the prompt. Here is a sample output:
(⎈|arn:aws:eks:us-east-2:111111111111:cluster/martian-bank:default)~ kubectl get pods
NAME READY STATUS RESTARTS AGE
accounts-755d8846dd-zkq79 1/1 Running 0 25h
atm-locator-645df86b54-z8vgk 1/1 Running 0 25h
customer-auth-5747bcf75f-x274s 1/1 Running 0 25h
dashboard-7d955cdddd-jbw5d 1/1 Running 0 25h
loan-dc5dc9c4-rpw2s 1/1 Running 0 25h
mongodb-deployment-59fb4db67d-22nnb 1/1 Running 0 25h
nginx-649867f568-jtj58 1/1 Running 0 25h
transactions-7cfc8d559-tfjdj 1/1 Running 0 25h
ui-76dcd475d5-6rft9 1/1 Running 0 25h
(⎈|arn:aws:eks:us-east-2:111111111111:cluster/martian-bank:default)~ <aws:haidong> <region:us-east-2>
Additional tips
alias
k
-> kubectl
get
d
-> describe
del
-> delete
kcuc
-> kubectl config use-context
kaf
-> kubectl apply -f
kubectl
plugin documentation for complete list of prebuilt aliasesglobalias
plugin like I mentioned above. The expansion leaves no doubt of what the alias doesaws eks
cluster’s full name is pretty long. If you don’t need it displayed in the prompt, simply run kubeo
ff and kubeon
to toggle it.