本仓库是 c456.com 的独立部署代码,使用 Kamal 管理容器化部署。
当前已实现:
- Kamal 部署配置(
config/deploy.yml) - Traefik 作为 Kamal accessory,与 kamal-proxy 配合路由流量
- 本地模拟服务器(
Dockerfile.box+bin/box),在本机通过 SSH 模拟远程主机,用于本地调试部署流程
- Ruby 3.x
- Bundler
- Docker(本机需已启动 Docker 守护进程)
bundle installKamal 通过 Gemfile 安装,版本约束为 ~> 2.12。
.
├── bin/box # 本地模拟服务器管理脚本
├── config/deploy.yml # Kamal 部署配置
├── docker/box-entrypoint.sh
├── Dockerfile.box # 模拟服务器镜像(SSH + Docker-in-Docker)
├── .kamal/secrets # 密钥引用(勿提交真实凭据)
└── Gemfile
bin/box 会构建并启动一个容器,模拟带 SSH 和 Docker 的远程服务器。Kamal 通过 SSH 连接该容器,就像连接真实主机一样。
本机
├── :2222 → box 容器 SSH
├── :8080 → box 容器 :80 (HTTP,Traefik 等)
└── :8443 → box 容器 :443 (HTTPS)
box 容器内部
Traefik → kamal-proxy → 应用
box 使用 Docker-in-Docker(DinD),Kamal 在 box 内启动的容器端口,通过 box 容器的端口映射暴露到本机。
# 构建镜像并启动模拟服务器(默认命令)
bin/box
bin/box up
# 进入 box 的 SSH 会话
bin/box ssh
# 查看容器状态
bin/box status
# 停止并删除容器
bin/box down
# 查看帮助
bin/box help首次运行 bin/box up 时,会自动在 .box/ssh/ 生成 SSH 密钥,并写入 Kamal 使用的 authorized_keys 和 SSH 配置。
| 变量 | 默认值 | 说明 |
|---|---|---|
BOX_SSH_PORT |
2222 |
本机 SSH 端口 |
BOX_HTTP_PORT |
8080 |
本机 HTTP 端口 |
BOX_HTTPS_PORT |
8443 |
本机 HTTPS 端口 |
BOX_PLATFORM |
linux/amd64 |
构建平台 |
BOX_NAME |
box |
容器名称 |
BOX_IMAGE |
box-server |
镜像名称 |
示例:修改 HTTP 端口后启动
BOX_HTTP_PORT=9080 bin/box up启动成功后,可通过以下地址访问:
- SSH:
ssh -p 2222 -i .box/ssh/id_ed25519 root@127.0.0.1 - HTTP:http://127.0.0.1:8080
- HTTPS:https://127.0.0.1:8443
主配置文件为 config/deploy.yml,当前面向本地 box 环境:
| 配置项 | 当前值 | 说明 |
|---|---|---|
| 部署目标 | 127.0.0.1 |
web 角色主机 |
| SSH 端口 | 2222 |
对应 bin/box 映射 |
| SSH 密钥 | .box/ssh/id_ed25519 |
由 bin/box 自动生成 |
| proxy.ssl | false |
本地开发关闭 Let's Encrypt |
| proxy.run.publish | false |
kamal-proxy 不直接暴露主机端口,由 Traefik 转发 |
Traefik 作为 accessory 部署在 web 角色主机上:
- 镜像:
traefik:v3.7.6(当前最新稳定版) - 监听 box 内 80 端口,映射到本机
8080 - 通过 Docker socket 自动发现容器
- Dashboard:http://dashboard.traefik.local:8080/dashboard/(需在本机
/etc/hosts添加127.0.0.1 dashboard.traefik.local) - kamal-proxy 配置了 Traefik 路由标签,流量路径为:Traefik → kamal-proxy → 应用
敏感信息通过 .kamal/secrets 引用,不要在其中写入明文凭据。详见文件内注释。
bin/box upbundle exec kamal config注意:Kamal 默认使用 git commit hash 作为版本号,需要 git 仓库;也可临时指定版本:
VERSION=test bundle exec kamal config
在 box 就绪、应用镜像和 registry 配置完成后:
# 首次部署(安装 Docker 网络、proxy、accessories 等)
bundle exec kamal setup
# 后续部署
bundle exec kamal deploybundle exec kamal accessory boot traefik # 单独启动 Traefik
bundle exec kamal accessory logs traefik # 查看 Traefik 日志
bundle exec kamal proxy logs # 查看 kamal-proxy 日志
bundle exec kamal app logs # 查看应用日志.box/ssh/目录已加入.gitignore,SSH 私钥不会提交到仓库bin/box需要--privileged模式运行 DinD- 当前
registry指向localhost:5555,部署前需确保镜像仓库可用 - 切换到真实服务器时,修改
config/deploy.yml中的servers、ssh和proxy配置即可