Fly.io 是一个运行全栈应用和数据库的平台,可以通过 Dockerfile 部署各种项目,并且提供了免费计划:(似乎 $5 以内都会免除费用,但我的用量还不足以付费)

更新:现在只提供一次性的 $5 额度,好在旧版计划仍然不收费 (~ ̄ ▽  ̄)~

When you sign up for a Fly.io account, you get a one-time $5 free trial credit to let you test-drive Fly.io at no cost.

If you were on the free Hobby plan at the time that the paid Hobby plan became the default for new users, your plan is now called the Legacy Hobby plan.Your costs stay the same as they were, with no monthly subscription fee, and no included usage beyond the free resource allowances that apply to all plans.

  • 最多 3 个 shared-cpu-1x 256mb VM
  • 总计 3GB 持久卷存储
  • 分地区的 160GB 出站传输数据

具体介绍可以看官方文档,注册和安装步骤就不再讲述(需要信用卡)。

最近想换个评论系统,虽然也没人评论。在网上找教程时发现用它来部署 Artalk 的挺少,而且略显繁琐,本文仅用一个 fly.toml 文件来部署,不需要额外连接数据库。当然 Docker 也是最近才在摸索,如果有什么写的不对的地方欢迎指正。

需要会基本的 CLI 操作

太长不看版: 复制下面的完整配置至一个 .toml 文件,修改 app 名字和地区,在此目录下直接运行 fly launch 命令即可,控制台会有步骤引导。

运行 flyctl

创建一个目录,在目录下运行 fly launch,如果有 Dockerfile 会自动识别然后通过终端交互来继续部署步骤,也可以不使用 Dockerfile:

1
2
3
4
5
@Misaka ➜ test  fly launch
Creating app in /home/misaka/test
Scanning source code
Could not find a Dockerfile, nor detect a runtime or framework from source code. Continuing with a blank app.
? Choose an app name (leave blank to generate one): test-artalk

提示创建应用名称,这里我使用 test-artalk

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
? Choose an app name (leave blank to generate one): test-artalk
automatically selected personal organization: Misaka
Some regions require a paid plan (bom, fra, maa).
See https://fly.io/plans to set up a plan.

? Choose a region for deployment: [Use arrows to move, type to filter]
Hong Kong, Hong Kong (hkg)
Ashburn, Virginia (US) (iad)
Johannesburg, South Africa (jnb)
Los Angeles, California (US) (lax)
London, United Kingdom (lhr)
Madrid, Spain (mad)
Miami, Florida (US) (mia)
> Tokyo, Japan (nrt)
Chicago, Illinois (US) (ord)
Bucharest, Romania (otp)
Phoenix, Arizona (US) (phx)
Querétaro, Mexico (qro)
Santiago, Chile (scl)
Seattle, Washington (US) (sea)
Singapore, Singapore (sin)

提示选择部署地区,通过上下键选择和 Enter 确认,默认离你最近,这里我选择 Tokyo, Japan (nrt)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
? Choose a region for deployment: Tokyo, Japan (nrt)
App will use 'nrt' region as primary

Created app 'test-artalk' in organization 'personal'
Admin URL: https://fly.io/apps/test-artalk
Hostname: test-artalk.fly.dev
? Would you like to set up a Postgresql database now? No
? Would you like to set up an Upstash Redis database now? No
Wrote config file fly.toml
? Would you like to deploy now? No
Validating /home/misaka/test/fly.toml
Platform: machines
✓ Configuration is valid
Your app is ready! Deploy with `flyctl deploy`

提示是否创建数据库和是否立即部署,这里全部选择 NO

当前目录已自动初始化一个部署模板 fly.toml

如果用 Fly.io 创建数据库,会消耗一个应用实例,但是绑定持久化储存卷只消耗 1GB 空间,所以接下来编辑 fly.toml,添加 Docker 的镜像源并挂载一个卷来储存 Artalk 的数据:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
app = "test-artalk"
primary_region = "nrt"

[build]
image = 'artalk/artalk-go:latest'

[[mounts]]
source = 'artalk_data'
destination = '/data'

[http_service]
internal_port = 23366
force_https = true
auto_stop_machines = true
auto_start_machines = true
min_machines_running = 1

[[vm]]
size = 'shared-cpu-1x'

