Traefik的模型是
route -> traefik service -> k8s service -> pod
这里构建了3个pod,分别对应3个 k8s service
apiVersion: traefik.containo.us/v1alpha1
kind: IngressRoute
metadata:
name: whoingressroute
namespace: who
spec:
entryPoints:
- web
routes:
- match: Host(`who.lizhe.com`)
kind: Rule
services:
- name: whots
kind: TraefikService
apiVersion: traefik.containo.us/v1alpha1
kind: TraefikService
metadata:
name: whots
namespace: who
spec:
weighted:
services:
- name: whoami1
weight: 3 # 定义权重
port: 80
kind: Service # 可选,默认就是 Service
- name: whoami2
weight: 3 # 定义权重
port: 80
kind: Service # 可选,默认就是 Service
- name: whoami3
weight: 3 # 定义权重
port: 80
kind: Service # 可选,默认就是 Service
修改ts
apiVersion: traefik.containo.us/v1alpha1
kind: TraefikService
metadata:
name: whots
namespace: who
spec:
weighted:
sticky:
cookie:
name: traefiktoken
services:
- name: whoami1
weight: 3 # 定义权重
port: 80
kind: Service # 可选,默认就是 Service
- name: whoami2
weight: 3 # 定义权重
port: 80
kind: Service # 可选,默认就是 Service
- name: whoami3
weight: 3 # 定义权重
port: 80
kind: Service # 可选,默认就是 Service
这种模型上,很难受的一个点是,它的负载均衡不是落在 pod 上的,而是落在 service 上,这样pod在伸缩之后,你还需要修改对应的 service,实际应用没有 ingress 那么方便
更新
不使用 traefik service 的话,可以直接将 sticky 写在 ingress route 上
apiVersion: traefik.containo.us/v1alpha1
kind: IngressRoute
metadata:
name: backendingress
namespace: backend
spec:
entryPoints:
- web
routes:
- match: Host(`nginx.lizhe.com`) && PathPrefix(`/backend`)
kind: Rule
services:
- name: myservice
port: 8080
sticky:
cookie:
httpOnly: true
name: mytickytoken
middlewares:
- name: backend-stripprefix
上面的例子也可以直接使 IngressRoute 指向 kubernetes的service
apiVersion: traefik.containo.us/v1alpha1
kind: IngressRoute
metadata:
name: whoingressroute
namespace: lizhe
spec:
entryPoints:
- web
routes:
- match: Host(`who.lizhe.com`)
kind: Rule
services:
- name: whoami
port: 80
sticky:
cookie:
httpOnly: true
name: lizhetoken