K3s and LoadBalancer service

50 Views Asked by At

Using K3D and K3S on my Macbook I deployed a POD with:

  • replicas: 2
  • image: nginxdemos/hello

then I exposed a service:

apiVersion: v1
kind: Service
metadata:
  name: test
spec:
  ports:
    - name: web
      port: 8187
      protocol: TCP
      targetPort: 80
  selector:
    app: test
  type: LoadBalancer

and I map the port to my macbook

k3d cluster edit clutser-01 --port-add 8187:8187@loadbalancer

At this point I started to test my deployment but when I use curl or the browser I have two different unexpected behaviour.

If I use curl:

curl http://localhost:8187/ | grep Server

I get almost every time a different IP because the POD whi asnwer is different. For example:

10.42.0.18:80
or 10.42.0.19:80

On the contrary using the browser, even if I press refresh hundreds of time, the answer is always the same: and it is the first that I got. Then sometimes, after 5-10 minutes, maybe if I restart the browser, it changes, but once it happens then it remains stable for a long time.

I checked and no cookie is set.

Why this different behavior? What happens on browser side?

1

There are 1 best solutions below

1
ha36d On

It seems that you are using sticky sessions of traefik in k3s. Please check if your service has the following annotations.

traefik.ingress.kubernetes.io/service.sticky.cookie: "true"
traefik.ingress.kubernetes.io/service.sticky.cookie.name: cookie
traefik.ingress.kubernetes.io/service.sticky.cookie.secure: "true"