LoginSignup
7
7

More than 5 years have passed since last update.

VagrantでローカルにCloud Foundry v2を構築(2)

Posted at

さて、挫折しかけたCloud FoundryのシングルVM版をついに起動させることができました(こちらの記事をご参照ください)。今回は簡単なアプリをデプロイして動かしてみたいと思います。

SSHログインの設定

Vagrantの機能を使ってSSHログインできるようにしておきます。

$ cd path/to/cf-vagrant-installer
$ vagrant ssh-config -host cf-host >> ~/.ssh/config
$ ssh cf-host
Welcome to Ubuntu 12.04.1 LTS (GNU/Linux 3.2.0-29-virtual x86_64)

 * Documentation:  https://help.ubuntu.com/

98 packages can be updated.
41 updates are security updates.

Last login: Thu Jan 31 13:48:53 2013
vagrant@cf:~$

Cloud Foundryコンポーネントの初期化

$ cd /vagrant
$ rake cf:init_f_cli

サンプルアプリを動かしてみる

このリポジトリに入っているSinatraのサンプルアプリをPushしてみましょう。Cloud Foundry上にアプリをデプロイして動かすことを"Push"といいます。
cfというのはCloud FoundryのCLI (Command Line Interface)です。
v1 APIの時から名前が変わりました(v1のときはvmc)。

$ cd test/fixtures/apps/sinatra
$ cf push
Using manifest file manifest.yml

Creating hello... OK

Creating route hello.192.168.12.34.xip.io... OK
Binding hello.192.168.12.34.xip.io to hello... OK
Uploading hello... OK
Preparing to start hello... OK
Checking status of app 'hello'...
  0 of 1 instances running (1 starting)
  0 of 1 instances running (1 starting)
  1 of 1 instances running (1 running)
Push successful! App 'hello' available at hello.192.168.12.34.xip.io

ホストのブラウザからhttp://hello.192.168.12.34.xip.io/にアクセスしてみることで、動作が確認できます。Cloud Foundry環境変数の出力一覧になっているみたいです。ちなみに環境変数の内容の説明は公式ドキュメントを参照ください。

アプリインスタンスに対するコマンドライン操作

Getting Startedに記載されている代表的なアプリインスタンス操作コマンドの出力を確認してみます。

起動中のアプリインスタンスの確認

$ cf apps
Getting applications in myspace... OK

name    status    usage      url                       
hello   running   1 x 256M   hello.192.168.12.34.xip.io

特定のアプリインスタンスのヘルスチェック

$ cf health hello
Using manifest file manifest.yml

Getting health status... OK

hello: running

特定のアプリインスタンスのメモリ使用状況を表示

$ cf stats hello
Using manifest file manifest.yml

Getting stats for hello... OK

instance   cpu    memory          disk       
#0         0.0%   20.7M of 256M   48.5M of 1G

特定のアプリインスタンスの環境変数とログを表示

Using manifest file manifest.yml

Getting logs for hello #0... OK

Reading logs/env.log... OK
TMPDIR=/home/vcap/tmp
VCAP_APP_PORT=61003
VCAP_CONSOLE_IP=0.0.0.0
USER=vcap
VCAP_APPLICATION={"application_users":[],"instance_id":"a67ad5c602427860688260c15b0bd3dc","instance_index":0,"application_version":"339ab2e8-062b-4242-885c-416fe696876c","application_name":"hello","application_uris":["hello.192.168.12.34.xip.io"],"started_at":"2013-11-01 07:00:02 -0700","started_at_timestamp":1383314402,"host":"0.0.0.0","port":61003,"limits":{"mem":256,"disk":1024,"fds":16384},"version":"339ab2e8-062b-4242-885c-416fe696876c","name":"hello","uris":["hello.192.168.12.34.xip.io"],"users":[],"start":"2013-11-01 07:00:02 -0700","state_timestamp":1383314402}
RACK_ENV=production
PATH=/home/vcap/app/bin:/home/vcap/app/vendor/bundle/ruby/1.9.1/bin:/bin:/usr/bin:/bin:/usr/bin
PWD=/home/vcap
LANG=en_US.UTF-8
VCAP_SERVICES={}
HOME=/home/vcap/app
SHLVL=2
GEM_PATH=/home/vcap/app/vendor/bundle/ruby/1.9.1:
PORT=61003
VCAP_APP_HOST=0.0.0.0
MEMORY_LIMIT=256m
VCAP_CONSOLE_PORT=61004
_=/usr/bin/env



