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
As the admin user, confirm that the server1 and server2 virtual machines (VMs) are running on the ha-loadbalance project.
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...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".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 TrueCreate a helloworld service and verify that the service endpoints resolve to the virtual machine instances (VMIs) of the server1 and server2 VMs.
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:helloworldnamespace:ha-loadbalancespec: type: ClusterIP selector:app: helloworldports: - protocol: TCP port: 80 targetPort: 80
Use the oc command to create the resource:
[student@workstation ~]$ oc create -f ~/DO316/labs/ha-loadbalance/svc.yml
service/helloworld createdVerify 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 TrueVerify 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 52sCreate a helloworld route for the helloworld service.
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:helloworldnamespace:
ha-loadbalanceannotations: router.openshift.io/cookie_name: "hello"spec: host: hello-ha-loadbalance.apps.ocp4.example.com
port: targetPort: 80 to: kind: Service name:
helloworld
The name of the route. | |
The name of the custom cookie for the route.
Applying the | |
The | |
The name of the service for the route. |
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.Confirm that the route is configured with the hello-ha-loadbalance.apps.ocp4.example.com host.
[student@workstation ~]$oc get routeNAME HOST/PORT PATH SERVICES ... helloworldhello-ha-loadbalance.apps.ocp4.example.comhelloworld ...
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.
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, fromserver1.</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, fromserver2.</p> </body> </html>
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, fromserver2.</p> </body> </html>
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 0helloa673...6722
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, fromserver2.</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, fromserver2.</p> </body> </html>
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, fromserver1.</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, fromserver2.</p> </body> </html>
Use the virtctl client to stop the server1 VMI.
Confirm that Kubernetes automatically updates the helloworld service endpoints not to include the server1 VMI.
Retrieve the helloworld service endpoints.
[student@workstation ~]$ oc get endpoints
NAME ENDPOINTS AGE
helloworld 10.11.0.36:80,10.8.2.43:80 4m21sRetrieve 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 TrueUse the virtctl client to shut down the server1 VM.
[student@workstation ~]$ virtctl stop server1
VM server1 was scheduled to stopConfirm 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 5m20sUse 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, fromserver2.</p> </body> </html>
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.
Start the server1 VM with the virtctl client.
[student@workstation ~]$ virtctl start server1
VM server1 was scheduled to startVerify 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 TrueConfirm 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 7m15sUse 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, fromserver2.</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, fromserver1.</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, fromserver2.</p> </body> </html>