fly.io ruby on rails

这篇博客详细介绍了如何将一个Ruby应用,特别是Sinatra应用,部署到Fly.io平台。从安装curl和flyctl开始,然后配置环境变量,运行应用,创建并部署fly.toml文件,最后设置SSL域名,实现远程访问。整个过程包括了代码示例和可能出现的问题解决方法。
摘要由CSDN通过智能技术生成

Build, Deploy and Run a Ruby Application · Fly Docsicon-default.png?t=M4ADhttps://fly.io/docs/getting-started/ruby/首先使用命令在项目文件夹安装curl -L https://fly.io/install.sh | sh

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  1415    0  1415    0     0   1889      0 --:--:-- --:--:-- --:--:--  1886
######################################################################## 100.0%
set channel to shell
flyctl was installed successfully to /home/linlin/.fly/bin/flyctl
Manually add the directory to your $HOME/.bash_profile (or similar)
  export FLYCTL_INSTALL="/home/linlin/.fly"
  export PATH="$FLYCTL_INSTALL/bin:$PATH"
Run '/home/linlin/.fly/bin/flyctl --help' to get started

把这些代码放进文件中vim ~/.bashrc

export FLYCTL_INSTALL="/home/linlin/.fly"
  export PATH="$FLYCTL_INSTALL/bin:$PATH"

flyctl auth login

登录进flyctl,再次运行新的APP就不需要登录了 ,直接创建运行就可以,运行命令rackup

把本地的一个简单的应用,部署到远程  

例如一个sinatra 应用。

app.rb

#!/usr/bin/env ruby

require 'rubygems'
require 'bundler/setup'
require 'sinatra'

get '/' do
  "<h1>Hello From Ruby on Fly!</h1>"
end

get '/:name' do
  "<h1>Hello From Ruby on Fly!</h1></br>and hello to #{params[:name]}"
end

Gemfile:

source 'https://rubygems.org'

ruby '3.1.0'
gem 'sinatra'
gem 'puma'

config.ru

require './app.rb'

run Sinatra::Application

本地运行:$ rackup

Puma starting in single mode...
* Puma version: 5.6.4 (ruby 3.1.0-p0) ("Birdie's Version")
*  Min threads: 0
*  Max threads: 5
*  Environment: development
*          PID: 300873
* Listening on http://127.0.0.1:9292
* Listening on http://[::1]:9292
Use Ctrl-C to stop

在远程运行   flyctl  launch 注意在存在rb  Gemfile文件夹内,此时是错误的文件夹

第一次在远程运行会出现加载较慢的情况,后续就不会了。

创建APP的时候,注意名称不要重复,不要下划线,可以选择公开与否和地区。

 可以发现,在本地代码文件夹,会多出一个文件: fly.toml 

# fly.toml file generated for 1234 on 2022-06-02T17:17:57+08:00

app = "1234"

kill_signal = "SIGINT"
kill_timeout = 5
processes = []

[env]

[experimental]
  allowed_public_ports = []
  auto_rollback = true

[[services]]
  http_checks = []
  internal_port = 8080
  processes = ["app"]
  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"

远程会出现项目,点击进入,是详情页面 可以看到详细的信息

如何做部署  flyctl deploy  (前提:需要有个  fly.toml 文件)

执行命令,报错也没关系,因为不是在本地的docker运行的

点击1234hostname的网址并不能打开页面,那么出错了,应该在hi目录下增加文件fly.toml,重新运行刚才的命令

linlin@linlin-i5:/workspace/blog_fly/hi$ flyctl  launch



===> EXPORTING
Adding layer 'heroku/ruby:env'
Adding layer 'heroku/ruby:gems'
Adding layer 'heroku/ruby:ruby'
Adding 1/1 app layer(s)
Adding layer 'launcher'
Adding layer 'config'
Adding layer 'process-types'
Adding label 'io.buildpacks.lifecycle.metadata'
Adding label 'io.buildpacks.build.metadata'
Adding label 'io.buildpacks.project.metadata'
Setting default process type 'web'
Saving registry.fly.io/1232:cache...
*** Images (dfe5ceea3db5):
      registry.fly.io/1232:cache
      registry.fly.io/1232:deployment-1654162525
Adding cache layer 'heroku/ruby:gems'
--> Building image done
==> Pushing image to fly
The push refers to repository [registry.fly.io/1232]
87dc756f0bc5: Pushed 
a248e8c4fc0a: Pushed 
195ce6778985: Pushed 
b8a76ee77a47: Pushed 
fd941a6af848: Pushed 
70d0bf6c5fc0: Pushed 
a1e3b8b5e582: Pushed 
0b9d6a2b9aef: Pushed 
219a884327cf: Pushed 
6c6007ce9a6d: Pushed 
c2fb59d1c64c: Pushed 
bf8cedc62fb3: Pushed 
deployment-1654162525: digest: sha256:3e73d65e03c43918798c6476038bbeefd61eab82b854c658a82c11b7e945c332 size: 2826
--> Pushing image done
image: registry.fly.io/1232:deployment-1654162525
image size: 657 MB
==> Creating release
--> release v2 created

--> You can detach the terminal anytime without stopping the deployment
==> Monitoring deployment

v0 is being deployed

运行的时间也是比较慢的,可以在远程看到新的APP已经是绿色的可以用了,

 在详情页面也可以看到,增加了许多内容

点击hostname进入网址,可以看到我们写的app

添加参数,也可以正常使用

 设置ssl 域名

点击图中的选项,可以进行域名的设置

 保存之后,就可以输入域名进行访问了

不需要的项目可以进行删除,比如最先创建的错误的APP1234

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值