Help us improve your experience.

Let us know what you think.

Do you have time for a two-minute survey?

 
 

Create a Load Balancer Service

SUMMARY This topic describes how to create a Load Balancer service in Juniper Cloud-Native Contrail® Networking (CN2). Juniper Networks supports this feature using Contrail Networking Release 22.1 or later in a Kubernetes-orchestrated environment.

Load Balancer Service Overview

In Kubernetes, a service is an abstract way to expose an application running on a set of pods as a network service. Kubernetes supports three types of services: ClusterIP, NodePort and LoadBalancer. This topic describes how to create a load balancer service in CN2.

In CN2, a load balancer service is implemented with the InstanceIP resource and FloatingIP resource as described below:

  • The FloatingIP is used in the service implementation to expose an external IP to the load balancer service. The FloatingIP resource is also associated with the pod's VirtualMachineInterfaces.

  • The InstanceIP resource is related to the VirtualNetwork. Two instanceIPs are created, one for the service network and one for the external network.

A controller service is implemented in Contrail's kube-manager. Kube-manager is the interface between Kubernetes core resources and the extended Contrail resources, such as the VirtualNetwork.When you create a load balancer service, kube-manager listens and allocates the IP from an external virtual network. This external virtual network exposes the load balancer service on the external IPs. Any requests received through the provisioned external IP is ECMP load-balanced across the pods associated with the load balancer.

Create a Load Balancer Service

The following sections describe how to create a load balancer service in CN2.

Prerequisites

Before you begin, make sure of the following:

  • You have set up a working cloud networking environment with Kubernetes.

  • CN2 is installed and is operational.

  • You have configured kube-manager to define the external networks to be used by the load balancer service.

Define an External Virtual Network

First, define an external virtual network. You can define the virtual network two ways: by creating a Network Attachment Definition (NAD) or by creating a virtual network.

Note:

A Multus deployment requires that you use a NAD to define an external network.

The following example shows how to define an external virtual network using a NetworkAttachmentDefinition. In this example, the external IP is allocated from the subnet range 192.168.102.0/24. When the NetworkAttachmentDefinition is applied, kube-manager creates a virtual network with the name ecmp-default in the namespace ecmp-project.

Specify the External Networks

By default, kube-manager allocates the external IP for a load balancer service from the default-external network. If desired, you can allocate the external IP from a different network by defining custom selectors, as shown in the following example:

The VirtualNetworks in this example match the labels shown in the previous example (in relative order).

Define Service-Level Annotations

Additionally, you can define the following service-level annotations for external network discovery.

Annotation: externalNetwork.

In this example, the externalNetwork annotation allocates an external IP from the evn virtual network in the namespace ns.

Annotation: externalNetworkSelector

In this example, the externalNetworkSelector matches the name of the externalNetworkSelector defined in kube-manager.

Note:

You can also define service-level annotations in the namespace of the Kubernetes cluster or in the namespace of the Contrail cluster. The service-level annotation take precedence.

Examples: External Network Selection

The external virtual network is selected from one of the following in priority order:

Note:

The virtual networks defined in Specify the External Networks are linked to the annotations in the following examples.

Example 1: Default Selector

Kube-manager first searches for the default external network. This example uses the default-external selector because no annotation is specified.

This example matches the network contrail/default-external-vn.

Example 2: Custom Namespace

This example matches the network custom-namespace/external-vn-1.

Example 3: External Network Matches Preconfigured Selector in a Namespace

This example matches the network custom-namespace/external-vn-1.

Example 4: External Network Matches Preconfigured Selector in Service Namespace

This example matches the network custom-namespace/external-vn-2.

Dual-Stack Networking Support

IPv4 or IPv6 dual-stack networking enables the allocation of both IPv4 and IPv6 addresses to pods and services. As an administrator, you might need to select the IP family (IPv4 or IPv6) to use when defining a service. If you do not define the IP family, the default IPv4 address is used.

For more information, see Overview: IPv4 and IPv6 Dual-Stack Networking.

Configure Load Balancer Services Without Selectors

In Kubernetes, you can expose an application running on a set of pods as a network service. Kubernetes uses selectors to automatically create a load balancer service, but only uses the default primary interface for load balancing.

Starting in CN2 Release 22.3, you can load balance a service across multiple secondary interfaces. You can create secondary interfaces in CN2 without using a selector. Because the load balancer service has no selector, you must create the endpoint manually.

To configure load balancer services without selectors:

  1. Create two virtual networks.
    The following example shows two networks. One network for the pod's secondary interface (pod-subnet) and another network (lb-subnet) for the load balancer service external IP. These networks are connected by a common route target that routes traffic between the two networks.
  2. Create the pods on which you want to load balance the service. You can create multiple pods.
    In this example, we'll create two pods in the my-lb namespace, each with its own IP address.
  3. Create a Load Balancer service.
    In this example, we'll create a load balancer service (service-lb) in the my-lb namespace without using a selector.
  4. Specify the endpoints (IP addresses) on which you want to load balance the service. Make sure that the endpoint has the same name as the load balancer service.
    In this example, we specified two pod endpoints for the secondary interfaces (ip: 172.16.23.0 and ip: 172.16.24.0).
Success! You can now load balance a service across multiple pods with the secondary interface.
Note:

In addition to creating a load balancer service on the secondary interface, you can use a selector to create a load balancer on the default primary interface. The default primary interface can work in tandem with the secondary interface. You can use either interface to load balance across your desired service.