Cloud Foundryを使ってみよう[1]
この記事は1年以上前に投稿されました。情報が古い可能性がありますので、ご注意ください。
Cloud Foundryとは
Cloud Foundryは米VMware社が提供するオープンソースのPaaSプラットフォームです。ソースコードを取得することでプライベートなPaaSを構築できるほか、VMware社が運営するCloud Foundryホスティングサービスに接続することができます。
Webアプリケーションフレームワークとして、Javaの「Spring」、Rubyの「Ruby on Rails3」、いわゆるサーバサイドJavaScriptと呼ばれている「Node.js」などを提供します。また、アプリケーションサービスとして、「MySQL」「PostgreSQL」といったRDBMSだけではなく、ドキュメント指向DB「MongoDB」、キーバリューストア型DB「Redis」といったNoSQL、メッセージングサービス「RabbitMQ」などを提供します。
Cloud Foundryのアカウント登録
Cloud Foundryを利用するには、アカウント登録が必要です。https://my.cloudfoundry.com/signup より、メールアドレスを入力してサインアップします。
すぐに登録を受け付けたメールが届きますが、この時点ではまだアカウントは有効となっていません。この後にパスワードが記載されたメールが届きますが、数時間から、数日かかることもあるようです。
VMC のインストール
Cloud Foundryを操作するためのツールとして、Rubyで書かれたコマンドラインインターフェイスプログラムVMCが提供されています。これを通して、Cloud Foundry上のアプリケーションの設定やデプロイなど、すべての操作が可能になっています。
Installing the Command-Line Interface (vmc) を参考に、VMCをインストールします。
本稿では、Debian GNU/Linux 6.0.3 (2012/01時点の安定版)をクライアントとして用います。各種手順は、お使いのクライアントや環境に応じて適宜読み替えてください。
まず、Ruby 1.9をaptitudeでインストールします。
root@debian:~# aptitude install ruby1.9.1
:
Setting up libffi5 (3.0.9-3) ...
Setting up libreadline5 (5.2-7) ...
Setting up libyaml-0-2 (0.1.3-1) ...
Setting up libruby1.9.1 (1.9.2.0-2) ...
Setting up ruby1.9.1 (1.9.2.0-2) ...
root@debian:~#
cf@debian:~$ ruby1.9.1 -v ruby 1.9.2p0 (2010-08-18 revision 29036) [x86_64-linux] cf@debian:~$ cf@debian:~$ gem1.9.1 -v 1.3.7 cf@debian:~$
次に、VMCをRubyGemsからインストールします。Cloud Foundryはオープンソースで提供されており、VMCも自由に取得することができます。
root@debian:~# gem1.9.1 install vmc
:
Successfully installed spruz-0.2.13
Successfully installed json_pure-1.5.4
Successfully installed rubyzip2-2.0.2
Successfully installed mime-types-1.17.2
Successfully installed rest-client-1.6.7
Successfully installed terminal-table-1.4.4
Successfully installed interact-0.3
Successfully installed addressable-2.2.6
Successfully installed vmc-0.3.15
9 gems installed
:
root@debian:~#
利用しやすいようにエイリアスの設定を行います。
cf@debian:~$ alias vmc=/var/lib/gems/1.9.1/bin/vmc cf@debian:~$ cf@debian:~$ echo 'alias vmc=/var/lib/gems/1.9.1/bin/vmc' >> ~/.bash_aliases cf@debian:~$
cf@debian:~$ vmc version vmc 0.3.15 cf@debian:~$
これでVMCのインストールが完了しました。
VMCでCloud Foundryにログインする
パスワードを記載したメールが届き、Cloud Foundryのアカウントが有効になったら、VMCでログインします。
まず、vmc targetコマンドで、操作対象のCloud Foundry環境を決定します。
cf@debian:~$ vmc target api.cloudfoundry.com Successfully targeted to [http://api.cloudfoundry.com] cf@debian:~$
vmc targetコマンドを引数なしで実行することで、現在の操作対象のCloud Foundry環境を確認できます。
cf@debian:~$ vmc target [http://api.cloudfoundry.com] cf@debian:~$
vmc loginコマンドで、Cloud Foundry環境にログインします。EmailとPasswordはあなたのアカウントとパスワードを入力します。
cf@debian:~$ vmc login Attempting login to [http://api.cloudfoundry.com] Email:
あなたのアカウント
Password:
あなたのパスワード
Successfully logged into [http://api.cloudfoundry.com] cf@debian:~$
vmc infoコマンドで、Cloud Foundry環境の情報を取得できます。利用可能なメモリ、サービス数、アプリケーション数が確認できます。
cf@debian:~$ vmc info VMware's Cloud Application Platform For support visit http://support.cloudfoundry.com Target: http://api.cloudfoundry.com (v0.999) Client: v0.3.15 User:
あなたのアカウント
Usage: Memory (0B of 2.0G total)
Services (0 of 16 total)
Apps (0 of 20 total)
cf@debian:~$
vmc passwdコマンドで、パスワードの変更が行えます。
cf@debian:~$ vmc passwd Changing password for '
あなたのアカウント
' New Password:
新しいパスワード
Verify Password:
新しいパスワード
Successfully changed password cf@debian:~$
vmc helpコマンドで、VMCで扱えるコマンドの一覧が確認できます。
cf@debian:~$ vmc help
Usage: vmc [options] command [] [command_options]
Try 'vmc help [command]' or 'vmc help options' for more information.
Currently available vmc commands are:
Getting Started
target [url] Reports current target or sets a new target
login [email] [--email, --passwd] Login
info System and account information
Applications
apps List deployed applications
Application Creation
push [appname] Create, push, map, and start a new application
push [appname] --path Push application from specified path
push [appname] --url Set the url for the application
push [appname] --instances Set the expected number of instances
push [appname] --mem M Set the memory reservation for the application
push [appname] --runtime RUNTIME Set the runtime to use for the application
push [appname] --debug [MODE] Push application and start in a debug mode
push [appname] --no-start Do not auto-start the application
Application Operations
start [--debug [MODE]] Start the application
stop Stop the application
restart [--debug [MODE]] Restart the application
delete Delete the application
rename Rename the application
Application Updates
update [--path,--debug [MODE]] Update the application bits
mem [memsize] Update the memory reservation for an application
map Register the application to the url
unmap Unregister the application from the url
instances Scale the application instances up or down
Application Information
crashes List recent application crashes
crashlogs Display log information for crashed applications
logs [--all] Display log information for the application
files [path] [--all] Display directory listing or file download for [path]
stats Display resource usage for the application
instances List application instances
Application Environment
env List application environment variables
env-add Add an environment variable to an application
env-del Delete an environment variable to an application
Services
services Lists of services available and provisioned
create-service [--name,--bind] Create a provisioned service
create-service Create a provisioned service and assign it
create-service Create a provisioned service and assign it , and bind to
delete-service [servicename] Delete a provisioned service
bind-service Bind a service to an application
unbind-service Unbind service from the application
clone-services Clone service bindings from application to
tunnel [--port] Create a local tunnel to a service
tunnel Create a local tunnel to a service and start a local client
Administration
user Display user account information
passwd Change the password for the current user
logout Logs current user out of the target system
add-user [--email, --passwd] Register a new user (requires admin privileges)
delete-user Delete a user and all apps and services (requires admin privileges)
System
runtimes Display the supported runtimes of the target system
frameworks Display the recognized frameworks of the target system
Misc
aliases List aliases
alias Create an alias for a command
unalias Remove an alias
targets List known targets and associated authorization tokens
Help
help [command] Get general help or help on a specific command
help options Get help on available options
cf@debian:~$
サンプルアプリケーションをデプロイする
VMCをインストールし、Cloud Foundry環境にログインできるようになりました。これでCloud Foundryにアプリケーションをデプロイする準備が整いました。
はじめに、サンプルとして簡単なアプリケーションをデプロイしてみます。ここではRubyのウェブアプリケーションフレームワークSinatra Web frameworkを用います。
まず、クライアント環境にSinatraのテスト環境を作成します。SinatraをRubyGemsからインストールします。
root@debian:~# gem1.9.1 install sinatra
Successfully installed rack-1.4.0
Successfully installed rack-protection-1.2.0
Successfully installed tilt-1.3.3
Successfully installed sinatra-1.3.2
4 gems installed
:
root@debian:~#
「Hello from Cloud Foundry」と表示するだけの簡単なWebアプリケーションを作成します。
cf@debian:~$ mkdir -p sample-apps/hello cf@debian:~$ cd sample-apps/hello/ cf@debian:~/sample-apps/hello$ cat > hello.rb require 'sinatra' get '/' do "Hello from Cloud Foundry" end cf@debian:~/sample-apps/hello$
クライアント環境でこのWebアプリケーションを起動します。
cf@debian:~/sample-apps/hello$ ruby1.9.1 hello.rb
[2012-01-11 21:35:43] INFO WEBrick 1.3.1
[2012-01-11 21:35:43] INFO ruby 1.9.2 (2010-08-18) [x86_64-linux]
== Sinatra/1.3.2 has taken the stage on 4567 for development with backup from WEBrick
[2012-01-11 21:35:43] INFO WEBrick::HTTPServer#start: pid=1142 port=4567
:
別シェルからtelnetコマンドで接続します。
cf@debian:~$ telnet localhost 4567 Trying 127.0.0.1... Connected to localhost. Escape character is '^]'. GET / HTTP/1.0 HTTP/1.1 200 OK X-Frame-Options: sameorigin X-Xss-Protection: 1; mode=block Content-Type: text/html;charset=utf-8 Content-Length: 24 Server: WEBrick/1.3.1 (Ruby/1.9.2/2010-08-18) Date: Wed, 11 Jan 2012 12:36:38 GMT Connection: close Hello from Cloud FoundryConnection closed by foreign host. cf@debian:~$
以上のように、問題なく動作することが確認できました。
では、このWebアプリケーションをCloud Foundryにデプロイします。vmc pushコマンドを用いることで、対話的にデプロイを行えます。
まず、Webアプリケーションのあるディレクトリに移動し、vmc pushコマンドを実行します。
cf@debian:~/sample-apps/hello$ vmc push
現在のディレクトリからデプロイを行うかの確認です。
yと入力するか、そのままEnterを入力します。
Would you like to deploy from the current directory? [Yn]:
y
アプリケーション名を入力します。ここではhelloとします。
Application Name:
hello
デプロイURLを入力します。ここではhello-creationline.cloudfoundry.comとします。デプロイURLは唯一のものでなくてはいけません。他で設定済のデプロイURLの場合、エラーとなります。
Application Deployed URL [hello.cloudfoundry.com]:
hello-creationline.cloudfoundry.com
WebアプリケーションがSinatraかどうかの確認です。正しければyと入力するか、そのままEnterを入力します。
Detected a Sinatra Application, is this correct? [Yn]:
y
割り当てるメモリ量を入力します。ここではデフォルトの128Mを割り当てることとし、そのままEnterを入力します。
Memory Reservation (64M, 128M, 256M, 512M, 1G, 2G) [128M]:
Creating Application: OK
デプロイするWebアプリケーションを何かサービスと結びつけるかの確認です。サービスは何も利用していないのでnと入力します。
Would you like to bind any services to 'hello'? [yN]:
n
Cloud Foundry環境にWebアプリケーションがアップロードされ、起動されます。
Uploading Application: Checking for available resources: OK Packing application: OK Uploading (0K): OK Push Status: OK Staging Application: OK Starting Application: OK cf@debian:~/sample-apps/hello$
vmc appsコマンドで、デプロイしたアプリケーションの状況が確認できます。動作中のアプリケーションはHealthがRUNNINGとなります。
cf@debian:~/sample-apps/hello$ vmc apps +-------------+----+---------+-------------------------------------+----------+ | Application | # | Health | URLS | Services | +-------------+----+---------+-------------------------------------+----------+ | hello | 1 | RUNNING | hello-creationline.cloudfoundry.com | | +-------------+----+---------+-------------------------------------+----------+ cf@debian:~/sample-apps/hello$
Webブラウザで http://hello-creationline.cloudfoundry.com/ にアクセスします。以下の表示が得られれば、デプロイしたWebアプリケーションは正常に動作しています。
Hello from Cloud Foundry