添加 [build][[mounts]] 项,[[vm]] 项为使用的机器配置,这样写会使用 shared-1x-cpu@256MB,即最低配置。

然后就可以通过 fly deploy 命令部署了,如果有安装 docker 想本地构建也可以 fly deploy --local-only,会自动创建一个 1GB 的卷与当前实例绑定。部署完成后还会自动分配一个域名,比如我的为 test-artalk.fly.dev

创建 Artalk 管理员

部署成功以后由于没有管理员账户,是无法登录控制台的,这里通过 fly ssh console 命令,可以连接到当前应用的运行实例:

1
2
3
4
5
6
@Misaka ➜ test  fly ssh console
Error: app test-artalk has no started VMs

@Misaka ➜ test fly ssh console
Connecting to fdaa:2:5bef:a7b:9b:1172:33f3:2... complete
3287079add3085:/#

如果出现第一个命令一样的 Error,只需要访问一下部署后给的域名就行(原因是空闲自动停止,后面会提到),成功连接则像第二个命令。然后输入 artalk admin 命令添加管理员信息:

1
2
3
4
5
6
7
8
9
10
11
12
3287079add3085:/# artalk admin
--------------------------------
Create admin account
--------------------------------
Enter Username: Misaka
Enter Email: i@mska.one
Enter Password:
Retype Password:
--------------------------------
Name: Misaka
Mail: i@mska.one
--------------------------------

按照如上提示输入就行了,非常简单,然后就可以使用管理员账户登录 Artalk 控制台了,后续 Artalk 的配置和迁移就不再赘述。

如有更新,只需在目录下运行 fly deploy 即可。如果不小心丢失了配置文件,可以执行 fly config save -a test-artalktest-artalk 替换为你的应用名)就会在当前目录生成该应用的 .toml 文件,再执行 fly deploy 就行了。

补充项

1
2
3
4
5
6
[http_service]
internal_port = 23366
force_https = true
auto_stop_machines = true
auto_start_machines = true
min_machines_running = 0

在部署模板 fly.toml 中,有几项可以看情况修改,auto_stop_machinesauto_start_machines,默认都为 true,即在应用不活动时自动停止,收到请求时自动启动,min_machines_running 为保持运行的最小机器数量,可以参考 Automatically Stop and Start Machines,实际用起来也就 300-500 ms 的启动延迟,对 Artalk 影响不大,所以我就没改,还能省一些资源:

1
2
3
2023-06-16T13:33:32.635 app[3287079add3085] nrt [info] [ 350.531811] reboot: Restarting system

2023-06-16T14:00:32.292 app[3287079add3085] nrt [info] [ 478.690768] reboot: Restarting system

需要自定义域名可以去网页的 dashboard 添加,也可以参考 Custom Domains and SSL Certificates 通过 flyctl certs create example.com 命令添加,使用 Let’s Encrypt 有速率限制,如果一周内重复添加同一域名超过 5 次(比如添加以后又删掉应用重新部署),SSL 证书就会发不下来,需要等一周或者换一个域名添加。使用 fly certs show example.com 查看证书颁发情况,通过 crt.sh 查看域名证书的所有记录。

后续可能会研究一下自动备份数据,看情况吧。

部署的其他应用

发现 Fly.io 很好用以后,我还部署了 AlistMemos 在上面,也就是我的网盘和碎语功能,下面同样给出 fly.toml 的配置:

Alist

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[build]
image = 'xhofe/alist:latest'

[[mounts]]
source = 'alist_data'
destination = '/opt/alist/data'

[http_service]
internal_port = 5244
force_https = true
auto_stop_machines = true
auto_start_machines = true
min_machines_running = 0

[[vm]]
size = 'shared-cpu-1x'

Alist 同样需要使用 fly ssh console 命令连接以后再通过 ./alist admin 命令创建一个管理员,默认用户名为 admin,密码可以通过 fly logs 命令查看,在 log 中显示,登录以后记得修改。

Memos

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[build]
image = 'neosmemo/memos:latest'

[[mounts]]
source = 'memos_data'
destination = '/var/opt/memos'

[http_service]
internal_port = 5230
force_https = true
auto_stop_machines = true
auto_start_machines = true
min_machines_running = 1

[[vm]]
size = 'shared-cpu-1x'

春のあいさつ

Image Source : b黑w白