DEV Community

Cover image for Configure a Kubernetes Cluster with Control Plane, Worker Nodes, and Load Balancer with Ingress Controller 🚀
Katherine Lin
Katherine Lin

Posted on

Configure a Kubernetes Cluster with Control Plane, Worker Nodes, and Load Balancer with Ingress Controller 🚀

Kubernetes is a powerful container orchestration system that simplifies the deployment, scaling, and management of containerized applications. Setting up a Kubernetes cluster involves configuring several key components: the control plane, worker nodes, and a load balancer with an ingress controller. In this guide, I’ll walk you through the step-by-step process of setting up a Kubernetes cluster with these essential parts.

Let’s dive right into it! 🚀


Table of Contents


Introduction

Kubernetes clusters are composed of two main parts: the control plane and worker nodes.

  • Control Plane: Manages the cluster and makes decisions about scheduling, scaling, and maintaining the overall state.
  • Worker Nodes: Run the actual applications, with containers managed by the control plane.
  • Load Balancer and Ingress Controller: Handle external traffic, distributing requests across the worker nodes, ensuring that services are accessible and highly available.

In this guide, I’ll help you configure a basic Kubernetes cluster using a control plane, worker nodes, and a load balancer with an ingress controller.


Setting Up the Control Plane

The control plane is the brain of the Kubernetes cluster, responsible for managing the cluster's lifecycle. It consists of several key components, such as the API server, etcd, scheduler, and controller manager.

Step 1: Initialize Kubernetes Control Plane

First, make sure your machine has kubeadm, kubelet, and kubectl installed. Once installed, initialize the control plane by running the following command on your master node:

sudo kubeadm init --pod-network-cidr=192.168.0.0/16
Enter fullscreen mode Exit fullscreen mode

The --pod-network-cidr specifies the range of IP addresses for the pod network.

After the initialization completes, the command will output instructions on how to set up kubectl for the master node.

Step 2: Configure kubectl Access

To allow the master node to use kubectl, copy the kubeconfig file to your home directory:

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

Now, your control plane is up and running!

Adding Worker Nodes

Worker nodes are responsible for running the actual applications inside containers. You’ll need to join these worker nodes to the control plane to form a full Kubernetes cluster.

Step 3: Join Worker Nodes to the Cluster

After initializing the control plane, kubeadm will output a join command. Run this on each of your worker nodes to add them to the cluster:

sudo kubeadm join <control-plane-ip>:6443 --token <token> --discovery-token-ca-cert-hash sha256:<hash>
Enter fullscreen mode Exit fullscreen mode

Make sure to replace , , and with the actual values provided by the kubeadm init command.

Step 4: Verify Nodes are Connected

Once your nodes are added, verify that they are connected to the cluster by running:

kubectl get nodes
Enter fullscreen mode Exit fullscreen mode

You should see all worker nodes listed as "Ready" in the output.

Configuring Load Balancer

To ensure high availability and balanced distribution of incoming traffic, you need to set up a load balancer. This example will use MetalLB, a load balancer specifically designed for bare metal Kubernetes clusters.

Step 5: Install MetalLB

First, install MetalLB by applying the manifest:

kubectl apply -f https://raw.githubusercontent.com/metallb/metallb/v0.9.3/manifests/metallb.yaml
Enter fullscreen mode Exit fullscreen mode

Next, configure a Layer 2 mode IP range for MetalLB. Create a ConfigMap for it:

apiVersion: v1
kind: ConfigMap
metadata:
  namespace: metallb-system
  name: config
data:
  config: |
    address-pools:
    - name: default
      protocol: layer2
      addresses:
      - 192.168.1.240-192.168.1.250
Enter fullscreen mode Exit fullscreen mode

This IP range should be from the same network as your Kubernetes nodes, but outside the range of IPs used by your DHCP.

Step 6: Install NGINX Ingress Controller

To install the NGINX ingress controller, apply the following manifest:

kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/main/deploy/static/provider/cloud/deploy.yaml
Enter fullscreen mode Exit fullscreen mode

This will create an ingress controller that listens to HTTP/HTTPS traffic and directs it based on your ingress rules.

Step 7: Create an Ingress Resource

Next, define an ingress resource for your services. Here's a sample ingress definition:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: example-ingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
  - host: example.com
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: example-service
            port:
              number: 80
Enter fullscreen mode Exit fullscreen mode

Make sure your DNS is pointing to the external IP provided by the load balancer.


Final Thoughts

By following these steps, you've set up a complete Kubernetes cluster with a control plane, worker nodes, a load balancer, and an ingress controller! Kubernetes clusters can be highly complex, but understanding how the components work together simplifies the management of containerized applications.

Feel free to ask questions or leave feedback below. Happy clustering! ✨


Related Resources:

Top comments (0)