启用Dashboard K3s (≥1.21)默认没有启用 Traefik Dashboard。如果要在 K3s 中启用 Dashborad,我们可以借助 HelmChartConfig 来自定义由 Helm 部署的 Traefik 并启用 Dashboard。
不建议手动编辑 /var/lib/rancher/K3s/server/manifests/traefik.yaml
来修改 Traefik 配置文件,因为 K3s 重启后会覆盖修改的内容。
建议通过在 /var/lib/rancher/K3s/server/manifests
中创建一个额外的 HelmChartConfig
清单来自定义 Traefik 配置,请参考:http://docs.rancher.cn/docs/K3
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 cat >> /var/lib/rancher/K3s/server/manifests/traefik-config.yaml << EOF apiVersion: helm.cattle.io/v1 kind: HelmChartConfig metadata: name: traefik namespace: kube-system spec: valuesContent: |- dashboard: enabled: true ports: traefik: expose: true logs: access: enabled: true EOF
K8s 修改 traefik-vlues.yaml 中ingressRoute.dashboard
并应用配置
1 2 helm upgrade traefik traefik/traefik \ --namespace traefik -f traefik-vlues.yaml
配置Service及路由规则 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 cat >> traefik-dashboard.yaml << EOF apiVersion: v1 kind: Service metadata: name: traefik namespace: kube-system spec: allocateLoadBalancerNodePorts: true ports: - name: web nodePort: 80 port: 80 protocol: TCP targetPort: web - name: websecure nodePort: 443 port: 443 protocol: TCP targetPort: websecure selector: app.kubernetes.io/instance: traefik-kube-system app.kubernetes.io/name: traefik type: LoadBalancer --- apiVersion: traefik.io/v1alpha1 kind: IngressRoute metadata: name: traefik-dashboard-web spec: entryPoints: - web routes: - kind: Rule match: PathPrefix(`/dashboard`) || PathPrefix(`/api`) services: - name: api@internal kind: TraefikService --- apiVersion: traefik.io/v1alpha1 kind: IngressRoute metadata: name: traefik-dashboard-websecure spec: entryPoints: - websecure routes: - kind: Rule match: PathPrefix(`/dashboard`) || PathPrefix(`/api`) services: - name: api@internal kind: TraefikService tls: secretName: traefik-dashboard-tls EOF
1 kubectl apply -f traefik-dashboard.yaml
访问Dashboard需要在最后加一个/
否则可能出现page not found
配置IngressRoute 1 2 3 kubectl create deploy whoami --image=traefik/whoami --replicas=2 kubectl expose deploy whoami --port=80
HTTP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 apiVersion: traefik.containo.us/v1alpha1 kind: IngressRoute metadata: name: whoami-ingress-web namespace: default spec: entryPoints: - web routes: - match: Host(`192.168.0.2`) && PathPrefix(`/notls`) kind: Rule services: - name: whoami port: 80
HTTPS
1 2 3 4 openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout tls.key -out tls.crt -subj "/CN=domain.example.com" kubectl create secret tls whoami-tls --cert=tls.crt --key=tls.key
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 apiVersion: traefik.containo.us/v1alpha1 kind: IngressRoute metadata: name: whoami-ingress-websecure namespace: default spec: entryPoints: - websecure routes: - match: Host(`192.168.0.2`) && PathPrefix(`/tls`) kind: Rule services: - name: whoami port: 80 tls: secretName: whoami-tls
证书生成参考[[K3s证书管理]]
TCP/UDP 默认配置文件下,只有traefik
(9000)、web
(80)、websecure
(443)以及metrics
(9100)开放,如果想要反代MySQL tcp又想自定义端口的话,需要单独在配置文件中进行配置。
1 2 3 4 5 6 7 8 9 10 11 12 13 apiVersion: traefik.containo.us/v1alpha1 kind: IngressRouteTCP metadata: name: redis namespace: devops spec: entryPoints: - redis routes: - match: HostSNI(`*`) services: - name: redis port: 6379
TCP Routers与HTTP Routers的routes有所不同:
TCP Routers match采用HostSNI,而HTTP Routers match直接匹配Host。
TCP Routers只能定位TCP服务(不能定位HTTP服务)。
如果HTTP Routers和TCP Routers都侦听相同的入口点,则TCP Routers将在HTTP Routers之前应用。如果找不到与TCP Routers匹配的路由,则HTTP Routers将接管。
参考
K3s版本 > v.121(Traefik 2.x)
K3S 中 Traefik v2 安装及采坑纪实 - 知乎 (zhihu.com)
k3s开启traefik的dashboard网页-CSDN博客
Step by Step!教你如何在k3s集群上使用Traefik 2.x - k3s中文社区 - 博客园 (cnblogs.com)
traefik系列之一 | 简介、部署和配置-腾讯云开发者社区-腾讯云 (tencent.com)
还不会Traefik?看这篇文章就够了! - 知乎 (zhihu.com)
k3s 使用 Letsencrypt 和 Traefik 完成 https 入口部署-腾讯云开发者社区-腾讯云 (tencent.com)
[Kubernetes环境Traefik部署与应用 - Tiscs - 博客园 (cnblogs.com)](https://www.cnblogs.com/tiscs/p/notes-k8s-traefik.html#:~:text=安装Traefik 1 配置 Helm Repo helm repo add, … 3 其他准备工作 获取 traefik 服务的负载均衡器地址。 )
Traefik - Kubernetes 配置TCP/HTTP服务-腾讯云开发者社区-腾讯云 (tencent.com)
cert-manager管理内网k8s开发环境证书 - hueidou163 - 博客园 (cnblogs.com)
Kubernetes (K8S)中Traefik路由(ingressRoute)-腾讯云开发者社区-腾讯云 (tencent.com)
Dashboard无法访问问题参考
如何在 K3s 中启用 Traefik Dashborad - RancherLabs - 博客园 (cnblogs.com)
How to Expose and Enable K3s with Traefik Dashboard (thriveread.com)
kubernetes - How to expose traefik v2 dashboard in k3d/k3s via configuration? - Stack Overflow
kubernetes - 云原生 07:改用 K3s,并使用 K3s 内置的 Traefik 做 Ingress 网关 - 小鲜 - SegmentFault 思否
K8s中使用traefik(基础) - AGou’s Blog