How to Add a Load Balancer to a Kubernetes Cluster

Kubernetes is currently in limited availability, so it may not be visible or available for your account. Learn more.

The DigitalOcean Cloud Controller supports provisioning DigitalOcean Load Balancers in a cluster’s resource configuration file.

The example configuration will define a load balancer and create it if one with the same name does not already exist.

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:

spec: 
  type: LoadBalancer
  ports:
    - port: 80
      targetPort: 3000
      name: http      

In the context of a service file, this might look like:

apiVersion: v1
kind: Service
metadata:
  name: sample-load-balancer
spec:
  type: LoadBalancer
  ports:
    - port: 80
      targetPort: 443
      name: http

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.

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      192.0.2.1       <none>           443/TCP        2h
sample-load-balancer   LoadBalancer   192.0.2.167     <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:              kubectl.kubernetes.io/last-applied-configuration={"apiVersion":"v1","kind":"Service","metadata":{"annotations":{},"name":"sample-load-balancer","namespace":"default"},"spec":{"ports":[{"name":"https",...
Selector:                 <none>
Type:                     LoadBalancer
IP:                       192.0.2.167
LoadBalancer Ingress:     203.0.113.86
Port:                     https  80/TCP
TargetPort:               443/TCP
NodePort:                 https  32490/TCP
Endpoints:                <none>
Session Affinity:         None
External Traffic Policy:  Cluster
Events:
  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

Delete Load Balancers

To delete a cluster’s load balancer with kubectl, use the delete service command:

kubectl -kubeconfig=[full path to cluster config file] delete service [NAME]

When this command is successful, it both removes the load balancer from the cluster and deletes it from your account.

After cluster deletion

When you delete a cluster with a load balancer service from the control panel, the load balancer is not automatically deleted. In this case, in the Networking section of the control panel, select Load Balancers, open the load balancer’s More menu, and select Edit settings, click Destroy, and confirm.

Note that while you can currently delete block storage volumes and load balancers from the control panel, we recommend that you use kubectl to manage all cluster-related resources.

References

For more about managing load balancers see: