Kubernetes中使用Traefik的sticky特性

文章楔子

在使用Nginx作为前端页面的反向代理时,有时我们需要在服务端为用户的本次访问保存一些临时状态,这种临时状态通常被称为会话。当访问压力增大时,常用的办法是开启多个服务端实例,然后使用Nginx一类的反向代理服务器进行负载均衡。然而对于依赖会话与用户进行交互的页面来说,由于负载均衡可能会将同一用户的两次访问分发到不同的服务端上,这样会话就无法正常运作了。而解决这个问题有最常用的两种方法,一种是在应用层面上作修改,以支持会话共享。另一种方式则是使用会话粘粘:在客户端第一次发起请求时,反向代理为客户端分配一个服务端,并且将该服务端的地址以SetCookie的形式发送给客户端,这样客户端下一次访问该反向代理时,便会带着这个cookie,里面包含了上一次反向代理分配给该客户端的服务端信息。在Nginx中,这种机制是通过一个名为Sticky的插件实现的。而Traefik6.0则集成了与Nginx的Sticky相同功能,并且可以在Kubernetes中方便的开启和配置该特性。

安装Traefik 6.0

首先需要在Kubernetes集群中安装Traefik,如果你已经安装过Traefik请直接跳至第三章
执行下面的yaml文件以安装traefik 6.0和对应的service对象

kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
  name: traefik-ingress-controller
rules:
  - apiGroups:
      - ""
    resources:
      - services
      - endpoints
      - secrets
    verbs:
      - get
      - list
      - watch
  - apiGroups:
      - extensions
    resources:
      - ingresses
    verbs:
      - get
      - list
      - watch
---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
  name: traefik-ingress-controller
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: traefik-ingress-controller
subjects:
- kind: ServiceAccount
  name: traefik-ingress-controller
  namespace: kube-system
---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: traefik-ingress-controller
  namespace: kube-system
---
kind: ConfigMap
apiVersion: v1
metadata:
  name: traefik-conf
  namespace: kube-system
data:
  traefik.toml: |
    defaultEntryPoints = ["http"]
    [entryPoints]
      [entryPoints.http]
      address = ":80"
---
kind: DaemonSet
apiVersion: extensions/v1beta1
metadata:
  name: traefik-ingress-controller
  namespace: kube-system
  labels:
    k8s-app: traefik-ingress-lb
spec:
  template:
    metadata:
      labels:
        k8s-app: traefik-ingress-lb
        name: traefik-ingress-lb
    spec:
      serviceAccountName: traefik-ingress-controller
      terminationGracePeriodSeconds: 60
      hostNetwork: true
      - name: config
        configMap:
          name: traefik-conf
      containers:
      - image: k8s.gcr.io/traefik:1.6
        name: traefik-ingress-lb
        ports:
        - name: http
          containerPort: 80
          hostPort: 80
        - name: admin
          containerPort: 8080
        securityContext:
          privileged: true
        args:
        - --configfile=/config/traefik.toml
        - -d
        - --web
        - --kubernetes
        volumeMounts:
        - mountPath: "/config"
          name: "config"
---
kind: Service
apiVersion: v1
metadata:
  name: traefik-ingress-service
spec:
  selector:
    k8s-app: traefik-ingress-lb
  ports:
    - protocol: TCP
      port: 80
      name: web
    - protocol: TCP
      port: 8080
      name: admin
    - protocol: TCP
      port: 443
      name: https
  type: NodePort
apiVersion: v1
kind: Service
metadata:
  name: traefik-web-ui
  namespace: kube-system
spec:
  selector:
    k8s-app: traefik-ingress-lb
  ports:
  - port: 80
    targetPort: 8080
---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: traefik-web-ui
  namespace: kube-system
  annotations:
    kubernetes.io/ingress.class: traefik
spec:
  rules:
  - host: kuber.ingress.io
    http:
      paths:
      - backend:
          serviceName: traefik-web-ui
          servicePort: 80
---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: traefik-web-ui
  namespace: kube-system
  annotations:
    kubernetes.io/ingress.class: traefik
spec:
  rules:
  - host: traefik.ingress.io
    http:
      paths:
      - backend:
          serviceName: traefik-web-ui
          servicePort: 80

随后将一个非master节点的IP和traefik.ingress.io配置到/etc/hosts文件中,如下图

 

并在浏览器上访问traefik.ingress.io,即可看到Traefik的监控页面

此时traefik安装完毕。

为服务端开启Sticky

正常的traefik通过下面的配置为指定的后端开启sticky

[backends]
  [backends.backend1]
    # Enable sticky session
    [backends.backend1.loadbalancer.stickiness]

    # Customize the cookie name
    #
    # Optional
    # Default: a sha1 (6 chars)
    #
    #  cookieName = "my_cookie"

但此处由于我们将Traefik作为ingress controller安装在kubernetes中,因此Traefik的配置文件是托管给K8s来动态更新的,因此不能通过修改配置文件配置sticky
Kubernetes中可以通过为指定的service对象或ingress对象声明annotation来为ingress controller做额外的详细配置。例如,如果要开启sticky,只需要在想要开启sticky的服务端对应的service上添加以下的annotation即可。

metadata:
  annotations:
    traefik.ingress.kubernetes.io/affinity: "true"

还有更多配置可以参考Traefik文档

举例

apiVersion: v1
kind: Service
metadata:
    name: gls
    namespace: {{ .Release.Namespace }}
    labels:
        name: gls
    annotations:
        traefik.ingress.kubernetes.io/affinity: "true"
        traefik.ingress.kubernetes.io/session-cookie-name: "gls-test"
        traefik.ingress.kubernetes.io/load-balancer-method: drr
spec:
    selector:
        name: gls
    ports:
    -   port: 80
        targetPort: 8080
---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
    name: gls
    namespace: {{ .Release.Namespace }}
spec:
    rules:
    -   host: {{ .Release.Namespace }}.{{ .Values.runtime.network.domainName }}
        http:
            paths:
            -   backend:
                    serviceName: gls
                    servicePort: 80
                path: /gls

至此Sticky特性开启成功。

K8S中文社区微信公众号