Reading logs/staging_task.log... OK
-----> Downloaded app package (4.0K)
Installing ruby.
-----> Using Ruby version: ruby-1.9.2
-----> Installing dependencies using Bundler version 1.3.2
       Running: bundle install --without development:test --path vendor/bundle --binstubs vendor/bundle/bin --deployment
       Fetching gem metadata from http://rubygems.org/..........
       Fetching gem metadata from http://rubygems.org/..
       Installing rack (1.5.1)
       Installing rack-protection (1.3.2)
       Installing tilt (1.3.3)
       Installing sinatra (1.3.4)
       Using bundler (1.3.2)
       Your bundle is complete! It was installed into ./vendor/bundle
       Cleaning up the bundler cache.



Reading logs/stderr.log... OK
[2013-11-01 14:00:04] INFO  WEBrick 1.3.1
[2013-11-01 14:00:04] INFO  ruby 1.9.2 (2012-04-20) [x86_64-linux]
[2013-11-01 14:00:04] INFO  WEBrick::HTTPServer#start: pid=25 port=61003
192.168.12.1 - - [01/Nov/2013 14:01:19] "GET / HTTP/1.1" 200 1172 0.0018
192.168.12.1 - - [01/Nov/2013 14:01:19] "GET /favicon.ico HTTP/1.1" 404 18 0.0004



Reading logs/stdout.log... OK

特定のアプリインスタンスを止める

$ cf stop hello
Using manifest file manifest.yml

Stopping hello... OK
$ cf apps
Getting applications in myspace... OK

name    status    usage      url                       
hello   stopped   1 x 256M   hello.192.168.12.34.xip.io

アプリを削除する

$ cf delete hello
Using manifest file manifest.yml

Really delete hello?> y

Deleting hello... OK
$ cf apps
Getting applications in myspace... OK

No applications.

その他

このアプリでは出力を確認できませんが他にもあります。

  • ログをtailして出力 cf tail appname
  • クラッシュログを表示 cf crashlogs appname

もちろん他にもあります。詳細はcfコマンドのドキュメントに記載されています。

アプリ設定ファイルを見てみる

このサンプルアプリの設定ファイルを見てみましょう。デフォルトではアプリのルートディレクトリ直下にmanifest.ymlという名前で置かれています。

manifest.yml
---
applications:
- name: hello
  memory: 256M
  instances: 1
  host: hello
  domain: 192.168.12.34.xip.io
  path: .

各項目の内容は以下です(見りゃ分かるよ、というツッコミが聞こえてきそうですが)。

key keyの説明 value
name アプリ名 hello
memory 使用可能上限メモリ 256M
instances インスタンス数 1
host ホスト名 hello
domain ドメイン名 192.168.12.34.xip.io
path パス .

アプリのURLはhello.192.168.12.34.xip.ioになります。

manifest.ymlをリネームなり削除して再度cf pushをすると、Cloud Foundryは対話的にパラメータを聞いてきます。
指定したパラメータは、最後にmanifest.ymlとして保存することができます。以下のように指定すれば元々あったものと全く同じmanifest.ymlができます。

$ mv manifest.yml manifest.yml.backup
$ cf push
Name> hello

Instances> 1

1: 128M
2: 256M
3: 512M
4: 1G
Memory Limit> 2   

Creating hello... OK

1: hello
2: none
Subdomain> 1    

1: 192.168.12.34.xip.io
2: none
Domain> 1                   

Binding hello.192.168.12.34.xip.io to hello... OK

Create services for application?> n

Save configuration?> y

Saving to manifest.yml... OK
Uploading hello... OK
Preparing to start hello... OK
Checking status of app 'hello'...
  0 of 1 instances running (1 starting)
  0 of 1 instances running (1 starting)
  1 of 1 instances running (1 running)
Push successful! App 'hello' available at hello.192.168.12.34.xip.io

今日はここまで。
VM動かすまでが予想外に大変でしたので、普通に動いてくれてよかったです。
次はサービスのバインドに挑戦してみたいと思います。

7
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
7