Instructions for installing and configuring the kubectl in Kubenetes
Necessary conditions
You need to use a kubectl version that is no more than one version different from the cluster version. For example, a client v1.2 should work with master v1.1, v1.2 and v1.3. Using the latest version of kubectl helps avoid unforeseen problems.
Install kubectl on Linux
Install kubectl binary with curl on Linux
Step 1: Download the latest version with the command:
To download a specific version, replace the section in the command with a specific version.$(curl -s https://
storage
.
googleapis.com/kubernetes-release/release/stable.txt
)
For example, to download version v1.17.0 on Linux, run the command:
Step 2: Create kubectl binary executable via command:
Step 3: Put the binary into your PATH environment variable via the command:
Step 4: Check to make sure that the version you installed is the latest via command:
Install kubectl using the package manager
With CentOS, RHEL or Fedora operating systems, you can run the command:
Install kubectl with snap
If you are using Ubuntu or another Linux distro that supports the snap package manager , kubectl is already available in snap .
Step 1: Switch to user snap and execute the installation command:
Step 2: Check the version you just installed is the latest:
Install kubectl on macOS
Install kubectl binary with curl on macOS
Step 1: Download the latest version:
To download a specific version, replace the section in the command with the specific version. For example, to download v1.17.0 on macOS, run the command:$(curl -s
https://storage.googleapis.com/kubernetes-release/release/stable.txt
)
Step 2: Create kubectl binary executable via command:
Step 3: Put the binary into your PATH environment variable via the command:
Step 4: Check to make sure that the version you installed is the latest via command:
Install with Homebrew on macOS
If you are on macOS and using the Homebrew package manager , you can install kubectl with Homebrew.
Step 1: Run the installation command:
or command:
Step 2: Check to make sure the version you installed is the latest:
Install with Macports on macOS
If you are on macOS and using the Macports package manager , you can install kubectl with Macports.
Step 1: Run the installation command:
Step 2: Check to make sure the version you installed is the latest:
Install kubectl on Windows
Install kubectl binary with curl on Windows
Step 1: Download the latest version v1.17.0 from this link . Or if you have already installed it curl
, use the following command:
To find out the latest stable version, see https://storage.googleapis.com/kubernetes-release/release/stable.txt .
Step 2: Include the binary in your PATH environment variable.
Step 3: Check to make sure the version kubectl
is the same as the downloaded version:
Note: Docker Desktop for Windows
kubectl
adds its own version to the PATH. If you have previously installed Docker Desktop, you may need to set your PATH before the Docker Desktop installation adds a PATH to or removeskubectl
Docker Desktop's.
Install with Powershell from PSGallery
If you are on Windows and using the Powershell Gallery package manager , you can install and update kubectl with Powershell.
Step 1: Execute the following installation commands (make sure you define your own DownloadLocation
):
Note: If you do not define it
DownloadLocation
,kubectl
it will be installed in the user's temp directory.
The installation will generate $HOME/.kube
and instruct you to create a configuration file
Step 1: Check to make sure the version you installed is the latest:
Note: Installation updates will be performed when re-running the commands from step 1.
Install on Windows using Chocolatey or Scoop
Step 1: To install kubectl on Windows you can use the Chocolatey package manager or the Scoop command installer .
If you use Choco
If you use Scoop
Step 2: Check to make sure the version you installed is the latest:
Step 3: Move to your home directory:
Step 4: Create folder .kube
:
Step 5: Move to the folder .kube
you just created:
Step 6: Configure kubectl to use a remote Kubernetes cluster:
Note: Edit the configuration file with a text editor, such as Notepad.
Download from part of the Google Cloud SDK
You can install kubectl from part of the Google Cloud SDK.
Step 1: Install Google Cloud SDK .
Step 2: Execute the installation command kubectl
:
Step 3: Check to make sure the version you installed is the latest:
Verify kubectl configuration
For kubectl to search and access your 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 defined at
~/.kube/config
.Check kubectl is configured correctly by viewing the cluster status: kubectl cluster-info
If you see a response URL, 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 planning 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 if it is configured correctly, by:
kubectl configuration options
Enable shell autocompletion
kubectl provides autocompletion support for Bash and Zsh, helping you reduce the need to type many commands.
Below are the steps to set up autocompletion for Bash (including the differences between Linux and macOS) and Zsh.
Bash on Linux
Introduce
Kubelet completion script for Bash is generated with the command . After the script is created, you need to source (execute) the script to enable the autocompletion feature.
kubectl completion bash
However, the completion script depends on bash-completion , so you must install bash-completion beforehand (check bash-completion exists with the command
type _init_completion
).
Install bash-completion
bash-completion is provided by many package managers (see here ) . You can install with command
apt-get install bash-completion
oryum install bash-completion
.The above commands generate
/usr/share/bash-completion/bash_completion
, which is the main script of bash-completion. Depending on your package manager, you may have to source this file in a~/.bashrc
.To find this file, reload your current shell and run the
type _init_completion
. If successful, you are done setting up, otherwise add the following to~/.bashrc
your file:
Reload your shell and confirm that bash-completion is installed correctly with the command
type _init_completion
.
Enable kubectl autocompletion
Now you need to ensure that the kubectl completion script is sourced across all shell sessions. There are 2 ways to do this:
Source script in file
~/.bashrc
:
Add script to folder
/etc/bash_completion.d
:
If you have an alias for kubectl, you can add another shell completion to that alias:
Copy
Note: bash-completion sources all completion scripts in
/etc/bash_completion.d
.
The above methods are equally effective. After reloading the shell, kubectl autocompletion will work.
Bash on macOS
Introduce
Kubectl completion script on Bash is generated by kubectl completion bash
. This source script will enable the kubectl completion feature.
However, the kubectl completion script depends on the bash-completion you installed earlier.
Warning: There are two versions of bash-completion, v1 and v2. V1 is for Bash 3.2 (default Bash on macOS), and v2 is for Bash 4.1+. Kubectl completion script does not work properly with bash-completion v1 and Bash 3.2. It is compatible with bash-completion v2 and Bash 4.1+ . Therefore, to use kubectl completion correctly on macOS, you must install Bash 4.1+ ( instructions ). The instructions that follow assume that you are using Bash 4.1+ (that is, any Bash version 4.1 or later).
Install bash-completion
Note: As mentioned, these instructions assume you are using Bash 4.1+, which means that you will be installing bash-completion v2 (as opposed to Bash 3.2 and bash-completion v1, in which case, kubectl completion will not work).
You can check if bash-completion v2 was previously installed with the command
type _init_completion
. If not, you can install it with Homebrew:
From the output of this command, add the following to
~/.bashrc
your file: export BASH_COMPLETION_COMPAT_DIR="/usr/local/etc/bash_completion.d"
Reload your shell and verify that bash-completion v2 is installed correctly using the type _init_completion
.
Enable kubectl autocompletion
Now you must ensure that the kubectl completion script has been sourced in all your shell sessions. There are many ways to achieve this:
Source completion script in file
~/.bashrc
:
Add completion script to the folder
/usr/local/etc/bash_completion.d
:
If you have an alias for kubectl, you can extend the completion shell to work with that alias:
Copy
If you have installed kubectl with Homebrew (as introduced above ) then the kubectl completion script will be included in the
/usr/local/etc/bash_completion.d/kubectl
. In this case, you don't need to do anything.
Note: Installing the Homebrew way already sources all the files in the
BASH_COMPLETION_COMPAT_DIR
, which is why the following two methods work.
In any case, after reloading your shell, kubectl completion should work.
Zsh
Kubectl completion script for Zsh is generated with the command
kubectl completion zsh
. Source completion script in your shell will enable kubectl autocompletion. To make it work for all shells, add the following line to the file~/.zshrc
:
If you have an alias for kubectl, you can extend the completion shell to work with that alias:
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 beginning of the file
~/.zshrc
:
For more details, see kubernetes.io
Last updated