Command Palette

Search for a command to run...

Kubernetes Installation Guide

Complete guide for installing Kubernetes (kubeadm, kubelet, kubectl) across different operating systems with package manager specific commands.

This guide provides step-by-step instructions for installing Kubernetes components on various operating systems.

Prerequisites

  • 2 CPUs or more
  • 2GB of RAM per node
  • Full network connectivity between nodes
  • Unique hostname, MAC address, and product_uuid for every node
  • Certain ports opened on your machines
  • Swap disabled

Installation Instructions

Ubuntu/Debian Installation

# Update package index
sudo apt update
 
# Install prerequisites
sudo apt install -y apt-transport-https ca-certificates curl
 
# Add Kubernetes GPG key
curl -fsSL https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo gpg --dearmor -o /usr/share/keyrings/kubernetes-archive-keyring.gpg
 
# Add Kubernetes repository
echo "deb [signed-by=/usr/share/keyrings/kubernetes-archive-keyring.gpg] https://apt.kubernetes.io/ kubernetes-xenial main" | sudo tee /etc/apt/sources.list.d/kubernetes.list
 
# Update package list
sudo apt update
 
# Install Kubernetes components
sudo apt install -y kubelet kubeadm kubectl
 
# Pin package versions
sudo apt-mark hold kubelet kubeadm kubectl
 
# Disable swap
sudo swapoff -a
sudo sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab
 
# Enable kernel modules
cat <<EOF | sudo tee /etc/modules-load.d/k8s.conf
overlay
br_netfilter
EOF
 
sudo modprobe overlay
sudo modprobe br_netfilter
 
# Configure sysctl params
cat <<EOF | sudo tee /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-iptables  = 1
net.bridge.bridge-nf-call-ip6tables = 1
net.ipv4.ip_forward                 = 1
EOF
 
sudo sysctl --system

Initializing the Control Plane

After installing the components, initialize the control plane node:

# Initialize the control plane
sudo kubeadm init --pod-network-cidr=192.168.0.0/16
 
# Configure kubectl for the current user
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
 
# Install Calico network plugin
kubectl apply -f https://docs.projectcalico.org/manifests/calico.yaml

Joining Worker Nodes

On worker nodes, run the join command that was output by kubeadm init:

sudo kubeadm join <control-plane-host>:<control-plane-port> \
    --token <token> \
    --discovery-token-ca-cert-hash sha256:<hash>

Verification

# Check node status
kubectl get nodes
 
# Check system pods
kubectl get pods -n kube-system

Troubleshooting

Security Recommendations

  1. Enable RBAC

    kubectl create role example-role --verb=get --resource=pods
    kubectl create rolebinding example-rolebinding --role=example-role --user=example-user
  2. Network Policies

    apiVersion: networking.k8s.io/v1
    kind: NetworkPolicy
    metadata:
      name: default-deny
    spec:
      podSelector: {}
      policyTypes:
      - Ingress
      - Egress
  3. Regular Updates

    sudo apt update
    sudo apt upgrade -y kubelet kubeadm kubectl
    sudo kubeadm upgrade plan
    sudo kubeadm upgrade apply

Next Steps