Step by step to install kubernetes cluster
Tags: kubernetes, cloud, docker
Published: 2021-06-07 00:21:49

This post was originally posted [here](https://datmt.com/linux/install-kubernetes-cluster-with-kubeadm-on-ubuntu-step-by-step/)
## Preface
I've been trying installing a Kubernetes cluster for while following the official documentation without any success. It turned out the official documentation was missing some important steps (or they put the missing steps else where I couldn’t find). Anyways, if you are struggling to get a Kubernetes up and running, this step by step tutorial is for you.

I’m going to setup a k8s cluster with 1 master node and 1 worker node. Once you have a master node up and running, adding one or more worker nodes does not require extra expertise.

I also use VirtualBox running two identical Ubuntu 18.04 VM. I guess that the newer Ubuntu versions should work fine (haven’t tested).

## Step by step to install kubernetes cluster

Here are the steps you need to run on all nodes

### Step 1: Disable swap
To disable swap, simply remove the line with swap in /etc/fstab

```bash
sudo vim /etc/fstab
```

Comment out the line with swap
![disable swap](https://datmt.com/wp-content/uploads/2021/06/image-12.png)


### Step 2: Install docker run time

```bash
 sudo apt-get update
 sudo apt-get install \
    apt-transport-https \
    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

 sudo apt-get update
 sudo apt-get install docker-ce docker-ce-cli containerd.io

```

### Step 3: Configure cgroup
Switch to root and run

```bash
cat > /etc/docker/daemon.json <<EOF
{
  "exec-opts": ["native.cgroupdriver=systemd"],
  "log-driver": "json-file",
  "log-opts": {
    "max-size": "100m"
  },
  "storage-driver": "overlay2",
  "storage-opts": [
    "overlay2.override_kernel_check=true"
  ]
}
EOF

systemctl restart docker

```

### Step 4: Install kubeadm, kubelet, kubectl

```bash
sudo curl -fsSLo /usr/share/keyrings/kubernetes-archive-keyring.gpg https://packages.cloud.google.com/apt/doc/apt-key.gpg

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

sudo apt-get update
sudo apt-get install -y kubelet kubeadm kubectl
sudo apt-mark hold kubelet kubeadm kubectl

```

Now, that's all the common commands you need to run on all nodes. Next comes the command you only run on the master node:

### Step 5: start master node

```bash
kubeadm init
```

You should see similar message after a few minutes:

![kubeadm init successfully](https://datmt.com/wp-content/uploads/2021/06/image-8-1024x626.png)

Copy the `kubeadm join...` command to later run on worker nodes.


Finally, you need to install network plugin for the master node (super important!)

```bash
sudo kubectl apply -f "https://cloud.weave.works/k8s/net?k8s-version=$(sudo kubectl version | base64 | tr -d '\n')"
```

Wait for a few minutes for the master node to be ready. You can run:
```bash
kubectl cluster-info
```

and wait until the status of the master node is `Ready`

### Step 6: Join the cluster on worker nodes

Then, switch to the worker node and run the join command (the one you got after `kubeadm init`)

```bash
kubeadm join 192.168.1.98:6443 --token 0mfz2s.4xt0waiyfnpxiyt9 \
        --discovery-token-ca-cert-hash sha256:12e48d3bbfb435536618fc293a77950c13ac975fbea934c49c39abe4b7335ce1
```

Back to the master node and run 
```bash
watch kubectl get nodes
```

It will watch the cluster and after a few minutes, you should see all the nodes are ready:
![Cluster nodes area ready](https://datmt.com/wp-content/uploads/2021/06/image-11-1024x629.png)

Congratulations! You have successfully setup a kubernetes cluster