Kubernetes Installation with kubeadm
Tags: kubernetes, ubuntu
Published: 2023-05-29 19:11:44

[Official Documentation](https://kubernetes.io/docs/setup/production-environment/tools/kubeadm/create-cluster-kubeadm/)


The kubeadm tool is good if you need:

- A simple way for you to try out Kubernetes, possibly for the first time.
- A way for existing users to automate setting up a cluster and test their application.
- A building block in other ecosystem and/or installer tools with a larger scope.

You can install and use kubeadm on various machines: your laptop, a set of cloud servers, a Raspberry Pi, and more. Whether you're deploying into the cloud or on-premises, you can integrate kubeadm into provisioning systems such as Ansible or Terraform.

## Requirements

A Linux host that meets the following requirements:
- x86-64, arm64, ppc64le, or s390x processor
- 2CPU
- 2GB RAM
- 10GB free disk space
- RedHat Enterprise Linux 7.x+, CentOS 7.x+, Ubuntu 16.04+, or Debian 9.x+

## Steps for Ubuntu 22.04 LTS
> Root access or sudo privileges

1. Update System Packages and install packages.
    ```sh
    sudo apt update
    sudo apt upgrade -y
    sudo apt install -y apt-transport-https ca-certificates curl
    ```
2. Install Docker
    ```sh
    sudo apt install docker.io -y
    sudo usermod -aG docker $(whoami)
    ```
    Enable Docker to start at boot:
    ```sh
    sudo systemctl enable docker
    ```
3. Disable Swap
    Kubernetes requires swap to be disabled. You can disable it on both nodes using
    ```sh
    sudo swapoff -a
    ```
    To make this change permanent, you have to edit the /etc/fstab file. Comment out the line that ends or include "swap".
    ```sh
    sudo vi /etc/fstab
    ```
4. Install Kubernetes
    Add the Kubernetes signing key:
    ```sh
    curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
    ```
    or
    ```sh
    sudo curl -fsSLo /usr/share/keyrings/kubernetes-archive-keyring.gpg https://packages.cloud.google.com/apt/doc/apt-key.gpg
    ```
    Add the Kubernetes package source list:
    ```sh
    echo "deb https://apt.kubernetes.io/ kubernetes-xenial main" | sudo tee /etc/apt/sources.list.d/kubernetes.list
    ```
    or
    ```sh
    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 your package list and install Kubernetes tools:
    ```sh
    sudo apt update
    sudo apt install -y kubelet kubeadm kubectl
    ```
5. Configure Control Plane Node
    ```sh
    sudo kubeadm init --pod-network-cidr=<server-ip>/16
    ```
    Please be aware, --pod-network-cidr argument is usually used to specify the range of IP addresses for the pod network. For example, if you plan to use Calico as your network plugin, you would use --pod-network-cidr=192.168.0.0/16.

    If you need to bind the API server to a specific IP address, you would typically use the --apiserver-advertise-address argument. So if you want to bind it to your server's IP, the command would be:
    ```sh
    sudo kubeadm init --apiserver-advertise-address=<server-ip>
    ```
    Sample Output:
    ```sh
    Your Kubernetes control-plane has initialized successfully!

    To start using your cluster, you need to run the following as a regular user:

    mkdir -p $HOME/.kube
    sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
    sudo chown $(id -u):$(id -g) $HOME/.kube/config

    Alternatively, if you are the root user, you can run:

    export KUBECONFIG=/etc/kubernetes/admin.conf

    You should now deploy a pod network to the cluster.
    Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at:
    https://kubernetes.io/docs/concepts/cluster-administration/addons/

    Then you can join any number of worker nodes by running the following on each as root:

    kubeadm join 10.3.42.180:6443 --token i07joy.ml3fnm94v7c9amlm \
        --discovery-token-ca-cert-hash sha256:c63c045c09ec9dbfc6184fd0adb1fa0fcd5f149b12585305cd84e78a85484369
    ```
    __The above command will output a kubeadm join command with a token. Keep note of the entire command; it's required to join the worker node to the cluster.__

    To make kubectl work for your non-root user, run these commands, which are also part of the kubeadm init output:
    ```sh
    mkdir -p $HOME/.kube
    sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
    sudo chown $(id -u):$(id -g) $HOME/.kube/config
    ```
6. Installing a Pod network add-on
__[Calico](https://docs.tigera.io/calico/latest/getting-started/kubernetes/quickstart):__

- Install the Tigera Calico operator and custom resource definitions.

```sh
    kubectl create -f https://raw.githubusercontent.com/projectcalico/calico/v3.25.1/manifests/tigera-operator.yaml
```
- Confirm that all of the pods are running with the following command.
```sh
watch kubectl get pods -n calico-system
```

7. Join Worker Node to Cluster
    Execute the `kubeadm join` command that was output at the end of `kubeadm init` on the master node.
    ```sh
    sudo kubeadm join --token <token> <master-ip>:<master-port> --discovery-token-ca-cert-hash sha256:<hash>
    ```
    Replace <token>, <master-ip>, <master-port>, and <hash> with the respective values from the output of the kubeadm init command.
    You can paste the section of the output generated in the step 5.

    Wait for a few minutes, then on the master node, check if the worker node has joined the cluster:
    ```sh
    kubectl get nodes
    ```
 
You should see both the master and worker nodes listed.

That's it! You have now a functional Kubernetes cluster running

## Manage the versions of the Cluster

[More Info](https://kubernetes.io/docs/tasks/administer-cluster/)

Kubeadm is a powerful tool in Kubernetes that allows you to set up and upgrade a secure Kubernetes cluster easily. To upgrade a Kubernetes cluster using kubeadm, you'd typically follow these general steps:

1. __Plan:__ Use `kubeadm upgrade` plan to check which versions you can upgrade to.
2. __Drain:__ Drain the control-plane node before upgrading it. This is done to ensure that the cluster remains available and no workloads will be interrupted during the upgrade.
    ```sh
    kubectl drain <control-plane-node-name> --ignore-daemonsets
    ```
3. __Upgrade Control Plane:__ Upgrade the control plane (kube-apiserver, kube-controller-manager, kube-scheduler, and etcd):
    ```sh
    sudo kubeadm upgrade apply <new-version>
    ```
4. __Uncordon Master Node:__ Make the master node schedulable again.
    ```sh
    kubectl uncordon <control-plane-node-name>
    ```
5. __Upgrade Kubeadm on Worker Nodes:__ On each worker node, upgrade kubeadm to the latest version.
6. __Drain the Worker Nodes:__ Before upgrading worker nodes, they should be drained to minimize disruption to running applications.
7. __Upgrade the Worker Nodes:__ Upgrade the Kubernetes configuration on each worker node.
    ```sh
    sudo kubeadm upgrade node
    ```
8. __Uncordon the Worker Nodes:__ Once the upgrade is complete, make the worker node schedulable again.
9. __Upgrade kubectl on Each Node:__ After all nodes are upgraded, make sure to upgrade kubectl to the new version.
10. __Verify the Upgrade:__ Finally, verify that the upgrade was successful:
    ```sh
    kubectl get nodes
    ```
Remember, it is important to read the [release notes](https://github.com/kubernetes/kubernetes/tree/master/CHANGELOG) for the version you're upgrading to before performing the upgrade, as there might be specific notes or issues related to that version.

I hope this step overview is helpful to you and your journey of learning more about Kubernetes.