How to Set Up a Highly Available Master Node in Kubernetes

Updated: January 31, 2024 By: Guest Contributor Post a comment

Introduction

Ensuring high availability within a Kubernetes cluster is critical for maintaining a reliable and resilient infrastructure. In this tutorial, we will walk you through setting up a highly available master node in Kubernetes. High Availability (HA) in Kubernetes is achieved when multiple master nodes are configured to manage the cluster, ensuring that if one master node fails, others can take over with no downtime.

Understanding Kubernetes Master Node Components

Before we dive into the setup, it is important to understand the key components of the Kubernetes master node:

  • etcd: A consistent and highly-available key-value store used as Kubernetes’ backing store for all cluster data.
  • API Server: Serves the Kubernetes API and acts as the front-end for the cluster’s shared state through which all other components interact.
  • Scheduler: Watches for newly created pods with no assigned node, and selects a node for them to run on.
  • Controller Manager: Runs controller processes that regulate the state of the cluster.

Prerequisites

To start with this setup, you need the following:

  1. At least 3 VMs or physical servers (for the master nodes).
  2. A configured Kubernetes cluster.
  3. kubeadm tool installed on all nodes.
  4. Knowledge of Linux systems and basic Kubernetes concepts.

Step-by-Step Instructions

Step 1: Setting Up the Load Balancer

The first step in creating an HA master node setup is putting a load balancer in front of your master nodes. This will distribute traffic across all available master nodes to ensure continuity should one fail.

# Install Nginx as a load balancer
sudo apt-get update
sudo apt-get install -y nginx

# Configure the load balancer
sudo nano /etc/nginx/nginx.conf

# The nginx.conf should include the upstream Kubernetes servers block
http {
	upstream kubernetes {
		server master1.example.com:6443 max_fails=3 fail_timeout=30s;
		server master2.example.com:6443 max_fails=3 fail_timeout=30s;
		server master3.example.com:6443 max_fails=3 fail_timeout=30s;
	}

	server {
		listen 6443;
		location / {
			proxy_pass https://kubernetes;
		}
	}
}

Once you have your load balancer configured, you should be able to ping the virtual IP assigned to it and get a response from one of the master nodes.

Step 2: Set Up etcd Cluster

A reliable etcd cluster is the foundation of an HA Kubernetes cluster because etcd stores all of the cluster’s state. Install and configure an etcd cluster across three master nodes:

# On each master node
sudo systemctl start etcd

# Verify that the etcd cluster is working by checking the member list on one of the nodes
etcdctl member list

Ensure the etcd cluster is healthy with all members listed before proceeding.

Step 3: Initialize the First Control Plane Node

Using kubeadm, now initialize your first control plane node:

kubeadm init --control-plane-endpoint "LOAD_BALANCER_IP:LOAD_BALANCER_PORT" --upload-certs

You should see an output indicating success, which will include a join command for other master and worker nodes.

Step 4: Joining Additional Master Nodes

Using the join command you received from the init output, you can now join additional master nodes to the cluster.

kubeadm join LOAD_BALANCER_IP:LOAD_BALANCER_PORT --token <token> --discovery-token-ca-cert-hash <hash> --certificate-key <certificate-key> --control-plane

Repeat this step for each of the remaining master nodes.

Step 5: Adding Worker Nodes

After your master nodes have been set up and joined to the load balancer, you can begin to add worker nodes to the cluster using the kubeadm join command without the –control-plane flag.

kubeadm join LOAD_BALANCER_IP:LOAD_BALANCER_PORT --token <token> --discovery-token-ca-cert-hash <hash>

Verify all nodes are joined successfully:

kubectl get nodes

The output should show all the nodes with their corresponding roles, executing the expected ‘Ready’ state.

Testing High Availability

To test the high availability of your master nodes, you can temporarily simulate a failure of your primary master node:

sudo systemctl stop kubelet
sudo systemctl stop docker

If everything is configured correctly, the other master nodes should take over, and the cluster should continue to operate normally.

Conclusion

This tutorial explained how to set up a highly available Kubernetes master node to ensure your cluster remains resilient. With these steps completed, your applications will benefit from improved uptime and the robustness needed to handle potential master node failures.