- 用tsuru部署Ruby应用
- 概述
- 在tsuru中创建应用
- 应用代码
- . 创建项目 (rails new blog)
- . 为Post生成脚手架(rails generate scaffold Post title:string body:text)
- 通过Git部署
- 列出依赖
- 运行应用
- 使用服务
- 部署钩子
- 进一步探索
用tsuru部署Ruby应用
概述
本文档是在tsuru中部署一个简单的Ruby应用的实战指南。例子应用是一个关联MySQL服务的简单Rails项目。
在tsuru中创建应用
使用app-create
命令创建应用:
$ tsuru app-create <app-name> <app-platform>
对于Ruby来说,应用平台是,你猜,ruby
!让我们脑洞打开,开发一个从未有人做过的教学应用:一个博客,而且它的名字也应该是很有创意,让我们叫它blog
:
$ tsuru app-create blog ruby
使用platform-list
命令列出所有的可用的平台。使用app-list
查看你所有的应用。
$ tsuru app-list
+-------------+-------------------------+-------------+
| Application | Units State Summary | Address |
+-------------+-------------------------+-------------+
| blog | 0 of 0 units in-service | |
+-------------+-------------------------+-------------+
应用代码
本文档不会专注于介绍如何用Rails实现博客,你可以从GitHub clone整个源代码:https://github.com/tsuru/tsuru-ruby-sample。 下面是我们在项目中所做的事情:
. 创建项目 (rails new blog)
. 为Post生成脚手架(rails generate scaffold Post title:string body:text)
通过Git部署
在创建新的应用时,tsuru会显示应该使用的Git远程分支。用app-info
命令可以获得其信息:
$ tsuru app-info --app blog
Application: blog
Repository: git@192.168.50.4.nip.io:blog.git
Platform: ruby
Teams: admin
Address: blog.192.168.50.4.nip.io
Owner: admin@example.com
Team owner: admin
Deploys: 0
Pool: theonepool
App Plan:
+---------------+--------+------+-----------+--------+---------+
| Name | Memory | Swap | Cpu Share | Router | Default |
+---------------+--------+------+-----------+--------+---------+
| autogenerated | 0 MB | 0 MB | 100 | | false |
+---------------+--------+------+-----------+--------+---------+
Git远程分支被用来通过Git部署应用。当修改被推送到tsuru远程分支时,项目同时也被部署:
$ git push git@192.168.50.4.nip.io:blog.git master
Counting objects: 86, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (75/75), done.
Writing objects: 100% (86/86), 29.75 KiB, done.
Total 86 (delta 2), reused 0 (delta 0)
remote: Cloning into '/home/application/current'...
remote: requirements.apt not found.
remote: Skipping...
remote: /home/application/current /
remote: Fetching gem metadata from https://rubygems.org/.........
remote: Fetching gem metadata from https://rubygems.org/..
#####################################
# OMIT (see below) #
#####################################
remote: ---> App will be restarted, please check its log for more details...
remote:
To git@192.168.50.4.nip.io:blog.git
* [new branch] master -> master
如果遇到"Permission denied (publickey)."的错误,请确保你是团队一员并把公钥加到tsuru中。用key-add
命令添加公钥:
$ tsuru key-add mykey ~/.ssh/id_rsa.pub
使用git remote add
命令来避免每次push代码时都要输入整个远程仓库的链接:
$ git remote add tsuru git@192.168.50.4.nip.io:blog.git
然后运行:
$ git push tsuru master
Everything up-to-date
从此之后就可以省略掉—app
标记:
$ tsuru app-info
Application: blog
Repository: git@192.168.50.4.nip.io:blog.git
Platform: ruby
Teams: admin
Address: blog.192.168.50.4.nip.io
Owner: admin@example.com
Team owner: admin
Deploys: 0
Pool: theonepool
Units: 1
+------------+---------+
| Unit | State |
+------------+---------+
| eab5151eff | started |
+------------+---------+
App Plan:
+---------------+--------+------+-----------+--------+---------+
| Name | Memory | Swap | Cpu Share | Router | Default |
+---------------+--------+------+-----------+--------+---------+
| autogenerated | 0 MB | 0 MB | 100 | | false |
+---------------+--------+------+-----------+--------+---------+
列出依赖
在上一个部分我们忽略了部署过程的依赖。在tsuru中,一个应用可以有两种依赖:
- 操作系统级别的依赖, 以底层操作系统使用的包管理器为代表(比如:
yum
和apt-get
); - 平台依赖, 以平台/语言依赖的包管理工具为代表(在Ruby中是
bundler
)。
所有的apt-get
依赖必须在requirements.apt
文件中指定,放在应用的根目录,同理,ruby的依赖也必须放在应用根目录名为Gemfile
的文件中。因为要用Rails连接MySQL,我们需要用gem
安装mysql
,这个包依赖于:libmysqlclient-dev
,因此requirements.apt
内容如下:
libmysqlclient-dev
下面是Gemfile
的内容:
source 'https://rubygems.org'
gem 'rails', '3.2.13'
gem 'mysql'
gem 'sass-rails', '~> 3.2.3'
gem 'coffee-rails', '~> 3.2.1'
gem 'therubyracer', platforms: 'ruby'
gem 'uglifier', '>= 1.0.3'
gem 'jquery-rails'
下面是安装这些依赖的完整输出:
$ git push tsuru master
#####################################
# OMIT #
#####################################
remote: Reading package lists...
remote: Building dependency tree...
remote: Reading state information...
remote: The following extra packages will be installed:
remote: libmysqlclient18 mysql-common
remote: The following NEW packages will be installed:
remote: libmysqlclient-dev libmysqlclient18 mysql-common
remote: 0 upgraded, 3 newly installed, 0 to remove and 0 not upgraded.
remote: Need to get 2360 kB of archives.
remote: After this operation, 9289 kB of additional disk space will be used.
remote: Get:1 http://archive.ubuntu.com/ubuntu/ quantal/main mysql-common all 5.5.27-0ubuntu2 [13.7 kB]
remote: Get:2 http://archive.ubuntu.com/ubuntu/ quantal/main libmysqlclient18 amd64 5.5.27-0ubuntu2 [949 kB]
remote: Get:3 http://archive.ubuntu.com/ubuntu/ quantal/main libmysqlclient-dev amd64 5.5.27-0ubuntu2 [1398 kB]
remote: Fetched 2360 kB in 2s (1112 kB/s)
remote: Selecting previously unselected package mysql-common.
remote: (Reading database ... 41063 files and directories currently installed.)
remote: Unpacking mysql-common (from .../mysql-common_5.5.27-0ubuntu2_all.deb) ...
remote: Selecting previously unselected package libmysqlclient18:amd64.
remote: Unpacking libmysqlclient18:amd64 (from .../libmysqlclient18_5.5.27-0ubuntu2_amd64.deb) ...
remote: Selecting previously unselected package libmysqlclient-dev.
remote: Unpacking libmysqlclient-dev (from .../libmysqlclient-dev_5.5.27-0ubuntu2_amd64.deb) ...
remote: Setting up mysql-common (5.5.27-0ubuntu2) ...
remote: Setting up libmysqlclient18:amd64 (5.5.27-0ubuntu2) ...
remote: Setting up libmysqlclient-dev (5.5.27-0ubuntu2) ...
remote: Processing triggers for libc-bin ...
remote: ldconfig deferred processing now taking place
remote: /home/application/current /
remote: Fetching gem metadata from https://rubygems.org/..........
remote: Fetching gem metadata from https://rubygems.org/..
remote: Using rake (10.1.0)
remote: Using i18n (0.6.1)
remote: Using multi_json (1.7.8)
remote: Using activesupport (3.2.13)
remote: Using builder (3.0.4)
remote: Using activemodel (3.2.13)
remote: Using erubis (2.7.0)
remote: Using journey (1.0.4)
remote: Using rack (1.4.5)
remote: Using rack-cache (1.2)
remote: Using rack-test (0.6.2)
remote: Using hike (1.2.3)
remote: Using tilt (1.4.1)
remote: Using sprockets (2.2.2)
remote: Using actionpack (3.2.13)
remote: Using mime-types (1.23)
remote: Using polyglot (0.3.3)
remote: Using treetop (1.4.14)
remote: Using mail (2.5.4)
remote: Using actionmailer (3.2.13)
remote: Using arel (3.0.2)
remote: Using tzinfo (0.3.37)
remote: Using activerecord (3.2.13)
remote: Using activeresource (3.2.13)
remote: Using coffee-script-source (1.6.3)
remote: Using execjs (1.4.0)
remote: Using coffee-script (2.2.0)
remote: Using rack-ssl (1.3.3)
remote: Using json (1.8.0)
remote: Using rdoc (3.12.2)
remote: Using thor (0.18.1)
remote: Using railties (3.2.13)
remote: Using coffee-rails (3.2.2)
remote: Using jquery-rails (3.0.4)
remote: Installing libv8 (3.11.8.17)
remote: Installing mysql (2.9.1)
remote: Using bundler (1.3.5)
remote: Using rails (3.2.13)
remote: Installing ref (1.0.5)
remote: Using sass (3.2.10)
remote: Using sass-rails (3.2.6)
remote: Installing therubyracer (0.11.4)
remote: Installing uglifier (2.1.2)
remote: Your bundle is complete!
remote: Gems in the groups test and development were not installed.
remote: It was installed into ./vendor/bundle
#####################################
# OMIT #
#####################################
To git@192.168.50.4.nip.io:blog.git
9515685..d67c3cd master -> master
运行应用
如你所见,部署的输出信息中有一个步骤叫做"重启你的应用"。在这个步骤中,如果应用在运行,tsuru会重启它,如果没有运行,tsuru会启动它。但是tsuru是如何启动一个应用呢?非常简单,它使用了一个Procfile(从Foreman中偷学的概念)。这个Procfile用来描述应用如何被启动。如下是Procfile的内容:
web: bundle exec rails server -p $PORT -e production
现在我们提交文件并将其推送到tsuru的git服务器,再运行一次部署任务:
$ git add Procfile
$ git commit -m "Procfile: added file"
$ git push tsuru master
#####################################
# OMIT #
#####################################
remote: ---> App will be restarted, please check its log for more details...
remote:
To git@192.168.50.4.nip.io:blog.git
d67c3cd..f2a5d2d master -> master
应用部署成功,可以通过app-list
命令获得IP或者主机名,然后用浏览器去访问。比如,在下面的列表中:
$ tsuru app-list
+-------------+-------------------------+---------------------+
| Application | Units State Summary | Address |
+-------------+-------------------------+---------------------+
| blog | 1 of 1 units in-service | blog.cloud.tsuru.io |
+-------------+-------------------------+---------------------+
使用服务
应用没有成功运行,原因在于rails不能连接到MySQL服务器,而我们在rails应用和MySQL实例之间添加了关联关系。要实现这点,必须使用一个服务.服务的工作流分为下面两步:
. 创建一个服务的实例. 将服务的实例绑定到应用
但是如何知道哪些服务是可用的呢?这很简单,运行service-list
命令:
$ tsuru service-list
+----------------+-----------+
| Services | Instances |
+----------------+-----------+
| elastic-search | |
| mysql | |
+----------------+-----------+
上面service-list
的输出说明有两个可用的服务:"elastic-search"和"mysql",并且没有实例。通过service-add
命令可以创建MySQL实例:
$ tsuru service-add mysql blogsql
Service successfully added.
现在,如果再次运行service-list
命令,可以从输出中看到新的服务实例:
$ tsuru service-list
+----------------+-----------+
| Services | Instances |
+----------------+-----------+
| elastic-search | |
| mysql | blogsql |
+----------------+-----------+
使用service-bind
命令将服务实例绑定到应用:
$ tsuru service-bind mysql blogsql
Instance blogsql is now bound to the app blog.
The following environment variables are now available for use in your app:
- MYSQL_PORT
- MYSQL_PASSWORD
- MYSQL_USER
- MYSQL_HOST
- MYSQL_DATABASE_NAME
For more details, please check the documentation for the service, using service-doc command.
从绑定的输出中我们看出,可以通过环境变量来连接MySQL服务器。下一步就是更新conf/database.yml
去使用这些环境变量去连接数据库:
production:
adapter: mysql
encoding: utf8
database: <%= ENV["MYSQL_DATABASE_NAME"] %>
pool: 5
username: <%= ENV["MYSQL_USER"] %>
password: <%= ENV["MYSQL_PASSWORD"] %>
host: <%= ENV["MYSQL_HOST"] %>
现在提交修改,再次运行部署:
$ git add conf/database.yml
$ git commit -m "database.yml: using environment variables to connect to MySQL"
$ git push tsuru master
Counting objects: 7, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (4/4), 535 bytes, done.
Total 4 (delta 3), reused 0 (delta 0)
remote:
remote: ---> tsuru receiving push
remote:
remote: ---> Installing dependencies
#####################################
# OMIT #
#####################################
remote:
remote: ---> Restarting your app
remote:
remote: ---> Deploy done!
remote:
To git@192.168.50.4.nip.io:blog.git
ab4e706..a780de9 master -> master
如果现在尝试访问admin页面,会得到另外一个错误:"Table 'blogsql.django_session' doesn't exist"
。这意味着绑定成功,可以访问数据库服务器,但是数据库还没有建立。需要在远程服务器运行rake db:migrate
。我们可以使用app-run
在机器上执行命令,所以对于运行rake db:migrate
,可以这么写:
$ tsuru app-run -- RAILS_ENV=production bundle exec rake db:migrate
== CreatePosts: migrating ====================================================
-- create_table(:posts)
-> 0.1126s
== CreatePosts: migrated (0.1128s) ===========================================
部署钩子
每次部署后手动运行rake db:migrate
会很烦人。所以我们可以配置一个自动的钩子,在每次应用重启前或者重启后运行。tsuru会解析一个名为tsuru.yaml
的文件,运行重启的钩子。如扩展所建议的那样,这是一个YAML文件,包含了一系列在重启前后要运行的命令。下面是一个tsuru.yaml文件的例子:
hooks:
restart:
before-each:
- RAILS_ENV=production bundle exec rake db:migrate
更多内容,请查看tsuru.yaml中的钩子文档部分。tsuru会在项目的根目录查找这个文件。让我们提交并且部署它:
$ git add tsuru.yaml
$ git commit -m "tsuru.yaml: added file"
$ git push tsuru master
#####################################
# OMIT #
#####################################
To git@192.168.50.4.nip.io:blog.git
a780de9..1b675b8 master -> master
在应用重启前,有必要编译资源文件。可以用rake assets:precompile
命令实现。让我们把这个命令加入tsuru.yaml文件中:
hooks:
build:
- RAILS_ENV=production bundle exec rake assets:precompile
$ git add tsuru.yaml
$ git commit -m "tsuru.yaml: added file"
$ git push tsuru master
#####################################
# OMIT #
#####################################
To git@192.168.50.4.nip.io:blog.git
a780de9..1b675b8 master -> master
顺利完成!现在我们有了一个部署在tsuru上,使用MySQL服务的Rails项目,现在我们可以访问app-info
返回的blog app
的URL了。
进一步探索
更多信息,可以查看tsuru文档
,或者阅读tsuru命令完全使用指南。
原文: http://doc.oschina.net/tsuru-paas?t=52818