avatarJohn David Luther

Summary

The web content provides guidance on using kubectl aliases to streamline Kubernetes cluster operations, which is particularly useful for efficiency during Kubernetes Certification (CKA, CKAD) exams.

Abstract

The article emphasizes the importance of creating and using shell aliases and environment variables for kubectl commands to enhance productivity when working with Kubernetes. It outlines a method for defining these shortcuts in a dedicated file (.krc) that can be sourced into the shell environment. The author shares their personal .krc file as an example, demonstrating how these aliases can be applied in practice, especially in the context of the Killercoda cluster environment. The article also discusses the strategy of quickly setting up these aliases at the start of the CKA/CKAD exams to save time and improve task execution efficiency. Additionally, the author points to community resources for further inspiration on creating a comprehensive set of kubectl aliases.

Opinions

  • The author advocates for the liberal use of aliases and environment variables to reduce typing and accelerate cluster operations, suggesting it's a natural extension of working with Kubernetes.
  • There is an emphasis on the balance between having enough aliases to be efficient without having so many that it becomes counterproductive.
  • The author suggests that forming a habit of using these aliases is crucial for their effectiveness, particularly during certification exams where time management is critical.
  • The article highlights the importance of being familiar with the Kubernetes community's practices for creating aliases, which can serve as a rich source of ideas for personal productivity enhancements.
  • It is recommended to use the provided aliases in daily work to become proficient with them before relying on them during the high-pressure environment of an exam.
  • The author stresses that during the exam, it's wise to only add aliases that are applicable to the majority of tasks to avoid wasting time.

Kubernetes Certifications (CKA, CKAD) — Prep Tip #3. Fast and Furious with ‘kubectl’ Aliases

3. Define these shell variables and aliases in your kubectl enabled dev. terminal. Use them daily and during the exam for cluster operations.

Table of Contents

  1. Introduction
  2. Kubectl Alias File
  3. Applying Aliases
  4. Recollecting Aliases
  5. Kubectl Alias File — During Exam
  6. Alias References from Kubernetes Community
  7. Conclusion

Introduction

As a long-time Linux user, I make liberal use of shell environment variables, aliases, and functions to save myself from excess typing, help navigate among a multitude of tasks quickly, and other convenience reasons. So it was natural that when I started working with Kubernetes, I created a whole bunch of kubectl related aliases to make my life easier to interact with clusters.

Then I discovered that the Kubernetes community has taken the aliasing game to a whole different level, as you will see soon in this post. When it comes to the CKA and CKAD certification, where time is of the essence, having a selected set of aliases and environment variables is an absolute must.

The only caveat is that, during the certification exam, we can’t bring our own list, which basically means as soon as the exam timer starts ticking, take a minute or two to type out these variables and aliases into a file, at least the important ones, right away, and then add a few more during the course of working on the tasks. You don’t need the complete list during the exam. However, this strategy works only if you’ve practiced using these aliases much before so that you miss them and feel compelled to have them during the exam. If you have not formed the habit, avoid wasting time on this and just stick with the basic k=kubectl alias.

As soon as the exam timer starts ticking, take a minute or two to type out these variables and aliases into a file and then ‘source <filename>’ to take effect in the shell.

In Prep Tip #3, I plan to show how my kubectl aliases and shortcuts file gradually evolved, what it looks like at present, the approach I took during the exam to ready myself with this necessity, and finally, how to put it to use in the daily life of Kubernetes.

Kubectl Alias File

We begin by identifying a file to house all our aliases and environment variables. And remember to source it whenever this file changes.

For everyday use, the source command can also be added to .bashrc (or the equivalent for any other shell) to take effect automatically with echo “source ~/.krc” >> ~/.bashrc command.

Build Your Own Mnemonics

1. Everybody has their own style of creating aliases and shortcuts. Applying consistent logic to create based on your preference and being able to easily recollect the alias at the time of use is what matters!

2. Strike the right balance in terms of too many aliases vs. too few. Capture the most frequently used commands and then grow gradually.

#
# File: $HOME/.krc
# Kubectl Alias and Environment Variables file.
# Run 'source $HOME/.krc' if file is modified to take effect in the current shell
#

