Skip to main content

Kubectl

kubectl is a command-line tool for controlling Kubernetes clusters. This guide covers installation, configuration, and basic usage.

Installation on Debian-based systems​

Follow these steps to install kubectl on Debian-based Linux distributions.

  1. Update the apt package index and install necessary packages:

    sudo apt-get update
    sudo apt-get install -y apt-transport-https ca-certificates curl
    note

    The apt-transport-https package may not be required on some newer distributions.

  2. Download the public signing key for the Kubernetes package repositories:

    curl -fsSL https://pkgs.k8s.io/core:/stable:/v1.29/deb/Release.key | sudo gpg --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg
    tip

    If the directory /etc/apt/keyrings does not exist, you can create it with the following command: sudo mkdir -p -m 755 /etc/apt/keyrings

  3. Add the Kubernetes apt repository:

    echo 'deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.gpg] https://pkgs.k8s.io/core:/stable:/v1.29/deb/ /' | sudo tee /etc/apt/sources.list.d/kubernetes.list
    info

    To use a different Kubernetes version, replace v1.29 with the desired minor version.

  4. Install kubectl:

    sudo apt-get update
    sudo apt-get install -y kubectl
  5. Verify the installation by checking the version:

    kubectl version --client

Configuration​

The default configuration file for kubectl is located at ~/.kube/config. This file is typically generated automatically when you create a cluster with tools like minikube or k3s.

To verify that kubectl is configured correctly, run the following command to get the cluster status:

kubectl cluster-info

Managing a Remote Kubernetes Cluster​

You can manage a Kubernetes cluster running on a different machine by updating your local ~/.kube/config file.

Example​

This example demonstrates how to connect to a k3s cluster running on a Raspberry Pi.

  1. Copy the k3s.yaml file from the remote machine to your local ~/.kube/ directory and rename it to config:

    scp user@remote-ip:/etc/rancher/k3s/k3s.yaml ~/.kube/config
  2. Edit the ~/.kube/config file and change the server IP address to the remote machine's IP address:

    server: https://<REMOTE_IP_ADDRESS>:6443
  3. (Optional) You can rename the default cluster, context, and user for clarity.

  4. Verify the connection to the remote cluster:

    kubectl cluster-info

    A successful connection will display the running services, similar to the output below:

    Kubernetes control plane is running at https://<REMOTE_IP_ADDRESS>:6443
    CoreDNS is running at https://<REMOTE_IP_ADDRESS>:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
    Metrics-server is running at https://<REMOTE_IP_ADDRESS>:6443/api/v1/namespaces/kube-system/services/https:metrics-server:https/proxy

References​