[Kubernetes] Install Kubernetes in Ubuntu Linux

There are different platforms to run Kubernetes (K8S) nowadays. In cloud, we can consider AWS Elastic Kubernetes Service, Azure Kubernetes Service, or we can install Talos in on-premise infrastructure as quick win solution. However, actually we can install K8S in different Linux distributions. This article will step by step on how to install Kubernetes 1.24.3 in Ubuntu 22.04 with 1 master node and 1 worker node. Since 1.22, platform K8S not support docker swarm and switch to containerd since 1.22, so there has a sigh different during installation. That why I wrote this page for recording.

Steps

  1. Enable kernal modules for containerd.
    In shell, execute command below.

    sudo cat << EOF | sudo tee /etc/modules-load.d/containerd.conf 
    overlay 
    br_netfilter 
    EOF
    sudo modprobe overlay
    sudo modprobe br_netfilter
  2. Initialize system settings for K8S networking.
    In shell, execute command below.

    sudo cat <<EOF | sudo tee /etc/sysctl.d/99-kubernetes-cri.conf
    net.bridge.bridge-nf-call-iptables = 1
    net.ipv4.ip_forward = 1
    net.bridge.bridge-nf-call-ip6tables = 1
    EOF
    sudo sysctl --system
  3. Prepare applications to use in following steps.
    In shell, execute command below.

    sudo apt update
    sudo apt install -y apt-transport-https curl
  4. Disable swap in OS.
    In shell, execute command below.

    sudo swapoff -a
    sudo sed -i '/ swap / s/^\(.*\)$/#/g' /etc/fstab
  5. Install containerd
    In shell, execute command below.

    sudo apt install -y ca-certificates curl gnupg lsb-release
    curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
    echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list
    sudo apt update
    sudo apt install -y containerd.io
  6. Configure containerd
    In shell, execute command below.

    sudo mkdir -p /etc/containerd 
    sudo containerd config default | sudo tee /etc/containerd/config.toml 
    sudo sed -i 's/SystemdCgroup = false/SystemdCgroup = true/' /etc/containerd/config.toml && grep 'SystemdCgroup' -B 11 /etc/containerd/config.toml
    sudo systemctl enable --now containerd
    sudo systemctl restart containerd
  7. Install K8S
    In shell, execute command below.

    curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
    cat << EOF | sudo tee /etc/apt/sources.list.d/kubernetes.list
    deb https://apt.kubernetes.io/ kubernetes-xenial main
    EOF
    sudo apt update
    sudo apt install -y kubelet=$KUBERNETES_VERSION kubectl=$KUBERNETES_VERSION kubeadm=$KUBERNETES_VERSION
    sudo apt-mark hold kubelet kubeadm kubectl
    sudo systemctl enable --now kubelet
  8. [For Master node only] Initialize K8S cluster.
    In shell, execute command below.

    sudo kubeadm init --pod-network-cidr $POD_NETWORK_CIDR --kubernetes-version $KUBERNETES_VERSION
  9. [For Master node only] Setup master node local settings.
    In shell, execute command below.

    mkdir -p $HOME/.kube
    sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
    sudo chown $(id -u):$(id -g) $HOME/.kube/config
  10. [For Master node only] Setup Calico.
    In shell, execute command below.

    kubectl apply -f https://docs.projectcalico.org/manifests/calico.yaml
  11. [For Master node only] Generate command for worker nodes joining cluster.
    In shell, execute command below.

    kubeadm token create --print-join-command

    It should be generate command like this, copy it to clipboard.

  12. [For Worker node only] Join K8S cluster.
    In shell, execute command which copied from previous steps.
  13. Verify result.
    In master node shell, execute command below

    kubectl get po -A -o wide

    Expected it will show all namespace pods and its deployed pod.

Related shell script has been push in GitHub repository.

About C.H. Ling 260 Articles
a .net / Java developer from Hong Kong and currently located in United Kingdom. Thanks for Google because it solve many technical problems so I build this blog as return. Besides coding and trying advance technology, hiking and traveling is other favorite to me, so I will write down something what I see and what I feel during it. Happy reading!!!

1 Comment

Leave a Reply

Your email address will not be published.


*


This site uses Akismet to reduce spam. Learn how your comment data is processed.