k8s 1.19.2 部署 traefik 2.2

目录
  1. 一、Traefik 简介
  2. 二、Kubernetes 部署 Traefik
    1. 1、ingress-route.yml
    2. 2、创建 RBAC 权限
    3. 3、创建 Traefik 配置文件
    4. 4、节点设置 Label 标签
    5. 5、Kubernetes 部署 Traefik
  3. 三、Traefik 路由规则配置
    1. 1、配置 HTTP 路由规则 (Traefik Dashboard 为例)

系统环境:

  • Traefik 版本:v2.2

  • Kubernetes 版本:1.19.2

地址:

一、Traefik 简介

Traefik 最新推出了 v2.2 版本,这里将 Traefik 升级到最新版本,简单的介绍了下如何在 Kubernetes 环境下安装 Traefik v2.2

二、Kubernetes 部署 Traefik

注意:这里 Traefik 是部署在 Kube-system Namespace 下,使用kustomize部署。

-rw-r--r--  1 yqwoe  staff   287 11 23 09:54 cluster-role-binding.yaml
-rw-r--r-- 1 yqwoe staff 736 11 23 09:54 cluster-role.yaml
-rw-r--r-- 1 yqwoe staff 2578 11 23 09:54 config.yaml
-rw-r--r-- 1 yqwoe staff 1110 11 23 09:54 daemon-set.yaml
-rw-r--r-- 1 yqwoe staff 2021 11 23 09:54 ingress-route.yml
-rw-r--r-- 1 yqwoe staff 268 11 23 09:54 kustomization.yaml
-rw-r--r-- 1 yqwoe staff 81 11 23 09:54 service-account.yaml
-rw-r--r-- 1 yqwoe staff 220 11 23 09:54 service.yaml
-rw-r--r-- 1 yqwoe staff 257 11 23 09:54 traefik-dashboard-route.yml

kustomization.yaml

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

resources:
- cluster-role.yaml
- cluster-role-binding.yaml
- service-account.yaml
- config.yaml
- daemon-set.yaml
- service.yaml
- ingress-route.yaml
namespace: kube-system

commonLabels:
app: traefik

1、ingress-route.yml

在 traefik v2.1 版本后,开始使用 CRD(Custom Resource Definition)来完成路由配置等,所以需要提前创建 CRD 资源。

创建 ingress-route.yml 文件

apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
name: ingressroutes.traefik.containo.us

spec:
group: traefik.containo.us
version: v1alpha1
names:
kind: IngressRoute
plural: ingressroutes
singular: ingressroute
scope: Namespaced

---
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
name: middlewares.traefik.containo.us

spec:
group: traefik.containo.us
version: v1alpha1
names:
kind: Middleware
plural: middlewares
singular: middleware
scope: Namespaced

---
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
name: ingressroutetcps.traefik.containo.us

spec:
group: traefik.containo.us
version: v1alpha1
names:
kind: IngressRouteTCP
plural: ingressroutetcps
singular: ingressroutetcp
scope: Namespaced

---
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
name: ingressrouteudps.traefik.containo.us

spec:
group: traefik.containo.us
version: v1alpha1
names:
kind: IngressRouteUDP
plural: ingressrouteudps
singular: ingressrouteudp
scope: Namespaced

---
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
name: tlsoptions.traefik.containo.us

spec:
group: traefik.containo.us
version: v1alpha1
names:
kind: TLSOption
plural: tlsoptions
singular: tlsoption
scope: Namespaced

---
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
name: tlsstores.traefik.containo.us

spec:
group: traefik.containo.us
version: v1alpha1
names:
kind: TLSStore
plural: tlsstores
singular: tlsstore
scope: Namespaced

---
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
name: traefikservices.traefik.containo.us

spec:
group: traefik.containo.us
version: v1alpha1
names:
kind: TraefikService
plural: traefikservices
singular: traefikservice
scope: Namespaced

2、创建 RBAC 权限

Kubernetes 在 1.6 版本中引入了基于角色的访问控制(RBAC)策略,方便对 Kubernetes 资源和 API 进行细粒度控制。Traefik 需要一定的权限,所以这里提前创建好 Traefik ServiceAccount 并分配一定的权限。

创建 service-account.yaml 文件

apiVersion: v1
kind: ServiceAccount
metadata:
name: traefik-ingress-controller

创建cluster-role.yaml

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
- apiGroups:
- extensions
resources:
- ingresses/status
verbs:
- update
- apiGroups:
- traefik.containo.us
resources:
- middlewares
- ingressroutes
- traefikservices
- ingressroutetcps
- ingressrouteudps
- tlsoptions
- tlsstores
verbs:
- get
- list
- watch

创建cluster-role-binding.yaml

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

3、创建 Traefik 配置文件

由于 Traefik 配置很多,通过 CLI 定义不是很方便,一般时候选择将其配置选项放到配置文件中,然后存入 ConfigMap,将其挂入 traefik 中。

创建 config.yaml 文件

