In this Cloud Native Short Take, Christian Lacina shares valuable tips and tricks to help you speed up your workflow using kubectl.
For those unfamiliar, kubectl (Kubernetes Control) is a command-line interface (CLI) used to interact with Kubernetes clusters. It offers a wide range of commands, subcommands, and options that simplify working with Kubernetes without the need for complex configurations.
Video Transcript
Hello everybody, welcome to another RX-M Cloud native short take. I’m Christian Lacsina and today I will be short taking some tips and tricks that will help you speed up your workflow using kubectl! kubectl is a command line interface (CLI) used to interact with Kubernetes clusters. It provides a variety of commands, subcommands, and options that allow users to make requests of their target Kubernetes cluster without needing to handle headers, provide API URLs, or convert their yaml to json. kubectl does that for us!
Let’s start with auto completion. Auto completion enables tab completion. It allows me to press tab as I formulate a kubectl command which fills in the next possible result or suggests them. You set this up by ensuring you have bash completion installed on your machine and you can check this by running the following command in your terminal: type _init_ completion and it looks like I don’t have that installed so I will need to use my package manager to install bash completion and then source it so that it’s enabled inside my shell session. I’ll do that real quick right now.
While this is downloading I’ll go ahead and grab the command that I need in order to enable bash completion on my shell session. That’s going to be done in the Kubernetes documentation and I’ll grab this right here: source /usr/share/bash-completion/bash_completion. Copy that over and I’ll go ahead and paste that in my terminal. Now if I rerun that earlier command we can see that I’ve got bash completion enabled. So now that I’ve confirmed that autocompletion is enabled I can then use the following command: source <(kubectl completion bash).
From here I can use tab to fill in things like resource types or names so if I do something like kubectl -n kube-system get I can use tab to help complete my query and let it figure out what I want to take a look at. So if I wanted to get something like pods I can type in p or po and it will complete that for me. It’s very useful if I’m working in an environment with very complex naming schemes which is something that can happen with Kubernetes if you’re using it in a production environment.
Next let’s talk about labeling and the -l switch. Labels are key value metadata that can be assigned to any Kubernetes API resource. You can assign labels to resources at creation or afterward using kubectl label followed by your key value that you want to assign. You can view labels by providing the --show-labels option with kubectl get.
I can also add labels with kubectl label as I mentioned earlier so let’s add the label key=value to the Pod “mypod”. Now “mypod” has the label key=value. Keep in mind that this only affects the target resource. Any child resources that the resource may have like replicaSets or pods managed by a Deployment will not inherit a label added with kubectl label. Once you have your labels you can use them to find all types of resources with that label with kubectl -l short for --selector. Let’s look at pods Deployments and replicaSets with the label app=webserver. We’ll do: kubectl get deployments,replicasets,pods -l app=webserver. I can easily see what’s associated with this label. The simpler the labels you provide the faster your workflows can be and also the -l switch is available for things like logs which will allow you to see the logs for all associated pods with that label.
Next up, let’s talk about aliases. Shell aliases have helped CLI these users shorten commands for a very long time. Chances are the shell that you’re using already has a command like alias to establish a shortcut. A popular shortcut I see is either k or kube whichever you choose your alias is easily established with alias k=kubectl. So let’s go ahead and do that in my cluster we’ll do: alias k=kubectl. That’s going to allow me to use k get pods to actually view pods in my cluster with fewer keystrokes.
Aliases can be used in conjunction with autocompletion though you do need to have a separate command for it. In the Kubernetes documentation we have the cheat sheet which actually shows us the additional command that we need to add. Here I’ll use complete -o default -F __start-kubectl k and move that into my cluster. Now I can use k get po, that’s going to do the auto completion and I can do my and complete the pod name with auto completion in conjunction with my alias.
kubectl plugins are separate modules that you will add to kubectl which will provide additional functionality in the form of new options or commands for kubectl. One great example of a kubectl plugin is kubectl convert. kubectl convert allows you to take an outdated pod yaml or any kind of yaml really like this one right here, which is describing a Deployment under the very old and long gone extensions API. This API was removed sometime around Kubernetes 1.16 or 1.17 and it used to house things like Deployments Ingresses and the like before they were all moved into their own SIGs. So what kubectl convert will do to this yaml is it will take this value and then update it into something that is recognized by newer or more modern versions of Kubernetes. In my case on Kubernetes 1.26 it has to be “apps”. It’s really great for dealing with those API deprecations that can occur between new versions of Kubernetes.
So, in order to use kubectl convert I do need to install it. I’ll defer to the instructions on the Kubernetes documentation. So first off I will go ahead and download the package and then once the package has been downloaded I’ll go ahead and install it. Now that it’s installed I can use kubectl convert as a subcommand for my kubectl instance. This was not the case before I installed it. So now what I can do with kubectl convert is take that Deployment yaml from earlier and run it through kubectl and we can see that among other things it has updated the API version to the more modern “apps/v1” which replaced the extensions API all those versions ago. I can even pipe that command into kubectl apply to do a conversion and then a deployment if I so desired!
You can create your own customized courses with this and many other Kubernetes related modules using the custom course builder right here on the RX-M website.
That’s our RX-M Cloud Native Short Take on some tips and tricks used to speed up kubectl-driven workflows!