High availability cluster setup in Kubernetes - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When setting up a high availability Kubernetes cluster, it's important to understand how the system handles tasks as the number of nodes grows.
We want to know how the time to coordinate and maintain the cluster changes as we add more machines.
Analyze the time complexity of the following Kubernetes cluster setup snippet.
apiVersion: v1
kind: Service
metadata:
name: kubernetes
namespace: default
spec:
type: LoadBalancer
selector:
component: apiserver
ports:
- port: 6443
targetPort: 6443
This snippet defines a LoadBalancer service to distribute requests to multiple API server pods for high availability.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The LoadBalancer routes incoming requests to multiple API server pods.
- How many times: The routing happens for each incoming request, and the number of pods can grow with cluster size.
As the number of API server pods increases, the LoadBalancer must manage more endpoints.
| Input Size (number of pods) | Approx. Operations (routing decisions) |
|---|---|
| 10 | Routing checks among 10 pods |
| 100 | Routing checks among 100 pods |
| 1000 | Routing checks among 1000 pods |
Pattern observation: The routing operation grows linearly with the number of pods to balance.
Time Complexity: O(n)
This means the time to route requests grows directly with the number of API server pods in the cluster.
[X] Wrong: "Adding more pods won't affect routing time because LoadBalancer is instant."
[OK] Correct: The LoadBalancer must check among all pods to route requests, so more pods mean more routing work.
Understanding how cluster components scale with size shows you can design systems that stay reliable as they grow.
"What if we used a different service type that caches endpoints? How would the time complexity change?"
Practice
high availability (HA) cluster in Kubernetes?Solution
Step 1: Understand HA cluster purpose
High availability clusters are designed to avoid downtime by having multiple master nodes so if one fails, others take over.Step 2: Compare options
Options B, C, and D do not relate to preventing downtime or multiple masters.Final Answer:
To prevent downtime by having multiple master nodes -> Option AQuick Check:
HA cluster = multiple masters for uptime [OK]
- Thinking HA reduces worker nodes
- Confusing HA with pod scaling
- Ignoring the role of multiple masters
kubeadm with a config file named ha-config.yaml?Solution
Step 1: Recall kubeadm init syntax
The correct command to initialize a cluster with a config file iskubeadm init --config filename.Step 2: Check options
kubeadm init --config ha-config.yaml matches the correct syntax. Options A, B, and D use incorrect commands or missing flags.Final Answer:
kubeadm init --config ha-config.yaml -> Option CQuick Check:
kubeadm init + --config = correct syntax [OK]
- Using 'start' instead of 'init'
- Omitting '--config' flag
- Passing config file without flag
ha-config.yaml:
apiVersion: kubeadm.k8s.io/v1beta3 kind: ClusterConfiguration controlPlaneEndpoint: "lb.example.com:6443" --- apiVersion: kubeproxy.config.k8s.io/v1alpha1 kind: KubeProxyConfiguration mode: ipvsWhat does the
controlPlaneEndpoint specify in this configuration?Solution
Step 1: Understand controlPlaneEndpoint role
This field defines the address (usually a load balancer) that routes traffic to the master nodes in an HA setup.Step 2: Analyze options
The load balancer address for master nodes correctly identifies it as the load balancer address. Other options do not relate to controlPlaneEndpoint.Final Answer:
The load balancer address for master nodes -> Option DQuick Check:
controlPlaneEndpoint = load balancer address [OK]
- Confusing it with worker node IP
- Thinking it is pod network DNS
- Mixing it with kubelet port
kubeadm join lb.example.com:6443 --token abcdef.0123456789abcdef --discovery-token-ca-cert-hash sha256:12345But it failed with an error about missing
--control-plane flag. What is the correct fix?Solution
Step 1: Identify the error cause
Joining a master node requires the--control-planeflag to indicate it is a control plane node.Step 2: Apply the fix
Add--control-planeto the join command to fix the error.Final Answer:
Add the --control-plane flag to the join command -> Option BQuick Check:
Joining master needs --control-plane flag [OK]
- Removing token breaks authentication
- Using init instead of join for adding nodes
- Changing port to wrong value
Solution
Step 1: Set up load balancer first
The load balancer must be ready to route traffic to masters before initializing the cluster.Step 2: Initialize first master with kubeadm and config
This creates the cluster control plane and configures the controlPlaneEndpoint.Step 3: Join other masters with --control-plane flag
Other masters join as control plane nodes to form HA.Step 4: Join worker nodes
Finally, worker nodes join the cluster to run workloads.Final Answer:
Set up load balancer -> Initialize first master with kubeadm and config -> Join other masters with --control-plane -> Join worker nodes -> Option AQuick Check:
Load balancer first, then masters, then workers [OK]
- Initializing all masters before load balancer
- Joining workers before masters
- Skipping --control-plane flag on masters
