世界のやまさ

SEKAI NO YAMASA

Cloud Foundry on Azure に Spring Boot をデプロイする

f:id:nnasaki:20151211212212p:plain

前回はセットアップを進めただけで、 cf コマンドまで動かせない状態でした。

blog.nnasaki.com

どうしようも無くわからなかったので、 Github でダメ元で聞いてみたら、親切丁寧にサポートしてもらえて cf login までうまく動かせるようになりました。どうやら、 DNS に BIND を使っているようで、 テンプレートファイルから IP を変えていたので DNS がうまく動いていなかったようです。

github.com

しかし、私の英語は本当に中学生以下ですが、やりたいことはなんとなく伝わって良かったです。

DNS の設定方法

次のドキュメントを参考にして、 setup_dns.py をダウンロード。

github.com

以下コマンドをdevboxで実行。IPはパブリックIPにより変わります。

sudo python setup_dns.py -d cf.azurelovecf.com -i 10.0.0.5 -e 40.74.xxx.xxx -n 40.74.xxx.xxx

cf login コマンドの実行

devbox で以下コマンドを実行

cf login -a https://api.cf.azurelovecf.com --skip-ssl-validation

ユーザー名パスワードを聞かれるので、デフォルトは次の通り

Email>  admin
Password> c1oudc0w

次の通り出力されればログイン成功

Authenticating...
OK

Targeted org default_organization


                   
API endpoint:   https://api.cf.azurelovecf.com (API version: 2.42.0)   
User:           admin   
Org:            default_organization   
Space:          No space targeted, use 'cf target -s SPACE'   

Spring Boot アプリをデプロイする

次の記事を参考に、Spring Boot のアプリケーションをデプロイします。Pivotal Cloud Foundry 向けの説明ですが、cf コマンドは共通なので、Open な Cloud Foundry でも同じように動きます。

qiita.com

Space を作る

アプリを置く場所に Space というものが必要みたいなので、次のコマンドを実行して作ります。すべてdevbox上で作業します。

$ cf create-space sandbox
Creating space sandbox in org default_organization as admin...
OK
Assigning role SpaceManager to user admin in org default_organization / space sandbox as admin...
OK
Assigning role SpaceDeveloper to user admin in org default_organization / space sandbox as admin...
OK

TIP: Use 'cf target -o "default_organization" -s "sandbox"' to target new space

成功したら、TIP のとおり cf target で作成した space を使うようにする

$ cf target -o "default_organization" -s "sandbox"
                   
API endpoint:   https://api.cf.azurelovecf.com (API version: 2.42.0)   
User:           admin   
Org:            default_organization   
Space:          sandbox   

JDK をセットアップする

JDKが入っていないので、次の記事を参考に Oracle JDK をセットアップする。 qiita.com

ソースをクローンしてビルドする

$ git clone https://github.com/making/hello-pws
$ cd hello-pws
$ ./mvnw package

パッケージをPUSHする

次のコマンドで Cloud Foundry にパッケージをデプロイします。 Starting app hello-pws in org default_organization / space sandbox as admin... のところでやたら待たされて不安になりましたが、待っていたら進みました。

$ cf push hello-pws -p target/hello-pws.jar -m 256M

Creating app hello-pws in org default_organization / space sandbox as admin...
OK

Creating route hello-pws.cf.azurelovecf.com...
OK

Binding hello-pws.cf.azurelovecf.com to hello-pws...
OK

Uploading hello-pws...
Uploading app files from: target/hello-pws.jar
Uploading 13.2M, 109 files
Done uploading               
OK

Starting app hello-pws in org default_organization / space sandbox as admin...
-----> Downloaded app package (12M)
-----> Java Buildpack Version: v3.3.1 | https://github.com/cloudfoundry/java-buildpack.git#063836b
-----> Downloading Open Jdk JRE 1.8.0_65 from https://download.run.pivotal.io/openjdk/trusty/x86_64/openjdk-1.8.0_65.tar.gz (3.8s)
       Expanding Open Jdk JRE to .java-buildpack/open_jdk_jre (1.8s)
-----> Downloading Open JDK Like Memory Calculator 2.0.1_RELEASE from https://download.run.pivotal.io/memory-calculator/trusty/x86_64/memory-calculator-2.0.1_RELEASE.tar.gz (0.1s)
       Memory Settings: -Xss853K -Xmx160M -XX:MetaspaceSize=64M -Xms160M -XX:MaxMetaspaceSize=64M
-----> Downloading Spring Auto Reconfiguration 1.10.0_RELEASE from https://download.run.pivotal.io/auto-reconfiguration/auto-reconfiguration-1.10.0_RELEASE.jar (0.5s)

-----> Uploading droplet (57M)

0 of 1 instances running, 1 starting
0 of 1 instances running, 1 starting
0 of 1 instances running, 1 starting
0 of 1 instances running, 1 starting
1 of 1 instances running

App started


OK

App hello-pws was started using this command `CALCULATED_MEMORY=$($PWD/.java-buildpack/open_jdk_jre/bin/java-buildpack-memory-calculator-2.0.1_RELEASE -memorySizes=metaspace:64m.. -memoryWeights=heap:75,metaspace:10,native:10,stack:5 -memoryInitials=heap:100%,metaspace:100% -totMemory=$MEMORY_LIMIT) && SERVER_PORT=$PORT $PWD/.java-buildpack/open_jdk_jre/bin/java -cp $PWD/.:$PWD/.java-buildpack/spring_auto_reconfiguration/spring_auto_reconfiguration-1.10.0_RELEASE.jar -Djava.io.tmpdir=$TMPDIR -XX:OnOutOfMemoryError=$PWD/.java-buildpack/open_jdk_jre/bin/killjava.sh $CALCULATED_MEMORY org.springframework.boot.loader.JarLauncher`

Showing health and status for app hello-pws in org default_organization / space sandbox as admin...
OK

requested state: started
instances: 1/1
usage: 256M x 1 instances
urls: hello-pws.cf.azurelovecf.com
last uploaded: Mon Dec 21 11:49:44 UTC 2015
stack: cflinuxfs2
buildpack: java-buildpack=v3.3.1-https://github.com/cloudfoundry/java-buildpack.git#063836b java-main open-jdk-like-jre=1.8.0_65 open-jdk-like-memory-calculator=2.0.1_RELEASE spring-auto-reconfiguration=1.10.0_RELEASE

     state     since                    cpu    memory           disk         details   
#0   running   2015-12-21 11:54:38 AM   0.0%   222.3M of 256M   135M of 1G      

curl で動作を確認する

curl で動作を確認します。レスポンスがあったときは嬉しくて小さくガッツポーズしてしました。

$ curl http://hello-pws.cf.azurelovecf.com
Hello from 10.0.0.5:61001

まとめ

なんとか Cloud Foundry にアプリケーションをデプロイするところまでたどり着けました。スケールさせたりとか、DBを作ったりとかはまだわからないので、引き続き調べます。

参考