Skip to main content

Complete Guide to Setting Up a Kubernetes Cluster (Master and Worker Nodes)

This document outlines the steps to set up a Kubernetes cluster with one master node and multiple worker nodes. It covers the installation of the necessary software, configuring the nodes, and deploying a basic application on the Kubernetes cluster. This guide assumes you have access to Linux-based virtual machines or servers (either on-premise or in a cloud environment).

Prerequisites​

  • Virtual Machines or Servers: Ensure you have at least 2 or more machines (1 for master, others for worker nodes) with Ubuntu 20.04 or newer.

  • Minimum Requirements for each node:

    • 2 CPUs
    • 2 GB RAM
    • 20 GB Disk
    • Internet access to pull necessary packages.
  • User Permissions: You should have sudo or root privileges on all machines.

Step 1: Configure the Master and Worker Nodes​

1.1 Update System on All Nodes​

Run the following commands on all nodes (master and workers):

bash
sudo apt update
sudo apt upgrade -y

1.2 Disable Swap on All Nodes​

Kubernetes requires that swap be disabled on all nodes.

  1. Turn off swap temporarily:
bash
sudo swapoff -a
  1. To disable swap permanently, open the /etc/fstab file:
bash
sudo nano /etc/fstab

Comment out the swap entry by adding # at the beginning of the line that references swap.

1.3 Configure Hostname and Hosts File​

Set up unique hostnames for each node.

  • On the master node, run:
bash
sudo hostnamectl set-hostname master-node
  • On each worker node, run:
bash
sudo hostnamectl set-hostname worker-node-1  # For worker 1
sudo hostnamectl set-hostname worker-node-2 # For worker 2 (if you have a second worker)

Add the IP addresses and hostnames to the /etc/hosts file on all nodes:

bash
sudo nano /etc/hosts

Add the following lines:

bash
<Master-IP> master-node
<Worker-1-IP> worker-node-1
<Worker-2-IP> worker-node-2 # If there is a second worker

1.4 Install Docker on All Nodes​

Kubernetes uses Docker as its container runtime. Run the following commands to install Docker on all nodes.

bash
sudo apt install -y apt-transport-https ca-certificates curl software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
sudo apt update
sudo apt install -y docker-ce

Verify Docker installation:

bash
sudo systemctl status docker

Make sure Docker is running:

bash
sudo systemctl enable docker
sudo systemctl start docker

1.5 Install Kubernetes (Kubeadm, Kubelet, Kubectl) on All Nodes​

On all nodes (master and worker nodes), install kubeadm, kubelet, and kubectl.

  1. Add the Kubernetes repository:
bash
sudo apt update
sudo apt install -y apt-transport-https ca-certificates curl
curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
sudo bash -c 'cat <<EOF >/etc/apt/sources.list.d/kubernetes.list
deb https://apt.kubernetes.io/ kubernetes-xenial main
EOF'
sudo apt update
  1. Install Kubernetes packages:
bash
sudo apt install -y kubelet kubeadm kubectl
sudo apt-mark hold kubelet kubeadm kubectl
  1. Enable and start kubelet:
bash
sudo systemctl enable kubelet
sudo systemctl start kubelet

Step 2: Set Up Kubernetes Master Node​

On the master node only:

2.1 Initialize the Kubernetes Master Node​

Run the following command on the master node to initialize the Kubernetes cluster:

bash
sudo kubeadm init --pod-network-cidr=192.168.0.0/16

The --pod-network-cidr is the range for the pod network. This is necessary for installing a network add-on later.

After a successful initialization, you should see a message like this:

plaintext
Your Kubernetes control-plane has initialized successfully!

2.2 Configure kubectl for the Master Node​

Run these commands to configure kubectl for the non-root user on the master node:

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

2.3 Install Pod Network (Flannel)​

Kubernetes needs a pod network add-on. We'll use Flannel in this guide.

Install Flannel by running the following command on the master node:

bash
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml

2.4 Save the Join Command​

At the end of the kubeadm init output, you will see a kubeadm join command. This is the command that the worker nodes will use to join the cluster. Save it for the next step.

The join command looks like this:

bash
kubeadm join <Master-IP>:6443 --token <token> --discovery-token-ca-cert-hash sha256:<hash>

Step 3: Set Up Worker Nodes​

On each worker node, perform the following steps.

3.1 Join the Worker Nodes to the Cluster​

Run the kubeadm join command (you saved earlier from the master node initialization) on each worker node:

bash
sudo kubeadm join <Master-IP>:6443 --token <token> --discovery-token-ca-cert-hash sha256:<hash>

After this command is run, the worker node will join the Kubernetes cluster.

Step 4: Verify the Cluster Setup​

On the master node, run the following command to verify that the worker nodes have successfully joined the cluster:

bash
kubectl get nodes

You should see the master and all the worker nodes listed with a STATUS of Ready.

Example output:

bash
NAME           STATUS   ROLES                  AGE   VERSION
master-node Ready control-plane,master 20m v1.20.0
worker-node-1 Ready <none> 10m v1.20.0
worker-node-2 Ready <none> 10m v1.20.0

Step 5: Deploy an Application on the Kubernetes Cluster​

To verify that your cluster is working properly, you can deploy a simple application.

  1. Deploy the nginx application:
bash
kubectl create deployment nginx --image=nginx
  1. Expose the nginx deployment as a service:
bash
kubectl expose deployment nginx --port=80 --type=NodePort
  1. Get the nginx service details:
bash
kubectl get svc

This will show the external NodePort through which you can access the nginx server.

  1. Open your browser and access the application via http://<Worker-Node-IP>:<NodePort>.

Step 6: Optional - Enable Dashboard (Optional)​

To enable the Kubernetes Dashboard for easy monitoring:

  1. Deploy the Kubernetes Dashboard:
bash
kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v2.2.0/aio/deploy/recommended.yaml
  1. Create a proxy to access the dashboard:
bash
kubectl proxy
  1. Access the dashboard by navigating to the following URL in your web browser:
bash
http://localhost:8001/api/v1/namespaces/kubernetes-dashboard/services/https:kubernetes-dashboard:/proxy/
  1. Create a token to log in to the dashboard:
bash
kubectl -n kubernetes-dashboard create token admin-user

Use this token to log in to the Kubernetes dashboard.

Transform Your Business with Expert DevOps Solutions

Our tailored DevOps consulting services help streamline your processes, accelerate delivery, and ensure scalability.

Conclusion​

You now have a working Kubernetes cluster with one master node and multiple worker nodes. The cluster can be used to deploy and manage containerized applications. You can scale the cluster by adding more worker nodes as needed, deploy complex applications, or even integrate monitoring and logging solutions to make your Kubernetes environment more robust.