Traefik 最新推出了 v2.0 版本,这里将尝试升级到最新,简单的介绍了下如何在 Kubernetes 环境下安装 Traefik v2.0,在 Traefik v2.0 版本后,配置 Ingress 路由规则其使用了自定义 CRD 对象来完成,并不像之前 1.0+ 版本使用 Kubernetes 自带的 Ingress 对象加注解方式来完成路由配置,下面将介绍如何在 Kubernetes 环境下部署并配置 Traefik v2.0。
注意:这里 Traefik 是部署在 Kube-system Namespace 下,如果不是需要修改下面部署文件中的 Namespace 属性。
在 traefik v2.0 版本后,开始使用 CRD(Custom Resource Definition)来完成路由配置等,所以需要提前创建 CRD 资源。
创建 traefik-crd.yaml 文件
## IngressRoute
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
name: ingressroutes.traefik.containo.us
spec:
scope: Namespaced
group: traefik.containo.us
version: v1alpha1
names:
kind: IngressRoute
plural: ingressroutes
singular: ingressroute
---
## IngressRouteTCP
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
name: ingressroutetcps.traefik.containo.us
spec:
scope: Namespaced
group: traefik.containo.us
version: v1alpha1
names:
kind: IngressRouteTCP
plural: ingressroutetcps
singular: ingressroutetcp
---
## Middleware
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
name: middlewares.traefik.containo.us
spec:
scope: Namespaced
group: traefik.containo.us
version: v1alpha1
names:
kind: Middleware
plural: middlewares
singular: middleware
---
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
name: tlsoptions.traefik.containo.us
spec:
scope: Namespaced
group: traefik.containo.us
version: v1alpha1
names:
kind: TLSOption
plural: tlsoptions
singular: tlsoption
创建 Traefik CRD 资源
$ kubectl apply -f traefik-crd.yaml -n kube-system
Kubernetes 在 1.6 版本中引入了基于角色的访问控制(RBAC)策略,方便对 Kubernetes 资源和 API 进行细粒度控制。Traefik 需要一定的权限,所以这里提前创建好 Traefik ServiceAccount 并分配一定的权限。
创建 traefik-rbac.yaml 文件
apiVersion: v1
kind: ServiceAccount
metadata:
namespace: kube-system
name: traefik-ingress-controller
---
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"]
verbs: ["get","list","watch"]
- apiGroups: ["traefik.containo.us"]
resources: ["ingressroutes"]
verbs: ["get","list","watch"]
- apiGroups: ["traefik.containo.us"]
resources: ["ingressroutetcps"]
verbs: ["get","list","watch"]
- apiGroups: ["traefik.containo.us"]
resources: ["tlsoptions"]
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
创建 Traefik RBAC 资源
$ kubectl apply -f traefik-rbac.yaml -n kube-system
由于 Traefik 配置很多,通过 CLI 定义不是很方便,一般时候选择将其配置选项放到配置文件中,然后存入 ConfigMap,将其挂入 traefik 中。
创建 traefik-config.yaml 文件
kind: ConfigMap
apiVersion: v1
metadata:
name: traefik-config
data:
traefik.yaml: |-
serversTransport:
insecureSkipVerify: true
api:
insecure: true
dashboard: true
debug: true
metrics:
prometheus: ""
entryPoints:
web:
address: ":80"
websecure:
address: ":443"
providers:
kubernetesCRD: ""
log:
filePath: ""
level: error
format: json
accessLog:
filePath: ""
format: json
bufferingSize: 0
filters:
retryAttempts: true
minDuration: 20
fields:
defaultMode: keep
names:
ClientUsername: drop
headers:
defaultMode: keep
names:
User-Agent: redact
Authorization: drop
Content-Type: keep
创建 Traefik ConfigMap 资源
$ kubectl apply -f traefik-config.yaml -n kube-system
由于是 Kubernetes DeamonSet 这种方式部署 Traefik,所以需要提前给节点设置 Label,这样当程序部署时 Pod 会自动调度到设置 Label 的节点上。
节点设置 Label 标签
$ kubectl label nodes k8s-node-2-12 IngressProxy=true
查看节点是否设置 Label 成功
$ kubectl get nodes --show-labels
NAME STATUS ROLES VERSION LABELS
k8s-master-2-11 Ready master v1.16.3 kubernetes.io/hostname=k8s-master-2-11,node-role.kubernetes.io/master=
k8s-node-2-12 Ready v1.16.3 kubernetes.io/hostname=k8s-node-2-12,IngressProxy=true
k8s-node-2-13 Ready v1.16.3 kubernetes.io/hostname=k8s-node-2-13
k8s-node-2-14 Ready v1.16.3 kubernetes.io/hostname=k8s-node-2-14
如果想删除标签,可以使用
kubectl label nodes k8s-node-2-12 IngressProxy-
命令
这里还是按以前部署 Traefik 1.7 一样,用 DaemonSet 方式部署,便于在多服务器间扩展,用 hostport 方式占用服务器 80、443 端口,方便流量进入。
创建 traefik 部署文件 traefik-deploy.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
---
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: traefik-ingress-controller
labels:
app: traefik
spec:
selector:
matchLabels:
app: traefik
template:
metadata:
name: traefik
labels:
app: traefik
spec:
serviceAccountName: traefik-ingress-controller
terminationGracePeriodSeconds: 1
containers:
- image: traefik:v2.0.5
name: traefik-ingress-lb
ports:
- name: web
containerPort: 80
hostPort: 80 #hostPort方式,将端口暴露到集群节点
- name: websecure
containerPort: 443
hostPort: 443 #hostPort方式,将端口暴露到集群节点
- name: admin
containerPort: 8080
resources:
limits:
cpu: 2000m
memory: 1024Mi
requests:
cpu: 1000m
memory: 1024Mi
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"
Kubernetes 部署 Traefik
$ kubectl apply -f traefik-deploy.yaml -n kube-system
到此 Traefik v2.0 应用已经部署完成。
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(`ts.123.top`)
kind: Rule
services:
- name: traefik
port: 8080
创建 Traefik Dashboard 路由规则对象
$ kubectl apply -f traefik-dashboard-route.yaml -n kube-system
这里的域名我用了阿里云的解析,国内服务器没备案 所以用curl了,就成功一次就提示要备案,上面附上traefik2.0截图
2、配置 HTTPS 路由规则 (Traefik 为例)
申请证书,我用的是腾讯云上申请的免费证书,因为nginx的证书下载格式是key和crt不用转证书格式,阿里云跨vps安全组不支持https访问service服务
下载下来上传到master主机上,在证书目录 重命名或者mv
cp xxxxx.crt tls.crt
cp xxxxx.key tls.key
或者
mv xxxxx.crt tls.crt
mv xxxxx.key tls.key
创建证书文件
kubectl create secret generic cloud-mydlq-tls2 --from-file=tls.crt --from-file=tls.key -n kube-system
这里的http和https用到了同一个文件同一个域名,做https之前把http的pod删掉,虽然apply也可
kubectl delete -f traefik-dashboard-route.yaml -n kube-system
apiVersion: traefik.containo.us/v1alpha1
kind: IngressRoute
metadata:
name: traefik-dashboard-route
spec:
entryPoints:
- websecure
tls:
secretName: cloud-mydlq-tls2
routes:
- match: Host(`ts.123.top`)
kind: Rule
services:
- name: traefik
port: 8080
创建
kubectl apply -f traefik-dashboard-route.yaml -n kube-system
阿里云和腾讯云截图
附上参考链接:
http://www.mydlq.club/article/50/
http://www.mydlq.club/article/41/
Traefik 2.1和2.2试了报重定向,曾在另一个文章看到试过一次2.1,5成功,但忘记了
最后附上ConfigMap解释
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