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.
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
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
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
Want even more of these? Visit this kubectl-aliases GitHub repository 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 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.
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/
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, 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 :
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 :
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 π»
Lets connect and chat! Open to anything under the sun ππΉ
π¦ Twitter : devangtomar7
π LinkedIn : devangtomar
π Stackoverflow : devangtomar
πΌ Instagram : be_ayushmann
Medium : Devang Tomar
Hashnode : devangtomar
π§π» Dev.to : devangtomar
Top comments (0)