Bookmark this page

Guided Exercise: Virtual Machine Load Balancing with Kubernetes Networking Resources

Create a service and a route with a custom cookie for two web servers to host the helloworld application to confirm session stickiness and service load balancing.

Outcomes

  • Create a helloworld service for the web servers, and verify the service endpoints.

  • Create a route with a custom cookie for the helloworld service.

  • Observe the session stickiness that the custom cookie provides.

  • Stop one of the web servers to observe the service resilience and the automatic load balancing from Kubernetes.

As the student user on the workstation machine, use the lab command to prepare your environment for this exercise, and to ensure that all required resources are available.

[student@workstation ~]$ lab start ha-loadbalance

Instructions

  1. As the admin user, confirm that the server1 and server2 virtual machines (VMs) are running on the ha-loadbalance project.

    1. From a command line, log in to your Red Hat OpenShift cluster as the admin user with redhatocp as the password.

      [student@workstation ~]$ oc login -u admin -p redhatocp \
        https://api.ocp4.example.com:6443
      Login Successful
      ...output omitted...
    2. Change to the ha-loadbalance project.

      [student@workstation ~]$ oc project ha-loadbalance
      Now using project "ha-loadbalance" on server "https://api.ocp4.example.com:6443".
    3. Verify that the server1 and server2 VMs are running.

      [student@workstation ~]$ oc get vm
      NAME      AGE     STATUS    READY
      server1   4m25s   Running   True
      server2   2m23s   Running   True
  2. Create a helloworld service and verify that the service endpoints resolve to the virtual machine instances (VMIs) of the server1 and server2 VMs.

    1. Create the helloworld service CR YAML file. You can find an incomplete example in the ~/DO316/labs/ha-loadbalance/svc.yml file.

      The resource file must define the helloworld service in the ha-loadbalance namespace. The resource file must also define the service selector as the app: helloworld label. The server1 and server2 VMIs contain the app: helloworld label.

      apiVersion: v1
      kind: Service
      metadata:
        name: helloworld
        namespace: ha-loadbalance
      spec:
        type: ClusterIP
        selector:
          app: helloworld
        ports:
          - protocol: TCP
            port: 80
            targetPort: 80
    2. Use the oc command to create the resource:

      [student@workstation ~]$ oc create -f ~/DO316/labs/ha-loadbalance/svc.yml
      service/helloworld created
    3. Verify the IP addresses for the server1 and server2 VMIs. The IP addresses might differ on your system.

      [student@workstation ~]$ oc get vmi
      NAME      AGE     PHASE     IP           NODENAME   READY
      server1   4m51s   Running   10.8.2.43    worker02   True
      server2   3m7s    Running   10.11.0.36   worker01   True
    4. Verify that the helloworld service endpoints resolve to the IP addresses of the server1 and server2 VMIs.

      [student@workstation ~]$ oc get endpoints
      NAME         ENDPOINTS                    AGE
      helloworld   10.11.0.36:80,10.8.2.43:80   52s
  3. Create a helloworld route for the helloworld service.

    1. Create the helloworld route CR YAML file. You can find an incomplete example in the ~/DO316/labs/ha-loadbalance/route.yml file.

      apiVersion: route.openshift.io/v1
      kind: Route
      metadata:
        name: helloworld 1
        namespace: ha-loadbalance
        annotations:
          router.openshift.io/cookie_name: "hello" 2
      spec:
        host: hello-ha-loadbalance.apps.ocp4.example.com 3
        port:
          targetPort: 80
        to:
          kind: Service
          name: helloworld 4

      1

      The name of the route.

      2

      The name of the custom cookie for the route. Applying the router.openshift.io/cookie_name="cookie_name" annotation to the route creates a custom-named cookie.

      3

      The .spec.host object defines a custom URL for the route. If the .spec.host object is not included, then the default naming convention, route_name-namespace.apps.ocp4.example.com, is used for the host.

      4

      The name of the service for the route.

    2. Use the oc command to create the resource.

      [student@workstation ~]$ oc create -f ~/DO316/labs/ha-loadbalance/route.yml
      route.route.openshift.io/helloworld created.
    3. Confirm that the route is configured with the hello-ha-loadbalance.apps.ocp4.example.com host.

      [student@workstation ~]$ oc get route
      NAME        HOST/PORT                                   PATH  SERVICES    ...
      helloworld  hello-ha-loadbalance.apps.ocp4.example.com        helloworld  ...
  4. Use the curl command to confirm that the helloworld route is accessible. Confirm that the route provides the hello custom-named cookie, and observe the session stickiness from the cookie.

    1. Use the curl command to access the helloworld route. The output states the name of the web server that is servicing the request. You might need to run the command many times to verify that the load is balanced between the two web servers.

      [student@workstation ~]$ curl hello-ha-loadbalance.apps.ocp4.example.com
      <!DOCTYPE html>
      <html>
          <head>
              <title>Hello, World!</title>
          </head>
          <body>
              <p>Hello, World, from server1.</p>
          </body>
      </html>
      [student@workstation ~]$ curl hello-ha-loadbalance.apps.ocp4.example.com
      <!DOCTYPE html>
      <html>
          <head>
              <title>Hello, World!</title>
          </head>
          <body>
              <p>Hello, World, from server2.</p>
          </body>
      </html>
    2. Use the curl command to save the hello cookie to the /tmp/cookie-jar file.

      [student@workstation ~]$ curl hello-ha-loadbalance.apps.ocp4.example.com \
        -c /tmp/cookie-jar
      <!DOCTYPE html>
      <html>
          <head>
              <title>Hello, World!</title>
          </head>
          <body>
              <p>Hello, World, from server2.</p>
          </body>
      </html>
    3. Confirm that the hello cookie exists in the /tmp/cookie-jar file.

      [student@workstation ~]$ cat /tmp/cookie-jar
      # Netscape HTTP Cookie File
      # https://curl.se/docs/http-cookies.html
      # This file was generated by libcurl! Edit at your own risk.
      
      #HttpOnly_hello-ha-loadbalance.apps.ocp4.example.com  FALSE  /  FALSE 0	hello	a673...6722
    4. The hello cookie provides session stickiness for connections to the helloworld route.

      Use the curl command and the hello cookie to connect to the helloworld route again. Run the command many times to confirm that you are connected to the same web server that handled the request in the previous step.

      [student@workstation ~]$ curl hello-ha-loadbalance.apps.ocp4.example.com \
        -b /tmp/cookie-jar
      <!DOCTYPE html>
      <html>
          <head>
              <title>Hello, World!</title>
          </head>
          <body>
              <p>Hello, World, from server2.</p>
          </body>
      </html>
      [student@workstation ~]$ curl hello-ha-loadbalance.apps.ocp4.example.com \
        -b /tmp/cookie-jar
      <!DOCTYPE html>
      <html>
          <head>
              <title>Hello, World!</title>
          </head>
          <body>
              <p>Hello, World, from server2.</p>
          </body>
      </html>
    5. Use the curl command to connect to the helloworld route without the hello cookie. Run the command many times and observe that session stickiness occurs only with the hello cookie.

      [student@workstation ~]$ curl hello-ha-loadbalance.apps.ocp4.example.com
      <!DOCTYPE html>
      <html>
          <head>
              <title>Hello, World!</title>
          </head>
          <body>
              <p>Hello, World, from server1.</p>
          </body>
      </html>
      [student@workstation ~]$ curl hello-ha-loadbalance.apps.ocp4.example.com
      <!DOCTYPE html>
      <html>
          <head>
              <title>Hello, World!</title>
          </head>
          <body>
              <p>Hello, World, from server2.</p>
          </body>
      </html>
  5. Use the virtctl client to stop the server1 VMI. Confirm that Kubernetes automatically updates the helloworld service endpoints not to include the server1 VMI.

    1. Retrieve the helloworld service endpoints.

      [student@workstation ~]$ oc get endpoints
      NAME         ENDPOINTS                    AGE
      helloworld   10.11.0.36:80,10.8.2.43:80   4m21s
    2. Retrieve the IP addresses for the server1 and server2 VMIs.

      [student@workstation ~]$ oc get vmi
      NAME      AGE     PHASE     IP           NODENAME   READY
      server1   9m31s   Running   10.8.2.43    worker02   True
      server2   7m47s   Running   10.11.0.36   worker01   True
    3. Use the virtctl client to shut down the server1 VM.

      [student@workstation ~]$ virtctl stop server1
      VM server1 was scheduled to stop
    4. Confirm that OpenShift updates the helloworld service endpoint to the IP address of the server2 VMI, and that it does not contain the IP address of the server1 VMI.

      [student@workstation ~]$ oc get endpoints
      NAME         ENDPOINTS       AGE
      helloworld   10.11.0.36:80   5m20s
    5. Use the curl command to verify that you can connect to the helloworld route. Confirm that the server2 web server responds to the request.

      [student@workstation ~]$ curl hello-ha-loadbalance.apps.ocp4.example.com
      <!DOCTYPE html>
      <html>
          <head>
              <title>Hello, World!</title>
          </head>
          <body>
              <p>Hello, World, from server2.</p>
          </body>
      </html>
  6. Use the virtctl client to start the server1 VM. Confirm that the helloworld service endpoint contains the IP address of the server1 VMI. Use the curl command to connect to the helloworld route, and confirm that the service is load balanced between the two web servers.

    1. Start the server1 VM with the virtctl client.

      [student@workstation ~]$ virtctl start server1
      VM server1 was scheduled to start
    2. Verify that the server1 VMI is running.

      [student@workstation ~]$ oc get vmi
      NAME      AGE     PHASE     IP           NODENAME   READY
      server1   63s     Running   10.8.2.44    worker02   True
      server2   9m55s   Running   10.11.0.36   worker01   True
    3. Confirm that OpenShift updates the helloworld service endpoint to include the IP address of the server1 VMI.

      [student@workstation ~]$ oc get endpoints
      NAME         ENDPOINTS                    AGE
      helloworld   10.11.0.36:80,10.8.2.44:80   7m15s
    4. Use the curl command to send three subsequent requests to the helloworld route. Observe that both the server1 and server2 VMIs respond to the request. OpenShift load balances the requests between the two web servers. You might need to run the command many times to verify the load balance.

      [student@workstation ~]$ curl hello-ha-loadbalance.apps.ocp4.example.com/?[1-3]
      
      [1/3]: hello-ha-loadbalance.apps.ocp4.example.com/?1 --> <stdout>
      --_curl_--hello-ha-loadbalance.apps.ocp4.example.com/?1
      <!DOCTYPE html>
      <html>
          <head>
              <title>Hello, World!</title>
          </head>
          <body>
              <p>Hello, World, from server2.</p>
          </body>
      </html>
      
      
      [2/3]: hello-ha-loadbalance.apps.ocp4.example.com/?2 --> <stdout>
      --_curl_--hello-ha-loadbalance.apps.ocp4.example.com/?2
      <!DOCTYPE html>
      <html>
          <head>
              <title>Hello, World!</title>
          </head>
          <body>
              <p>Hello, World, from server1.</p>
          </body>
      </html>
      
      
      [3/3]: hello-ha-loadbalance.apps.ocp4.example.com/?3 --> <stdout>
      --_curl_--hello-ha-loadbalance.apps.ocp4.example.com/?3
      <!DOCTYPE html>
      <html>
          <head>
              <title>Hello, World!</title>
          </head>
          <body>
              <p>Hello, World, from server2.</p>
          </body>
      </html>

Finish

On the workstation machine, use the lab command to complete this exercise. This step is important to ensure that resources from previous exercises do not impact upcoming exercises.

[student@workstation ~]$ lab finish ha-loadbalance

Revision: do316-4.14-d8a6b80