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
orroot
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):
sudo apt update
sudo apt upgrade -y
1.2 Disable Swap on All Nodes​
Kubernetes requires that swap be disabled on all nodes.
- Turn off swap temporarily:
sudo swapoff -a
- To disable swap permanently, open the
/etc/fstab
file:
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:
sudo hostnamectl set-hostname master-node
- On each worker node, run:
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:
sudo nano /etc/hosts
Add the following lines:
<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.
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:
sudo systemctl status docker
Make sure Docker is running:
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
.
- Add the Kubernetes repository:
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
- Install Kubernetes packages:
sudo apt install -y kubelet kubeadm kubectl
sudo apt-mark hold kubelet kubeadm kubectl
- Enable and start
kubelet
:
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:
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:
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:
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:
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:
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:
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:
kubectl get nodes
You should see the master and all the worker nodes listed with a STATUS of Ready.
Example output:
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.
- Deploy the nginx application:
kubectl create deployment nginx --image=nginx
- Expose the nginx deployment as a service:
kubectl expose deployment nginx --port=80 --type=NodePort
- Get the nginx service details:
kubectl get svc
This will show the external NodePort
through which you can access the nginx server.
-
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:
- Deploy the Kubernetes Dashboard:
kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v2.2.0/aio/deploy/recommended.yaml
- Create a proxy to access the dashboard:
kubectl proxy
-
Access the dashboard by navigating to the following URL in your web browser:
http://localhost:8001/api/v1/namespaces/kubernetes-dashboard/services/https:kubernetes-dashboard:/proxy/
- Create a token to log in to the dashboard:
kubectl -n kubernetes-dashboard create token admin-user
Use this token to log in to the Kubernetes dashboard.
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.