Creating external load balancers in Kubernetes

In Kubernetes, you can create a service with an external load balancer that provides access to it from public networks. The load balancer will receive a publicly accessible IP address and route incoming requests to the correct port on the Kubernetes cluster nodes.

Prerequisites

  • To be able to assign a specific floating IP address to an external load balancer during its deployment, this floating IP address must be created in advance, as described in Managing floating IP addresses.

To create a service with an external load balancer

  1. Access the Kubernetes cluster via the dashboard. Click Kubernetes access for instructions.
  2. On the Kubernetes dashboard, create a deployment and service of the LoadBalancer type. To do it, click + Create and specify a YAML file that defines these objects. For example:

    • If you have deployed the Kubernetes cluster in a shared physical network, specify the following manifest:

      apiVersion: apps/v1
      kind: Deployment
      metadata:
        name: nginx
      spec:
        replicas: 2
        selector:
          matchLabels:
            app: nginx
        template:
          metadata:
            labels:
              app: nginx
          spec:
            containers:
            - name: nginx
              image: nginx
              ports:
              - containerPort: 80
      ---
      kind: Service
      apiVersion: v1
      metadata:
        name: load-balancer
        annotations:
          service.beta.kubernetes.io/openstack-internal-load-balancer: "true"
      spec:
        selector:
          app: nginx
        type: LoadBalancer
        ports:
        - port: 80
          targetPort: 80
          protocol: TCP
      

      The manifest above describes the deployment nginx with a replica set of two pods and the service load-balancer with the LoadBalancer type. The annotation used for the service indicates that the load balancer will be internal.

      Once the load balancer is created, it will be allocated an IP address from the shared physical network and can be accessed at this external endpoint.

    • If you have deployed the Kubernetes cluster in a virtual network linked to a physical one via a virtual router, you can use the YAML file above without the annotations section for the load-balancer service. The created load balancer will receive a floating IP address from the physical network and can be accessed at this external endpoint. To use a specific floating IP address, create it in the self-service panel in advance, and then specify it with the loadBalancerIP parameter:

      <...>
      ---
      kind: Service
      apiVersion: v1
      metadata:
        name: load-balancer
      spec:
        selector:
          app: nginx
        type: LoadBalancer
        loadBalancerIP: 10.10.10.100
        ports:
        - port: 80
          targetPort: 80
          protocol: TCP
      
    • If you want to choose whether to create highly available load balancers for your service or not, you can make use of load balancer flavors. To specify a flavor for a load balancer add loadbalancer.openstack.org/flavor-id: <flavor-id> to the annotations section. The flavor ID can be obtained from your system administrator.

The load balancer will also appear in the self-service panel, where you can monitor its performance and health. For example: