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
withoh-my-zsh
framework installedkubectl
installed.kube/config
file is populated with Kubernetes clusters you manage
Foraws
eks
clusters, you can auto populate this cluster by running this command:aws eks update-kubeconfig --name eksClusterName
.zshrc
customization
- Add
kubectl
andkube-ps1
into theplugins
Here is my plugins line:plugins=(git aws kubectl kube-ps1 globalias)
Note: globalias is useful because it expands alias when able. I found it convenient when I type inkgp
then space, it expands tokubectl get pods
. - Add the following line toward the end of
.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
- Remembering the following helps you to type the right
alias
k
->kubectl
- g ->
get
d
->describe
del
->delete
kcuc
->kubectl config use-context
kaf
->kubectl apply -f
- See the
kubectl
plugin documentation for complete list of prebuilt aliases
- Use the
globalias
plugin like I mentioned above. The expansion leaves no doubt of what the alias does aws eks
cluster’s full name is pretty long. If you don’t need it displayed in the prompt, simply runkubeo
ff andkubeon
to toggle it.