DEV Community

giveitatry
giveitatry

Posted on

How to Create a Load Balancer Using hetzner-k3s

Hetzner-k3s is a powerful tool that simplifies the deployment of Kubernetes clusters on Hetzner Cloud infrastructure. One of its key benefits is the ability to automatically provision and manage Hetzner Cloud Load Balancers for your Kubernetes services.

This guide will walk you through the steps required to create a LoadBalancer service in a hetzner-k3s cluster.


Prerequisites

  • A Kubernetes cluster deployed using hetzner-k3s.
  • Access to the Kubernetes cluster (e.g., via kubectl).
  • Your cluster nodes are deployed in a known Hetzner Cloud location (e.g., fsn1, nbg1, or hel1).

Step-by-Step Guide

1. Create a Kubernetes Service of Type LoadBalancer

Hetzner-k3s includes the Hetzner Cloud Controller Manager (HCCM) by default. This allows you to create LoadBalancer services that automatically provision Hetzner Cloud Load Balancers.

Here's a minimal example of a Kubernetes service that uses Hetzner's Load Balancer:

env: production
---
apiVersion: v1
kind: Service
metadata:
  name: my-app-lb
  annotations:
    load-balancer.hetzner.cloud/location: fsn1
    load-balancer.hetzner.cloud/use-private-ip: "true"
spec:
  type: LoadBalancer
  selector:
    app: my-app
  ports:
    - protocol: TCP
      port: 80
      targetPort: 8080
Enter fullscreen mode Exit fullscreen mode

Explanation of Annotations

  • load-balancer.hetzner.cloud/location: Specifies the Hetzner data center location where the load balancer should be created. This must match the region of your nodes.
  • load-balancer.hetzner.cloud/use-private-ip: If set to "true", the load balancer will route traffic to the private IPs of the cluster nodes (typically required for internal networking efficiency).

2. Apply the Service Manifest

Save the above YAML to a file, e.g., my-app-lb.yaml, then apply it:

kubectl apply -f my-app-lb.yaml
Enter fullscreen mode Exit fullscreen mode

3. Verify Load Balancer Creation

Check the status of the service:

kubectl get svc my-app-lb
Enter fullscreen mode Exit fullscreen mode

You should see the EXTERNAL-IP field populated with the IP of the newly created Hetzner Load Balancer.

To see more details:

kubectl describe svc my-app-lb
Enter fullscreen mode Exit fullscreen mode

Look for events related to load balancer provisioning. If there are issues, you can inspect the logs of the cloud controller manager:

kubectl logs -n kube-system -l k8s-app=hcloud-cloud-controller-manager
Enter fullscreen mode Exit fullscreen mode

Troubleshooting

  • EXTERNAL-IP remains Pending:

    • Ensure required annotations are present.
    • Confirm location matches the actual Hetzner region of your cluster nodes.
    • Validate that the app label selector matches deployed pods.

Additional Configuration Options

Hetzner Load Balancers support several annotations for customization:

  • load-balancer.hetzner.cloud/hostname: Assign a hostname.
  • load-balancer.hetzner.cloud/ssl-certificates: Attach SSL certificates.
  • load-balancer.hetzner.cloud/algorithm: Load balancing algorithm (e.g., round_robin, least_connections).

For a full list, refer to Hetzner Cloud Load Balancer documentation.


Conclusion

Using hetzner-k3s, creating and managing Load Balancer services becomes straightforward thanks to its built-in Hetzner Cloud integration. By simply annotating your services correctly, you can leverage Hetzner's robust load balancing infrastructure without any additional setup or installation.

Top comments (0)