traefik-gateway-dashboard
本文为dashboard用法,基础用法请参考:traefik-gateway
三个镜像
- traefik
- swr.cn-south-1.myhuaweicloud.com/vp-whdev/all-in-devops/traefik:20230516
内置插件:rewritebody
内置插件:rewriteHeaders
-
动态服务发现
- swr.cn-south-1.myhuaweicloud.com/vp-whdev/all-in-devops/traefik-service-discovery:202306051
更新日志:2023-06-21:
- 打包dashboard面板到一个服务,无需再部署dashboard
- 增加服务监控
-
dashboard面板:
- swr.cn-south-1.myhuaweicloud.com/vp-whdev/all-in-devops/traefik-service-discovery-dashboard-front:stage
更新日志:2023-06-21
- dashboard面板已经集成到动态服务发现,无需部署
安装部署说明
- 部署: traefik
# 端口开放
80,http端口
443,https端口
8080,traefik自带的dashboard端口,可以不开放
- 部署traefik-service-discovery
# 服务名必须为 traefik-service-discovery
# 端口开方
8080,非必须
# 环境变量
spring.profiles.active = mysqlProd
# mysqlProd,使用mysql数据库,prod,使用sqlite数据库
# 如果使用sqlite数据库,需要注意数据库持久化路径,容器目录/data,容器初始化时会自动复制初始化数据库文件。
# sqlite不支持服务监控
app.datasource.host =
app.datasource.nam e=
app.datasource.password =
app.datasource.username =
- 数据库初始化脚本
CREATE TABLE `traefik_gateway_config` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键id',
`route_name` varchar(255) DEFAULT NULL COMMENT '路由名称',
`access_path` varchar(255) DEFAULT NULL COMMENT '访问路径',
`service_type` int(2) DEFAULT NULL COMMENT '后端服务类型: 1:http 2:tcp 3:udp',
`service_address` varchar(255) DEFAULT NULL COMMENT '后端服务地址',
`middleware` text COMMENT '中间件',
`status` int(255) DEFAULT NULL COMMENT '启用/禁用状态: 0:禁用 1:启用',
`deleted` int(255) DEFAULT '0' COMMENT '删除状态: 0:不删除 1:删除',
PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC COMMENT='网关配置';
CREATE TABLE `global_middleware` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键id',
`global_middleware` longtext COMMENT '全局中间件',
PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC COMMENT='全局中间件';
- traefik-service-discovery-dashboard-fron
# 端口开放:80
# 访问路径/front/
- 服务发现配置
路径 | 服务 | 备注 |
---|---|---|
/ | traefik | 必须 |
/traefik-dashboard/ | traefik内置的dashboard | 非必须 |
/traefikconfig-dashboard/ | 动态路由dashboard | 非必须 |
/traefik-service-discovery/ | 动态路由api | 非必须 |
- 首页不是从根目录开始
如果你的首页不是从根目录开始,则需要做一些单独d配置,复制下面的配置文件,挂载到traefik-gatewy的容器路径
./index-not-root.yaml:/etc/traefik/conf.d/conf2/
http:
routers:
# 首页不是根目录
index-not-root:
# 注意路由匹配路径与你所需要的保持一致
# 注意中间件中的删除路径前缀,理论上应该和路由路径一致
rule: "PathPrefix(`/minio/`)"
service: index-not-root
middlewares:
- stripPrefix-index-not-root
- rewritebody-index-not-root
- rewriteHeaders-index-not-root
services:
index-not-root:
loadBalancer:
servers:
- url: http://traefik-gateway:80 # 部署的traefik主程序必须叫这个名字
middlewares:
# 去除前缀
stripPrefix-index-not-root:
stripPrefix:
prefixes:
- "/minio/"
# 正文路径改写
rewritebody-index-not-root:
plugin:
plugin-rewritebody: # 要和静态配置文件的插件名称对应上
rewrites:
- regex: "/traefik-service-discovery/api/"
replacement: "/minio/traefik-service-discovery/api/"
- regex: "/traefik-dashboard/api"
replacement: "/minio/traefik-dashboard/api"
- regex: "/minio/dashboard/"
replacement: "/minio/traefik-dashboard/dashboard/"
# header改写
rewriteHeaders-index-not-root:
plugin:
plugin-rewriteHeaders:
rewrites:
- header: "Location"
regex: "/minio/dashboard/"
replacement: "/minio/traefik-dashboard/dashboard/"
插件使用
middlewares:
# 删除路径前置
stripPrefix-traefikconfig-dashboard:
stripPrefix:
prefixes:
- "/traefikconfig-dashboard/"
# 增加路径前缀
addPrefixFront-traefikconfig-dashboard:
addPrefix:
prefix: "/front/"
# 正文路径改写
rewritebody-traefikconfig-dashboard:
plugin:
plugin-rewritebody: # 要和静态配置文件的插件名称对应上
rewrites:
- regex: "/api/"
replacement: "/traefik-service-discovery/api/"
# 删除路径前缀
stripPrefix-traefikconfig-service-discovery:
stripPrefix:
prefixes:
- "/traefik-service-discovery/"
# 基础身份验证
basicAuth-traefikconfig-dashboard:
basicAuth:
removeHeader: true
users:
- "admin:" # 在线生成 http://www.jsons.cn/htpasswd/
# 发送时修改host
headers-peisong:
headers:
customRequestHeaders:
host: "peisong.gxzszs.cn"
# 重写 header
rewriteHeaders-peisong:
plugin:
plugin-rewriteHeaders:
rewrites:
- header: "Location"
regex: "peisong.gxzszs.cn"
replacement: "peisong.gxfusui.com"
- header: "Location"
regex: "http://peisong.gxfusui.com/plugin.php"
replacement: "/plugin.php"