Kubectl. How to connect to Kubernetes cluster

What is kubectl
How to install kubectl on Windows and connect to a cluster 
How to install kubectl on Ubuntu, Linux and connect to a cluster 
How to install kubectl on MacOS and connect to a cluster 

What is kubectl 

Kubectl is a command-line tool which allows you to control Kubernetes clusters. With kubectl, you can create and configure resources, manage pods and perform other operations. 

The utility runs as follows: you enter a command, kubeclt accesses the cluster and sends an API request to it. The cluster executes the command and makes changes. For the latest list of kubectl operations, see the kubectl official documentation.

Alternatively, there is a tool with a graphical user interface called “Lens”. It lets you manage the cluster not from the command line, but from the application window. You can download it on k8slens.dev. The guide below applies only to kubectl.

How to install kubectl on Windows and connect to a cluster

  1. To install kubectl, use a CURL request. For version v1.23.0, use the request below: 
    curl -LO https://storage.googleapis.com/kubernetes-release/release/v1.23.0/bin/windows/amd64/kubectl.exe
    Instead of a CURL request, you can use a link to download the installation file. Find the link and CURL request for the latest Kubernetes version in Kubernetes official documentation. 

  2. Add the kubectl.exe file directory into the PATH variable (so, the system can access this file when executing kubectl commands). To do this, first copy the path from the command line (this is the directory where kubectl.exe has been downloaded to): 

    image_28.png
    Then go to Windows Settings → System → About → Advanced System Settings (or right-click "This PC", then Properties → Advanced System Settings). In the next dialog, select "Environment Variables" and double-click “Path system variable”. Paste the path to the kubectl.exe in the lowest empty field and save the changes. 

    image_61.png
    image_62.png
  3. In C:\Users\[username] create the ".kube" directory (the cluster configuration file will be stored there).

    image_46.png
  4. Download the configuration file.

    image_60.png
  5. Rename the configuration file to "config" (file should not have an extension) and add it to the ".kube" folder — now kubectl can access it and manage your cluster. 

    image_45.png
  6. Verify kubectl configuration. Type the command in the command line: 
    kubectl get pod
    The system is supposed to output either "No resources found in default namespace" (when there are no pods in the cluster) or pods' data. Any of these results means that kubectl is correctly configured and you can access your cluster. 

How to install kubectl on Ubuntu, Linux and connect to a cluster

  1. Download the latest version of the kubectl binary file with the CURL request. 
    curl -LO https://storage.googleapis.com/kubernetes-release/release/`curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt`/bin/linux/amd64/kubectl 
  2. Make the kubectl binary file executable.
    chmod +x ./kubectl
  3. Move the binary file into the directory from the PATH variable. 
    sudo mv ./kubectl /usr/local/bin/kubectl
  4. Create the ".kube" directory in your home directory: 
    mkdir ~/.kube
  5. Download the configuration file.

    image_60.png
  6. Rename the configuration file to "config" (file should not have an extension) and add it to the ".kube" folder — now kubectl can access it and manage your cluster.

    image_47.png
  7. Verify kubectl configuration with the command below: 
    kubectl get pod
    The system is supposed to output either "No resources found in default namespace"(when there are no pods in the cluster) or pods' data. Any of these results means that kubectl is correctly configured and you can access your cluster. 

How to install kubectl on MacOS and connect to a cluster

  1. Download the latest version of the kubectl binary file with the CURL request. 
    curl -LO "https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/darwin/amd64/kubectl"
  2. Make the kubectl binary file executable: 
    chmod +x ./kubectl
  3. Move the kubectl binary file into the directory from the PATH variable. To do this, first check which directories correspond to the variable: 
    echo $PATH
    The systems returns a list of directories. Select one of them and move the kubectl file there using the command below: 
    sudo mv ./kubectl [one of the directories that has been returned by the "echo $PATH" command]
    For example, for the directory "/usr/local/bin/kubectl" the command looks like this: 
    sudo mv ./kubectl /usr/local/bin/kubectl
  4. Create the ".kube" directory in your home directory: 
    mkdir ~/.kube/config
  5. Download the configuration file.

    image_60.png

  6. Rename the configuration file to "config" (file should not have an extension) and add it to the ".kube" folder — now kubectl can access it and manage your cluster. 
  7. Verify kubectl configuration with the command below: 
    kubectl get pod
    The system is supposed to output either "No resources found in default namespace"(when there are no pods in the cluster) or pods' data. Any of these results means that kubectl is correctly configured and you can access your cluster. 
Was this article helpful?
Recently viewed articles