Steps:
Deploy a simple web-application:
kubectl run mywebapp --image="vavy/weballheaders" --labels app=mytestapp --port 80 --replicas=2
Next, expose the app by setting up a NodePort service with two additional annotations:
traefik.ingress.kubernetes.io/affinity: "true"
traefik.ingress.kubernetes.io/session-cookie-name: "mysession"
as defined in the mynodeport.yaml file:
apiVersion: v1
kind: Service
metadata:
name: mynodeport
labels:
app: mytestapp
annotations:
traefik.ingress.kubernetes.io/affinity: "true"
traefik.ingress.kubernetes.io/session-cookie-name: "mysession"
spec:
ports:
- protocol: TCP
port: 80
targetPort: 80
selector:
app: mytestapp
type: NodePort
Next, Apply the configuration with the command:
kubectl create -f mynodeport.yaml
Now let's configure and create the Traefik ingress object to forward incoming traffic from clients to the applications pods by using the annotation:
kubernetes.io/ingress.class: traefik as defined in the myingressyaml file:
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: myingress
annotations:
kubernetes.io/ingress.class: traefik
spec:
rules:
- host: "app1.vavilov.org"
http:
paths:
- backend:
serviceName: mynodeport
servicePort: 80
Next, apply the configuration with the command:
kubectl create -f myingress.yaml
As a result, we'll see that according to the service's annotation we now have cookie-based stickiness.
The url of the specific pod is stored in the cookie "mysession" (See the following screenshot illustrating this):
You can also observe the enabled applications backend stickiness in the Traefik UI as illustrated in the following screenshot: