Published on

Setup a load balancer with Kind Kubernetes Cluster

Authors
  • avatar
    Name
    codebuff
    Twitter

How to Enable LoadBalancer in Kind with cloud-provider-kind

Kind (Kubernetes in Docker) is a tool for running local Kubernetes clusters using Docker containers. While Kind is lightweight and convenient for development and testing, it does not natively support LoadBalancer services, which are typically used in cloud environments to expose services externally. However, with the cloud-provider-kind binary, you can simulate LoadBalancer behavior in your Kind cluster.


Step 1: Install cloud-provider-kind

The cloud-provider-kind binary is a lightweight tool designed to handle LoadBalancer requests for Kind. Here's how to install it:

go install sigs.k8s.io/cloud-provider-kind@latest

Note: This command installs the binary into your ~/go/bin directory. Ensure this directory is added to your PATH to use the binary.


Step 2: Run the Binary

Start the cloud-provider-kind binary in a terminal session. It will monitor and handle LoadBalancer requests for your Kind cluster:

cloud-provider-kind

Sample output:

I1118 10:22:53.638416   75582 app.go:46] FLAG: --enable-lb-port-mapping="false"
I1118 10:22:53.638439   75582 app.go:46] FLAG: --enable-log-dumping="false"
I1118 10:22:53.761270   75582 controller.go:174] probe HTTP address https://webserver-control-plane:6443

Step 3: Deploy a LoadBalancer Example

Use the following example YAML to deploy a LoadBalancer service and some test Pods in your Kind cluster:

Apply the Example

kubectl apply -f https://kind.sigs.k8s.io/examples/loadbalancer/usage.yaml

YAML Configuration

This configuration creates two Pods (foo-app and bar-app) and a LoadBalancer service (foo-service):

kind: Pod
apiVersion: v1
metadata:
  name: foo-app
  labels:
    app: http-echo
spec:
  containers:
    - command:
        - /agnhost
        - serve-hostname
        - --http=true
        - --port=8080
      image: registry.k8s.io/e2e-test-images/agnhost:2.39
      name: foo-app
---
kind: Pod
apiVersion: v1
metadata:
  name: bar-app
  labels:
    app: http-echo
spec:
  containers:
    - command:
        - /agnhost
        - serve-hostname
        - --http=true
        - --port=8080
      image: registry.k8s.io/e2e-test-images/agnhost:2.39
      name: bar-app
---
kind: Service
apiVersion: v1
metadata:
  name: foo-service
spec:
  type: LoadBalancer
  selector:
    app: http-echo
  ports:
    - port: 5678
      targetPort: 8080

Step 4: Test the LoadBalancer

  1. Retrieve the LoadBalancer IP:

    LB_IP=$(kubectl get svc/foo-service -o=jsonpath='{.status.loadBalancer.ingress[0].ip}')
    
  2. Send Requests to the LoadBalancer: Test the LoadBalancer's round-robin routing by sending multiple requests:

    for _ in {1..10}; do
      curl ${LB_IP}:5678
    done
    

    Example output:

    foo-app
    bar-app
    bar-app
    foo-app
    bar-app
    bar-app
    foo-app
    foo-app
    

This output confirms that traffic is being distributed across the two Pods (foo-app and bar-app) in a round-robin manner.


Conclusion

With the cloud-provider-kind binary, you can effectively simulate LoadBalancer services in Kind clusters, enabling you to test applications with configurations closer to production setups.

For further details, visit the Kind LoadBalancer documentation and the cloud-provider-kind GitHub repository.