kubectl
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.
Installing kubectl on Linux Copy link
There are two methods for installing kubectl on Linux.
Installing with curl Copy link
-
Download the
kubectlbinary:
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/kubectlInstalling with apt Copy link
-
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 kubectlInstalling on macOS Copy link
For macOS, you can install kubectl manually or with Homebrew.
Installing with Homebrew Copy link
Install kubectl:
brew install kubectl@1.30Or install the entire Kubernetes CLI toolchain:
brew install kubernetes-cli@1.30Manual Installation Copy link
For Intel
-
Download
kubectlfor 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 --checkFor Apple Silicon
-
Download
kubectlfor 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/bindirectory and set the owner toroot:
chmod +x ./kubectl && chmod 644 ./kubectl && sudo mv ./kubectl /usr/local/bin/kubectl && sudo chown root: /usr/local/bin/kubectlInstalling on Windows Copy link
You can install kubectl on Windows manually or with Chocolatey.
Installation with Chocolatey Copy link
Install kubectl by running:
choco install kubernetes-cli --version=1.30.2Manual Installation Copy link
-
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
kubectldirectory path to yourPATHby 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
kubectldirectory.
-
Restart PowerShell for the changes to take effect.
kubectl is now installed on your machine. You can verify the installation by running the command:
kubectl versionConfiguring Access to the Kubernetes Cluster Copy link
To connect to the cluster, you will need the kubeconfig configuration file.
Obtaining the kubeconfig Configuration File Copy link
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.
You can download the configuration file from the cluster's Dashboard.

Place kubeconfig in your home directory Copy link
Once you have the config.yaml file, you need to place it in the correct directory so kubectl can use it.
macOS and Linux
-
Create a
.kubedirectory in your home directory if it doesn't exist yet:
mkdir -p ~/.kube-
Copy the
config.yamlfile to this directory and rename it toconfig:
cp ~/Downloads/config.yaml ~/.kube/configAfter completing these steps, kubectl will automatically use the configuration from the ~/.kube/config file.
Windows
-
Open PowerShell.
-
Create a
.kubedirectory in your home directory if it doesn't already exist:
mkdir $env:USERPROFILE\.kube-
Copy the
config.yamlfile to this directory and rename it toconfig:
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.
Using the KUBECONFIG environment variable Copy link
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
-
Open the terminal.
-
Set the
KUBECONFIGenvironment 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>" >> ~/.bashrcWindows
-
Open PowerShell.
-
Set the
KUBECONFIGenvironment variable:
$env:KUBECONFIG = "<path-to-your-kubeconfig>"Using multiple configuration files Copy link
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 Copy link
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-infoThis 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 Copy link
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-contextsThis 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.
Configuring Command Completion Copy link
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 Copy link
- Install the
bash-completionpackage.
Ubuntu/Debian:
sudo apt install bash-completionCentOS/RHEL:
sudo yum install bash-completionmacOS (Homebrew):
brew install bash-completion- Enable autocompletion for the current session.
bash (Linux):
source <(kubectl completion bash)zsh (macOS and Linux):
source <(kubectl completion zsh)- To always keep autocompletion enabled, run the command:
bash (Linux):
echo "source <(kubectl completion bash)" >> ~/.bashrczsh (macOS and Linux):
echo "source <(kubectl completion zsh)" >> ~/.zshrcBasic kubectl Commands Copy link
Getting Cluster Information Copy link
To get general information about your cluster, including component addresses, use the command:
kubectl cluster-infoViewing Pods Copy link
To list all pods in the current namespace, use the command:
kubectl get podsIf you want to see pods in a specific namespace, add the -n option:
kubectl get pods -n <namespace>Viewing Services Copy link
To see all services in the current namespace, run the command:
kubectl get servicesYou can also specify a namespace:
kubectl get services -n <namespace>Viewing Configuration Copy link
To see the current configuration and active context:
kubectl config viewResources Description Copy link
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 Copy link
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>