Kubernetes中使用Traefik的sticky特性

店家小二發表於2018-12-14

文章楔子

在使用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特性開啟成功。

本文轉自kubernetes中文社群-Kubernetes中使用Traefik的sticky特性


相關文章