|
|
mixlone を実装していく前に Ruby on Rails とその背景について軽くおさらいしておきます。 Ruby on Rails には Key となる Component が 2 つあります。
Active Record
Model-View-Contoroller Pattern で言うところの Model 担当。
いわゆる ORM(Object-Relational Maping)。 Java で言うところの Hibernate とか JPA とか 。 .NET で言えば ADO.NET か。
他の ORM と比較しての一番の特徴は “(ほぼ)設定不要” 。
たとえば以下のようなテーブルがあるとすると、
CREATE TABLE products (
id int(11) NOT NULL auto_increment,
name varchar(255),
PRIMARY KEY (id)
);
最低限必要なコードは、
class Product < ActiveRecord::Base; end
だけ。
これだけでこんなことができるようになる。
# あたらしい Product を登録
product = Product.new
product.name = "Product Name"
product.save
# Product を探し名前を更新
product = Product.find(:first, :conditions => "name = 'Old Product'")
product.name = "New Product"
product.save
Active Record が背後でテーブルの情報を調べて setter や getter を準備してくれる。
EJB 2.1 の EntityBean なんかと比較すると涙が出てくるくらい簡単。
でもこんだけじゃ使い物にならないじゃん、などとは思うことなかれ。当然、リレーションの設定も、ヴァリデーションの設定も、数行追加するだけで出来るようになる。
詳細は以下のドキュメント参照。
Action Pack
Model-View-Controller Pattern で言うところの View と Controller 担当。
View を担当する Action View と Controller を担当する Action Controllers がセットになって Action Pack 。
Java で言うところの Struts Model 2 を実現するためのもの。 ASP.NET で言えば code behind を実現するためのもの(?)。
Action Pack では URL から実行する Controller/Action を決定する仕組みになっている。
例えば http://mixlone.jp/ が base directory として、
http://mixlone.jp/xxx/yyy
という URL へのリクエストあったときは XxxController というクラスの yyy というメソッドを実行する、という暗黙のルールがあるので app/controllers/xxx_controller.rb に、
XxxController < ActionController::Base
def yyy
# なにか処理を行う
end
end
というクラスを定義すれば http://mixlone.jp/xxx/yyy にアクセスしたとき実行されるようになる。
でそんとき自動的に app/views/xxx/yyy.rhtml を探してもしあったらそのファイルを View として利用するようになっている。
Action メソッドから View へはインスタンス変数が渡されるので、 Action メソッドでビジネスロジックを実行して View ではそれを表示する場所だけを指定するようなかたちになる。
当然、フォームを扱ったり、 View の部品化(Struts Tiles みたいな)をしたり、すべての Action メソッドの前に実行するメソッドとか後に実行するメソッドとかを定義できたり、いろいろ出来る。
詳細は以下のドキュメント参照。
Ruby on Rails の勉強してるけど、やっぱり本読んでるだけじゃ身に付かないよね、ってことでなんか適当なネタを Rails で実装してみようと思います。
昔友達と PHP の勉強のために mixi のクローン(要するにパクり)を実装しようとした(そして途中で飽きてやめた)ことがあったけど、同じことを Ruby on Rails でやってみます。
また途中で飽きる可能性大ですけど。
Rails 環境の準備
Mac mini の上の VMware Fusion の上の Debian 4.0 の上に作ります。一通り必要となりそうなものを aptitude でインストール。
まずは rails から。
debian:~# aptitude install rails
Reading package lists... Done
Building dependency tree... Done
Reading extended state information
Initializing package states... Done
Writing extended state information... Done
Reading task descriptions... Done
Building tag database... Done
The following NEW packages will be automatically installed:
irb irb1.8 liberb-ruby libpgsql-ruby1.8 libpq4 libreadline-ruby1.8
libredcloth-ruby1.8 libruby1.8 rake rdoc rdoc1.8 ruby ruby1.8
The following packages have been kept back:
initramfs-tools linux-image-2.6-amd64 linux-image-2.6.18-4-amd64
The following NEW packages will be installed:
irb irb1.8 liberb-ruby libpgsql-ruby1.8 libpq4 libreadline-ruby1.8
libredcloth-ruby1.8 libruby1.8 rails rake rdoc rdoc1.8 ruby ruby1.8
0 packages upgraded, 14 newly installed, 0 to remove and 3 not upgraded.
Need to get 4840kB of archives. After unpacking 23.8MB will be used.
Do you want to continue? [Y/n/?]
Writing extended state information... Done
Get:1 http://ftp.jp.debian.org etch/main libruby1.8 1.8.5-4 [1581kB]
Get:2 http://ftp.jp.debian.org etch/main ruby1.8 1.8.5-4 [218kB]
Get:3 http://ftp.jp.debian.org etch/main libreadline-ruby1.8 1.8.5-4 [197kB]
Get:4 http://ftp.jp.debian.org etch/main irb1.8 1.8.5-4 [235kB]
Get:5 http://ftp.jp.debian.org etch/main irb 1.8.2-1 [3662B]
Get:6 http://ftp.jp.debian.org etch/main liberb-ruby 2.0.4+ruby1.8.2-1 [3530B]
Get:7 http://ftp.jp.debian.org etch/main libpq4 8.1.9-0etch1 [274kB]
Get:8 http://ftp.jp.debian.org etch/main libpgsql-ruby1.8 0.7.1-10 [40.7kB]
Get:9 http://ftp.jp.debian.org etch/main libredcloth-ruby1.8 3.0.99.0.svn.20060519-1 [68.0kB]
Get:10 http://ftp.jp.debian.org etch/main ruby 1.8.2-1 [19.0kB]
Get:11 http://ftp.jp.debian.org etch/main rake 0.7.1-1 [109kB]
Get:12 http://ftp.jp.debian.org etch/main rdoc1.8 1.8.5-4 [309kB]
Get:13 http://ftp.jp.debian.org etch/main rdoc 1.8.2-1 [3856B]
Get:14 http://ftp.jp.debian.org etch/main rails 1.1.6-3 [1777kB]
Fetched 4840kB in 9s (508kB/s)
Selecting previously deselected package libruby1.8.
(Reading database ... 22297 files and directories currently installed.)
Unpacking libruby1.8 (from .../libruby1.8_1.8.5-4_amd64.deb) ...
Selecting previously deselected package ruby1.8.
Unpacking ruby1.8 (from .../ruby1.8_1.8.5-4_amd64.deb) ...
Selecting previously deselected package libreadline-ruby1.8.
Unpacking libreadline-ruby1.8 (from .../libreadline-ruby1.8_1.8.5-4_amd64.deb) ...
Selecting previously deselected package irb1.8.
Unpacking irb1.8 (from .../irb1.8_1.8.5-4_all.deb) ...
Selecting previously deselected package irb.
Unpacking irb (from .../archives/irb_1.8.2-1_all.deb) ...
Selecting previously deselected package liberb-ruby.
Unpacking liberb-ruby (from .../liberb-ruby_2.0.4+ruby1.8.2-1_all.deb) ...
Selecting previously deselected package libpq4.
Unpacking libpq4 (from .../libpq4_8.1.9-0etch1_amd64.deb) ...
Selecting previously deselected package libpgsql-ruby1.8.
Unpacking libpgsql-ruby1.8 (from .../libpgsql-ruby1.8_0.7.1-10_amd64.deb) ...
Selecting previously deselected package libredcloth-ruby1.8.
Unpacking libredcloth-ruby1.8 (from .../libredcloth-ruby1.8_3.0.99.0.svn.20060519-1_all.deb) ...
Selecting previously deselected package ruby.
Unpacking ruby (from .../archives/ruby_1.8.2-1_all.deb) ...
Selecting previously deselected package rake.
Unpacking rake (from .../archives/rake_0.7.1-1_all.deb) ...
Selecting previously deselected package rdoc1.8.
Unpacking rdoc1.8 (from .../rdoc1.8_1.8.5-4_all.deb) ...
Selecting previously deselected package rdoc.
Unpacking rdoc (from .../archives/rdoc_1.8.2-1_all.deb) ...
Selecting previously deselected package rails.
Unpacking rails (from .../archives/rails_1.1.6-3_all.deb) ...
Setting up libruby1.8 (1.8.5-4) ...
Setting up ruby1.8 (1.8.5-4) ...
Setting up libreadline-ruby1.8 (1.8.5-4) ...
Setting up irb1.8 (1.8.5-4) ...
Setting up irb (1.8.2-1) ...
Setting up liberb-ruby (2.0.4+ruby1.8.2-1) ...
Setting up libpq4 (8.1.9-0etch1) ...
Setting up libpgsql-ruby1.8 (0.7.1-10) ...
Setting up libredcloth-ruby1.8 (3.0.99.0.svn.20060519-1) ...
Setting up ruby (1.8.2-1) ...
Setting up rake (0.7.1-1) ...
Setting up rdoc1.8 (1.8.5-4) ...
Setting up rdoc (1.8.2-1) ...
Setting up rails (1.1.6-3) ...
データベースには PostgreSQL 8.1 を使います。 MySQL 慣れてないんで。
debian:~# aptitude install postgresql-8.1
Reading package lists... Done
Building dependency tree... Done
Reading extended state information
Initializing package states... Done
Writing extended state information... Done
Reading task descriptions... Done
Building tag database... Done
The following NEW packages will be automatically installed:
openssl postgresql-client-8.1 postgresql-client-common postgresql-common
ssl-cert
The following packages have been kept back:
initramfs-tools linux-image-2.6-amd64 linux-image-2.6.18-4-amd64
The following NEW packages will be installed:
openssl postgresql-8.1 postgresql-client-8.1 postgresql-client-common
postgresql-common ssl-cert
0 packages upgraded, 6 newly installed, 0 to remove and 3 not upgraded.
Need to get 7037kB of archives. After unpacking 20.7MB will be used.
Do you want to continue? [Y/n/?]
Writing extended state information... Done
Get:1 http://ftp.jp.debian.org etch/main openssl 0.9.8c-4 [1005kB]
Get:2 http://ftp.jp.debian.org etch/main postgresql-client-common 71 [40.2kB]
Get:3 http://ftp.jp.debian.org etch/main postgresql-client-8.1 8.1.9-0etch1 [1454kB]
Get:4 http://ftp.jp.debian.org etch/main ssl-cert 1.0.14 [11.1kB]
Get:5 http://ftp.jp.debian.org etch/main postgresql-common 71 [102kB]
Get:6 http://ftp.jp.debian.org etch/main postgresql-8.1 8.1.9-0etch1 [4424kB]
Fetched 7037kB in 16s (432kB/s)
Preconfiguring packages ...
Selecting previously deselected package openssl.
(Reading database ... 24886 files and directories currently installed.)
Unpacking openssl (from .../openssl_0.9.8c-4_amd64.deb) ...
Creating directory /etc/ssl
Selecting previously deselected package postgresql-client-common.
Unpacking postgresql-client-common (from .../postgresql-client-common_71_all.deb) ...
Selecting previously deselected package postgresql-client-8.1.
Unpacking postgresql-client-8.1 (from .../postgresql-client-8.1_8.1.9-0etch1_amd64.deb) ...
Selecting previously deselected package ssl-cert.
Unpacking ssl-cert (from .../ssl-cert_1.0.14_all.deb) ...
Selecting previously deselected package postgresql-common.
Unpacking postgresql-common (from .../postgresql-common_71_all.deb) ...
Selecting previously deselected package postgresql-8.1.
Unpacking postgresql-8.1 (from .../postgresql-8.1_8.1.9-0etch1_amd64.deb) ...
Setting up openssl (0.9.8c-4) ...
Setting up postgresql-client-common (71) ...
Setting up postgresql-client-8.1 (8.1.9-0etch1) ...
Setting up ssl-cert (1.0.14) ...
Setting up postgresql-common (71) ...
adduser: Warning: that home directory does not belong to the user you are currently creating.
Setting up postgresql-8.1 (8.1.9-0etch1) ...
Creating new cluster (configuration: /etc/postgresql/8.1/main, data: /var/lib/postgresql/8.1/main)...
Moving configuration file /var/lib/postgresql/8.1/main/postgresql.conf to /etc/postgresql/8.1/main...
Moving configuration file /var/lib/postgresql/8.1/main/pg_hba.conf to /etc/postgresql/8.1/main...
Moving configuration file /var/lib/postgresql/8.1/main/pg_ident.conf to /etc/postgresql/8.1/main...
Configuring postgresql.conf to use port 5432...
Starting PostgreSQL 8.1 database server: main.
以上でとりあえず必要となるパッケージのインストールは終わり。
では Rails のプロジェクトを作りましょう。
mixi の clone ということで mixlone というプロジェクト名にします。
一般ユーザで以下のコマンドを実行します。
masakazu@debian:~$ rails mixlone
create
create app/controllers
create app/helpers
create app/models
create app/views/layouts
create config/environments
create components
create db
create doc
create lib
create lib/tasks
create log
create public/images
create public/javascripts
create public/stylesheets
create script/performance
create script/process
create test/fixtures
create test/functional
create test/integration
create test/mocks/development
create test/mocks/test
create test/unit
create vendor
create vendor/plugins
create tmp/sessions
create tmp/sockets
create tmp/cache
create Rakefile
create README
create app/controllers/application.rb
create app/helpers/application_helper.rb
create test/test_helper.rb
create config/database.yml
create config/routes.rb
create public/.htaccess
create config/boot.rb
create config/environment.rb
create config/environments/production.rb
create config/environments/development.rb
create config/environments/test.rb
create script/about
create script/breakpointer
create script/console
create script/destroy
create script/generate
create script/performance/benchmarker
create script/performance/profiler
create script/process/reaper
create script/process/spawner
create script/runner
create script/server
create script/plugin
create public/dispatch.rb
create public/dispatch.cgi
create public/dispatch.fcgi
create public/404.html
create public/500.html
create public/index.html
create public/favicon.ico
create public/robots.txt
create public/images/rails.png
create public/javascripts/prototype.js
create public/javascripts/effects.js
create public/javascripts/dragdrop.js
create public/javascripts/controls.js
create public/javascripts/application.js
create doc/README_FOR_APP
create log/server.log
create log/production.log
create log/development.log
create log/test.log
Rails には ActiveRecord という Ruby オブジェクトと RDBMS を結びつけるための仕組みがありますけど、この ActiveRecord のデータベース関連の設定は config/database.yml で行います。デフォルトでは MySQL に接続するような設定になってますけど、今回は PostgreSQL を使いますので、アカウントとパスワードもあわせて以下のように修正します(修正箇所を bold にしてます)。
FILE: config/database.yml
1 # MySQL (default setup). Versions 4.1 and 5.0 are recommended.
2 #
3 # Install the MySQL driver:
4 # gem install mysql
5 # On MacOS X:
6 # gem install mysql -- --include=/usr/local/lib
7 # On Windows:
8 # There is no gem for Windows. Install mysql.so from RubyForApache.
9 # http://rubyforge.org/projects/rubyforapache
10 #
11 # And be sure to use new-style password hashing:
12 # http://dev.mysql.com/doc/refman/5.0/en/old-client.html
13 development:
14 adapter: postgresql
15 database: mixlone_development
16 username: mixlone
17 password: mixlone
18 host: localhost
19
20 # Warning: The database defined as 'test' will be erased and
21 # re-generated from your development database when you run 'rake'.
22 # Do not set this db to the same as development or production.
23 test:
24 adapter: postgresql
25 database: mixlone_test
26 username: mixlone
27 password: mixlone
28 host: localhost
29
30 production:
31 adapter: postgresql
32 database: mixlone_production
33 username: mixlone
34 password: mixlone
35 host: localhost
この通りにデータベースを準備しましょう。 postgres アカウントになって以下のコマンド実行します。
debian:~# su - postgres
postgres@debian:~$ createuser mixlone
Shall the new role be a superuser? (y/n) n
Shall the new role be allowed to create databases? (y/n) n
Shall the new role be allowed to create more new roles? (y/n) n
CREATE ROLE
postgres@debian:~$ createdb -O mixlone -E UTF-8 mixlone_development
CREATE DATABASE
postgres@debian:~$ createdb -O mixlone -E UTF-8 mixlone_test
CREATE DATABASE
postgres@debian:~$ createdb -O mixlone -E UTF-8 mixlone_production
CREATE DATABASE
postgres@debian:~$ psql template1
Welcome to psql 8.1.9, the PostgreSQL interactive terminal.
Type: \copyright for distribution terms
\h for help with SQL commands
\? for help with psql commands
\g or terminate with semicolon to execute query
\q to quit
template1=# alter user mixlone password 'mixlone';
ALTER ROLE
template1=# \q
以上でとりあえずの準備は完了です。
試しにサーバを起動して接続してみましょう。
masakazu@debian:~/mixlone-$ script/server -b 0.0.0.0
=> Booting WEBrick...
=> Rails application started on http://0.0.0.0:3000
=> Ctrl-C to shutdown server; call with --help for options
[2007-09-04 08:39:27] INFO WEBrick 1.3.1
[2007-09-04 08:39:27] INFO ruby 1.8.5 (2006-08-25) [x86_64-linux]
[2007-09-04 08:39:27] INFO WEBrick::HTTPServer#start: pid=3849 port=3000
適当なブラウザからアクセスするとこんな画面が表示されます。

