Fly.io 一个 app 运行多个进程的方法
在之前的 Fly.io 部署教程中,web 进程和 worker 进程是分开部署的。这会造成一些问题:
- 设置环境变量需要两边重复设置。
- 需要分别部署或分别回滚,过程可能产生错误。
我在 fly.io 的社区中发现,单个 app 部署多个进程的方法已经作为预览状态提供,有兴趣的可以先行试用。
Preview: multi process apps (get your workers here!) https://community.fly.io/t/preview-multi-process-apps-get-your-workers-here/2316
GeekNote 的部署配置合并后是这个样子,可以用于一般的 Rails 应用:
app = "YOU-APP-NAME"
kill_signal = "SIGINT"
kill_timeout = 5
[env]
[processes]
web = "bin/rails server"
worker = "bundle exec sidekiq"
[deploy]
release_command = "bin/rails db:migrate"
[experimental]
allowed_public_ports = []
auto_rollback = true
[[services]]
processes = ["web"]
http_checks = []
internal_port = 3000
protocol = "tcp"
script_checks = []
[services.concurrency]
hard_limit = 25
soft_limit = 20
type = "connections"
[[services.ports]]
force_https = true
handlers = ["http"]
port = 80
[[services.ports]]
handlers = ["tls", "http"]
port = 443
[[services.tcp_checks]]
grace_period = "1s"
interval = "15s"
restart_limit = 0
timeout = "2s"
需要注意的就是 [processes]
段和 [[services]]
里面的 processes
设置。
另外,每个进程都是独立一个虚拟机的,而不是在同一个虚拟机里运行,这跟 heroku 类似。
需要调整虚拟机实例,可以用以下命令:
# Change VM counts:
fly scale count web=2 worker=1
# Change VM size:
fly scale vm shared-cpu-1x --group worker
# Change regions:
fly regions set hkg --group worker
目前 GeekNote 使用下来没有什么问题,后续有问题会继续更新。