|
| 1 | +--- |
| 2 | +title: "RustFS traefik 反向代理配置指南" |
| 3 | +description: "使用 traefik 作为 RustFS 的反向代理,实现服务发现和负载均衡。" |
| 4 | +--- |
| 5 | + |
| 6 | +# 关于 traefki |
| 7 | + |
| 8 | +[traefik](https://doc.traefik.io/) 是一个开源的云原生应用程序代理,具有服务发现、请求路由以及负载均衡等功能。 |
| 9 | + |
| 10 | +## traefik 与 RustFS 集成 |
| 11 | + |
| 12 | +traefik 和 RustFS 都支持 docker 安装,因此使用 `docker label` 的方式来让 traefik 将流量转发到 RustFS 容器。涉及到 traefik 和 RustFS 两部分配置。 |
| 13 | + |
| 14 | +### traefik 配置 |
| 15 | + |
| 16 | +需要在 traefik 中开启 `docker` provider,以告知 traefik 通过 `docker lable` 的方式来捕获要进行流量转发的服务。配置如下: |
| 17 | + |
| 18 | +``` |
| 19 | +- --providers.docker=true |
| 20 | +- --providers.docker.endpoint=unix:///var/run/docker.sock |
| 21 | +- --providers.docker.exposedbydefault=false |
| 22 | +``` |
| 23 | + |
| 24 | +开启 traefik 监听端口: |
| 25 | + |
| 26 | +``` |
| 27 | +- --entrypoints.web.address=:80 |
| 28 | +- --entrypoints.websecure.address=:443 |
| 29 | +``` |
| 30 | + |
| 31 | +> 其中 80 对应 http,443 对应 https。 |
| 32 | +
|
| 33 | +如果要为应用配置 https 访问,还需要设置证书相关配置,以 [Let's Encrypt](https://letsencrypt.org/)为例,需要开启以下配置: |
| 34 | + |
| 35 | +``` |
| 36 | + |
| 37 | +- --certificatesresolvers.le.acme.storage=/etc/traefik/acme.json |
| 38 | +- --entrypoints.web.http.redirections.entrypoint.to=websecure |
| 39 | +- --entrypoints.web.http.redirections.entrypoint.scheme=https |
| 40 | +- --certificatesresolvers.le.acme.httpchallenge=true |
| 41 | +- --certificatesresolvers.le.acme.httpchallenge.entrypoint=web |
| 42 | +``` |
| 43 | + |
| 44 | +开启端口映射: |
| 45 | + |
| 46 | +``` |
| 47 | +- "80:80" |
| 48 | +- "443:443" |
| 49 | +- "8080:8080" # Traefik Dashboard |
| 50 | +``` |
| 51 | + |
| 52 | +### RustFS 配置 |
| 53 | + |
| 54 | +在 RustFS 的容器配置中,通过 `label` 来设置如下参数: |
| 55 | + |
| 56 | +``` |
| 57 | +# 告诉 traefik 启用对 RustFS 容器的路由和服务发现。如果缺少此标签,traefik 将忽略该容器上的所有其他 traefik 标签。 |
| 58 | +- "traefik.enable=true" |
| 59 | +# 定义了一个名为 rustfs 的 路由器。rule=Host(...) 指定了 traefik 应该将所有发往 your.rustfs.com 这个域名的 HTTP/HTTPS 请求路由给该路由器处理。 |
| 60 | +- "traefik.http.routers.rustfs.rule=Host(`your.rustfs.com`)" |
| 61 | +# 指定该路由器应该监听哪个或哪些 EntryPoints(入口点)。websecure 对应您在静态配置中定义的 443 端口,意味着该路由只处理 HTTPS 流量。 |
| 62 | +- "traefik.http.routers.rustfs.entrypoints=websecure" |
| 63 | +# 告诉该路由器使用名为 le 的证书解析器(即您之前配置的 Let's Encrypt / ACME 解析器)来自动为 your.rustfs.com 获取和管理 SSL/TLS 证书。 |
| 64 | +- "traefik.http.routers.rustfs.tls.certresolver=le" |
| 65 | +# 定义了一个名为 rustfs 的 服务。该服务指定了流量的最终目的地是 Docker 网络内部 rustfs 容器的 9001 端口。 |
| 66 | +- "traefik.http.services.rustfs.loadbalancer.server.port=9001" |
| 67 | +# 显式启用该路由器上的 TLS(传输层安全协议)。这是激活 HTTPS 的必要步骤。结合 certresolver=le,意味着使用 Let's Encrypt 证书进行加密连接。 |
| 68 | +- "traefik.http.routers.rustfs.tls=true" |
| 69 | +``` |
| 70 | + |
| 71 | +### 测试验证 |
| 72 | + |
| 73 | +将 traefik 和 RustFS 内容写入一个 `docker-compose.yml` 文件,内容如下: |
| 74 | + |
| 75 | +``` |
| 76 | +services: |
| 77 | + traefik: |
| 78 | + image: traefik:v3.5.4 |
| 79 | + container_name: traefik |
| 80 | + command: |
| 81 | + - --log.level=DEBUG |
| 82 | + - --accesslog=true |
| 83 | + - --api.insecure=true # 可选: 启用 Traefik Web UI(调试用) |
| 84 | + - --providers.docker=true |
| 85 | + - --providers.docker.endpoint=unix:///var/run/docker.sock |
| 86 | + - --providers.docker.exposedbydefault=false |
| 87 | + - --entrypoints.web.address=:80 |
| 88 | + - --entrypoints.websecure.address=:443 |
| 89 | + - --providers.docker.network=rustfs |
| 90 | + |
| 91 | + - --certificatesresolvers.le.acme.storage=/etc/traefik/acme.json |
| 92 | + - --entrypoints.web.http.redirections.entrypoint.to=websecure |
| 93 | + - --entrypoints.web.http.redirections.entrypoint.scheme=https |
| 94 | + - --certificatesresolvers.le.acme.httpchallenge=true |
| 95 | + - --certificatesresolvers.le.acme.httpchallenge.entrypoint=web |
| 96 | + ports: |
| 97 | + - "80:80" |
| 98 | + - "443:443" |
| 99 | + - "8443:8443" |
| 100 | + - "8080:8080" # Traefik Dashboard (http://localhost:8080) |
| 101 | + labels: |
| 102 | + - "traefik.enable=true" |
| 103 | + volumes: |
| 104 | + - /var/run/docker.sock:/var/run/docker.sock:ro |
| 105 | + - ./acme.json:/etc/traefik/acme.json |
| 106 | + # - ./traefik.yml:/etc/traefik/traefik.yml |
| 107 | + networks: |
| 108 | + - rustfs |
| 109 | +
|
| 110 | + rustfs: |
| 111 | + image: rustfs/rustfs:1.0.0-alpha.66 |
| 112 | + container_name: rustfs-traefik |
| 113 | + hostname: rustfs |
| 114 | + labels: |
| 115 | + - "traefik.enable=true" |
| 116 | + - "traefik.http.routers.rustfs.rule=Host(`your.rustfs.com`)" |
| 117 | + - "traefik.http.routers.rustfs.entrypoints=websecure" |
| 118 | + - "traefik.http.routers.rustfs.tls.certresolver=le" |
| 119 | + - "traefik.http.services.rustfs.loadbalancer.server.port=9001" |
| 120 | + - "traefik.http.routers.rustfs.tls=true" |
| 121 | + - "traefik.http.routers.rustfs.priority=100" |
| 122 | + environment: |
| 123 | + # Use service names and correct disk indexing (1..4 to match mounted paths) |
| 124 | + - RUSTFS_VOLUMES=/data |
| 125 | + - RUSTFS_ADDRESS=0.0.0.0:9000 |
| 126 | + - RUSTFS_CONSOLE_ENABLE=true |
| 127 | + - RUSTFS_CONSOLE_ADDRESS=0.0.0.0:9001 |
| 128 | + - RUSTFS_ACCESS_KEY=rustfsadmin |
| 129 | + - RUSTFS_SECRET_KEY=rustfsadmin |
| 130 | + - RUSTFS_CMD=rustfs |
| 131 | + ports: |
| 132 | + - "9000:9000" # API endpoint |
| 133 | + - "9001:9001" # Console |
| 134 | + volumes: |
| 135 | + - data:/data |
| 136 | + #command: ["sh", "-c", "sleep 3 && rustfs"] |
| 137 | + healthcheck: |
| 138 | + test: |
| 139 | + [ |
| 140 | + "CMD", |
| 141 | + "sh", "-c", |
| 142 | + "curl -f http://localhost:9000/health && curl -f http://localhost:9001/health" |
| 143 | + ] |
| 144 | + interval: 10s |
| 145 | + timeout: 5s |
| 146 | + retries: 3 |
| 147 | + start_period: 30s |
| 148 | + networks: |
| 149 | + - rustfs |
| 150 | +
|
| 151 | +networks: |
| 152 | + rustfs: |
| 153 | + driver: bridge |
| 154 | + name: rustfs |
| 155 | +
|
| 156 | +volumes: |
| 157 | + data: |
| 158 | + driver: local |
| 159 | +``` |
| 160 | + |
| 161 | +创建一个用于存储 let's encrypt 证书的文件 acme.json,**并确保权限是 600**,和 `docker-compose.yml` 位于同目录: |
| 162 | + |
| 163 | +``` |
| 164 | +touch acme.json |
| 165 | +chmod 600 acme.json |
| 166 | +``` |
| 167 | + |
| 168 | +执行命令: |
| 169 | + |
| 170 | +``` |
| 171 | +docker compose up -d |
| 172 | +``` |
| 173 | + |
| 174 | +查看结果: |
| 175 | + |
| 176 | +``` |
| 177 | +docker compose ps |
| 178 | +NAME IMAGE COMMAND SERVICE CREATED STATUS PORTS |
| 179 | +rustfs-traefik rustfs/rustfs:1.0.0-alpha.66 "/entrypoint.sh rust…" rustfs 46 minutes ago Up 46 minutes (healthy) 0.0.0.0:9000-9001->9000-9001/tcp, [::]:9000-9001->9000-9001/tcp |
| 180 | +traefik traefik:v3.5.4 "/entrypoint.sh --lo…" traefik 46 minutes ago Up 46 minutes 0.0.0.0:80->80/tcp, [::]:80->80/tcp, 0.0.0.0:443->443/tcp, [::]:443->443/tcp, 0.0.0.0:8080->8080/tcp, [::]:8080->8080/tcp, 0.0.0.0:8443->8443/tcp, [::]:8443->8443/tcp |
| 181 | +``` |
| 182 | + |
| 183 | +通过 `https://your.rustfs.com` 访问 RustFS 服务(用户名和密码默认为 `rustfsadmin`)。 |
| 184 | + |
| 185 | +当然,也可以通过[mc](../developer/mc.md)来进行验证: |
| 186 | + |
| 187 | +``` |
| 188 | +mc alias set rustfs https://your.rustfs.com rustfsadmin rustfsadmin |
| 189 | +mc ls rustfs |
| 190 | +``` |
| 191 | + |
| 192 | +**注意**:上述配置使用了 https 访问应用,如果不想使用 https,而是通过 http 来访问应用,删除证书和 tls 相关配置即可。**但是建议用 https 访问应用,这样更安全**。 |
0 commit comments