中文文档: https://www.qikqiak.com/traefik-book/
国外文档: https://doc.traefik.io/traefik/providers/kubernetes-crd/
kubernetes安装traefik2.4.12
创建traefik目录
mkdir traefik ; cd traefik
生成配置文件
当前 https://github.com/traefik/traefik/tree/v2.4.12/docs/content/reference/dynamic-configuration
wget https://raw.githubusercontent.com/traefik/traefik/v2.4.12/docs/content/reference/dynamic-configuration/kubernetes-crd-definition.yml
wget https://raw.githubusercontent.com/traefik/traefik/v2.4.12/docs/content/reference/dynamic-configuration/kubernetes-crd-rbac.yml
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 方式来配置路由规则
kubernetesGateway: "" ## 启用 Kubernetes Gateway API
experimental:
kubernetesGateway: true ## 允许使用 Kubernetes Gateway API
log:
filePath: "/opt/logs/traefik.log" ## 设置调试日志文件存储路径,如果为空则输出到控制台
level: error ## 设置调试日志级别
format: json ## 设置调试日志格式
accessLog:
filePath: "/opt/logs/access.log" ## 设置访问日志文件存储路径,如果为空则输出到控制台
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
#tracing: ## 链路追踪配置,支持 zipkin、datadog、jaeger、instana、haystack 等
# serviceName: ## 设置服务名称(在链路追踪端收集后显示的服务名)
# zipkin: ## zipkin配置
# sameSpan: true ## 是否启用 Zipkin SameSpan RPC 类型追踪方式
# id128Bit: true ## 是否启用 Zipkin 128bit 的跟踪 ID
# sampleRate: 0.1 ## 设置链路日志采样率(可以配置0.0到1.0之间的值)
# httpEndpoint: http://localhost:9411/api/v2/spans ## 配置 Zipkin Server 端点
当前可使用deployment或者daemonset来部署traefik
Deployment和DaemonSet的区别
Deployment 部署的副本 Pod 会分布在各个 Node 上,每个 Node 都可能运行好几个副本。DaemonSet 的不同之处在于:每个 Node 上最多只能运行一个副本。
主要区别:
- The scalability is much better when using a Deployment, because you will have a Single-Pod-per-Node model when using the DaemonSet.
- It is possible to exclusively run a Service on a dedicated set of machines using taints and tolerations with a DaemonSet.
- On the other hand the DaemonSet allows you to access any Node directly on Port 80 and 443, where you have to setup a Service object with a Deployment.
traefik-daemonSet.yaml
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: traefik-ingress-controller
namespace: kube-system
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.4.3
name: traefik-ingress-lb
ports:
- name: web
containerPort: 80
hostPort: 80 ## 将容器端口绑定所在服务器的 80 端口
- name: websecure
containerPort: 443
hostPort: 443 ## 将容器端口绑定所在服务器的 443 端口
- name: admin
containerPort: 8080 ## Traefik Dashboard 端口
resources:
limits:
cpu: 2000m
memory: 1024Mi
requests:
cpu: 1000m
memory: 200Mi
securityContext:
capabilities:
drop:
- ALL
add:
- NET_BIND_SERVICE
args:
- --configfile=/config/traefik.yaml
volumeMounts:
- mountPath: "/config"
name: "config"
readinessProbe:
httpGet:
path: /ping
port: 8080
failureThreshold: 3
initialDelaySeconds: 10
periodSeconds: 10
successThreshold: 1
timeoutSeconds: 5
livenessProbe:
httpGet:
path: /ping
port: 8080
failureThreshold: 3
initialDelaySeconds: 10
periodSeconds: 10
successThreshold: 1
timeoutSeconds: 5
volumes:
- name: config
configMap:
name: traefik-config
traefik-svc.yaml
apiVersion: v1
kind: Service
metadata:
name: traefik
namespace: kube-system
labels:
app: traefik
spec:
ports:
- name: web
port: 80
- name: websecure
port: 443
- name: admin
port: 8080
selector:
app: traefik
apply执行
kubectl -n kube-system applu -f kubernetes-crd-definition.yml
kubectl -n kube-system applu -f kubernetes-crd-rbac.yml
kubectl -n kube-system applu -f traefik-config.yaml
kubectl -n kube-system applu -f traefik-daemonSet.yaml
kubectl -n kube-system applu -f kubernetes-crd-definition.yml
生成traefik-ingreeroute.yaml
- traefik默认会监听所有命名空间的ingreeroute相关资源
apiVersion: traefik.containo.us/v1alpha1 kind: IngressRoute metadata: name: dashboard namespace: kube-system spec: entryPoints: # web为配置文件endpoinit的web - web routes: - match: Host(`traefik.k8s-host.com`) kind: Rule services: # 服务的service名 - name: traefik port: 8080客户端写入/etc/hosts中 进行访问
问题
traefik api和dashboard无法添加路径前缀进行访问
githuip 帖子: https://github.com/traefik/traefik/issues/5853
问题描述: 在当前kubernetes1.20版本环境中, traefik2.4.12版本反向代理traefik-dashboard(包括api和dashboard), 访问链接为 http://xx.xx:80/dashboard (包括http://xx.xx:80/api), 当其他服务域名端口路径一致的时候就会出现冲突情况, 在当前traefik2.4.12版本中, 并没有相关参数让 访问链接添加前缀路径为 http://xx.xx:80/traefik/dashboard 和 http://xx.xx:80/traefik/api.
traefik反向代理的服务出现重定向,导致404
前提条件: 使用相同域名端口, 以路径区分服务
问题描述: 当traefik反向代理具有重定向的服务(prometheus和grafana)时, 重定向到新的页面导致无法访问
举例: http://xx.xx.xx.xx/prometheus —> http://xxx.xx.xx.xx/graph
http://xx.xx.xx.xx/grafana —> http://xxx.xx.xx.xx/login
如下面的IngressRoute可得, 路径并没有graph和login, 那么能否将这两个路径添加就好呢?
可以, 但新的问题又出现了, 如果某服务也有graph和login路径, 那就会发生冲突.
修复建议:
- 使用不同域名进行路由(浪费)
- 使用不同端口进行路由(浪费)
- 让后端服务修改访问链接路径 举例: prometheus 默认链接为 http://xx.xx.xx.xx:9090 添加参数externalUrl提供外部链接 http://xx.xx.xx.xx:9090/prometheus (根据实际情况填写) , 配置依旧不变, 服务正常访问 http://xx.xx.xx.xx/prometheus —> http://xxx.xx.xx.xx/prometheus/graph
当前方法只适用于能修改访问路径的服务, 如trafik的WEBUI暂时没发现有相关参数修改
apiVersion: traefik.containo.us/v1alpha1
kind: IngressRoute
metadata:
name: promethues-grafana
namespace: monitoring
spec:
entryPoints:
- web
routes:
- match: Host(`wsptest.k8s-host.net`) && PathPrefix(`/prometheus`)
kind: Rule
services:
- name: prometheus-k8s
port: 9090
- match: Host(`wsptest.k8s-host.net`) && PathPrefix(`/grafana`)
kind: Rule
services:
- name: grafana
port: 3000
路径(/grafana)与实际路径(/login)不符合,出现404情况
