These command line tricks will help you expedite your Kubernetes tasks 🏃🏻‍♂️💨
Published: 2022-11-01 17:25:49

---
title: These command line tricks will help you expedite your Kubernetes tasks 🏃🏻‍♂️💨
published: true
date: 2022-11-01 09:00:04 UTC
tags: 
canonical_url: https://devangtomar.hashnode.dev/these-command-line-tricks-will-help-you-expedite-your-kubernetes-tasks-efb88f-9db8cf1049d5
---

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1667322635431/vdm6rEeIf.jpeg)

The most crucial Kubernetes command-line tool, `kubectl`, enables you to execute commands against clusters. You control Kubernetes using `Kubectl`, which is the primary command-line tool for Kubernetes. `Kubectl` is best thought of as SSH for Kubernetes, which is a useful analogy. It is offered for Windows, Mac, and Linux.

In general, `kubectl` transforms simple commands into the JSON payload needed by the API server. To determine the cluster and API server endpoint to POST to, it needs a configuration file.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1667322637082/cJiCiV3Rh.jpeg)

In this article, were going to go through some quick and easy tips & tricks that will help saving valuable time and making the most out of `kubectl`. If youre still new to Kubernetes, you might want to go through my previous article, which introduces you to k8s concepts etc.

[https://devangtomar.medium.com/kubernetes-for-dummies-yes-literally-2734cf1a229](https://devangtomar.medium.com/kubernetes-for-dummies-yes-literally-2734cf1a229)

#### 1. Make use of aliases 🖇

Setting up some aliases for running `kubectl` is really helpful because Kubernetes commands can be very long. When you wish to run several Kubernetes commands at once, it will be much simpler because you wont have to repeat the complete command every time.

Heres how you create aliases on your machine : [https://phoenixnap.com/kb/linux-alias-command](https://phoenixnap.com/kb/linux-alias-command)

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1667322638473/JZukBoPS4.jpeg)

Aliases for a few regularly used commands are shown below. To save time, run these before executing `kubectl` instructions. Instead of typing `kubectl` , you just need to type `k` :

[https://gist.github.com/devangtomar/7c71346c1a60a9c37561851e52f0caa0](https://gist.github.com/devangtomar/7c71346c1a60a9c37561851e52f0caa0)

Want even more of these? Visit this [kubectl-aliases GitHub repository](https://github.com/ahmetb/kubectl-aliases) which is a true haven for fans of aliases.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1667322640282/hB5Kb0g9q.jpeg)

> **Note :** Aliases can be dangerous at times, please use wisely.

#### 2. Dry run like a pro 😎

The `--dry-run` flag of the kubectl run command (as well as create, apply, and patch) is a fantastic feature that lets you see the anticipated changes without actually executing them.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1667322641717/O7uZiuPcB.jpeg)

This command outputs the manifest of the needed object when used with `-o yaml`

For instance :

`kubectl --dry-run=client -o yaml run alpine --image=alpine > alpine.yaml`

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1667322643837/PzAy_q2oH.png)

this will produce the following YAML (alpine.yaml) :

[https://gist.github.com/devangtomar/4b41cddeacfe13d247d24b3dae55c1d9](https://gist.github.com/devangtomar/4b41cddeacfe13d247d24b3dae55c1d9)

Now all you have to do is save it to a file, remove a few system or superfluous fields, and youre done.

#### 3. Using autocomplete 🤖

In Kubernetes, you can autocomplete fields! Even though setting this up takes around five minutes, its worth the first investment.

How many times have you tried to type `kubectl` commands while pressing the TAB key before realizing it doesnt work? Heres a trick, though. A bash autocomplete plug-in that you install in your `.bashrc` file will function flawlessly.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1667322646615/5egSzqk5J.png)

You must first configure bash autocompletion before you can enable `kubectl` autocompletion. This is really helpful if your aliases arent enough or if writing out the entire command would make you too lazy.

Use the following command to do that :

`echo "source <(kubectl completion bash)" >> ~/.bashrc`

By merely pressing the TAB key, you can now autocomplete commands, which is quite useful and saves a tonne of time.

> **Note :** Your operating system will have some influence over the instructions.
> 
> **_MacOS :_** [_https://kubernetes.io/docs/tasks/tools/included/optional-kubectl-configs-bash-mac/_](https://kubernetes.io/docs/tasks/tools/included/optional-kubectl-configs-bash-mac/)
> 
> **_Linux :_** [_https://kubernetes.io/docs/tasks/tools/included/optional-kubectl-configs-bash-linux/_](https://kubernetes.io/docs/tasks/tools/included/optional-kubectl-configs-bash-linux/)

#### 4. Setting default namespaces 🚀🛰

`Kubectl` is one of the most crucial sets of Kubernetes commands. It is simple, adaptable, and highly effective. But `kubectl` has one significant drawback. To define where you want to construct your pods, services, or deployments, you must always use the option `--namespace`.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1667322648245/JZazHEwdb.jpeg)

If you choose not to use this option, your objects will probably end up in the wrong location.

The following command can be used to avoid this :

`kubectl config set-context $(kubectl config current-context) --namespace=yournamespace`

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1667322650437/Vq-GIc0K4.png)

> **Note :** Replace `yournamespace`in the above command to replace with your desired namespace

#### 5. Using kubectl explain 📚

Instead of repeatedly visiting the online documentation, use `kubectl` explain. It is simple to comprehend and gives you sufficient details about a resource standard.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1667322651975/ljkNj9REc.jpeg)

For illustration, lets use `kubectl dry-run` to build a pod and then `kubectl explain` to learn how to add resource requests and limits to the pod.

Lets now create the manifest for the pod using a dry-run :

`kubectl --dry-run=client -o yaml run alpine --image=alpine`

As you can see, the pods `spec.containers` section contains the resources section. Now lets do `kubectl` explain :

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1667322654804/FHg9UmAZc.png)

That should be sufficient information to get you going. To examine potential values and pertinent information, see the `dnsPolicy` and `restartPolicy` specifications.

`kubectl explain pod.spec.dnsPolicy  
kubectl explain pod.spec.restartPolicy`

#### 6. Using these kubectl cheatsheets 🎼🏃🏻

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1667322657719/MrPaLbHoY.png)

> **Courtesy :** [https://intellipaat.com/blog/tutorial/devops-tutorial/kubernetes-cheat-sheet/](https://intellipaat.com/blog/tutorial/devops-tutorial/kubernetes-cheat-sheet/)

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1667322661372/2-u6-d5uw.png)

> **Courtesy :** [https://www.upgrad.com/blog/kubernetes-cheat-sheet/](https://www.upgrad.com/blog/kubernetes-cheat-sheet/)

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1667322663715/YH58rg81z.jpeg)

> **Courtesy :** [https://acloudguru.com/](https://acloudguru.com/)

#### Conclusion 🤔

These pointers can really help you advance in your Kubernetes profession and have a great Kubernetes experience. You are free to employ whatever makes your task simpler and to come up with your own creative strategies for doing so.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1667322666010/3nwLQBMaW.jpeg)

Gratitude for reading. The article was enjoyable, I hope.

#### **Here are some related interesting stories that you might find helpful :**

[ **Docker for rookies 🐳**  
_One of those services you may have never used but always hear about is Docker. Prior to my investigation into the_devangtomar.medium.com](https://devangtomar.medium.com/how-to-get-started-with-docker-b2d924cbe9bb "https://devangtomar.medium.com/how-to-get-started-with-docker-b2d924cbe9bb")

[**Colima (Containers on Linux on Mac) 🐋**  
_What is Colima? 🤔_devangtomar.medium.com](https://devangtomar.medium.com/colima-containers-on-linux-on-mac-f6396c27e39b "https://devangtomar.medium.com/colima-containers-on-linux-on-mac-f6396c27e39b")

[**Podman (An alternative to Docker !?!) 🦭**  
_What is Podman? 🤔_devangtomar.medium.com](https://devangtomar.medium.com/podman-an-alternative-to-docker-desktop-c30370edc98b "https://devangtomar.medium.com/podman-an-alternative-to-docker-desktop-c30370edc98b")

#### GitHub URL for this article 💻

[ **GitHub - devangtomar/medium-k8s-tips-and-tricks**  
_You can't perform that action at this time. You signed in with another tab or window. You signed out in another tab or_github.com](https://github.com/devangtomar/medium-k8s-tips-and-tricks "https://github.com/devangtomar/medium-k8s-tips-and-tricks")

#### Lets connect and chat! Open to anything under the sun 🏖🍹

**🐦 Twitter :** [devangtomar7](https://twitter.com/devangtomar7)  
**🔗 LinkedIn :** [devangtomar](https://www.linkedin.com/in/devangtomar)  
**📚 Stackoverflow :** [devangtomar](https://stackoverflow.com/users/8198097/devangtomar)  
**🖼 Instagram :** [be\_ayushmann](https://instagram.com/be_ayushmann)  
 **Medium :** [Devang Tomar](https://medium.com/u/8f5e1c86129d?source=post_page-----e42119a306ca--------------------------------)  
 **Hashnode :** [devangtomar](https://devangtomar.hashnode.dev/)  
**🧑💻 Dev.to :** [devangtomar](https://dev.to/devangtomar)