---
title: Cloud Native Buildpacks Tutorial - 8.1. ┗ Cloud Native BuildpacksでビルドしたOCIイメージをCloud Foundry(Pivotal Web Services)へデプロイ
tags: ["Cloud Native Buildpacks Tutorial", "Cloud Native Buildpacks", "Spring Boot", "Cloud Foundry", "Pivotal Web Services", "Series"]
categories: ["Dev", "Infrastructure", "CloudNativeBuildpacks", "CloudFoundry"]
date: 2020-04-28T16:34:57Z
updated: 2020-05-11T09:05:49Z
---

Cloud Native BuildpacksでビルドしたOCIイメージ([`making/hello-cnb`](https://hub.docker.com/r/making/hello-cnb))を[Cloud Foundry](https://www.cloudfoundry.org/) ([Pivotal Web Services](https://run.pivotal.io/))にデプロイします。

Pivotal Web Services以外のCloud Foundryでも同じようにデプロイできます。

**目次**
<!-- toc -->

### CF CLIのインストール

```
brew install cloudfoundry/tap/cf-cli
```

### Pivotal Web Servicesへログイン

```
cf login -a api.run.pivotal.io
```

Pivotal Web Services以外の場合は、次のコマンドでDockerイメージのデプロイがサポートされているか確認してください。

```
$ cf feature-flags | grep docker
diego_docker                                  enabled
```

`disabled`が出力される場合は、`admin`ユーザーとして次のコマンドを実行し、Dockerイメージのデプロイを有効にしてください。

```
cf enable-feature-flag diego_docker
```

### アプリケーションのデプロイ

次のコマンドで`manifest.yml`を作成します。

```yaml
mkdir deploy-to-cf
cd deploy-to-cf

cat <<'EOF' > manifest.yml
applications:
- name: hello-cnb
  memory: 768m
  docker:
    image: making/hello-cnb:latest
  env:
    INFO_MESSAGE: Hello World!
  health-check-type: http
  health-check-http-endpoint: /actuator/health
EOF
```

`cf push`コマンドでデプロイしてください。Routeが重複しないように`--random-route`オプションをつけます。

```
cf push --random-route
```

次のようなログが出力されます。

```
Pushing from manifest to org APJ / space development as tmaki@pivotal.io...
Using manifest file /tmp/pack/deploy-to-cf/manifest.yml
Getting app info...
Creating app with these attributes...
+ name:                         hello-cnb:latest
+ docker image:                 making/hello-cnb
+ health check http endpoint:   /actuator/health
+ health check type:            http
+ memory:                       768M
  env:
+   INFO_MESSAGE
  routes:
+   hello-cnb-lean-ostrich-nj.cfapps.io

Creating app hello-cnb...
Mapping routes...

Staging app and tracing logs...
   Cell a827573d-ee33-414e-bebd-3532a9afb61e creating container for instance f2c03eec-80f8-4fad-be56-5aa05449285a
   Cell a827573d-ee33-414e-bebd-3532a9afb61e successfully created container for instance f2c03eec-80f8-4fad-be56-5aa05449285a
   Staging...
   Staging process started ...
   Staging process finished
   Exit status 0
   Staging Complete
   Cell a827573d-ee33-414e-bebd-3532a9afb61e stopping instance f2c03eec-80f8-4fad-be56-5aa05449285a
   Cell a827573d-ee33-414e-bebd-3532a9afb61e destroying container for instance f2c03eec-80f8-4fad-be56-5aa05449285a
   Cell a827573d-ee33-414e-bebd-3532a9afb61e successfully destroyed container for instance f2c03eec-80f8-4fad-be56-5aa05449285a

Waiting for app to start...

name:              hello-cnb
requested state:   started
routes:            hello-cnb-lean-ostrich-nj.cfapps.io
last uploaded:     Wed 29 Apr 01:05:13 JST 2020
stack:             
docker image:      making/hello-cnb:latest

type:            web
instances:       1/1
memory usage:    768M
start command:   /cnb/lifecycle/launcher 
     state     since                  cpu     memory           disk           details
#0   running   2020-04-28T16:05:52Z   77.8%   119.9M of 768M   249.4M of 1G  
```

`cf logs`でログを確認します。

```
$ cf logs hello-cnb --recent
Retrieving logs for app hello-cnb in org APJ / space development as tmaki@pivotal.io...

   2020-04-29T01:05:08.97+0900 [API/11] OUT Created app with guid 980ea444-39d4-460d-b314-c816a64fc78b
   2020-04-29T01:05:09.88+0900 [API/12] OUT Creating build for app with guid 980ea444-39d4-460d-b314-c816a64fc78b
   2020-04-29T01:05:09.95+0900 [API/12] OUT Updated app with guid 980ea444-39d4-460d-b314-c816a64fc78b ({"state"=>"STARTED"})
   2020-04-29T01:05:10.52+0900 [STG/0] OUT Cell a827573d-ee33-414e-bebd-3532a9afb61e creating container for instance f2c03eec-80f8-4fad-be56-5aa05449285a
   2020-04-29T01:05:11.69+0900 [STG/0] OUT Cell a827573d-ee33-414e-bebd-3532a9afb61e successfully created container for instance f2c03eec-80f8-4fad-be56-5aa05449285a
   2020-04-29T01:05:12.04+0900 [STG/0] OUT Staging...
   2020-04-29T01:05:12.62+0900 [STG/0] OUT Staging process started ...
   2020-04-29T01:05:12.92+0900 [STG/0] OUT Staging process finished
   2020-04-29T01:05:12.92+0900 [STG/0] OUT Exit status 0
   2020-04-29T01:05:12.92+0900 [STG/0] OUT Staging Complete
   2020-04-29T01:05:13.41+0900 [STG/0] OUT Cell a827573d-ee33-414e-bebd-3532a9afb61e stopping instance f2c03eec-80f8-4fad-be56-5aa05449285a
   2020-04-29T01:05:13.41+0900 [STG/0] OUT Cell a827573d-ee33-414e-bebd-3532a9afb61e destroying container for instance f2c03eec-80f8-4fad-be56-5aa05449285a
   2020-04-29T01:05:13.47+0900 [API/7] OUT Creating droplet for app with guid 980ea444-39d4-460d-b314-c816a64fc78b
   2020-04-29T01:05:13.99+0900 [CELL/0] OUT Cell 427f4537-d113-4050-8f1f-eb1875759ae8 creating container for instance ddffd3a2-4721-4ac7-5adc-e701
   2020-04-29T01:05:14.09+0900 [STG/0] OUT Cell a827573d-ee33-414e-bebd-3532a9afb61e successfully destroyed container for instance f2c03eec-80f8-4fad-be56-5aa05449285a
   2020-04-29T01:05:25.18+0900 [CELL/0] OUT Cell 427f4537-d113-4050-8f1f-eb1875759ae8 successfully created container for instance ddffd3a2-4721-4ac7-5adc-e701
   2020-04-29T01:05:25.55+0900 [CELL/0] OUT Starting health monitoring of container
   2020-04-29T01:05:27.62+0900 [APP/PROC/WEB/0] OUT JVM DNS caching disabled in favor of link-local DNS caching
   2020-04-29T01:05:27.74+0900 [APP/PROC/WEB/0] OUT Calculated JVM Memory Configuration: -XX:MaxDirectMemorySize=10M -XX:MaxMetaspaceSize=88833K -XX:ReservedCodeCacheSize=240M -Xss1M -Xmx218366K (Head Room: 0%, Loaded Class Count: 13270, Thread Count: 250, Total Memory: 838860800)
   2020-04-29T01:05:35.73+0900 [APP/PROC/WEB/0] OUT   .   ____          _            __ _ _
   2020-04-29T01:05:35.73+0900 [APP/PROC/WEB/0] OUT  /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
   2020-04-29T01:05:35.73+0900 [APP/PROC/WEB/0] OUT ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
   2020-04-29T01:05:35.73+0900 [APP/PROC/WEB/0] OUT  \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
   2020-04-29T01:05:35.73+0900 [APP/PROC/WEB/0] OUT   '  |____| .__|_| |_|_| |_\__, | / / / /
   2020-04-29T01:05:35.73+0900 [APP/PROC/WEB/0] OUT  =========|_|==============|___/=/_/_/_/
   2020-04-29T01:05:35.74+0900 [APP/PROC/WEB/0] OUT  :: Spring Boot ::        (v2.2.6.RELEASE)
   2020-04-29T01:05:36.17+0900 [APP/PROC/WEB/0] OUT 2020-04-28 16:05:36.165  INFO 12 --- [           main] hello.HelloCnbApplication                : Starting HelloCnbApplication on ddffd3a2-4721-4ac7-5adc-e701 with PID 12 (/workspace/BOOT-INF/classes started by cnb in /workspace)
   2020-04-29T01:05:36.17+0900 [APP/PROC/WEB/0] OUT 2020-04-28 16:05:36.178  INFO 12 --- [           main] hello.HelloCnbApplication                : No active profile set, falling back to default profiles: default
   2020-04-29T01:05:44.96+0900 [APP/PROC/WEB/0] OUT 2020-04-28 16:05:44.939  INFO 12 --- [           main] o.s.b.a.e.web.EndpointLinksResolver      : Exposing 2 endpoint(s) beneath base path '/actuator'
   2020-04-29T01:05:48.77+0900 [APP/PROC/WEB/0] OUT 2020-04-28 16:05:48.769  INFO 12 --- [           main] o.s.b.web.embedded.netty.NettyWebServer  : Netty started on port(s): 8080
   2020-04-29T01:05:48.80+0900 [APP/PROC/WEB/0] OUT 2020-04-28 16:05:48.790  INFO 12 --- [           main] hello.HelloCnbApplication                : Started HelloCnbApplication in 16.825 seconds (JVM running for 20.864)
   2020-04-29T01:05:51.12+0900 [CELL/0] OUT Container became healthy
```

指定したコンテナのメモリに合わせて、JVMのメモリが自動で設定されていることに注目してください。CF Buildpackを使った場合と同じです。

アプリにマッピングされたURL(この例では[https://hello-cnb-lean-ostrich-nj.cfapps.io](https://hello-cnb-lean-ostrich-nj.cfapps.io)))にアクセスしてください。

```
$ curl https://hello-cnb-lean-ostrich-nj.cfapps.io/actuator/health -w '\n'
{"status":"UP"}

$ curl https://hello-cnb-lean-ostrich-nj.cfapps.io/actuator/info -w '\n'
{"message":"Hello World!"}
```

### Apps Managerにアクセス

[Apps Manager](https://console.run.pivotal.io)にアクセスして、`hello-cnb`アプリを選択してください。

![image](https://user-images.githubusercontent.com/106908/80510817-35ed0380-89b6-11ea-9f2e-e408949687ac.png)

[Spring Boot Actuatorの管理機能](https://docs.run.pivotal.io/console/using-actuators.html)が利用できます。

### メモリの調整

Buildpackに含まれる[Java Buildpack Memory Calculator](https://github.com/cloudfoundry/java-buildpack-memory-calculator)の機能を使って、
自動設定されるJVMのメモリを調整し、少ないメモリ量(256MB)で起動できるようにします。

`manifest.yml`を次のように変更してください。

```
cat <<'EOF' > manifest.yml
applications:
- name: hello-cnb
  memory: 256m
  docker:
    image: making/hello-cnb:latest
  env:
    INFO_MESSAGE: Hello World!
    JAVA_OPTS: "-XX:ReservedCodeCacheSize=32M -Xss512k"
    BPL_JVM_THREAD_COUNT: 20
    BPL_JVM_HEAD_ROOM: 5
  health-check-type: http
  health-check-http-endpoint: /actuator/health
EOF
```

アップデートします。

```
cf push
```

`cf logs hello-cnb --recent`でメモリの設定が次のように出力されていることを確認してください。

```
   2020-04-29T01:26:22.83+0900 [APP/PROC/WEB/0] OUT Calculated JVM Memory Configuration: -XX:MaxDirectMemorySize=10M -XX:MaxMetaspaceSize=88833K -Xmx138084K (Head Room: 5%, Loaded Class Count: 13270, Thread Count: 20, Total Memory: 301989888)
```

Apps Managerを見ると、256MB中、約150MBが使用されていることがわかります。

![image](https://user-images.githubusercontent.com/106908/80513355-e4df0e80-89b9-11ea-8150-f1c118474b2d.png)

### アプリケーションの削除

次のコマンドでアプリとRouteを削除します。

```
cf delete hello-cnb -r -f
```
