Skip to content

GitHub repo created for medium article : "These command line tricks will help you expedite your Kubernetes tasks 🏃🏻‍♂️💨"

Notifications You must be signed in to change notification settings

devangtomar/medium-k8s-tips-and-tricks

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 

Repository files navigation

These command line tricks will help you expedite your Kubernetes tasks 🏃🏻‍♂️💨

The most crucial Kubernetes command-line tool, kubectl, enables you to execute commands against clusters. Applications may be deployed, cluster resources can be inspected and managed, and logs can also be seen using kubectl. 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.

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

https://github.com/devangtomar/medium-k8s-tips-and-tricks

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 won't have to repeat the complete command every time.

Here's how you create aliases on your machine : https://phoenixnap.com/kb/linux-alias-command

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.

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.

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

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

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 you're done. ⏩

3. Using autocomplete 🤖

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

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

You must first configure bash autocompletion before you can enable kubectl autocompletion. This is really helpful if your aliases aren't 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/

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.

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

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.

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

Let's now create the manifest for the pod using a dry-run :

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

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

As you can see, the pod's spec.containers section contains the resources section. Now let's do kubectl explain :

[https://gist.github.com/devangtomar/2efca01ff8330a9f88193751e3234628] (https://gist.github.com/devangtomar/2efca01ff8330a9f88193751e3234628)

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 🎼🏃🏻

Courtesy : https://intellipaat.com/blog/tutorial/devops-tutorial/kubernetes-cheat-sheet/

Courtesy : https://www.upgrad.com/blog/kubernetes-cheat-sheet/

Courtesy : 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.

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

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

Kubernetes for Dummies, yes literally! ⚓ *Kubernetes, an often coupled technology with Docker, was something I wanted to write about after receiving a lot of...*devangtomar.medium.com

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

Colima (Containers on Linux on Mac) 🐋 What is Colima? 🤔devangtomar.medium.com

Podman (An alternative to Docker !?!) 🦭 What is Podman? 🤔devangtomar.medium.com

GitHub URL for this article 💻

[https://github.com/devangtomar/medium-k8s-tips-and-tricks] (https://github.com/devangtomar/medium-k8s-tips-and-tricks)

Let's connect and chat! Open to anything under the sun 🏖️🍹

🐦 Twitter : devangtomar7 🔗 LinkedIn : devangtomar 📚 Stackoverflow : devangtomar 🖼️ Instagram : be_ayushmann Ⓜ️ Medium : Devang TomarHashnode : devangtomar 🧑‍💻 Dev.to : devangtomar

About

GitHub repo created for medium article : "These command line tricks will help you expedite your Kubernetes tasks 🏃🏻‍♂️💨"

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages