Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nota-ja/849054b75cc5fa539b1e to your computer and use it in GitHub Desktop.
Save nota-ja/849054b75cc5fa539b1e to your computer and use it in GitHub Desktop.
cf blue-green-deploy プラグインを試してみる

cf blue-green-deploy プラグインを試してみる

この投稿は Open PaaS Advent Calendar 2015 の11日目の記事です。

先日, making@github さんが書かれた Java EE アドベントカレンダー 2015 の記事 Cloud FoundryにSpring Boot/Java EEアプリケーションをデプロイしよう を読む機会がありました。その中で Blue/Greenデプロイメント について書かれていたのを見て,そういえばこれを簡単にやってくれる cf コマンドのプラグインがあったことを思い出し,試してみることにしました。

cf プラグインについて

Cloud Foundry の CLI である cf コマンドは, プラグインによって機能拡張できる仕組み を持っています。各プラグインは新しいサブコマンドを占有し,さまざまな機能を cf CLI に追加することができます。今回紹介する blue-green-deploy プラグイン以外では,例えば,

  • kibana-me-logs
    kibana を立ち上げて,アプリのログを表示する

  • CLI-Recorder
    cf CLI の操作を記録&再生する

  • Open
    アプリの URL をブラウザーで開く

などがあります。

blue-green-deploy プラグインのインストール

プラグインをインストールできるのは,cf CLI v6.7.0 以降 なので,まずバージョンを確認します。

$ cf -v
cf version 6.11.2-2a26d55-2015-04-27T21:11:49+00:00

blue-green-deploy プラグインが存在するプラグイン・リポジトリーを追加します。

$ cf add-plugin-repo cf-repo http://plugins.cloudfoundry.org/

OK
http://plugins.cloudfoundry.org/list added as 'cf-repo'

ちなみにこのプラグイン・リポジトリーに含まれているプラグインの一覧は以下の通りです。

$ cf repo-plugins
Getting plugins from all repositories ...

Repository: cf-repo
name                      version   description
manifest-generator        1.0.0     Help you to generate a manifest from 0 (CLI v6.7.0+)
Firehose Plugin           0.7.0     This plugin allows you to connect to the firehose (CF admins only)
Diego-Enabler             1.0.1     Enable/Disable Diego support for an app (CLI v6.13.0+)
kibana-me-logs            0.4.3     Launches the Kibana UI (from kibana-me-logs) for an application.
Cloud Deployment Plugin   v1.0.1    This plugin allows you to manage application deployment details and manifests in a github repo (like spring cloud config server for deployments)
CLI-Recorder              1.0.2     Records and playbacks CLI commands.
Buildpack Management      1.0.0     Provides a declarative way to configure system buildpacks in a Cloud Foundry installation
Targets                   1.1.0     Easily manage multiple CF targets
blue-green-deploy         1.0.4     Zero downtime deploys with smoke test support
Usage Report              1.1.0     Report on memory usage and application instances for orgs and spaces
Statistics                1.0.0     Display live metrics/statistics about an app
wildcard_plugin           1.0.0     Allows user to search and delete apps using wildcard(*)
Scaleover                 1.0.1     Roll traffic from one app to another by scaling over time
CF App Stack Changer      1.1.0     Allows admins to list and update applications with outdated lucid64 stacks.
cf-download               1.0.0     Plugin for downloading your application contents after staging.
Copy Env                  1.0.0     Expose remote VCAP_SERVICES on the client environment
Buildpack Usage           1.0.0     View all buildpacks used in the current CLI target context.
Test User                 0.0.1     Create a user and assign all possible permissions, organisation and space are created if they do not already exist as well. If no organisation or space name are specified then the default value of 'development' is used
Brooklyn                  0.1.1     Interact with Service Broker for Apache Brooklyn
autopilot                 0.0.1     zero downtime deploy plugin for cf applications
Open                      1.1.0     Open app url in browser
Console                   1.0.0     Start a tmate session on an application container
Live Stats                1.0.0     Monitor CPU and Memory usage on an app via the browser.

blue-green-deploy プラグインをインストールします。

$ cf install-plugin blue-green-deploy -r cf-repo
Looking up 'blue-green-deploy' from repository 'cf-repo'
10093904 bytes downloaded...
Installing plugin /var/folders/5q/1zf_9ch97yj5ybh5rnd06fz40000gq/T/ filename=blue-green-deploy.osx...
OK
Plugin blue-green-deploy successfully installed.

blue-green-deploy サブコマンドのヘルプは以下の通りです。略称として bgd が使えるようです。

$ cf help blue-green-deploy
NAME:
   blue-green-deploy - Zero-downtime deploys with smoke tests

ALIAS:
   bgd

USAGE:
   blue-green-deploy APP_NAME [--smoke-test TEST_SCRIPT]

OPTIONS:
   --smoke-test The test script to run.

サンプル・アプリのデプロイ

blue-green-deploy を試すサンプル・アプリをデプロイします。今回はこれ

を使います。

$ git clone https://bitbucket.org/nota-ja/cf-example-ruby-ver.git
..
$ cd cf-example-ruby-ver
$ cf push rubyver
..
     state     since                    cpu    memory          disk      details
#0   running   2015-12-11 07:29:36 PM   0.0%   44.5M of 256M   0 of 1G

このアプリは,実行環境の Ruby のバージョンを返すものです。

$ curl rubyver.10.244.0.34.xip.io
Ruby 2.2.2-p95

1秒毎にアプリにアクセスするスクリプトを作成

以下のようなスクリプトを作りました。

$ cat loop.sh
#!/bin/bash

while :
do
    date +%Y-%m-%dT%H:%M:%S%Z
    curl rubyver.10.244.0.34.xip.io
    sleep 1
done

アプリ起動中に実行すると,こんな感じの出力になります。

$ ./loop.sh
2015-12-11T19:37:32JST
Ruby 2.2.2-p95
2015-12-11T19:37:33JST
Ruby 2.2.2-p95
2015-12-11T19:37:34JST
Ruby 2.2.2-p95
2015-12-11T19:37:35JST
Ruby 2.2.2-p95
2015-12-11T19:37:36JST
Ruby 2.2.2-p95
..

従来通りの方法でアプリを更新

まず,前項のスクリプトを走らせつつ,単純にアプリを更新すると,以下のようになります。

cf CLI の出力

(更新開始時刻: 2015-12-11T22:42:05)

$ cf push rubyver
Updating app rubyver in org nota / space hundred as nota...
OK
..
state     since                    cpu    memory        disk      details
#0   running   2015-12-11 10:42:23 PM   0.0%   44M of 256M   0 of 1G

(更新終了時刻: 2015-12-11T22:42:26)

loop.sh の出力

$ ./loop.sh
2015-12-11T22:41:59JST
Ruby 2.2.2-p95
2015-12-11T22:42:00JST
Ruby 2.2.2-p95
2015-12-11T22:42:01JST
Ruby 2.2.2-p95
2015-12-11T22:42:02JST
Ruby 2.2.2-p95
2015-12-11T22:42:03JST
Ruby 2.2.2-p95
2015-12-11T22:42:04JST
Ruby 2.2.2-p95
2015-12-11T22:42:05JST
Ruby 2.2.2-p95
2015-12-11T22:42:07JST
Ruby 2.2.2-p95
2015-12-11T22:42:08JST
Ruby 2.2.2-p95
2015-12-11T22:42:09JST
Ruby 2.2.2-p95
2015-12-11T22:42:10JST
Ruby 2.2.2-p95
2015-12-11T22:42:11JST
404 Not Found: Requested route ('rubyver.10.244.0.34.xip.io') does not exist.
2015-12-11T22:42:12JST
404 Not Found: Requested route ('rubyver.10.244.0.34.xip.io') does not exist.
2015-12-11T22:42:13JST
404 Not Found: Requested route ('rubyver.10.244.0.34.xip.io') does not exist.
2015-12-11T22:42:14JST
404 Not Found: Requested route ('rubyver.10.244.0.34.xip.io') does not exist.
2015-12-11T22:42:15JST
404 Not Found: Requested route ('rubyver.10.244.0.34.xip.io') does not exist.
2015-12-11T22:42:16JST
404 Not Found: Requested route ('rubyver.10.244.0.34.xip.io') does not exist.
2015-12-11T22:42:17JST
404 Not Found: Requested route ('rubyver.10.244.0.34.xip.io') does not exist.
2015-12-11T22:42:18JST
404 Not Found: Requested route ('rubyver.10.244.0.34.xip.io') does not exist.
2015-12-11T22:42:19JST
404 Not Found: Requested route ('rubyver.10.244.0.34.xip.io') does not exist.
2015-12-11T22:42:20JST
404 Not Found: Requested route ('rubyver.10.244.0.34.xip.io') does not exist.
2015-12-11T22:42:21JST
404 Not Found: Requested route ('rubyver.10.244.0.34.xip.io') does not exist.
2015-12-11T22:42:22JST
404 Not Found: Requested route ('rubyver.10.244.0.34.xip.io') does not exist.
2015-12-11T22:42:23JST
Ruby 2.2.2-p95
2015-12-11T22:42:24JST
Ruby 2.2.2-p95
2015-12-11T22:42:26JST
Ruby 2.2.2-p95
2015-12-11T22:42:27JST
Ruby 2.2.2-p95
2015-12-11T22:42:28JST
Ruby 2.2.2-p95
2015-12-11T22:42:29JST
Ruby 2.2.2-p95
^C

途中10秒ほどアプリへのアクセスが404エラーになっています。

blue-green-deploy プラグインを使った更新

今度は blue-green-deploy プラグインを使ってアプリを更新してみます。

cf CLI の出力

(更新開始時刻: 2015-12-11T22:56:27)

$ cf bgd rubyver
Deleting app rubyver-old in org nota / space hundred as nota...
OK
Creating app rubyver-new in org nota / space hundred as nota...
OK

Using route rubyver-new.10.244.0.34.xip.io
Binding rubyver-new.10.244.0.34.xip.io to rubyver-new...
OK

Uploading rubyver-new...
Uploading app files from: /Users/nota/repos/My/cf-example-ruby-ver
Uploading 1.5K, 5 files
Done uploading
OK

Starting app rubyver-new in org nota / space hundred as nota...
-----> Downloaded app package (4.0K)
-------> Buildpack version 1.6.2
-----> Compiling Ruby/Rack
-----> Using Ruby version: ruby-2.2.2
-----> Installing dependencies using 1.9.7
       Running: bundle install --without development:test --path vendor/bundle --binstubs vendor/bundle/bin -j4 --deployment
       Fetching gem metadata from https://rubygems.org/............
       Fetching version metadata from https://rubygems.org/..
       Installing tilt 1.4.1
       Using bundler 1.9.7
       Installing cf-runtime 0.2.0
       Installing rack 1.5.2
       Installing cf-autoconfig 0.2.1
       Installing rack-protection 1.5.0
       Installing sinatra 1.4.2
       Bundle complete! 2 Gemfile dependencies, 7 gems now installed.
       Gems in the groups development and test were not installed.
       Bundled gems are installed into ./vendor/bundle.
       Bundle completed (10.50s)
       Cleaning up the bundler cache.
###### WARNING:
       You have not declared a Ruby version in your Gemfile.
       To set your Ruby version add this line to your Gemfile:
       ruby '2.2.2'
       # See https://devcenter.heroku.com/articles/ruby-versions for more information.
###### WARNING:
       No Procfile detected, using the default web server (webrick)
       https://devcenter.heroku.com/articles/ruby-default-web-server

-----> Uploading droplet (17M)

1 of 1 instances running

App started


OK

App rubyver-new was started using this command `bundle exec rackup config.ru -p $PORT`

Showing health and status for app rubyver-new in org nota / space hundred as nota...
OK

requested state: started
instances: 1/1
usage: 256M x 1 instances
urls: rubyver-new.10.244.0.34.xip.io
package uploaded: Fri Dec 11 13:56:35 UTC 2015
stack: cflinuxfs2

     state     since                    cpu    memory          disk      details
#0   running   2015-12-11 10:56:58 PM   0.0%   44.1M of 256M   0 of 1G
Removing route rubyver-new.10.244.0.34.xip.io from app rubyver-new in org nota / space hundred as nota...
OK
Creating route rubyver.10.244.0.34.xip.io for org nota / space hundred as nota...
OK
Route rubyver.10.244.0.34.xip.io already exists
Adding route rubyver.10.244.0.34.xip.io to app rubyver-new in org nota / space hundred as nota...
OK
Renaming app rubyver to rubyver-old in org nota / space hundred as nota...
OK
Renaming app rubyver-new to rubyver in org nota / space hundred as nota...
OK
Removing route rubyver.10.244.0.34.xip.io from app rubyver-old in org nota / space hundred as nota...
OK

(更新終了時刻: 2015-12-11T22:57:02)

出力を見るとだいたいわかっていただけるかと思いますが,making@github さんの記事の Blue/Greenデプロイメント で行っている処理をほぼそのまま自動で実行しています。

loop.sh の出力

$ ./loop.sh
2015-12-11T22:56:22JST
Ruby 2.2.2-p95
2015-12-11T22:56:23JST
Ruby 2.2.2-p95
2015-12-11T22:56:24JST
Ruby 2.2.2-p95
2015-12-11T22:56:25JST
Ruby 2.2.2-p95
2015-12-11T22:56:27JST
Ruby 2.2.2-p95
2015-12-11T22:56:28JST
Ruby 2.2.2-p95
2015-12-11T22:56:29JST
Ruby 2.2.2-p95
2015-12-11T22:56:30JST
Ruby 2.2.2-p95
2015-12-11T22:56:31JST
Ruby 2.2.2-p95
2015-12-11T22:56:32JST
Ruby 2.2.2-p95
2015-12-11T22:56:33JST
Ruby 2.2.2-p95
2015-12-11T22:56:34JST
Ruby 2.2.2-p95
2015-12-11T22:56:35JST
Ruby 2.2.2-p95
2015-12-11T22:56:36JST
Ruby 2.2.2-p95
2015-12-11T22:56:37JST
Ruby 2.2.2-p95
2015-12-11T22:56:39JST
Ruby 2.2.2-p95
2015-12-11T22:56:40JST
Ruby 2.2.2-p95
2015-12-11T22:56:41JST
Ruby 2.2.2-p95
2015-12-11T22:56:42JST
Ruby 2.2.2-p95
2015-12-11T22:56:43JST
Ruby 2.2.2-p95
2015-12-11T22:56:44JST
Ruby 2.2.2-p95
2015-12-11T22:56:45JST
Ruby 2.2.2-p95
2015-12-11T22:56:46JST
Ruby 2.2.2-p95
2015-12-11T22:56:47JST
Ruby 2.2.2-p95
2015-12-11T22:56:49JST
Ruby 2.2.2-p95
2015-12-11T22:56:50JST
Ruby 2.2.2-p95
2015-12-11T22:56:51JST
Ruby 2.2.2-p95
2015-12-11T22:56:52JST
Ruby 2.2.2-p95
2015-12-11T22:56:53JST
Ruby 2.2.2-p95
2015-12-11T22:56:54JST
Ruby 2.2.2-p95
2015-12-11T22:56:55JST
Ruby 2.2.2-p95
2015-12-11T22:56:56JST
Ruby 2.2.2-p95
2015-12-11T22:56:57JST
Ruby 2.2.2-p95
2015-12-11T22:56:59JST
Ruby 2.2.2-p95
2015-12-11T22:57:00JST
Ruby 2.2.2-p95
2015-12-11T22:57:01JST
Ruby 2.2.2-p95
2015-12-11T22:57:02JST
Ruby 2.2.2-p95
2015-12-11T22:57:03JST
Ruby 2.2.2-p95
2015-12-11T22:57:04JST
Ruby 2.2.2-p95
2015-12-11T22:57:05JST
Ruby 2.2.2-p95
2015-12-11T22:57:06JST
Ruby 2.2.2-p95
2015-12-11T22:57:08JST
Ruby 2.2.2-p95
2015-12-11T22:57:09JST
Ruby 2.2.2-p95
2015-12-11T22:57:10JST
Ruby 2.2.2-p95
2015-12-11T22:57:11JST
Ruby 2.2.2-p95
2015-12-11T22:57:12JST
Ruby 2.2.2-p95
^C

コマンドの開始から終了までの時間は,元の21秒から35秒に延びましたが,更新実行中もアプリへのアクセスは一度もエラーになりませんでした。

まとめ

このプラグインを使うと,面倒な blue / green デプロイがコマンド一つでできるようになります。本当は --smoke-test オプションも試したかったのですが,記事も長くなり,時間も無くなってきたので,今日はここまでにしたいと思います。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment