4 Using the Oracle Cloud Infrastructure Load Balancer

This chapter discusses how to use the Oracle Cloud Infrastructure Cloud Controller Manager module to set up a load balancer for Kubernetes applications in Oracle Cloud Native Environment on Oracle Cloud Infrastructure instances.

Introduction to Oracle Cloud Infrastructure Load Balancers

The Oracle Cloud Infrastructure Flexible Network Load Balancing service (Oracle Cloud Infrastructure load balancer) provides automated traffic distribution from one entry point to many backend servers in a Virtual Cloud Network (VCN). It operates at the connection level and load balances incoming client connections to healthy backend servers based on Layer 3/Layer 4 (IP protocol) data.

For more information on the Oracle Cloud Infrastructure load balancer, see the Oracle Cloud Infrastructure documentation.

The Kubernetes Cloud Controller Manager ServiceController is responsible for creating load balancers when a Kubernetes LoadBalancer service is created. The Platform API Server communicates with the Oracle Cloud Infrastructure API to provision and manage Oracle Cloud Infrastructure load balancers.

Creating an Application Using an Oracle Cloud Infrastructure Load Balancer

This section contains a basic test to verify you can create a Kubernetes application that uses an Oracle Cloud Infrastructure load balancer to provide external IP addresses.

To create a test application to use an Oracle Cloud Infrastructure load balancer:

  1. Create a Kubernetes application that uses a LoadBalancer service. The deployment in this example creates an NGINX application with a replica count of 2, and an associated LoadBalancer service.

    On a control plane node, create a file named nginx-oci-lb.yaml and copy the following into the file.

    ---
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: nginx-deployment
      labels:
        app: nginx
    spec:
      replicas: 2
      selector:
        matchLabels:
          app: nginx
      template:
        metadata:
          labels:
            app: nginx
        spec:
          containers:
          - name: nginx
            image: container-registry.oracle.com/olcne/nginx:1.17.7 
            ports:
            - containerPort: 80
    ---
    kind: Service
    apiVersion: v1
    metadata:
      name: nginx-service
      annotations: 
        service.beta.kubernetes.io/oci-load-balancer-security-list-management-mode: "None"
        service.beta.kubernetes.io/oci-load-balancer-internal: "true"
        service.beta.kubernetes.io/oci-load-balancer-shape: "flexible"
        service.beta.kubernetes.io/oci-load-balancer-shape-flex-min: "10"
        service.beta.kubernetes.io/oci-load-balancer-shape-flex-max: "10"
    spec:
      selector:
        app: nginx
      type: LoadBalancer
      ports:
      - name: http
        port: 80
        targetPort: 80

    The annotations section contains the information required to provision an Oracle Cloud Infrastructure load balancer. This is where you set the load balancer shape. For example, to use a 10Mbps shape instead of the flexible shape, you might use:

     annotations: 
        service.beta.kubernetes.io/oci-load-balancer-security-list-management-mode: "None"
        service.beta.kubernetes.io/oci-load-balancer-internal: "true"
        service.beta.kubernetes.io/oci-load-balancer-shape: "10Mbps"

    In some Oracle Cloud Infrastructure tenancies, you might also need to include the oci-load-balancer-subnet1 annotation to identify the network subnet, for example:

        service.beta.kubernetes.io/oci-load-balancer-subnet1: "ocid1.subnet.oc1..unique_ID" 

    For the full list of annotations you can include, see the upstream documentation at:

    https://github.com/oracle/oci-cloud-controller-manager/blob/master/docs/load-balancer-annotations.md

  2. Start the NGINX deployment and LoadBalancer service:

    kubectl apply -f nginx-oci-lb.yaml
  3. You can see the nginx-deployment application is running using the kubectl get deployment command:

    kubectl get deployments.apps

    The output looks similar to:

    NAME               READY   UP-TO-DATE   AVAILABLE   AGE
    nginx-deployment   2/2     2            2           31s
  4. You can see the nginx-deployment service is running using the kubectl get svc command:

    kubectl get svc nginx-service

    The output looks similar to:

    NAME            TYPE           CLUSTER-IP      EXTERNAL-IP       PORT(S)        AGE
    nginx-service   LoadBalancer   10.99.107.243   203.0.113.10      80:31288/TCP   10m

    Oracle Cloud Infrastructure might take a few minutes to assign an IP address. Until this completes, the EXTERNAL-IP column shows the pending state for the nginx-service. When the IP address is assigned, this field changes to show the IP address.

    Tip:

    You can see the load balancer is created in Oracle Cloud Infrastructure under Networking > Load Balancers.

    You can see the EXTERNAL-IP for the nginx-service LoadBalancer has an IP address of 203.0.113.10. This IP address is provided by Oracle Cloud Infrastructure and is the external IP address that you can use to connect to the application.

  5. Use curl to connect to the NGINX application's IP address and add the port for the application (203.0.113.10:80 in this example) to show the NGINX default page.

    curl 203.0.113.10:80

    The output looks similar to:

    <!DOCTYPE html>
    <html>
    <head>
    <title>Welcome to nginx!</title>
    <style>
        body {
            width: 35em;
            margin: 0 auto;
            font-family: Tahoma, Verdana, Arial, sans-serif;
        }
    </style>
    </head>
    <body>
    <h1>Welcome to nginx!</h1>
    <p>If you see this page, the nginx web server is successfully installed and
    working. Further configuration is required.</p>
    
    <p>For online documentation and support please refer to
    <a href="http://nginx.org/">nginx.org</a>.<br/>
    Commercial support is available at
    <a href="http://nginx.com/">nginx.com</a>.</p>
    
    <p><em>Thank you for using nginx.</em></p>
    </body>
    </html>
  6. You can delete the nginx-service LoadBalancer service using:

    kubectl delete svc nginx-service

    Tip:

    You can see the load balancer is removed in Oracle Cloud Infrastructure under Networking > Load Balancers.

  7. You can delete the nginx-deployment application using:

    kubectl delete deployments.apps nginx-deployment