kind: ConfigMap
apiVersion: v1
metadata:
name: traefik-config
data:
traefik.yaml: |-
ping: "" ## 启用 Ping
serversTransport:
insecureSkipVerify: true ## Traefik 忽略验证代理服务的 TLS 证书
api:
insecure: true ## 允许 HTTP 方式访问 API
dashboard: true ## 启用 Dashboard
debug: false ## 启用 Debug 调试模式
metrics:
prometheus: "" ## 配置 Prometheus 监控指标数据,并使用默认配置
entryPoints:
web:
address: ":80" ## 配置 80 端口,并设置入口名称为 web
websecure:
address: ":443" ## 配置 443 端口,并设置入口名称为 websecure
providers:
kubernetesCRD: "" ## 启用 Kubernetes CRD 方式来配置路由规则
kubernetesIngress: "" ## 启动 Kubernetes Ingress 方式来配置路由规则
log:
filePath: "" ## 设置调试日志文件存储路径,如果为空则输出到控制台
level: error ## 设置调试日志级别
format: json ## 设置调试日志格式
accessLog:
filePath: "" ## 设置访问日志文件存储路径,如果为空则输出到控制台
format: json ## 设置访问调试日志格式
bufferingSize: 0 ## 设置访问日志缓存行数
filters:
#statusCodes: ["200"] ## 设置只保留指定状态码范围内的访问日志
retryAttempts: true ## 设置代理访问重试失败时,保留访问日志
minDuration: 20 ## 设置保留请求时间超过指定持续时间的访问日志
fields: ## 设置访问日志中的字段是否保留(keep 保留、drop 不保留)
defaultMode: keep ## 设置默认保留访问日志字段
names: ## 针对访问日志特别字段特别配置保留模式
ClientUsername: drop
headers: ## 设置 Header 中字段是否保留
defaultMode: keep ## 设置默认保留 Header 中字段
names: ## 针对 Header 中特别字段特别配置保留模式
User-Agent: redact
Authorization: drop
Content-Type: keep
certificatesresolvers:
default:
acme:
tlsChallenge: true
email: "yqwoe@live.cn"
storage: "acme.json"
caserver: "https://acme-v02.api.letsencrypt.org/directory"

4、节点设置 Label 标签

由于是 Kubernetes DeamonSet 这种方式部署 Traefik,所以需要提前给节点设置 Label,这样当程序部署时 Pod 会自动调度到设置 Label 的节点上。

节点设置 Label 标签

  • 格式:kubectl label nodes [节点名] [key=value]

$ kubectl label nodes minikube IngressProxy=true

查看节点是否设置 Label 成功

$ kubectl get nodes --show-labels

NAME STATUS ROLES AGE VERSION LABELS
minikube Ready master 3d v1.19.2 IngressProxy=true,beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=minikube,kubernetes.io/os=linux,minikube.k8s.io/commit=b09ee50ec047410326a85435f4d99026f9c4f5c4,minikube.k8s.io/name=minikube,minikube.k8s.io/updated_at=2020_11_10T10_20_51_0700,minikube.k8s.io/version=v1.14.0,node-role.kubernetes.io/master=

如果想删除标签,可以使用 kubectl label nodes minikube IngressProxy- 命令

5、Kubernetes 部署 Traefik

DaemonSet 方式部署,便于在多服务器间扩展,用 hostport 方式占用服务器 80、443 端口,方便流量进入。

创建 traefik 部署文件 daemon-set.yaml

kind: DaemonSet
apiVersion: apps/v1
metadata:
name: traefik
spec:
template:
spec:
serviceAccountName: traefik-ingress-controller
terminationGracePeriodSeconds: 60
containers:
- image: traefik:v2.2
name: traefik-ingress-lb
ports:
- name: web
containerPort: 80
hostPort: 80
- name: websecure
containerPort: 443
hostPort: 443
- name: admin
containerPort: 8080
hostPort: 8080
securityContext:
capabilities:
drop:
- ALL
add:
- NET_BIND_SERVICE
args:
- --configfile=/config/traefik.yaml
volumeMounts:
- mountPath: "/config"
name: "config"
volumes:
- name: config
configMap:
name: traefik-config
tolerations: ## 设置容忍所有污点,防止节点被设置污点
- operator: "Exists"
nodeSelector: ## 设置node筛选器,在特定label的节点上启动
IngressProxy: "true"

创建Traefik Service service.yaml

apiVersion: v1
kind: Service
metadata:
name: traefik
spec:
ports:
- name: web
port: 80
- name: websecure
port: 443
- name: admin
port: 8080
selector:
app: traefik
type: NodePort

至此,traefik的配置基本完成。

执行 kustomization.yaml

kubectl apply -k . -n kube-system

三、Traefik 路由规则配置

1、配置 HTTP 路由规则 (Traefik Dashboard 为例)

Traefik 应用已经部署完成,但是想让外部访问 Kubernetes 内部服务,还需要配置路由规则,这里开启了 Traefik Dashboard 配置,所以首先配置 Traefik Dashboard 看板的路由规则,使外部能够访问 Traefik Dashboard。

创建 Traefik Dashboard 路由规则文件 traefik-dashboard-route.yaml

apiVersion: traefik.containo.us/v1alpha1
kind: IngressRoute
metadata:
name: traefik-dashboard-route
spec:
entryPoints:
- web
routes:
- match: Host(`yqwoe.k8s.com`)
kind: Rule
services:
- name: traefik
port: 8080

创建 Traefik Dashboard 路由规则对象

$ kubectl apply -f traefik-dashboard-route.yaml -n kube-system

接下来配置 Hosts,客户端想通过域名访问服务,必须要进行 DNS 解析,由于这里没有 DNS 服务器进行域名解析,所以修改 hosts 文件将 Traefik 指定节点的 IP 和自定义 host 绑定。打开电脑的 Hosts 配置文件,往其加入下面配置:

192.168.10.2  yqwoe.k8s.com

配置完成后,打开浏览器输入地址:http://yqwoe.k8s.com 打开 Traefik Dashboard。

26.1k