DEV Community

Cover image for Step by step to install kubernetes cluster
Mạnh Đạt
Mạnh Đạt

Posted on

Step by step to install kubernetes cluster

This post was originally posted here

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

sudo vim /etc/fstab
Enter fullscreen mode Exit fullscreen mode

Comment out the line with swap
disable swap

Step 2: Install docker run time

 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

Enter fullscreen mode Exit fullscreen mode

Step 3: Configure cgroup

Switch to root and run

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

Enter fullscreen mode Exit fullscreen mode

Step 4: Install kubeadm, kubelet, kubectl

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

Enter fullscreen mode Exit fullscreen mode

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

kubeadm init
Enter fullscreen mode Exit fullscreen mode

You should see similar message after a few minutes:

kubeadm init successfully

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

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

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

Wait for a few minutes for the master node to be ready. You can run:

kubectl cluster-info
Enter fullscreen mode Exit fullscreen mode

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)

kubeadm join 192.168.1.98:6443 --token 0mfz2s.4xt0waiyfnpxiyt9 \
        --discovery-token-ca-cert-hash sha256:12e48d3bbfb435536618fc293a77950c13ac975fbea934c49c39abe4b7335ce1
Enter fullscreen mode Exit fullscreen mode

Back to the master node and run

watch kubectl get nodes
Enter fullscreen mode Exit fullscreen mode

It will watch the cluster and after a few minutes, you should see all the nodes are ready:
Cluster nodes area ready

Congratulations! You have successfully setup a kubernetes cluster

Top comments (0)