Kubernetes Shell Tricks
When working with Kubernetes, it allows users to deploy cloud-based native applications that can be managed from any location on physical and virtual infrastructure. Kubernetes has the potential to be the platform that can meet the scaling requirements of any potential organization.
This article will take you through the tips and tricks that one can keep up their sleeves to fasten the development process in Kubernetes shells—also explaining why it is important and why Kubernetes manages to be a step ahead in the DevOps team. The goal is to ensure that your daily work with Kubernetes is not only efficient but more interactive.
Explained below are the tips and tricks to tackle Kubernetes.
Set default namespaces command
When using a formal code structure that has a cluster that goes into multiple environments by using namespaces, where each of them can be managed by a different user. This forces users to add a namespace command on each command line. This process is exhausting and repetitive. People tend to make mistakes by forgetting to add them or creating objects with the wrong namespace.
To avoid this instance, you can set a namespace preference beforehand. Run the following:
kubectl config set-context $(kubectl config current- context) - - namespace=mynamespace
It will execute and save the namespace for all upcoming commands in the same context.
Other common case commands include:
kubectl get po -o wide -n <namespace1> -n <namespace2> - n <namespace3>
To ensure that the namespace was correctly implemented use:
kubectl config view --minify | grep namespace
The completion command is a shell feature that is used with the help of the complete script. This completion script functions based on the inclination of the command. Kubectl is used to implement command scripts with bash and zsh. To ensure smooth functionality and flexibility in Kubernetes, users should add the autocomplete commands to increase efficiency and fasten the overall development process.
“source <(kubectl completion bash)” ~/. bashrc
Use this command if using bash, so that whenever a shell is initiated the autocompletion command will enable it. Most viable for long entries in Kubernetes.
The completion script for Zsh is not dependent on any sourced link, simply install the setup so that the data can be directly sourced to the shell sessions.
source <(kubectl completion zsh)
In case of an error like -
command not found: compdef
enable the compdef by adding the following lines at the start of ~/.zshrc file
autoload -Uz compinit
Cleaning docker images
It is quite essential to always clean docker images, as it may affect functionality when the capacity reaches 90%. When Kubernetes is launched, the garbage collection process commences where a flag needs to be set for the kubelet.
If not set, it can create an issue for the operators you can have docker using only 50% of the disk space but still occupying all inodes, thus it is made sure that the flag of kubelet for users is set for versions below 1.7 using the following command.
Use Shell efficiently
There is no need to use separate tools to change and moderate the context and the namespaces. Because, kubectl provides definitive commands that can offer the same functionalities, the command provides a feature to edit them
Here are some of them
· kubectl config use-context: change the current context
· kubectl config current-context: get the current context
· kubectl config get-contexts: list all contexts
· kubectl config set-context: change an element of a context
Now, it is hard to execute the above commands as they are long, the efficient way is to club them into a shell and execute them simultaneously.
To install these aliases make sure that you go through the definitions of ~/.bashrc or ~/.zshrc and reload your shell.
Kubernetes allows the usage of plugins and installing it makes it easy to use the native commands. To streamline functions to a much greater level users can create their plugins and upload them into the mainframe.
To install a plugin, you just have to copy the kubectl-x file to any directory in your path and make it executable (for example, with chmod +x). Immediately after that, one can invoke the plugin with kubectl x.
These plugins can be written in any programming language. The higher advantage being if used in shell scripts it can be easily invoked from kubectl. You can also search for other plugins that can be used to fasten the development stage.
Utilize these tips and tricks to work with ease on Kubernetes. To get more acquainted with Kubernetes ensure to learn the basics and gain additional insights by referring to additional information.
Want to get a real hands-on real-life Kubernetes architecture experience? Take advantage of the E2E Networks and get the best structured and streamlined configuration process for your needs.