# Enable Kubectl autocomplete as recommended by Kubernetes cheatsheet
# Ref: https://kubernetes.io/docs/reference/kubectl/cheatsheet/
source <(kubectl completion bash) 


#
# Environment Variables
#

export KC=~/.kube/config
export do='--dry-run=client -o yaml'
export dos='--dry-run=server -o yaml'
export now='--force --grace-period 0'
export dd='describe deploy'
export dn='describe node'
export dp='describe pod'
export ds='describe service'

#
# Aliases
#
alias k=kubectl

# Context and namespace related
alias kcc='egrep "(current-context|namespace)" $KC'
alias kn='kubectl config set-context --current --namespace'
alias knd='kubectl config set-context --current --namespace default'
alias kcg='kubectl config get-contexts'
alias kgns='kubectl get namespaces'

# Dealing with kube-system namespace
alias kk='kubectl -n kube-system'
alias kgpk='kubectl -n kube-system get pods'
alias kgpkw='kubectl -n kube-system get pods -o wide'

# Resource creation, updates and verification
alias ka='kubectl apply -f'
alias kad='kubectl apply --dry-run=client -f'
alias kads='kubectl apply --dry-run=server -f'
alias kd='kubectl delete -f'
alias kdelp='kubectl delete pod'
alias kr='kubectl replace -f'

# Get commands
alias kg='kubectl get -f'
alias kgd='kubectl get deploy'
alias kgp='kubectl get pods'
alias kgpf='kubectl get pods -w'
alias kgpw='kubectl get pods -o wide'
alias kgpwf='kubectl get pods -o wide -watch'
alias kgn='kubectl get nodes'
alias kgnw='kubectl get nodes -o wide'
alias kgs='kubectl get svc'
alias kgsw='kubectl get svc -o wide'


# Edit commands
alias kep='kubectl edit pod'
alias ked='kubectl edit deploy'
alias kes='kubectl edit svc'

# Describe commands
alias kde='kubectl describe'
alias kdp='kubectl describe pod'
alias kdn='kubectl describe node'
alias kds='kubectl describe service'
alias kdd='kubectl describe deployment'

# Cluster commands
alias ke='kubectl exec -it'
alias kge="kubectl get events --sort-by='.lastTimestamp'"
alias kl='kubectl logs'

# End of $HOME/.krc

Applying Aliases

Let’s practice some of these aliases on the Killercoda cluster, as mentioned in the Tip #2 post.

Killercoda clusterhttps://killercoda.com/playgrounds/scenario/kubernetes

# Remember to use this command everytime the file changes
source ~/.krc

# Test to make sure it took effect
kgn

NAME           STATUS   ROLES           AGE     VERSION
controlplane   Ready    control-plane   3d21h   v1.27.1
node01         Ready    <none>          3d21h   v1.27.1


# Test autocomplete works as well
k de <TAB>

debug     (Create debugging sessions for troubleshooting workloads and nodes)
delete    (Delete resources by file names, stdin, resources and names, or by resources and label selector)
describe  (Show details of a specific resource or group of resources)


# Aliases and variables have taken effect

# Let's create a deployment manifest
k create deployment webapp --image=nginx --replicas=2 $do | tee webapp.yaml

# Create the deployment (alias ka='kubectl apply -f')
ka webapp.yaml

# Verify deployment status (alias kg='kubectl get -f')
kg webapp.yaml

# Check the pods of the webapp deployment (alias kgp='kubectl get pods')
kgp

# Describe the deployment (alias kdd='kubectl describe deployment')
kdd webapp

# Let's go inside one of the pods(alias ke='kubectl exec -it')
ke webapp-8474645868-8b5hb -- sh

# Check the log of the (alias kl='kubectl logs')
kl deploy/webapp

# Delete a pod (alias kdelp='kubectl delete pod')
kdelp webapp-8474645868-8b5hb

# Finally delete the deployment (alias kd='kubectl delete -f')
kd webapp.yaml 

# Well done! everything is working as expected. 
# Try others on your own. Don't forget to expand and improvise.

Recollecting Aliases

Sometimes the mind fails to apply the mnemonics used to construct the aliases. In that case, looking up the .krc file is the best way to refresh the mind. The following commands help in that regard.

#  prints the list of aliases 
alias

# Filter all kubectl aliases
alias | grep kubectl

# Check a particular alias 
alias kdelp

# Check an environment variable
echo $do

# Grep for environment variable
env | grep do

Context and Namespace Aliases

In both CKA and CKAD exams, each question asks to change to the correct cluster context, and they supply the full command to do so. Each task may also require to be performed in a given namespace. Pay attention to both.

It’s important to pay attention to both the context and namespace before working on a CKA/CKAD task.

It’s a personal preference to stay on the default namespace and specify the namespace in every kubectl command or change the namespace to make it sticky and stay there until all the work in that namespace is finished.

These aliases are helpful while dealing with cluster context and cluster namespace.

# List all the available contexts (alias kcg='kubectl config get-contexts')
kcg

# List all current namespaces (alias kgns='kubectl get namespaces')
kgns

# Change to a non default namespace (e.g. maintenance)
# (alias kn='kubectl config set-context --current --namespace')
# Note: this requires right access to ~/.kube/config
kn maintenance

# Check current context and namespace
# (alias kcc='egrep "(current-context|namespace)" $KC')
kcc

# Switch back to default namespace
# (alias knd='kubectl config set-context --current --namespace default')
knd

# Verify change happened
kcc

Kubectl Alias File — During Exam

As I said before, as soon as the exam begins, type out the most commonly used aliases and environment variables into a file ~/.krc in the first 1 or 2 minutes, run source ~/.krc , and put the aliases to use right away. This is easily doable if you’ve been using these aliases daily for regular work or during exam practice sessions. Then add more as the tasks demand, source it quickly and repeat the process. Again, it is important to remember that time is precious during the exam, so only add the aliases applicable to most of the tasks.

I’ve used the following frequently during the exams in addition to the other aliases, as illustrated earlier.

# Creating manifest with dry run (export do='--dry-run=client -o yaml')
k run testpod --image=radial/busyboxplus \
  -n maintenance $do --command -- sh -c "sleep 3600" | tee testpod.yaml

# If making manual changes to the manifest, verify with a dry run
# (alias kad='kubectl apply --dry-run=client -f')
# This will succeed
kad testpod.yaml

# Also helps to do a dry run by submitting the request to the cluster
# (alias kads='kubectl apply --dry-run=server -f')
# This will fail since the namespace 'maintenance' doesn't exist
kads testpod.yaml

# I could've caught the above error by using --dry-run=server
# (export dos='--dry-run=server -o yaml')
k run testpod --image=radial/busyboxplus \
>   -n maintenance $dos --command -- sh -c "sleep 3600" | tee testpod.yaml

Alias References from Kubernetes Community

I said in the beginning that the Kubernetes community has taken this aliasing game to a whole different level. The following two links easily prove my point. I had not seen them when I was putting together my alias list, but both provide fecund ideas on how to be creative to assemble a list that serves you best, so both are worth your time reading. Again balancing too many vs. too few is key.

  1. Kubectl tips and tricks https://discuss.kubernetes.io/t/kubectl-tips-and-tricks/192
  2. kubectl-aliases, a script-generated list https://github.com/ahmetb/kubectl-aliases/blob/master/.kubectl_aliases

Conclusion

In Kubernetes Certifications (CKA, CKAD) — Prep Tip #3, I provided a quick list of kubectl command-related aliases and environment variables commonly used in the daily life of interacting and operating with Kubernetes clusters. This list also plays a key role during the CKA and CKAD exams as they help save valuable time, the most precious commodity during the exam.

Additionally, I provided two helpful links from the Kubernetes community to show how the community members think about being productive and efficient in using the kubectl command. Reading both links along with this post will help you to manage your own list of aliases, grow it over time and be productive in your daily interaction with Kubernetes clusters, and also apply it during the CKA and CKAD exams to save time if you have the plan to take them in the near future.

At your convenience, please visit Prep Tip #4. Three https://k8s.io Pages. Know ’em, Do ’em!

If you benefited from reading the post, please 👏 a few times before parting, and help others by sharing it; I highly appreciate that!

Please follow to stay in touch, track, and be the first to get notified of all future writings on AWS Cloud, Containers, Kubernetes, and Machine Learning. Also, check all my stories on The AWS Way publication.

Kubernetes
Docker
DevOps
Certification
Recommended from ReadMedium