How to Add Load Balancers to Kubernetes Clusters

DigitalOcean Kubernetes (DOKS) is a managed Kubernetes service that lets you deploy Kubernetes clusters without the complexities of handling the control plane and containerized infrastructure. Clusters are compatible with standard Kubernetes toolchains and integrate natively with DigitalOcean Load Balancers and block storage volumes.

The DigitalOcean Cloud Controller supports provisioning DigitalOcean Load Balancers in a cluster’s resource configuration file. The DigitalOcean Load Balancer Service routes load balancer traffic to all worker nodes on the cluster. Only nodes configured to accept the traffic will pass health checks. Any other nodes will fail and show as unhealthy, but this is expected. Our community article, How to Set Up an Nginx Ingress with Cert-Manager on DigitalOcean Kubernetes, provides a detailed, practical example.

The example configuration below defines a load balancer and creates it if one with the same name does not already exist. Additional configuration examples are available in the DigitalOcean Cloud Controller Manager repository.

Create a Configuration File

You can add an external load balancer to a cluster by creating a new configuration file or adding the following lines to your existing service config file. Note that both the type and ports values are required for type: LoadBalancer:

  type: LoadBalancer
    app: nginx-example
    - name: http
      protocol: TCP
      port: 80
      targetPort: 80


You can configure the size of the load balancer at creation by setting the slug size annotation. In the context of a service file, this looks like:

apiVersion: v1
kind: Service
  name: ngnix-lb
  annotations: "http" "lb-large"
  type: LoadBalancer
    app: nginx-example
    - name: http
      protocol: TCP
      port: 80
      targetPort: 80


This is the minimum definition required to trigger creation of a DigitalOcean Load Balancer on your account and billing begins once the creation is completed. Currently, you cannot assign a floating IP address to a DigitalOcean Load Balancer.

Load balancer size can be set to lb-small, lb-medium or lb-large. The default size is lb-small. You can resize the load balancer after creation but not more than once per hour.

Show Load Balancers

Once you apply the config file to a deployment, use kubectl get services to see its status:

kubectl --kubeconfig=[full path to cluster config file] get services
NAME                   TYPE           CLUSTER-IP      EXTERNAL-IP      PORT(S)        AGE
kubernetes             ClusterIP       <none>           443/TCP        2h
sample-load-balancer   LoadBalancer     <pending>   80:32490/TCP        6s

When the load balancer creation is complete, <pending> will show the external IP address instead. In the PORT(S) column, the first port is the incoming port (80), and the second port is the node port (32490), not the container port supplied in the targetPort parameter.

Show Details for One Load Balancer

To get detailed information about the load balancer configuration of a single load balancer, use kubectl’s describe service command:

kubectl -kubeconfig=[full path to cluster config file] describe service [NAME]
Name:                     sample-load-balancer
Namespace:                default
Labels:                   <none>
Annotations:    {"apiVersion":"v1","kind":"Service","metadata":{"annotations":{},"name":"sample-load-balancer","namespace":"default"},"spec":{"ports":[{"name":"https",...
Selector:                 <none>
Type:                     LoadBalancer
LoadBalancer Ingress:
Port:                     https  80/TCP
TargetPort:               443/TCP
NodePort:                 https  32490/TCP
Endpoints:                <none>
Session Affinity:         None
External Traffic Policy:  Cluster
  Type    Reason                Age               From                Message
  ----    ------                ----              ----                -------
  Normal  EnsuringLoadBalancer  3m (x2 over 38m)  service-controller  Ensuring load balancer
  Normal  EnsuredLoadBalancer   1m (x2 over 37m)  service-controller  Ensured load balancer


For more about managing load balancers, see: