Step 1: How to install and configure kubectl tool in Kubernetes
Last updated
Last updated
Address
VNG CorporationThe command-line tool in Kubernetes, kubectl, allows you to execute commands in Kubernetes clusters. You can use kubectl to deploy applications, monitor and manage cluster resources, and view logs.
You need to use a kubectl version that is no more than one version wrong with the cluster version. For example, a v1.2 client should work with master v1.1, v1.2 and v1.3. Using the latest version of kubectl helps to avoid unforeseen problems.
Download the latest version with the command:
curl -LO https://storage.googleapis.com/kubernetes-release/release/`curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt`/bin/linux/amd64/kubectl
To download a specific version, replace the $(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt) part of the command with a specific version.
For example, to download v1.17.0 on Linux, enter the following:
curl -LO https://
storage
.googleapis.com/kubernetes-release/release/v1.17.0/bin/linux/amd64/kubectl
Make the kubectl binary executable: chmod +x ./kubectl
Include the binary in your PATH environment: variable.sudo mv ./kubectl /usr/local/bin/kubectl
Make sure that the version you have installed is the latest: kubectl version
Ubuntu, Debian or HypriotOS
CentOS, RHEL or Fedora
sudo apt-get update && sudo apt-get install -y apt-transport-https curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add - echo "deb https://apt.kubernetes.io/ kubernetes-xenial main" | sudo tee -a /etc/apt/sources.list.d/kubernetes.list sudo apt-get update sudo apt-get install -y kubectl
If you are using Ubuntu or another Linux distro that supports snap package manager, then kubectl is already available in snap.
Switch to user snap and execute the install command:
sudo snap install kubectl --classic
Check that the version you just installed is the latest:
kubectl version
Download the latest version: curl -LO "https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/darwin/ amd64/kubectl" To download a specific version, replace the $(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt) part of the command with the specific version. For example, to download v1.17.0 on macOS, type: curl -LO https://storage.googleapis.com/kubernetes-release/releases/v1.17.0/bin/darwin/amd64/kubectl
Make the kubectl binary executable: chmod +x ./kubectl
Include the binary in your PATH environment variable. sudo mv ./kubectl /usr/local/bin/kubectl
Make sure that the version you have installed is the latest: kubectl version
If you are on macOS and using the Homebrew package manager, you can install kubectl with Homebrew.
Run the install command:
brew install kubectl
or
brew install kubernetes-cli
Make sure that the version you have installed is the latest:
kubectl version
If you are on macOS and use the Macports package manager, you can install kubectl with Macports.
Run the install command:
sudo port self-update
sudo port install kubectl
Make sure that the version you have installed is the latest:
kubectl version
Download the latest version v1.17.0 from this link. Or if you have curl installed, use the following command:
curl -LO
https://storage.googleapis.com/kubernetes-release/releases/v1.17.0/bin/windows/amd64/kubectl.exe
To find out the latest stable version, see https://storage.googleapis.com/kubernetes-release/releases/stable.txt.
Include the binary in your PATH environment variable.
Check that the kubectl version is the same as the downloaded one:
kubectl version
Note: Docker Desktop for Windows adds its own version of kubectl to PATH. If you have installed Docker Desktop before, you may need to set your PATH before the Docker Desktop installation adds a PATH to or removes Docker Desktop's kubectl.
If you are on Windows and using the package manager Powershell Gallery, you can install and update kubectl with Powershell.
Execute the following installation commands (make sure you define DownloadLocation yourself):
Install-Script -Name install-kubectl -Scope CurrentUser -Force
install-kubectl.ps1 [-DownloadLocation <path>]
Note: If you do not define DownloadLocation, kubectl will be installed in the user's temp directory.
The installation will generate $HOME/.kube
and instructions for creating the configuration file
Make sure that the version you have installed is the latest:
kubectl version
Note: Update of the installation will be performed when rerunning the commands from step 1.
To install kubectl on Windows you can use the package manager Chocolatey or the command installer Scoop.
choco
choco install kubernetes-cli
scoop scoop install kubectl
Make sure that the version you have installed is the latest:
kubectl version
Navigate to your home directory:
cd %USERPROFILE%
Create folder .kube:
mkdir .kube
Navigate to the .kube folder you just created:
cd .kube
Configure kubectl to use a remote Kubernetes cluster:
New-Item config -type file
Note: Edit the configuration file with a text editor, such as Notepad.
You can install kubectl from part of the Google Cloud SDK.
Install Google Cloud SDK.
Execute the kubectl install command:
gcloud components install kubectl
Make sure that the version you have installed is the latest:
kubectl version
In order for kubectl to find and access the Kubernetes cluster, it needs a kubeconfig file, which is automatically created when you create a new cluster using kube-up.sh or successfully deploy a Minikube cluster. By default, kubectl's configuration is located at ~/.kube/config.
Check that kubectl is properly configured by viewing the state of the cluster:
kubectl cluster-info
If you see a response URL, then kubectl is properly configured to access your cluster.
If you see a message similar to the one below, kuberctl is not configured correctly or cannot connect to the Kubernetes cluster. The connection to the server <server-name:port> was refused - did you specify the right host or port?
For example, if you are going to run a Kubernetes cluster on your laptop (locally), you will need a tool like Minikube installed previously and run the commands above again.
If kubectl cluster-info returns the url but you cannot access your cluster, then check it is configured correctly by:
kubectl cluster-info dump
kubectl provides autocompletion support for Bash and Zsh, reducing the need to type multiple commands.
Below are the steps to set up autocompletion for Bash (including the differences between Linux and macOS) and Zsh.
The kubelet completion script for Bash is generated with the kubectl completion bash command. After the script is generated, you need to source (execute) the script to enable autocompletion.
However, completion script depends on bash-completion, so you must install bash-completion first (check bash-completion exists with type _init_completion statement).
bash-completion is provided by many package managers (see here). You can install with apt-get install bash-completion
or yum install bash-completion
command.
The above commands create /usr/share/bash-completion/bash_completion
, which is the main bash-completion script. Depending on your package manager, you must source (execute) this file in the file ~/.bashrc
.
To find this file, reload your current shell and run the command type _init_completion
. If successful, you're all set, otherwise add the following to your ~/.bashrc
file:
source /usr/share/bash-completion/bash_completion
Reload your shell and confirm bash-completion is properly installed using the type _init_completion
command.
Now you need to make sure that the kubectl completion script is sourced across all shell sessions. There are 2 ways to do this:
Source script in file ~/.bashrc: echo 'source <(kubectl completion bash)' >>~/.bashrc
Add the script to the /etc/bash_completion.d directory: kubectl completion bash >/etc/bash_completion.d/kubectl
If you have an alias for kubectl, you can add another shell completion for that alias: echo 'alias k=kubectl' >>~/.bashrc echo 'complete -F __start_kubectl k' >>~/.bashrc
Note: bash-completion sources all completion scripts in /etc/bash_completion.d.
The above methods are equally effective. After reloading the shell, kubectl autocompletion should work.
The Kubectl completion script for Zsh is generated with the kubectl completion zsh command. The source completion script in your shell will trigger kubectl autocompletion.
To make it work for all shells, add the following line to your ~/.zshrc
file:
source <(kubectl completion zsh)
If you have an alias for kubectl, you can extend shell completion to work with that alias:
echo 'alias k=kubectl' >>~/.zshrc
echo 'complete -F __start_kubectl k' >>~/.zshrc
After reloading the shell, kubectl autocompletion should work.
If you get the error complete:13: command not found: compdef
, add the following line at the top of your ~/.zshrc
file:
autoload -Uz compinit
compinit
Source: kubernetes.io