Introduction to Kubectl
Almost everyone will use kubectl to talk to their Kubernetes cluster. In this lesson we’ll show some tips and tricks to level-up your kubectl-fu.
Hey everyone and welcome to KubeAcademy. My name is John Harris and I’m a senior cloud native architect of VMware. In today’s video we’re going to be looking at kubectl and interacting with your Kubernetes clusters.
So if you’ve used Kubernetes at all, I’m sure you’ve used kubectl before to talk to those clusters and to do things on them. Today we’re going to be looking at making you more efficient with kubectl and looking at number of third-party tooling to enhance kubectl even further.
Let’s dive in, the first thing we’re going to look at is kubectl completion. We can use the help to show us how to do this. This is going to set up auto completion in your shell for kubectl. I’m using bash, I’m going to set this correctly. Now we can do things like kubectl get pods, and it’s gonna tell me, I’m in the set manager name space, it’s gonna automatically choose the pods in that name space, if I hit tab twice, gonna show me the pods in there.
Choose one of these, hit enter, we see that this is namespace aware so finding kubectl, get pods in the kube system namespace and hit tab. This tab is gonna show me all the pods that are in that name space. So this is really useful for what’s seeing what’s in where and moving around the cluster.
A lot of the times you’ll be dealing with multiple clusters from our machines. So your kube config might have their credentials and their endpoints from different clusters, it would be really useful to see and be able to switch between them really easily. So you can do that using the kubectl config context commands. But, there’s a product called kubectx and it comes with kubens that’ll allow you to move around context and name space really easily. I’ve got a script that’s gonna install kubectx on here. Ready?
What you can do is things like, kubectx and this is going to show the context that I’m currently pointed at which is, admin at Kubernetes. I only have one cluster that I’m talking to right now. I can also use kubeNS to show me the name spaces in my cluster. You can see we have a bunch of them here and we can see that I’m pointed at my set manager name space, which is selected in yellow. I can use kubens to change name space. So let’s go into my dex name space here. If I run kubens again, it’s gonna show me I’m pointing to dex.
This is nice, but I might want some way of knowing which context and which name space I’m pointing to at any time, I might want to put this in my prompt. There’s another cool project called kubePS1 that allows us to do that. I have another script set up here. I’m gonna go ahead and install that.
I’m gonna pull that out from get harm and I’m gonna source my bash LC and I’m gonna type kube on, and this is gonna enable that prompt. We can see now that I’m in the admin at Kubernetes context and I’m in the dex pw cluster and if I change my name space to default, we can see that my prompt updates to the default.
Now, I don’t know about you but I always find myself forgetting the syntax or the feels for particular things. Let’s say a pod, like in a container, containers have ports like what type is it? What kind of things can I put in there? So we can use kubectl explain to do just that. We could use kubectl explain and let’s say, “pod” and it’s gonna show us what a pod looks like the version and all the fields so I know the field that I care about is in kubectl explain pod dot spec dot containers dot ports. Cool, it’s gonna show us what the port looks like, so it’s an integer. It’s tells us it needs to be between zero and six five five three six, the host IP is a string et cetera et cetera. So, kubectl explain is a really cool tool if you forget things you can recurse through all the resources in the fields and it’s great if you’re doing things like CKAA or the CKAAD in the exam because all this is built into kubectl.
Now let’s take a look at some of the different output formats you can get from kubectl. So the format that we saw so far is this tabular output where everything just comes out in a table. Let’s take a look at json. You can actually get the details in json. You’d get pod MIkube system and let’s choose calico … calico CD … I’m going to O. Output json, that’s going to output json format. Now what I can do is I’m going to go ahead and pipe that to jq and I’m gonna look at the meta data dot labels.I can get this in alpha v 5 tooling, I can pip this json to that and if the tooling understands json I can use that to manipulate it.
Now ,let’s take a look at a different output format which is custom columns. I need kubectl and do the same thing. Instead of O json, I’m going to do custom columns. This is where I can choose the columns I want to appear on my tabular later. I’m going to have a column called name and that’s going to refer to the meta data dot name of my pod and I have a column called name space which is gonna refer to the meta data dot namespace. Now, if I hit that, I’ve only selected one pod, let’s go ahead and get rid of this pod. Just take all the pods from jube system. We see it’s constructed a custom table format for me. Name and name space maps to the metadata that I told it that I wanted.
We can also take a look at all of our pods, in jube system namespace and we can add labels. Show labels, okay that’s kind of busy, there’s a lot of labels here but let’s say I want to add one of those labels as a custom column heading. Instead of showing all labels I’m going to do dash capital L, K, A, S app. What’s that’s going to do is add a custom column heading to my output called KASapp and it’s going to give me the value of that label for all of those pods. This is really useful, we can go a bit further, we can also select all the pods we want by a particular app. Let’s say we want anything that’s got the K, A, S app of calico node and instead of using uppercase L we’re going to use lowercase L and this it’s going to do a selection just on those pods.
We can go a step further and we can sort these. You can see the restart count for all these pods is different. It might be useful to sort this restart count so we can see which one has the most restarts easily, which one has the least. This time we’re going to do a bit of jsonpath kind of magic. We’re going to get these pods again, we’re going to use the same app, this time we’re going to sort by. This is where our jsonpath expression comes in, we’re going to status dot container statuses and this returns an array. We’re going to take all of these and we’re going to take the restart count parameter.
Now if I run this we can see it’s now sorted it, so we have three restarts at the top, two have four, two have five, and then nine at the bottom. We can actually manipulate our output from kubectl really well.
One of the things that’s also really cool with kubectl is a lot of the times we’ll hit issues; our output won’t come back the way we want, it will say we’re unauthenticated, we’ll get some error from the API server or certificate, or something like that. So how do we debug issues with kubectl? The best I’ve found of doing that is to increase the verbosity. I’m gonna run the same command again, I’m just gonna do v tanbn[inaudible 00:06:59], so verbosity tan blogging. Just a little warning this output a whole bunch of stuff to the screen. This is gonna be a bit unreadable to begin with. I’m just gonna go through it really quickly.
If you run this, we get the same output at the bottom because it’s worked okay. But if we scroll up to the top we can see this is actually outputting all of the rest API cores that kubectl is doing to the API server, we can see if it’s bringing anything back from our cache, on disk, we can see all of the authentication pieces it’s doing. If you get any issues with kubectl, if you get errors coming back from our cluster you can run with a really high verbosity logging level on kubectl, and you can get a whole bunch of extra information. We can even see the actual request that it’s making to the API server here.
I hope this video’s been really useful in giving you extra tips and tricks to be more productive with kubectl and definitely check out those tools I’ve recommended. The links will be in the references for this video. Thanks for joining us and we’ll see in another video.
Have questions about the material in this lesson?
We’ve got answers!
Post your questions in the Kubernetes community Slack. Questions about this lesson are best suited for the #kubernetes-users channel.
Not yet a part of the Kubernetes Slack community? Join the discussion here.
Have feedback about this course or lesson? We want to hear it!
Send your thoughts to KubeAcademy@VMware.com.