Skip to main content

Kubectl

Install

Debian-based distributions:

  1. Update the apt package index and install packages needed to use the Kubernetes apt repository:

    sudo apt-get update
    # apt-transport-https may be a dummy package; if so, you can skip that package
    sudo apt-get install -y apt-transport-https ca-certificates curl
  2. Download the public signing key for the Kubernetes package repositories. The same signing key is used for all repositories so you can disregard the version in the URL:

    # If the folder `/etc/apt/keyrings` does not exist, it should be created before the curl command, read the note below.
    # sudo mkdir -p -m 755 /etc/apt/keyrings
    curl -fsSL https://pkgs.k8s.io/core:/stable:/v1.29/deb/Release.key | sudo gpg --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg
  3. Add the appropriate Kubernetes apt repository. If you want to use Kubernetes version different than v1.29, replace v1.29 with the desired minor version in the command below:

    # This overwrites any existing configuration in /etc/apt/sources.list.d/kubernetes.list
    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
  4. Update apt package index, then install kubectl:

    sudo apt-get update
    sudo apt-get install -y kubectl
  5. Verify kubectl version

    kubectl version

Configuration

  • By default, kubectl configuration is located at ~/.kube/config. (File created automatically when you create a cluster, for example deploy Minikube or k3s)

Check that kubectl is properly configured by getting the cluster state:

kubectl cluster-info

Source: Official documentation

Manage kubernetes cluster

  • Manage kubernetes cluster running on different machine from localhost

Example:

  1. Install k3s on Raspberry Pi
  2. Connect to RPi
  3. Copy file from RPi /etc/rancher/k3s/k3s.yaml to localhost ~/.kube/config
    scp tritoxid@rpi3:/etc/rancher/k3s/k3s.yaml ~/.kube/config
  4. Chnage IP address from localhost to machine IP
    vim ~/.kube/config
    server: https://xxx.xxx.xxx.xxx:6443
  5. You can change default cluster name and user to something else.
  6. Check configuration
    kubectl cluster-info
    Correct output:
    Kubernetes control plane is running at https://xxx.xxx.xxx.xxx:6443
    CoreDNS is running at https://xxx.xxx.xxx.xxx:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
    Metrics-server is running at https://xxx.xxx.xxx.xxx:6443/api/v1/namespaces/kube-system/services/https:metrics-server:https/proxy