Skip to content
c456-comPublic

About

How to deploy c456.com apps to box

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

2 Commits

Folders and files

Repository files navigation

c456.com 独立部署

本仓库是 c456.com 的独立部署代码,使用 Kamal 管理容器化部署。

当前已实现:

  • Kamal 部署配置(config/deploy.yml)
  • Traefik 作为 Kamal accessory,与 kamal-proxy 配合路由流量
  • 本地模拟服务器(Dockerfile.box + bin/box),在本机通过 SSH 模拟远程主机,用于本地调试部署流程

前置要求

  • Ruby 3.x
  • Bundler
  • Docker(本机需已启动 Docker 守护进程)

安装

bundle install

Kamal 通过 Gemfile 安装,版本约束为 ~> 2.12。

项目结构

.
├── bin/box                 # 本地模拟服务器管理脚本
├── config/deploy.yml       # Kamal 部署配置
├── docker/box-entrypoint.sh
├── Dockerfile.box          # 模拟服务器镜像(SSH + Docker-in-Docker)
├── .kamal/secrets          # 密钥引用(勿提交真实凭据)
└── Gemfile

本地模拟服务器(bin/box)

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

启动成功后,可通过以下地址访问:

Kamal 部署配置

主配置文件为 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

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 引用,不要在其中写入明文凭据。详见文件内注释。

使用流程

1. 启动本地模拟服务器

bin/box up

2. 验证 Kamal 配置

bundle exec kamal config

注意:Kamal 默认使用 git commit hash 作为版本号,需要 git 仓库;也可临时指定版本:

VERSION=test bundle exec kamal config

3. 部署

在 box 就绪、应用镜像和 registry 配置完成后:

# 首次部署(安装 Docker 网络、proxy、accessories 等)
bundle exec kamal setup

# 后续部署
bundle exec kamal deploy

常用 Kamal 命令

bundle 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 配置即可

About

How to deploy c456.com apps to box

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages