Command Line Tips
This lesson covers key topics around optimizing the command line for the CKA/D exams. It covers a lot of useful tips for kubectl and the shell, all of which can save you precious time during the exam.
Hi, my name is Olive Power, and I’m a Cloud-Native Architect at VMware. Today we’re going to talk about the Kubernetes CKA exam. It stands for Certified Kubernetes Administrator. And in this lesson we’re going to talk about command line tips that are built for speed which will simply save you time in the exam. So we’re going to cover three sections today. We’re going to cover some editors setups, and in particular we’re going to talk about Vi, as that’s readily available in the exam.
We’re also going to talk about some shell shortcuts that will allow you to run some commands quicker. Again, saving you some time. And we’re also going to talk about kubectl commands themselves and how best to run them quickly and to get the information you need to run the correct kubectl command that you need to answer the question. Okay, so let’s navigate over to our terminal window.
Let me go here and what we’re going to do is we’re going to create a YAML file, and then we’re going to navigate our way around in Vi. So you can create the file from scratch. If you know the manifest instances for the resource that you’re trying to create, but time is precious in the exam. So if you have some sample YAMLs that you know where to grab, remembering that the Kubernetes documentation pages are available to you in the exam, then I recommend doing that. That’s kind of what I’d do.
So if we go to the documentation page, as I mentioned, this is a really useful resource. So if you’re looking for a sample YAML file, and if you know your way around this documentation, you can find one pretty quickly for most of the purposes that you need. And if you have pretty much 50% of what’s in the YAML file and you can edit it, then that’s a really good place to start rather than starting from scratch.
So for example, if we wanted a sample YAML file for a persistent volume for storage for a pod, then we can navigate to this area here under tasks. And here we have a sample YAML file here that we’re going to use. So let’s just grab that. Go back to our terminal window, and if we pipe that out to a file, we’ll just call it XXX so we know what it is.
I’m going to pipe and paste the content in, mark the end of the file, and now we should have a file called XXX. And if we Vi that, Oh, okay. We put a yml file extension on the end of it and here we have our YAML file. So again, it looks good, but navigating your way around through Vi commands will save you some time. So a few simple ones like G, which would get you to the end of the page, GG, back to the top of the page. And certain commands will work better for some people than others. So try and figure out which ones that really work for you and get you around the file as quickly as possible.
You will need to add stuff and edit stuff in the files during the exam so quickly being able to maybe insert a line after a particular space in the file or even deleting files, deleting lines at a particular place in the file. Then that will again save you some time. So well worth knowing your way around there.
Okay, so good. So next thing we’re going to talk about is the shell shortcuts. Okay. So I mentioned already that we’re going to run a lot of kubectl commands. So that’s a given. Anything you do with Kubernetes involves kubectl. So how best to run this quicker? So for example, if I do kubectl get pods or my current cluster, see I’ve got a list of pods there. Now instead of typing all that out all the time, I can make an alias. So K equals kubectl. So that alias does is mean instead of typing kubectl, I can just type K get pods. And again, I get the same information back, but I’ve saved a little bit of time. And that’s going to be really useful for us as these commands build up and we’re running, 10, 20, 30 of these commands.
Okay. But you don’t have to stop there, right? Because aliases are super powerful in this context. So you can run stuff like, let me clear our screen, if we go stuff like alias KGP, and we’re going to alias the kubectl get pods command in its entirety. So now we can just do KGP and we get the same information back, information on all the pods in our cluster. That’s awesome. Okay. So that has saved us a lot of time, especially if you’re going to run that a lot. So I would make aliases for the things you run all the time. So k get pods, k get nodes, k get deployments, and so on and so on for whatever ones you need. It doesn’t take you long to set them up at the start of the exam, but it will really save time as you go along.
Okay. So the last thing we’re going to look at is kubectl commands themselves and some key things that you can run that will help you save time. So there’s a lot of information in the help on kubectl you may not be aware of. But if we look at this [inaudible 00:04:34], so if I go k run minus minus H, a lot of information on what kubectl needs and what kind of commands it can run. So this is a very useful piece of information for you to use. So rather than go into the Kubernetes documentation, that webpage that’s available to you during the exam, you might be able to get all the information you need purely from the command line.
So for example, if you really wanted some information on the command to run in order to answer a particular question, then if you grab it for kubectl, you can output some of the exact syntaxes of the kubectl command lines that you want to run. So rather than trying to remember it and try and find an example maybe on the Kubernetes documentation website, as I mentioned before, you can scroll here in the terminal and just find the exact image that you need. And the exact command line that you need.
Now another useful tip, and this is really super useful as well, again, if we grab something like, we want to run something and grep it for the word restart. And what’s very interesting here is that you can see that there’s three different values for the restart option on a kubectl command. You see the restart always is the default. This is the restart policy here. And there’s three values always, on failure, and never. And it tells you here in the help if it’s to always, a deployment is created. Set to on failure, a job is created. And if it’s set to never, a regular pod is created.
So this is really useful, right? So this means that the same command line just with a different switch for the restart parameter will create you three very different resources. So let’s just take that a little bit further. So let’s just show you this on the command line. So let’s just clear this. And if we take one of the ones that was taught to us, okay, so like restart never is an option, and that will create us a pod. So let’s just run that because the option is never is pod. So if we go KGP, we should be able to see our OP test pod. Excellent.
And this is really useful because it’s created me a pod called the exact name that I want to do. If I didn’t specify any parameter for restart, because it said always was the default. So in reality, the restart parameter is always, this is actually creating me a deployment. And obviously the other value is restart on failure, which will create you a job. So you will be asked to create these three different resources in real life all the time. And it’s just really useful to know that with one flick of an option, of restart, you can create those three different resources.
So there you go. We’ve come to the end of our set up Kubernetes on command line tips and tricks. I hope for good look on the exam, and thank you for joining today’s session.
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 #cka-exam-prep or #ckad-exam-prep channels.
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.