kubectl
is a command-line utility for working with Kubernetes cluster entities. It allows you to manage cluster resources, deploy applications, view logs, and perform other administrative tasks.
Please note that the minor version of
kubectl
(the second digit in the version) may differ from the cluster version by +/- 1. Using newer or older versions may lead to compatibility issues. The instructions specify certain versions as examples, but you can replace them with the versions you need.
Kubernetes version currently available at Hostman: 1.26.15, 1.27.15, 1.28.11, 1.29.6, 1.30.2.
There are two methods for installing kubectl
on Linux.
Download the kubectl
binary:
curl -LO "https://storage.googleapis.com/kubernetes-release/release/v1.30.2/bin/linux/amd64/kubectl"
Check the integrity of the file:
echo "$(curl -sL https://dl.k8s.io/release/v1.30.2/bin/linux/amd64/kubectl.sha256) kubectl" | sha256sum --check
Install kubectl
:
sudo install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl
Update the package list:
sudo apt update
Install the required packages:
sudo apt install -y apt-transport-https ca-certificates curl gnupg
Add the key for the Kubernetes repository and set the correct permissions for the key:
curl -fsSL https://pkgs.k8s.io/core:/stable:/v1.30/deb/Release.key | sudo gpg --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg && sudo chmod 644 /etc/apt/keyrings/kubernetes-apt-keyring.gpg
Add the Kubernetes repository to the APT sources list and set the correct permissions on the sources list file:
echo 'deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.gpg] https://pkgs.k8s.io/core:/stable:/v1.30/deb/ /' | sudo tee /etc/apt/sources.list.d/kubernetes.list && sudo chmod 644 /etc/apt/sources.list.d/kubernetes.list
Update the package list again:
sudo apt update
Install kubectl
:
sudo apt install -y kubectl
For macOS, you can install kubectl
manually or with Homebrew.
Install kubectl
:
brew install [email protected]
Or install the entire Kubernetes CLI toolchain:
brew install [email protected]
The homebrew repositories currently only have versions 1.29, 1.30, and 1.31 available. Do not use this installation method for Kubernetes versions below 1.28.
For Intel
Download kubectl
for Intel:
curl -LO "https://dl.k8s.io/release/v1.30.2/bin/darwin/amd64/kubectl"
Check the integrity of the file:
echo "$(curl -sL https://dl.k8s.io/release/v1.30.2/bin/darwin/amd64/kubectl.sha256) kubectl" | sha256sum --check
For Apple Silicon
Download kubectl
for Apple Silicon:
curl -LO "https://dl.k8s.io/release/v1.30.2/bin/darwin/arm64/kubectl"
Check the integrity of the file:
echo "$(curl -sL https://dl.k8s.io/release/v1.30.2/bin/darwin/arm64/kubectl.sha256) kubectl" | sha256sum --check
Make the file executable, set the permissions to 644
, move it to the /usr/local/bin
directory and set the owner to root
:
chmod +x ./kubectl && chmod 644 ./kubectl && sudo mv ./kubectl /usr/local/bin/kubectl && sudo chown root: /usr/local/bin/kubectl
You can install kubectl
on Windows manually or with Chocolatey.
Install kubectl
by running:
choco install kubernetes-cli --version=1.30.2
Open PowerShell and run:
mkdir kubectl
cd .\kubectl\
curl.exe -LO "https://dl.k8s.io/release/v1.30.2/bin/windows/amd64/kubectl.exe"
Add the kubectl
directory path to your PATH
by running the following script in PowerShell:
$destination="$env:USERPROFILE\kubectl"
$currentPath = [System.Environment]::GetEnvironmentVariable("Path", [System.EnvironmentVariableTarget]::User)
if ($currentPath -notlike "*$destination*") {
[System.Environment]::SetEnvironmentVariable("Path", "$currentPath;$destination", [System.EnvironmentVariableTarget]::User)
}
Or add the path manually:
kubectl
directory.Restart PowerShell for the changes to take effect.
Docker Desktop for Windows adds its version of
kubectl
to thePATH
. If you have Docker Desktop installed, you may need to place thePATH
entry forkubectl
before the one added by the Docker Desktop installer, or remove the version ofkubectl
installed by Docker Desktop.
kubectl
is now installed on your machine. You can verify the installation by running the command:
kubectl version
To connect to the cluster, you will need the kubeconfig
configuration file.
The config.yaml
file is used by kubectl
to determine which cluster to connect to and what credentials to use. It contains information about clusters, users, and contexts, allowing for easy switching between different Kubernetes configurations.
Since the config.yaml
file contains important credentials, it should be stored in a location that is not accessible to unauthorized persons to prevent unauthorized access to the cluster. If an outsider gains access to this file, they can manage the cluster and its resources.
To obtain the configuration file, go to the cluster management page, and on the Dashboard tab, click the Download button.
Once you have the config.yaml
file, you need to place it in the correct directory so kubectl
can use it.
Create a .kube
directory in your home directory if it doesn't exist yet:
mkdir -p ~/.kube
Copy the config.yaml
file to this directory and rename it to config
:
cp ~/Downloads/config.yaml ~/.kube/config
After completing these steps, kubectl
will automatically use the configuration from the ~/.kube/config
file.
Open PowerShell.
Create a .kube
directory in your home directory if it doesn't already exist:
mkdir $env:USERPROFILE\.kube
Copy the config.yaml
file to this directory and rename it to config
:
copy <path_to_config.yaml> $env:USERPROFILE\.kube\config
Run PowerShell as administrator and execute:
[Environment]::SetEnvironmentVariable("KUBECONFIG", $HOME + "\.kube\config", [EnvironmentVariableTarget]::Machine)
After completing these steps, kubectl
will automatically use the configuration from the $env:USERPROFILE\.kube\config
file.
If you need to use multiple configuration files or place config.yaml
in a different location, you can use the KUBECONFIG
environment variable.
Open the terminal.
Set the KUBECONFIG
environment variable:
export KUBECONFIG=<path-to-your-kubeconfig>
To make these settings persist across system reboots, run the following code changing ~/.bashrc
to ~/.zshrc
(for MacOS) or ~/.profile
if necessary:
echo "export KUBECONFIG=<path-to-your-kubeconfig>" >> ~/.bashrc
Open PowerShell.
Set the KUBECONFIG
environment variable:
$env:KUBECONFIG = "<path-to-your-kubeconfig>"
You can specify multiple kubeconfig files by separating their paths with colons (on macOS and Linux) or semicolons (on Windows):
$env:KUBECONFIG="<path-to-your-first-kubeconfig>;<path-to-your-second-kubeconfig>"
export KUBECONFIG=<path-to-your-first-kubeconfig>:<path-to-your-second-kubeconfig>
Once you have access set up, you need to make sure that kubectl
can connect to the cluster. To do this, run the following command:
kubectl cluster-info
This command will output information about your cluster, including the addresses of the API and other components. If the command is successful, then kubectl
is connected to the cluster.
Contexts allow you to switch between different clusters and credentials. Each context is a combination of clusters, users, and namespaces.
To see a list of available contexts and which one is currently active, use the command:
kubectl config get-contexts
This command will output a table of available contexts and indicate which one is currently active.
If you have multiple clusters configured and want to switch to a different context, use the following command:
kubectl config use-context <context-name>
Replace <context-name>
with the context name that you got from the previous command. After running this command, kubectl
will use the new context for all subsequent commands.
Command completion helps you type commands faster and reduce errors by automatically suggesting possible options as you type. This is especially useful for long, complex kubectl
commands.
bash-completion
package.Ubuntu/Debian:
sudo apt install bash-completion
CentOS/RHEL:
sudo yum install bash-completion
macOS (Homebrew):
brew install bash-completion
bash (Linux):
source <(kubectl completion bash)
zsh (macOS и Linux):
source <(kubectl completion zsh)
bash (Linux):
echo "source <(kubectl completion bash)" >> ~/.bashrc
zsh (macOS и Linux):
echo "source <(kubectl completion zsh)" >> ~/.zshrc
To get general information about your cluster, including component addresses, use the command:
kubectl cluster-info
To list all pods in the current namespace, use the command:
kubectl get pods
If you want to see pods in a specific namespace, add the -n
option:
kubectl get pods -n <namespace>
To see all services in the current namespace, run the command:
kubectl get services
You can also specify a namespace:
kubectl get services -n <namespace>
To see the current configuration and active context:
kubectl config view
To get detailed information about any resource, such as a pod or service:
kubectl describe <resource-type> <resource-name>
For example, to get information about a pod:
kubectl describe pod <pod-name>
To delete a resource, such as a pod or service:
kubectl delete <resource-type> <resource-name>
For example, to delete a pod:
kubectl delete pod <pod-name>