Setting up Environments with Autocompletion and Shortcuts
In most Linux environments, you can set up autocompletion for kubectl commands before you start working with the instructions mentioned in this chapter. Learning how autocompletion and shortcuts work in Linux environments will be significantly helpful for those who are interested in getting certifications such as Certified Kubernetes Administrator (CKA) and Certified Kubernetes Application Developer (CKAD), which are conferred by the Linux Foundation. We'll learn how to set up autocompletion in the following exercise.
Exercise 3.01: Setting up Autocompletion
In this exercise, we will show you how to set up autocompletion and an alias for kubectl commands in Bash. This is a useful feature that will help you save time and avoid typos. Perform the following steps to complete this exercise:
- We will need the bash-completion package, so install it if it is not already installed. You can go to the GitHub repository to get installation instructions for various platforms, at https://github.com/scop/bash-completion. If you are running Ubuntu 20.04, you can install it via the APT package manager using the following command:
sudo apt-get install bash-completion
- You can use the following command to set up autocomplete in Bash:
source <(kubectl completion bash)
Note
This command, as well as the subsequent commands in this exercise, will not show any responses in the terminal upon successful execution.
- If you want to make autocomplete persistent in your Bash shell, you can use the following command, which will write kubectl autocomplete to the .bashrc file in your current user directory:
echo "source <(kubectl completion bash)" >> ~/.bashrc
- You can also set up an alias for your kubectl commands by using the alias keyword, as follows:
alias k=kubectl
- Similarly, if you want to set up an alias for some specific commands, you can use commands similar to the following:
alias kcdp='kubectl describe po'
alias kcds='kubectl describe svc'
alias kcdd='kubectl describe deploy'
- Finally, you can use the following command to set up the completion of kubectl commands when you press Tab:
complete -F __start_kubectl k
Note
You can also to set up autocomplete in zsh (an alternative to the Bash shell) by using the following commands:
source <(kubectl completion zsh)
echo "if [ $commands[kubectl] ]; then source <(kubectl completion zsh); fi" >> ~/.zshrc
By the end of this exercise, you will have an autocomplete set up for your Bash shell. You can also use aliases such as k instead of kubectl in your commands. However, to avoid confusion and maintain a standardized structure, we will use the full commands throughout this book.
Setting up the kubeconfig Configuration File
In most enterprise environments, there is generally more than one Kubernetes cluster, depending on the strategy of the organization. An administrator, developer, or any other role dealing with Kubernetes clusters would need to interact with several of those clusters and switch between them to perform different operations on different clusters.
A configuration file makes things a lot easier. You can use this file to store information about different clusters, users, namespaces, and authentication mechanisms. Such configuration files are referred to as kubeconfig files. Note that kubeconfig is a generic way to refer to kubectl configuration files and that it is not the name of the config file. kubectl uses such files to store the information needed for us to choose a cluster and communicate with its API server.
By default, kubectl looks for the file in the $HOME/.kube directory. In most scenarios, you can specify a KUBECONFIG environment variable or use the --kubeconfig flag to specify the kubeconfig files. Those files are usually saved in $HOME/.kube/config.
Note
You can find out more about how to configure access to multiple clusters by setting up the KUBECONFIG environment variable and the --kubeconfig flag at https://kubernetes.io/docs/tasks/access-application-cluster/configure-access-multiple-clusters/#set-the-kubeconfig-environment-variable.
Security contexts are used to define the privilege and access control settings for the pods. We will revisit the idea of access control and security in Chapter 13, Runtime and Network Security in Kubernetes.
Let's take a look at the kubeconfig file to understand how this works. You can view the kubeconfig file using the following command:
kubectl config view
Alternatively, you can also use the following command:
cat $HOME/.kube/config
You should get an output similar to the following:
A context is a set of information that you need to access a cluster. It contains the name of the cluster, the user, and the namespace. The current-context field in Figure 3.2 shows the current context that you are working with. If you want to switch the current context, you can use the following command:
kubectl config use-context <the cluster you want to switch to>
For example, if we wanted to switch to a context named minikube, we would use the following command:
kubectl config use-context minikube
This would give an output similar to the following:
Switched to context "minikube".