---
title: Cloud Native Buildpacks Tutorial - 8.3. ┗ Cloud Native BuildpacksでビルドしたOCIイメージをCloud Runへデプロイ
tags: ["Cloud Native Buildpacks Tutorial", "Cloud Native Buildpacks", "Spring Boot", "Cloud Run", "Series"]
categories: ["Dev", "Infrastructure", "CloudNativeBuildpacks", "CloudRun"]
date: 2020-04-28T17:36:05Z
updated: 2020-05-11T09:05:59Z
---

Cloud Native BuildpacksでビルドしたOCIイメージ([`making/hello-cnb`](https://hub.docker.com/r/making/hello-cnb))を[Cloud Run](https://cloud.google.com/run)にデプロイします。

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

### Google CloudのService Account作成

```
export PROJECT_ID=****
export SERVICE_ACCOUNT=cnb-user
export GCR_HOSTNAME=asia.gcr.io
gcloud auth login
gcloud iam service-accounts create ${SERVICE_ACCOUNT}
gcloud projects add-iam-policy-binding ${PROJECT_ID} --member "serviceAccount:${SERVICE_ACCOUNT}@${PROJECT_ID}.iam.gserviceaccount.com" --role "roles/storage.admin"
gcloud iam service-accounts keys create ${SERVICE_ACCOUNT}.json --iam-account ${SERVICE_ACCOUNT}@${PROJECT_ID}.iam.gserviceaccount.com
gcloud components install beta
```

### イメージをGoogle Cloud Registryにrelocate

Cloud RunにデプロイするにはDocker ImageがGCR上にある必要があるため、Docker HubのイメージをGCRにrelocateする。

```
cat ${SERVICE_ACCOUNT}.json  | docker login -u _json_key --password-stdin https://${GCR_HOSTNAME}
docker pull making/hello-cnb
docker tag making/hello-cnb:latest ${GCR_HOSTNAME}/${PROJECT_ID}/hello-cnb:latest
docker push ${GCR_HOSTNAME}/${PROJECT_ID}/hello-cnb:latest
```

> あるいは`pack build`の段階でGCRにpushした方が早い
>
> ```
> pack build ${GCR_HOSTNAME}/${PROJECT_ID}/hello-cnb \
>   --no-pull \
>   --builder gcr.io/paketo-buildpacks/builder:base \
>   --publish
> ```

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

次のコマンドでアプリケーションをデプロイします。

```yaml
gcloud run deploy hello-cnb \
  --image ${GCR_HOSTNAME}/${PROJECT_ID}/hello-cnb:latest \
  --region asia-northeast1 \
  --platform managed \
  --memory=768Mi \
  --allow-unauthenticated \
  --set-env-vars=INFO_MESSAGE="Hello World\!" \
  --set-env-vars=MANAGEMENT_HEALTH_DISKSPACE_ENABLED=false
```

> Cloud RunではSpring Boot ActuatorのデフォルトヘルスチェックでDiskエラーになるため、`MANAGEMENT_HEALTH_DISKSPACE_ENABLED`を`false`にします。

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

```
Deploying container to Cloud Run service [hello-cnb] in project [****] region [asia-northeast1]
Deploying new service...                                                                                                                       
  Setting IAM Policy...done                                                                                                                    
  Creating Revision...done                                                                                                                     
  Routing traffic...done                                                                                                                       
Done.                                                                                                                                          
Service [hello-cnb] revision [hello-cnb-00001-qip] has been deployed and is serving 100 percent of traffic at https://hello-cnb-cotqcte6bq-an.a.run.app
```


[Google Cloud Console](https://console.cloud.google.com/run)でログを確認します。

![image](https://user-images.githubusercontent.com/106908/80517950-caf4fa00-89c0-11ea-8c33-33ddaf2f893f.png)

指定したコンテナのメモリに合わせて、JVMのメモリが自動で設定されていることに注目してください。

アプリにマッピングされたURL(この例では[https://hello-cnb-cotqcte6bq-an.a.run.app](https://hello-cnb-cotqcte6bq-an.a.run.app)))にアクセスしてください。

```
$ curl https://hello-cnb-cotqcte6bq-an.a.run.app/actuator/health -w '\n'
{"status":"UP"}

$ curl https://hello-cnb-cotqcte6bq-an.a.run.app/actuator/info -w '\n'
{"message":"Hello World!"}
```

### メモリの調整

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

次のコマンドでアプリケーションをアップデートします。

```yaml
gcloud run deploy hello-cnb \
  --image ${GCR_HOSTNAME}/${PROJECT_ID}/hello-cnb:latest \
  --region asia-northeast1 \
  --platform managed \
  --memory=256Mi \
  --allow-unauthenticated \
  --set-env-vars=JAVA_OPTS="-XX:ReservedCodeCacheSize=32M -Xss512k" \
  --set-env-vars=BPL_JVM_THREAD_COUNT=20 \
  --set-env-vars=BPL_JVM_HEAD_ROOM=5 \
  --set-env-vars=INFO_MESSAGE="Hello World\!" \
  --set-env-vars=MANAGEMENT_HEALTH_DISKSPACE_ENABLED=false
```

[Google Cloud Console](https://console.cloud.google.com/run)でメモリの設定が次のように出力されていることを確認してください。

![image](https://user-images.githubusercontent.com/106908/80518194-20c9a200-89c1-11ea-9d8b-8fc2fc488a06.png)

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

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

```
gcloud run services delete hello-cnb --quiet --platform=managed --region asia-northeast1 
```

Cloud Runはリクエストが来なければインスタンスが0になるので、削除せずにそのままにしておいても良いかもしれません。