サーバに接続できることを確認できたら Ctrl+C で抜けましょう。
192.168.1.102 - - [04/Sep/2007:08:49:55 JST] "GET / HTTP/1.1" 304 0
- -> /
192.168.1.102 - - [04/Sep/2007:08:49:55 JST] "GET /javascripts/prototype.js HTTP/1.1" 304 0
http://192.168.1.194:3000/ -> /javascripts/prototype.js
192.168.1.102 - - [04/Sep/2007:08:49:55 JST] "GET /javascripts/effects.js HTTP/1.1" 304 0
http://192.168.1.194:3000/ -> /javascripts/effects.js
192.168.1.102 - - [04/Sep/2007:08:49:55 JST] "GET /images/rails.png HTTP/1.1" 304 0
http://192.168.1.194:3000/ -> /images/rails.png
[2007-09-04 08:49:59] INFO going to shutdown ...
[2007-09-04 08:49:59] INFO WEBrick::HTTPServer#start done.
今日はとりあえずここまで。
DRBD – Integrating with Heartbeat V2(http://www.drbd.org/drbd8-howto2.html) の対訳。
誤訳がありましたらコメントにてご指摘ください。
Getting and installing Heartbeat
Heartbeat の入手とインストール
As with Heartbeat V1, the preferred way of installing Heartbeat V2 is through your distribution’s package management frontend (yum, apt, rug, whatever). If you do want to build and install from source, the Linux-HA download area has the necessary archives.
Heartbeat V1 と同様、 Heartbeat V2 のお勧めのインストール方法はディストリビューションのパッケージ管理ツール(yum, apt, rug, など)を利用する方法である。もしソースからビルドとインストールを行いたいのであれば、 Linux-HA のダウンロードサイトに必要となるアーカイブがある。
On the Linux-HA site, you will also find the Getting Started V2 howto that will enable you to get an initial Heartbeat V2 configuration up and running.
Linux-HA サイトには、 Heartbeat V2 の初期設定と起動のための Getting Starget V2 howto も見つけられるだろう。
Prerequisites
前提条件
The same prerequisites apply for Heartbeat V2 as do for Heartbeat V1. In fact, you might run Heartbeat V2 just like V1, with unchanged configuration. If, however, you want to use Heartbeat’s new Cluster Resource Manager (CRM) introduced in V2, you need to include this line in your /etc/ha.d/ha.cf:
Heartbeat V1 と同様の前提条件が Heartbeat V2 でも適用される。実際、設定を変更せず Heartbeat V2 を V1 と同じように実行できるかもしれない。しかし、Heartbeat V2 で新しく導入されたクラスターリソースマネージャ(CRM)を利用したいのであれば、 /etc/ha.d/ha.cf に以下の行を追加する必要がある:
crm yes
Simple, isn’t it? Note that if you have that option enabled, Heartbeat will no longer read the haresources file on startup, and will issue a warning if that file exists.
単純だろう。このオプションを有効にすると、 Heartbeat はもう haresources ファイルを起動時に読み込まなくなり、もしこのファイルが存在した場合に警告するようになる。
Cluster Information Base (CIB) configuration
クラスター情報ベース(CIB) の設定
Heartbeat V2 resources are defined in the Cluster Information Base XML file, usually found in /var/lib/heartbeat/crm/cib.xml. This may be edited manually, see the Linux-HA site for more information (however, see the following paragraph if you prefer a more user-friendly approach).
Heartbeat V2 のリソースはクラスター情報ベースという XML ファイルで定義される。このファイルは普通 /var/lib/heartbeat/crm/cib.xml にある。このファイルは手動で編集される。詳細な情報は Linux-HA サイトを参照せよ。(ただしよりユーザーフレンドリーな方法を好むようであれば次の節を参照。)
Using the Heartbeat V2 GUI
Heartbeat V2 GUI を利用する
Heartbeat V2 includes a GUI for the cluster resource manager, which will manipulate the Cluster Information Base for you. It is written in Python and requires GTK+, Glade, and the associated Python bindings. It’s named ha_client.py, though some distributions rename it to hbgui or similar.
Heartbeat V2 はクラスターリソースマネージャのための GUI を含んでいる。これはクラスター情報ベースを操作する。これは Python で書かれ GTK+, Glade, そして関連する Python バインディングを必要とする。 ha_client.py という名前で、いくつかのディストリビューションはこれを hbgui などと名前を変更している。
Heartbeat V2 DRBD resources
Heartbeat V2 での DRBD リソース
With Heartbeat V2, you may address DRBD resources in one of two ways:
Heartbeat V2 では DRBD リソースを二つのうち一つの方法で示すことになる。
- the drbddisk resource type, as in Heartbeat V1,
Heartbeat V1 で drbddisk リソースタイプ
- the drbd resource type, using the Heartbeat V2/OCF interface.
Heartbeat V2/OCF インターフェースで drbd リソースタイプ
DRBD/Heartbeart V2 configuration check list
DRBD/Heartbeat V2 設定チェックリスト
In order to set up a working DRBD configuration with Heartbeat V2, follow these steps:
Heartbeat V2 と動作する DRBD の設定をするためには以下の手順に従う:
- Set up your DRBD resource (using either the drbddisk or drbd resource type)
DRBD リソースを設定する(drbddisk か drbd リソースタイプを使って)
- Set up your filesystem resource
ファイルシステムリソースを設定する
- Set up your IP address resource
IP アドレスリソースを設定する
- Set up your service resource
サービスリソースを設定する
- Create an order constraint so the DRBD resource is activated before the filesystem resource (so Heartbeat doesn’t attempt to mount a file system on a node that’s still in secondary mode)
ファイルシステムの前に DRBD リソースが有効になるよう順序制約を作る(Heartbeat はまだセカンダリモードのノードのファイルシステムをマウントしようとしない)
- Create a colocation constraint so the filesystem, IP address, and service resources always run on the same node
ファイルシステムと IP アドレスとサービスリソースが同じノードで実行されるよう位置制約を作る
- (optional) Set a preferred node for the service resource.
(オプションで) そのサービスの優先ノードを設定する
DRBD – Integrating with Heartbeat V1(http://www.drbd.org/drbd8-howto1.html) の対訳。
誤訳がありましたらコメントにてご指摘ください。
Note
ノート
DRBD is cluster manager agnostic. You may use any cluster manager for managing DRBD resources. Heartbeat (as described here) serves as the canonical example.
DRBD はクラスターマネージャ不可知論者である。あなたは DRBD のリソースを管理するために様々なクラスターマネージャを利用するかもしれない。(ここで説明する)Heartbeat は正規の例を提供する。
Getting and installing Heartbeat
Heartbeat の取得とインストール
The preferred way of installing Heartbeat is through your distribution’s package management frontend (yum, apt, rug, whatever). If you do want to build and install from source, the Linux-HA download area has the necessary archives.
Heartbeat をインストールする完全な方法はあたなが利用しているディストリビュージョンのパッケージ管理ツール(yum, apt, run, など)を利用することだ。もしソースコードからビルドしインストールしたいのであれば Linux-HA のダウンロードサイトに必要なアーカイブがある。
On the Linux-HA site, you will also find the Getting Started howto that will enable you to get an initial Heartbeat configuration up and running.
Linux-HA サイトには、 Heartbeat の初期設定と起動のための Getting Starget howto も見つけられるだろう。
Prerequisites
前提条件
In order to have your DRBD resources managed by Heartbeat, the following conditions must be met beforehand:
あなたの DRBD リソースを Heartbeat で管理するためには、次の条件を事前に満たさなければならない:
- Heartbeat, the DRBD kernel module, and the DRBD userland tools must be installed on both cluster nodes (if you’re thinking “duh” now, that’s perfectly appropriate)
Heartbeat, DRBD kernel モジュール, そして DRBD のツールが両方のクラスターノードでインストールされていなければならない(あなたが現在 “duh” を考えているなら、それは完全に適切です)。
- You must have your /etc/ha.d/ha.cf and /etc/ha.d/authkeys configured correctly
/etc/ha.d/ha.cf と /etc/ha.d/authkeys が適切に設定されていなければならない。
- DBRD resources must be configured in /etc/drbd.conf, and that file must have been distributed cluster-wide
RDBD のリソースが /etc/drbd.conf で設定されていなければならない。そしてこのファイルはクラスター間で配布されていなければならない。
- The DRBD kernel module must be loaded (the resource scripts won’t do that for you, your specific distribution will, however, offer a facility to automate this at system boot)
DRBD kernel モジュールがロードされていなければならない(リソーススクリプトはこの作業を行ってくれない、しかしながら、特定のディストリビューションは、システムリブートの時点でこの作業を自動で行ってくれる)。
- Network connectivity must be established on the DRBD replication path and all Heartbeat communication paths
ネットワーク接続性が DRBD レプリケーション経路と Heartbeat 通信経路で確立されていなければならない。
- Not required, but highly recommended: you should have tested and verified manual migration of your DRBD resources
必須ではないが強く勧める: DRBD リソースへのテストと手動でのマイグレーションをすでに行っている。
Configuring haresources
haresources の設定
The haresources file, /etc/ha.d/haresources, is where you’ll configure your managed DRBD as a cluster resource. Normally it makes little sense to have DRBD managed without an associated file system. An entry for DRBD in the haresources file might look like this:
haresources ファイル /etc/ha.d/haresources は、あなたの管理する DRBD をクラスターリソースとして設定する場所である。通常、ファイルシステムと関連しない DRBD を管理することには意味がない。DRBD に対する haresources ファイルの設定は以下のようになる:
joe drbddisk::r0 Filesystem::/dev/drbd0::/var/lib/mysql::ext3
This will instruct Heartbeat to manage the DRBD resource r0 concurrently with the ext3 filesystem /var/lib/mysql, which will be mounted from the corresponding block device /dev/drbd0. The preferred node for this resource is joe.
これは /dev/drbd0 というブロックデバイス上の ext3 ファイルシステムで /var/lib/mysql にマウントされている DRBD リソース r0 を管理するよう Heartbeat に指示する。このリソースの優先ノードは joe である。
Of course, the more typical (and complete) usage would be this:
もちろん、より典型的(かつ完全)な利用はこのようになる:
joe drbddisk::r0 Filesystem::/dev/drbd0::/var/lib/mysql::ext3 192.168.0.42 mysqld
… which in addition to the DRBD device and file system will also include a service IP address (192.168.0.42) and a MySQL RDBMS daemon (mysqld) in the same cluster resource.
… DRBD デバイスとファイルシステムに加え、同じクラスターリソース上のサービス IP アドレス(192.168.0.42) と MySQL RDBMS デーモン(mysqld) も設定している。
Note
ノート
Like /etc/drbd.conf, /etc/ha.d/haresources must be identical on both cluster nodes.
/etc/drbd.conf のように、 /etc/ha.d/haresources は両方のクラスターノードで一致していなければならない。
DRBD – Migrating DRBD resources(http://www.drbd.org/drbd8-howto0.html) の対訳。
誤訳がありましたらコメントにてご指摘ください。
Suppose we have our DRBD resource r0, corresponding to /dev/drbd0, mounted on /mnt on one cluster node (joe), and want to mount it on the other (jane). This page outlines the process of doing this manually.
以下を想定する。DRBD リソース r0 がある。r0 は /dev/drbd0 に該当する。r0 はクラスターノードの一方(joe) で /mnt にマウントされている。そしてもう一方のクラスターノード(jane) でマウントしたい。このページではこれを手動で行う方法について説明していく。
Notes
ノート
This walk-through demonstrates the steps in switching resources over manually, which is helpful if you want to test DRBD or see how it works. In any practical configuration, you would have the switch-over/fail-over process managed by a cluster manager.
このデモではリソースの切り替えを手動で行う。もし DRBD のテストをしたいとかどんな感じで動作するのか知りたいのであればとても参考になるだろう。実際の設定ではクラスターマネージャを用いたスイッチオーバ/フェイルオーバをすることになるだろう。
In either scenario, you need to ensure that /etc/drbd.conf is identical on both nodes.
いずれにせよ、確実に両方のノードの /etc/drbd.conf が同じ内容である必要がある。
Initial status
初期状態
At first, let’s have a look what the current DRBD status is from joe’s perspective:
最初に、 joe 側で DRBD の状態がどのようになっているかを確認する。
root@joe:~# cat /proc/drbd
version: 8.0.0 (api:86/proto:86)
SVN Revision: 2759 build by florian@sour, 2007-02-21 16:06:15
0: cs:Connected st:Primary/Secondary ds:UpToDate/UpToDate C r---
ns:0 nr:0 dw:0 dr:0 al:0 bm:0 lo:0 pe:0 ua:0 ap:0
resync: used:0/31 hits:0 misses:0 starving:0 dirty:0 changed:0
act_log: used:0/257 hits:0 misses:0 starving:0 dirty:0 changed:0
Releasing the device
デバイスの解放
The first thing we do now is free /dev/drbd0 (if we don’t do this, DRBD will refuse to change that device’s status):
まず最初にすることは /dev/drbd0 を解放すること(こうしないと DRBD はデバイスの状態を変えようとしても拒否する)。
root@joe:~# umount /dev/drbd0
Demoting the resource
リソースを降格させる
Then we demote r0 on joe, so that the device is secondary on both nodes:
そしたら joe の r0 を降格させる。そうすると両方のノードでデバイスはセカンダリとなる。
root@joe:~# drbdadm secondary r0
root@joe:~# cat /proc/drbd
version: 8.0.0 (api:86/proto:86)
SVN Revision: 2759 build by florian@sour, 2007-02-21 16:06:15
0: cs:Connected st:Secondary/Secondary ds:UpToDate/UpToDate C r---
ns:0 nr:0 dw:0 dr:0 al:0 bm:81 lo:0 pe:0 ua:0 ap:0
resync: used:0/31 hits:0 misses:0 starving:0 dirty:0 changed:0
act_log: used:0/257 hits:0 misses:0 starving:0 dirty:0 changed:0
Promoting the resource
リソースを昇格させる
At this time we can promote jane to become the primary node for this resource:
この時点で jane をこのリソースに対するプライマリノードに昇格させることができる。
root@jane:~# drbdadm primary r0
root@jane:~# cat /proc/drbd
version: 8.0.0 (api:86/proto:86)
SVN Revision: 2759 build by florian@sour, 2007-02-21 16:06:15
0: cs:Connected st:Primary/Secondary ds:UpToDate/UpToDate C r---
ns:0 nr:0 dw:0 dr:0 al:0 bm:81 lo:0 pe:0 ua:0 ap:0
resync: used:0/31 hits:0 misses:0 starving:0 dirty:0 changed:0
act_log: used:0/257 hits:0 misses:0 starving:0 dirty:0 changed:0
Using the resource
リソースを利用する
Finally, we can mount the device on Jane:
最後に jane でデバイスをマウント出来る。
root@jane:~# mount /dev/drbd0 /mnt
Other things you might want to try
ほかに試してみたいこと
DRBD disallows a few things so as to keep you from shooting yourself in the foot. You might try the following things just to see what happens:
DRBD は自分で自分の足を撃ってしまわないよういくつかの制限を設けている。なにが起きるのかを確認するために次の事柄をためしてみたいかもしれない。
- Demoting a primary device to secondary while it’s still mounted. This will fail with a “device held open by someone” error.
まだマウントされている状態でプライマリデバイスをセカンダリに降格させようとする。この場合 “device held open by someone” というエラーで失敗するだろう。
- Making a device primary while the other one is primary too, unless this is explicitly allowed.
(明示的に許可されていない状態で)他方がプライマリの状態でデバイスをプライマリにしようとする。
- Mounting the backing device directly, while being used by DRBD.
DRBD で利用されている状態でブロックデバイスを直接マウントしようとする。
DRBD – Getting started(http://www.drbd.org/drbd8-howto.html) の対訳。
誤訳がありましたらコメントにてご指摘ください。
Installation
インストール
If you install via tarball, you want to read the README and INSTALL notes first.
もし tarball からインストールしたいのであれば、 README と INSTALL を最初に読んだ方がよい。
You want to also read the upgrade*.txt files in the drbd src directory, or online in the drbd subversion repository.
また、 drbd ソースディレクトリかオンラインの drbd subversion リポジトリに存在する upgrade*.txt ファイル群も読んだ方がよい。
Along with the DRBD tarball or your installed DRBD package, you should find a well commented example configuration file. In the tarball this is in ./scripts/drbd.conf, in installed binary packages it most likely ends up in /usr/share/doc/drbd, /usr/share/doc/packages/drbd, or similar.
DRBD の tarball もしくはインストールされた DRBD パッケージと一緒に、たくさんコメントが書かれた設定ファイル例を見つけられるだろう。 tarball の場合 ./scripts/drbd.conf に、バイナリパッケージでインストールした場合はたいてい /usr/share/doc/drbd とか /usr/share/doc/packages/drbd とかにある。
You edit that to your needs, copy it to /etc/drbd.conf on both nodes, and check that the meta-disk points to the right location.
必要に応じてそのファイルを編集し、両方のノードに /etc/drbd.conf としてコピーし、 meta-disk が正しい場所を向いているか確認する。
DRBD meta data
DRBD のメタデータ
DRBD allows you to either place its meta data on the same backing device where it puts the actual usable production data (internal meta data), or on a separate block device (external meta data). Before DRBD 8, the space required for DRBD meta data was fixed at 128MB, regardless of device size. Starting with DRBD 8, you may refer to this table for estimates of DRBD meta data sizes:
DRBD ではデータを格納するデバイス上にメタデータを同居させる方法(内部メタデータ)と、別のデバイスに分ける方法(外部メタデータ)のどちらかの方法でメタデータを格納出来る。 DRBD 8 以前では、DRBD メタデータに要求されるスペースはデバイスのサイズに関わらず 128MB に固定されていた。 DRBD 8 からは、DRBD メタデータのサイズは以下のテーブルから見積もることができる。
| Block device size |
DRBD meta data |
| 1 GB |
2 MB |
| 100 GB |
5 MB |
| 1 TB |
33 MB |
| 4 TB |
128 MB |
Make sure you don’t direct DRBD to write its meta data to a place where you already have existing data you still need. Also, if you are using internal meta data on an existing block device with a file system on it, make sure you shrink that file system first!
まだ必要としているデータが存在している場所に対して DRBD がメタデータを書き込まないように注意しよう。また、すでにファイルシステムを持っているブロックデバイスで内部メタデータを使用する場合は、はじめにファイルシステムを圧縮している(縮めている)ことを確認すること。
Bringing up your DRBDs
DRBD を立ち上げる
Now that we have that drbd.conf, copy it to /etc/drbd.conf on both nodes. Then, on both nodes, do a drbdadm up all. It should come up as both nodes Secondary and Inconsistent.
drbd.conf が出来たら、両方のノードに /etc/drbd.conf としてコピーする。そしたら両方のノードで drbdadm up all を実行する。両方ともセカンダリかつ不整合として起動するはず。
The latter is because the underlying storage is not yet synchronized, and DRBD has no way of knowing whether you want the initial sync from left-to-right, or right-to-left. So you have to tell it. If you have no data in the underlying block devices, it does not matter. But if you have an existing filesystem on the lower level device, and now you sync in the wrong direction, you’d need to get your latest backup ready afterwards…
なんでそうなるか。それはストレージがまだ同期されていないからで、その上 DRBD は右から左に同期すべきなのか、それとも左から右なのか、あなたが最初の同期をどちらにしたいのか知る方法がないから。なのでそれを教えてあげなければならない。もしなにもデータを持っていないブロックデバイスで構成しているのであれば、それはどうでもいいことだろう。しかしすでに構成するデバイスがファイルシステムを持っていて、仮に間違った方向に同期してしまったなら、あなたは最新のバックアップデータの準備をしなければならなくなるだろう。
Performing the initial device synchronization
最初のデバイス同期の実行
You choose which node shall become Primary for the first time (the one with the good data, if you have some), then you do drbdadm — –overwrite-data-of-peer primary all. The result is a full synchronization of the underlying devices (we call this the initial full sync).
まず最初にプライマリになってほしいノード(もしデータがある状態なら条件の良いデータを持っている方)を選択する。そして drbdadm — -overwrite-data-of-peer primary all を実行する。結果としてデバイスの完全な同期がとられる(これを初期フル同期と呼ぶ)。
The device is usable right away, so if you have no file system already, you should create one now.
実行後すぐにデバイスは利用可能になる。もしファイルシステムがない状態なら作ることが出来る。
Change from versions prior to DRBD 8
DRBD 8 からの変更されたこと
Prior to DRBD 8, what is now the –overwrite-data-of-peer option used to be –do-what-I-say. We changed the option because we found out that people mistakenly believed –do-what-I-say could be used to force any drbdadm command, regardless of its implications.
DRBD 8 から –do-what-I-say のために –overwrite-data-of-peer が利用される。このオプションを変更した理由は、その実装にも関わらず –do-what-I-say がすべての drbdadm コマンドを強制することに使えると勘違いする人たちがいることに気づいたからだ。
Solaris 10 で iSCSI Initiator を設定したときのメモ。
Solaris 10 で iSCSI Initiator を利用する場合、 Solaris 10 が 1/06 以降のリリースである必要があります。
以下の文書を参考にしています。
Solaris では iSCSI Initiator を設定するためのコマンドとして iscsiadm というモノが用意されています。
iSCSI Initiator が iSCSI Target を利用しようとした場合、 iSCSI Initiator はネットワーク上の iSCSI Target を何らかの方法で認識しなければならないわけですが、 iSCSI Initiator が iSCSI Target を発見するための方法として、以下の 3 つの方法が用意されています。
- 静的デバイス発見 – iSCSI Target を静的に構成する方法
- SendTargets による動的デバイス発見 – iSCSI Target の IP アドレスとポートの組み合わせから iSCSI Target を発見する方法
- iSNS による動的デバイス発見 – iSNS サーバの IP アドレスとポートの組み合わせから iSCSI Target を発見する方法
ここでは SendTargets による動的デバイス発見で iSCSI Target を認識させます。
なお、前回の例で iSCSI Target として利用した Solaris Express では iSNS は利用できませんが、将来的には iSNS も実装することになっているようです。Solaris Express の iSCSI Target で iSNS が利用できるようになったら評価してみようと思っています。
まずは iSCSI Target を SendTargets の発見アドレスとして登録します。以下で 192.168.1.15 は iSCSI Target の IP アドレスで、 3260 はポート番号です。
bash-3.00# iscsiadm add discovery-address 192.168.1.15:3260
そして SendTargets を有効化します。
bash-3.00# iscsiadm modify discovery --sendtargets enable
これで 192.168.1.15 に登録された iSCSI Target が利用できるようになります。
iscsiadm コマンドで現在認識している iSCSI Target の一覧を表示することができます。
bash-3.00# iscsiadm list target
Target: iqn.1986-03.com.sun:02:5185308e-59e1-efe5-dcac-b4ce033808ba.target01
Alias: target01
TPGT: 1
ISID: 4000002a0000
Connections: 1
Target: iqn.1986-03.com.sun:02:66dbbe37-13fa-6637-c597-cf1f57b3d85c.target02
Alias: target02
TPGT: 1
ISID: 4000002a0000
Connections: 1
Target: iqn.1986-03.com.sun:02:861c6552-3a19-43b8-ccff-91ef396f98de.target03
Alias: target03
TPGT: 1
ISID: 4000002a0000
Connections: 1
試しに format コマンドを実行してみると、以下のようにデバイスが増えています。
bash-3.00# format
Searching for disks...done
AVAILABLE DISK SELECTIONS:
0. c1t0d0 <DEFAULT cyl 2607 alt 2 hd 255 sec 63>
/pci@0,0/pci1000,30@10/sd@0,0
1. c2t0100000C29AE111100002A0046CD8AAFd0 <DEFAULT cyl 97 alt 2 hd 64 sec 32>
/scsi_vhci/disk@g0100000c29ae111100002a0046cd8aaf
2. c2t0100000C29AE111100002A0046CD8C5Dd0 <DEFAULT cyl 97 alt 2 hd 64 sec 32>
/scsi_vhci/disk@g0100000c29ae111100002a0046cd8c5d
3. c2t0100000C29AE111100002A0046CD8C5Ed0 <DEFAULT cyl 97 alt 2 hd 64 sec 32>
/scsi_vhci/disk@g0100000c29ae111100002a0046cd8c5e
Specify disk (enter its number):
あとは普通の SCSI ディスクと同じように扱えます。
例えば上記 3 つで raidz pool を作りたければこんな感じです。
bash-3.00# zpool create tank raidz c2t0100000C29AE111100002A0046CD8AAFd0 \
> c2t0100000C29AE111100002A0046CD8C5Dd0 \
> c2t0100000C29AE111100002A0046CD8C5Ed0
bash-3.00# zpool status
pool: tank
state: ONLINE
scrub: none requested
config:
NAME STATE READ WRITE CKSUM
tank ONLINE 0 0 0
raidz1 ONLINE 0 0 0
c2t0100000C29AE111100002A0046CD8AAFd0 ONLINE 0 0 0
c2t0100000C29AE111100002A0046CD8C5Dd0 ONLINE 0 0 0
c2t0100000C29AE111100002A0046CD8C5Ed0 ONLINE 0 0 0
errors: No known data errors
bash-3.00# df -k
Filesystem kbytes used avail capacity Mounted on
/dev/dsk/c1t0d0s0 20093023 3593070 16299023 19% /
/devices 0 0 0 0% /devices
ctfs 0 0 0 0% /system/contract
proc 0 0 0 0% /proc
mnttab 0 0 0 0% /etc/mnttab
swap 684520 724 683796 1% /etc/svc/volatile
objfs 0 0 0 0% /system/object
/usr/lib/libc/libc_hwcap1.so.1
20093023 3593070 16299023 19% /lib/libc.so.1
fd 0 0 0 0% /dev/fd
swap 683832 36 683796 1% /tmp
swap 683824 28 683796 1% /var/run
tank 144552 32 144462 1% /tank
Solaris Express で iSCSI Target を設定したときのメモ。
将来的には Solaris 10 でも iSCSI Target の設定が出来るようになるようですが、現時点では Solaris Express でないと iSCSI Target は設定できません。
ちなみに iSCSI Initiator は Solaris 10 でも利用可能です。この Entry に続いて Solaris 10 で iSCSI Initiator を設定するためのメモを書く予定でいます。
以下の文書を参考にしています。
Solaris Express では iSCSI Target を設定するためのコマンドとして iscsitadm というモノが用意されています。
iSCSI Target Daemon を起動するために、まずは base directory を設定します。 base directory には Target と LU を構成するためのデータが格納されます。もし利用可能な ZFS pool があるようであればそこに格納した方が望ましい、と上記 “Creating your first target” には書かれていますが、ここではめんどくさいので普通に UFS 上の directory を指定します。
以下の例では /export/iscsi という directory を作成し base directory として設定しています。
bash-3.00# mkdir /export/iscsi
bash-3.00# iscsitadm modify admin -d /export/iscsi
base directory の設定が終われば Target を作成できます。
以下の例では 1GB の Target を disk01, disk02, … という名前で 5 個作成しています。
bash-3.00# iscsitadm create target --size 1g disk01
bash-3.00# iscsitadm create target --size 1g disk02
bash-3.00# iscsitadm create target --size 1g disk03
bash-3.00# iscsitadm create target --size 1g disk04
bash-3.00# iscsitadm create target --size 1g disk05
ps コマンドで確認してみると iscsitgtd という Daemon が起動していることがわかります。
bash-3.00# ps -ef | grep scsi
root 644 566 0 19:46:49 pts/1 0:00 grep scsi
root 627 1 0 19:15:12 ? 1:28 /usr/sbin/iscsitgtd
また base directory には以下のような file と directory が作成されています。
bash-3.00# ls -l /export/iscsi
total 24
-rw------- 1 root sys 1183 Aug 20 19:15 config.xml
lrwxrwxrwx 1 root sys 80 Aug 20 19:15 disk01 -> /export/iscsi/iqn.1986-03.com.sun:02:ed52dc45-b3f1-e1be-cec0-9572450a2fff.disk01
lrwxrwxrwx 1 root sys 80 Aug 20 19:15 disk02 -> /export/iscsi/iqn.1986-03.com.sun:02:66528f32-b1b9-6b0a-cfe9-c8c98f8578dc.disk02
lrwxrwxrwx 1 root sys 80 Aug 20 19:15 disk03 -> /export/iscsi/iqn.1986-03.com.sun:02:fd06731d-42b9-c1c9-bf0e-e4b41ccd8d17.disk03
lrwxrwxrwx 1 root sys 80 Aug 20 19:15 disk04 -> /export/iscsi/iqn.1986-03.com.sun:02:6a93da14-9ddb-4913-e502-fd4f846be66a.disk04
lrwxrwxrwx 1 root sys 80 Aug 20 19:15 disk05 -> /export/iscsi/iqn.1986-03.com.sun:02:32e0173b-5155-6722-e3d1-ae1cf42a71d6.disk05
drwxr-xr-x 2 root sys 512 Aug 20 19:15 iqn.1986-03.com.sun:02:32e0173b-5155-6722-e3d1-ae1cf42a71d6.disk05
drwxr-xr-x 2 root sys 512 Aug 20 19:15 iqn.1986-03.com.sun:02:66528f32-b1b9-6b0a-cfe9-c8c98f8578dc.disk02
drwxr-xr-x 2 root sys 512 Aug 20 19:15 iqn.1986-03.com.sun:02:6a93da14-9ddb-4913-e502-fd4f846be66a.disk04
drwxr-xr-x 2 root sys 512 Aug 20 19:15 iqn.1986-03.com.sun:02:ed52dc45-b3f1-e1be-cec0-9572450a2fff.disk01
drwxr-xr-x 2 root sys 512 Aug 20 19:15 iqn.1986-03.com.sun:02:fd06731d-42b9-c1c9-bf0e-e4b41ccd8d17.disk03
config.xml の中身は次のようになっています。
<config version='1.0'>
<target>
disk01
<lun-list>
<lun>0x0</lun>
</lun-list>
<iscsi-name>
iqn.1986-03.com.sun:02:ed52dc45-b3f1-e1be-cec0-9572450a2fff.disk01
</iscsi-name>
</target>
... 以下 <target></target> が disk02, disk02 ... の分記述されている ...
</config>
disk01(実態は iqn.1986-03.com.sun… という長ったらしい名前の directory) の中身はこんな感じです。
bash-3.00# ls -l /export/iscsi/disk01/
total 2098194
-rw------- 1 root sys 1073741824 Aug 20 19:20 lun.0
-rw------- 1 root sys 345 Aug 20 19:20 params.0
恐らく file size から lun.0 は scsi device image だと思われます。
params.0 の中身はこんな感じで、こちらは LU の設定情報でしょう。
<params version='1.0'>
<size>0x200000</size>
<status>online</status>
<interleave>1</interleave>
<bps>512</bps>
<spt>16</spt>
<cylinders>32768</cylinders>
<heads>4</heads>
<rpm>7200</rpm>
<dtype>disk</dtype>
<vid>SUN</vid>
<pid>SOLARIS</pid>
<guid>0100000c29ae111100002a0046c969cc</guid>
</params>
近所においしくていつもすいてるお店ないかなーってずっと思ってたんだけど、いいとこ見つけちゃった。
そういや最近どっかで FRESHNESS BURGER が SPAM BURGER 発売みたいな記事読んだな、そしてたしか近所に FRESHNESS BURGER あったな、ってことで行ってみたんだけど、心配になるくらいガラガラで、 SPAM BURGER はしょっぱくておいしかった。
今日たまたますいてただけじゃありませんように。徒歩 3 分なので、たぶん通います。
disable 化されてた新しい Mac mini の VT-x オプションだけど、 Parallels の Support Forum にあった以下の方法で enable 化できました。
rEFIt のバージョンが違ったからか “14. In rEFIt menu, select Console.” の部分が微妙に違ってたけど、それ以外は書いてある通りに進んでいけば OK でした。
CentOS 4 と CentOS 5 の x86_64 版インストールしてみたけど、問題なく動作してるみたいです。
|
|