Sign In
Sign In

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.

Installing kubectl on Linux

There are two methods for installing kubectl on Linux.

Installing with curl

  1. Download the kubectl binary:

curl -LO "https://storage.googleapis.com/kubernetes-release/release/v1.30.2/bin/linux/amd64/kubectl"
  1. 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
  1. Install kubectl

sudo install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl

Installing with apt

  1. Update the package list:

sudo apt update
  1. Install the required packages: 

sudo apt install -y apt-transport-https ca-certificates curl gnupg
  1. 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
  1. 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
  1. Update the package list again: 

sudo apt update
  1. Install kubectl

sudo apt install -y kubectl

Installing on macOS

For macOS, you can install kubectl manually or with Homebrew.

Installing 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.

Manual Installation

For Intel

  1. Download kubectl for Intel: 

curl -LO "https://dl.k8s.io/release/v1.30.2/bin/darwin/amd64/kubectl"
  1. 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

  1. Download kubectl for Apple Silicon: 

curl -LO "https://dl.k8s.io/release/v1.30.2/bin/darwin/arm64/kubectl"
  1. 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
  1. 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

Installing on Windows

You can install kubectl on Windows manually or with Chocolatey.

Installation with Chocolatey

Install kubectl by running: 

choco install kubernetes-cli --version=1.30.2

Manual Installation

  1. Open PowerShell and run:

mkdir kubectl
cd .\kubectl\
curl.exe -LO "https://dl.k8s.io/release/v1.30.2/bin/windows/amd64/kubectl.exe"
  1. 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:

    • Right-click on This PC.
    • Select Properties.
    • Go to About.
    • Click on Advanced system settings.
    • Navigate to the Advanced tab.
    • Click on Environment Variables.
    • In the System variables section, find the Path variable and add the path to the kubectl directory.
  1. Restart PowerShell for the changes to take effect.

Docker Desktop for Windows adds its version of kubectl to the PATH. If you have Docker Desktop installed, you may need to place the PATH entry for kubectl before the one added by the Docker Desktop installer, or remove the version of kubectl installed by Docker Desktop.

kubectl is now installed on your machine. You can verify the installation by running the command:

kubectl version

Configuring Access to the Kubernetes Cluster

To connect to the cluster, you will need the kubeconfig configuration file.

Obtaining 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.

Eba39dba 6516 438e Bfbc E5e97e37953e

Place kubeconfig in your home directory

Once you have the config.yaml file, you need to place it in the correct directory so kubectl can use it.

macOS and Linux

  1. Create a .kube directory in your home directory if it doesn't exist yet:

mkdir -p ~/.kube
  1. 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.

Windows

  1. Open PowerShell.

  2. Create a .kube directory in your home directory if it doesn't already exist:

mkdir $env:USERPROFILE\.kube
  1. Copy the config.yaml file to this directory and rename it to config:

copy <path_to_config.yaml> $env:USERPROFILE\.kube\config
  1. 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.

Using the KUBECONFIG environment variable

If you need to use multiple configuration files or place config.yaml in a different location, you can use the KUBECONFIG environment variable.

macOS and Linux

  1. Open the terminal.

  2. 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

Windows

  1. Open PowerShell.

  2. Set the KUBECONFIG environment variable:

$env:KUBECONFIG = "<path-to-your-kubeconfig>"

Using multiple configuration files

You can specify multiple kubeconfig files by separating their paths with colons (on macOS and Linux) or semicolons (on Windows):

Windows

$env:KUBECONFIG="<path-to-your-first-kubeconfig>;<path-to-your-second-kubeconfig>"

macOS and Linux

export KUBECONFIG=<path-to-your-first-kubeconfig>:<path-to-your-second-kubeconfig>

Setting Up a Connection to the Kubernetes Cluster

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.

Checking a context

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.

Image3 (1)

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.

Configuring Command Completion

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.

Installing bash-completion

  1. Install the bash-completion package.

Ubuntu/Debian:

sudo apt install bash-completion

CentOS/RHEL:

sudo yum install bash-completion

macOS (Homebrew):

brew install bash-completion
  1. Enable autocompletion for the current session.

bash (Linux):

source <(kubectl completion bash)

zsh (macOS и Linux):

source <(kubectl completion zsh)
  1. To always keep autocompletion enabled, run the command:

bash (Linux):

echo "source <(kubectl completion bash)" >> ~/.bashrc

zsh (macOS и Linux):

echo "source <(kubectl completion zsh)" >> ~/.zshrc

Basic kubectl Commands

Getting Cluster Information

To get general information about your cluster, including component addresses, use the command:

kubectl cluster-info

Viewing Pods

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>

Viewing Services

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>

Viewing Configuration

To see the current configuration and active context:

kubectl config view

Resources Description

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>

Deleting Resources

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>
Was this page helpful?
Updated on 18 October 2024

Do you have questions,
comments, or concerns?

Our professionals are available to assist you at any moment,
whether you need help or are just unsure of where to start.
Email us
Hostman's Support