Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Kubernetes - Multi Node Deployment

Introduction

Deploying Kubernetes on multiple nodes allows you to create a highly available and scalable cluster. This guide provides an intermediate-level overview of how to deploy Kubernetes on multiple nodes using kubeadm, a tool built to provide best-practice "fast paths" for creating Kubernetes clusters.

Key Points:

  • Multi-node deployments provide high availability and scalability.
  • kubeadm is a popular tool for setting up Kubernetes clusters.
  • This guide covers the installation and configuration of a multi-node Kubernetes cluster using kubeadm.

Prerequisites

Before you begin, ensure you have the following prerequisites:

  • At least two machines (virtual or physical) with Linux installed (Ubuntu 20.04 is recommended).
  • Each machine should have at least 2 CPUs, 2GB of RAM, and 10GB of free disk space.
  • All machines should be able to communicate with each other over the network.
  • Ensure swap is disabled on all machines: sudo swapoff -a.
  • Install Docker on all machines. Follow the instructions here.

Installing kubeadm, kubelet, and kubectl

Install the necessary Kubernetes components on all machines:

# Update the apt package index and install packages needed to use the Kubernetes apt repository
sudo apt-get update
sudo apt-get install -y apt-transport-https ca-certificates curl

# Download the Google Cloud public signing key
sudo curl -fsSLo /usr/share/keyrings/kubernetes-archive-keyring.gpg https://packages.cloud.google.com/apt/doc/apt-key.gpg

# Add the Kubernetes apt repository
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 apt package index, install kubelet, kubeadm and kubectl, and pin their version
sudo apt-get update
sudo apt-get install -y kubelet kubeadm kubectl
sudo apt-mark hold kubelet kubeadm kubectl
                

Creating the Cluster

Initialize the Control Plane Node

Run the following commands on the control plane node (master node):

# Initialize the control plane
sudo kubeadm init --pod-network-cidr=192.168.0.0/16

# Set up the kubeconfig for the root user
sudo mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
                

Note the kubeadm join command output by kubeadm init. You will need this to join the worker nodes to the cluster.

Deploying a Pod Network

Deploy a pod network to enable communication between the pods. Here, we use Calico:

# Apply Calico manifest
kubectl apply -f https://docs.projectcalico.org/manifests/calico.yaml
                

Joining Worker Nodes

Run the following command on each worker node to join them to the cluster:

# Use the kubeadm join command output by kubeadm init on the control plane node
sudo kubeadm join : --token  --discovery-token-ca-cert-hash sha256:
                

Verifying the Cluster

After joining the worker nodes, verify that all nodes are part of the cluster:

# List all nodes in the cluster
kubectl get nodes
                

You should see all the nodes listed as Ready.

Deploying a Sample Application

To deploy a sample application, follow these steps:

# Create a deployment
kubectl create deployment hello-node --image=k8s.gcr.io/echoserver:1.4

# Expose the deployment as a service
kubectl expose deployment hello-node --type=NodePort --port=8080

# Get the URL of the service
kubectl get svc
                

You can access the sample application by opening the NodePort provided in the service details.

Conclusion

Deploying Kubernetes on multiple nodes using kubeadm provides a scalable and highly available cluster setup. By following the steps outlined in this guide, you can create a multi-node Kubernetes cluster, deploy applications, and explore Kubernetes features. This setup is suitable for both development and production environments.