Creating an external cloud load balancer

When you create an application and expose it as a Kubernetes service, you usually need the service to be reachable externally via an IP address or URL. In this recipe, you will learn how to create a load balancer, also referred to as a cloud load balancer.

In the previous chapters, we have seen a couple of examples that used the load balancer service type to expose IP addresses, including the Configuring and managing S3 object storage using MinIO and Application backup and recovery using Kasten recipes in the previous chapter, as well as the To-Do application that was provided in this chapter in the Assigning applications to nodes recipe.

Let's use the MinIO application to learn how to create a load balancer. Follow these steps to create a service and expose it using an external load balancer service:

  1. Review the content of the minio.yaml file in the examples directory in src/chapter7/lb and deploy it using the following command. This will create a StatefulSet and a service where the MinIO port is exposed internally to the cluster via port number 9000. You can choose to apply the same steps and create a load balancer for your own application. In that case, skip to Step 2:
$ kubectl apply -f minio.yaml
  1. List the available services on Kubernetes. You will see that the MinIO service shows ClusterIP as the service type and none under the EXTERNAL-IP field: 
$ kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 100.64.0.1 <none> 443/TCP 5d
minio ClusterIP None <none> 9000/TCP 4m
  1. Create a new service with the TYPE set to LoadBalancer. The following command will expose port: 9000 of our MinIO application at targetPort: 9000 using the TCP protocol, as shown here:
cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: Service
metadata:
name: minio-service
spec:
type: LoadBalancer
ports:
- port: 9000
targetPort: 9000
protocol: TCP
selector:
app: minio
EOF

The preceding command will immediately create the Service object, but the actual load balancer on the cloud provider side may take 30 seconds to a minute to be completely initialized. Although the object will state that it's ready, it will not function until the load balancer is initialized. This is one of the disadvantages of cloud load balancers compared to ingress controllers, which we will look at in the next recipe, Creating an ingress service and service mesh using Istio.

As an alternative to Step 3, you can also create the load balancer by using the following command:

$ kubectl expose rc example --port=9000 --target-port=9000 --name=minio-service --type=LoadBalancer