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:
zshwithoh-my-zshframework installedkubectlinstalled.kube/configfile is populated with Kubernetes clusters you manage
Forawseksclusters, you can auto populate this cluster by running this command:aws eks update-kubeconfig --name eksClusterName
.zshrc customization
- Add
kubectlandkube-ps1into 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 inkgpthen 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
aliask->kubectl- g ->
get d->describedel->deletekcuc->kubectl config use-contextkaf->kubectl apply -f- See the
kubectlplugin documentation for complete list of prebuilt aliases
- Use the
globaliasplugin like I mentioned above. The expansion leaves no doubt of what the alias does aws ekscluster’s full name is pretty long. If you don’t need it displayed in the prompt, simply runkubeoff andkubeonto toggle it.