VK Cloud logo
Updated at April 15, 2024   08:50 AM

Using load balancers

For all Kubernetes services of the spec.type: LoadBalancer type, the VK Cloud platform can automatically create an appropriate TCP load balancer with the specified behavior. The load balancer can:

  • Have a public IP address that is accessible from the Internet, or have a private IP address that is not accessible from the Internet. The IP address can be assigned to the balancer manually or automatically.

  • Use different algorithms for balancing connections between application instances:

    • Selecting random replica (default).

      The balancer behaves this way because kube-proxy in Cloud Containers clusters works in iptables mode. See official Kubernetes documentation for details.

    • Bounding a replica to a specific IP address.

      In this case, the balancer will assign a specific application replica to the IP address from which the first request came. As long as that replica remains available, all requests from that address will be forwarded to it.

  • Allow access only from specific IP addresses.

Preparatory steps

  1. Create a Kubernetes cluster with the most current version.

    Select the cluster parameters at your own discretion.

  2. Make sure you can connect to the cluster with kubectl.

1. Create application

Requests to this application will be served by the load balancer. To demonstrate the behavior of the load balancer, the application will be deployed as a StatefulSet of two replicas. In this case, all the sub-applications will be numbered, and it will be easy to determine which replica the load balancer will send the request to.

To create such an application:

  1. Create a manifest file:

  2. Create the Kubernetes resource based on the manifest:

    kubectl apply -f coffee.yaml

2. Create load balancers

Create several load balancers with different behaviors that serve the deployed coffee application:

  1. Select the public IP address you want to assign to the balancer or create a new one. You can do this in VK Cloud personal account.

    There should be no internal IP address attached to the IP address.

  2. Create the lb-static-public-ip.yaml manifest file.

    In the spec.loadBalancerIP parameter, specify the selected IP address.

    apiVersion: v1kind: Servicemetadata:  name: coffee-svc-public-static-ipspec:  type: LoadBalancer  loadBalancerIP: <selected IP address>  ports:  - port: 80    targetPort: 8080    protocol: TCP    name: http  selector:    app: coffee
  3. Create the Kubernetes resource based on the manifest:

    kubectl apply -f lb-static-public-ip.yaml
  4. Periodically check the status of the service with the command:

    kubectl get svc coffee-svc-public-static-ip

    Wait for the service to be assigned a static public IP address that was set manually earlier: in the EXTERNAL-IP table column, an IP address will appear instead of <pending>.

Read more about services and load balancers in official Kubernetes documentation.

3. Check the operation of the load balancers

  1. Get the IP address assigned to the load balancer:

    kubectl get svc coffee-svc-public-static-ip

    The required address is contained in the EXTERNAL-IP column of the table.

  2. Request the application several times via the load balancer:

    curl http://<load balancer IP address>

    Different pods may respond: both coffee-0 and coffee-1. This behavior means that the load balancer is sending requests to random replicas of the application.

Delete unused resources

  1. If the Kubernetes resources you created are no longer needed, delete them.

    kubectl delete svc coffee-svc-public-static-ipkubectl delete svc coffee-svc-session-affinitykubectl delete svc coffee-svc-restrict-access-by-ipkubectl delete svc coffee-svc-private-ipkubectl delete statefulset coffee
  2. If you no longer need the static public IP address that was assigned to the coffee-svc-public-static-ip service, delete it.

  3. A running cluster consumes computing resources. If you no longer need it: