l Without Session Persistence
1. Prerequisites
Create a nginx workload, and ensure the workload includes more than one instance. This workload does not require any additional special configurations.
Create a ClusterIP type of Service and associate it with the above nginx workload. Note that there is no need to adjust the Session Affinity, and you can just maintain its default value.
2. Test Verification
Initiate service calls by executing the following command on the cluster nodes to generate 100 service calls: > for i in {1..100};do curl 10.96.116.221:80;done;
The IP and port in the above curl command originate from the following locations:
Check the workload logs. The log output of the first Pod instance is shown below:
The log output of the second Pod instance is shown below:
Conclusion: Service requests will be randomly forwarded to any Pod instance
l Request for Session Persistence in Cluster
1. Prerequisites
Create a nginx workload, same as above.
Create a ClusterIP type of Service and associate it with the above nginx workload. Note that you need to expand advanced settings and set Session Affinity to Client IP.
2. Test Verification
Initiate service calls, same as above. Check the workload logs.
The log output of the first Pod instance is shown below:
The log output of the second Pod instance is shown below:
Conclusion: All service requests will be forwarded to a specific Pod instance, enabling session persistence.
l Request for Session Persistence in NodePort Access Outside Cluster
1. Prerequisites
Create a nginx workload, same as above. Create a NodePort type of Service and associate it with the above nginx workload. Specify a valid host port. Note that you need to expand advanced settings and set Session Affinity to Client IP.
2. Test Verification
Initiate service calls by executing the following command outside the cluster to generate 100 service calls: > for i in {1..100};do curl 10.142.232.160:30080;done;
The IP in the above curl command can be the VIP of the cluster or the IP of any cluster node. The port is the host port specified in the Service.
Check the workload logs. The log output of the first Pod instance is shown below:
The log output of the second Pod instance is shown below:
Conclusion: All requests outside the cluster will be forwarded to a specific Pod instance, enabling session persistence.
l Request for Session Persistence in Ingress Access Outside Cluster
1. Prerequisites
Create a nginx workload, same as above.
Create a ClusterIP type of Service and associate it with the above nginx workload. Note that there is no need to adjust the Session Affinity, and you can just maintain its default value.
Ensure the current namespace is already bound to a Load Balancer:
Create a production route Ingress and associate it with the above Service.
Manually edit the Ingress resource file, add the following annotation: # kubectl edit ingresses nginx-http -napp-istio annotations: nginx.ingress.kubernetes.io/affinity: cookie
Add local host mapping: # ip is the access address of the Nginx-Ingress-Controller # Domain name is the domain name entered when creating Ingress 10.142.232.160 nginx.ccse.io
2. Initiate service calls
Initiate multiple service requests in the browser > http://nginx.ccse.io:10080/
Curl-based testing verification is not applicable here, because Curl requests cannot retain Cookies.
3. Check the workload logs
The log output of the first Pod instance is shown below:
The log output of the second Pod instance is shown below:
Conclusion: All requests in the browser will be forwarded to a specific Pod instance, enabling session persistence.
l TCP/UDP Access Outside Cluster
Conclusion: Access through TCP/UDP outside the cluster cannot achieve session persistence.
2. Reference
https://github.com/kubernetes/ingress-nginx/tree/master/docs/examples/affinity/cookie