Cloud Run に移行する Cloud Foundry アプリケーションごとに構成の移行プロセスを完了する必要があります。構成の移行では以下の処理が行われます。
- Cloud Foundry 
manifest.yamlを Cloud Runservice.yamlに変換する。 - Cloud Run にデプロイするためにバッキング サービスとアプリケーションを接続する。
 - Cloud Run サービスにアプリケーションをデプロイする。
 
manifest.yaml を service.yaml に変換する
Cloud Foundry マニフェストまたは cf CLI フラグを同等の Cloud Run サービス定義 YAML に変換する必要があります。
Cloud Run では、アプリケーションごとに独自のサービス YAML ファイルが必要です。Cloud Foundry マニフェスト内のアプリケーションをサービス YAML ファイルに移行するには:
アプリケーションについて、次の表に示すプロパティを収集します。アプリケーション レベルで変更されないプロパティは、グローバル Cloud Foundry プラットフォームの構成によってオーバーライドされる可能性があります。実際の値については、プラットフォーム管理者が提供するドキュメントをご覧ください。
アプリケーションのプロパティ cfCLI v6 フラグ説明 nameNAME引数Cloud Foundry 内のアプリケーションの一意の名前。 command-c/bin/shまたは/bin/bashで実行されるコマンド。disk_quota-kアプリケーションに割り当てられるディスクの量。 
有効な単位:
M、MB、G、r Bデフォルト(推定): 1G
docker.image--docker-image、-o実行するアプリケーションを含むイメージ。 health-check-http-endpointなし ヘルスチェック タイプが HTTP の場合に HTTP の状態を判断するために使用されるエンドポイント。 デフォルト:
/health-check-invocation-timeoutなし 個々のポートと HTTP ベースのヘルスチェックの間の時間(秒)。 デフォルト: 1
health-check-type--health-check-type、-uアプリケーションで実行するヘルスチェックのタイプ。有効な値: port、http、none、processデフォルト:
portinstances-iCloud Foundry が実行するアプリのインスタンスの数。 デフォルト: 1
memory-mアプリケーションのインスタンスごとのメモリ上限。 有効な単位:
M、MB、G、GBデフォルト(推定): 1G
timeout-tアプリの起動から最初のヘルスチェックで正常と判断されるまでの秒数。 デフォルト(推定): 60
Google Cloud プロジェクトと Cloud Run の設定について、次の情報を収集します。
プロパティ 説明 project_numberデプロイ先の Google Cloud プロジェクトのプロジェクト番号。 regionアプリのデプロイ先のリージョン。 vpc-access-connectorプラットフォーム管理者がアプリケーションを配置する VPC コネクタ名。 vpc-access-egressプラットフォーム管理者がアプリケーションを配置する VPC 下り(外向き)の名前。 custom-audiencesアプリケーションの認証が可能なカスタム オーディエンス。 serviceAccountNameGoogle Cloud でのアプリケーションの ID。 image前の手順で作成したアプリケーションのイメージ。 プロジェクトのルートにある
service.yamlファイルに次のテンプレートを挿入します。
apiVersion: serving.knative.dev/v1
kind: Service
metadata:
  # Set this to be the name of your app
  name: "APP_NAME"
  # Set this to be the project number of the project you're deploying to.
  namespace: "PROJECT_NUMBER"
  labels:
    # Set this to be the region you're deploying in.
    cloud.googleapis.com/location: REGION
    migrated-from: cloud-foundry
  annotations:
    run.googleapis.com/ingress: internal-and-cloud-load-balancing
spec:
  template:
    metadata:
      annotations:
        # Set to the greater of 1 or the `instances` attribute.
        autoscaling.knative.dev/minScale: '1'
        # Set to the greater of 1 or the `instances` attribute.
        autoscaling.knative.dev/maxScale: '1'
        run.googleapis.com/cpu-throttling: CPU_ALLOCATION
        run.googleapis.com/startup-cpu-boost: 'true'
        # Set to true if you rely on sticky sessions. These will be turned
        # on in Cloud Foundry if the server sends a JSESSIONID cookie back
        # on responses.
        run.googleapis.com/sessionAffinity: 'false'
        run.googleapis.com/execution-environment: gen2
        # Set the following values to match what your platform administrator recommends.
        run.googleapis.com/vpc-access-connector: ADMINISTRATOR_PROVIDED
        run.googleapis.com/vpc-access-egress: ADMINISTRATOR_PROVIDED
        run.googleapis.com/custom-audiences: ADMINISTRATOR_PROVIDED
    spec:
      # CF doesn't limit, but CR has a max of 1000.
      containerConcurrency: 1000
      # Default value for gorouter in PCF.
      timeoutSeconds: 900
      # Set the following value to match what your platform administrator recommends.
      serviceAccountName: ADMINISTRATOR_PROVIDED
      containers:
      - name: user-container
        # Set the following value to either:
        # - The image you built for your application in the last section of the guide.
        # - The docker.image attribute of your app's configuration if it's a Docker app.
        image: IMAGE
        # Set `command` based on the following rules:
        # - If your app has no `command` attribute: null.
        # - If your app has a docker.image attribute: ['/bin/sh', '-c']
        # - Otherwise: ['/bin/bash', '-c']
        command: null
        # Set `args` based on the following rules:
        # - If your app has no `command` attribute: null.
        # - If your app has a `command` attribute: ['value of command']
        args: null
        ports:
          # Set name based on the following rules:
          # - If your app is HTTP/2 or gRPC: "h2c"
          # - Else: "http1"
        - name: HTTP1_OR_H2C
          containerPort: 8080
        env:
          # For each key/value pair in your space's running environment variable groups,
          # which can be retried by running `cf running-environment-variable-group`,
          # add the following:
        - name: KEY
          value: VALUE
          # For each key/value pair in your manifest's `env` map, add the following:
        - name: KEY
          value: VALUE
          # Populate MEMORY_LIMIT with the amount of memory supplied to this instance
          # in MiB with 'M' as a suffix.
        - name: MEMORY_LIMIT
          value: '0M'
          # Set the following values in the JSON below:
          # - `application_name` and `name` to match metadata.name in this file.
          # - `application_uris` and `uris` to be the URI you want to assign the app on the
          #    load balancer.
          # - `limits.disk` to be the amount (in MiB) of disk assigned to your app.
          #   The amount will be in the `disk_quota` attribute of the CF manifest, or a
          #   default value for your cluster, typically 1GiB.
          # - `limits.mem` to be the amount (in MiB) of memory assigned to your app.
          #   The amount will be in your `memory` attribute of the CF manifest, or a
          #   default value for your cluster, typically 1GiB.
          # - `space_name` to be the value of metadata.space in this file.
        - name: VCAP_APPLICATION
          value: |-
                  {
                    "application_id": "00000000-0000-0000-0000-000000000000",
                    "application_name": "app-name",
                    "application_uris": [],
                    "limits": {
                      "disk": 1024,
                      "mem": 256
                    },
                    "name": "app-name",
                    "process_id": "00000000-0000-0000-0000-000000000000",
                    "process_type": "web",
                    "space_name": "none",
                    "uris": []
                  }
        resources:
          limits:
            # Set memory limit to be the sum of the memory and disk assigned to your app in CF.
            # 
            # Disk amount will be in the `disk_quota` attribute of the CF manifest, or a
            # default value for your cluster, typically 1GiB.
            #
            # Memory will be in your `memory` attribute of the CF manifest, or a
            # default value for your cluster, typically 1GiB.
            memory: MEMORY_LIMIT
            # Set cpu according to the following calculation:
            #
            # 1. Take the amount of memory in your `memory` attribute of the CF
            #    manifest, or a default value for your cluster, typically 1GiB.
            # 2. Divide that by the total amount of memory on the underlying BOSH VM.
            # 3. Multiply that by the total number of CPUs on the BOSH VM.
            # 4. Find the nearest valid value based on the rules in:
            #    https://cloud.google.com/run/docs/configuring/cpu#setting
            cpu: CPU_LIMIT
        # If `health-check-type` is "process" or "none", delete the startupProbe section.
        startupProbe:
          # If `health-check-type` is "port" or blank, delete the httpGet section.
          httpGet:
            # Set to be the value of `health-check-http-endpoint` or / if blank.
            path: CHECK_PATH
            port: 8080
          # If `health-check-type` is "http", delete the tcpSocket section.
          tcpSocket:
            port: 8080
          # Set to the value of `health-check-invocation-timeout` or 1
          timeoutSeconds: 1
          # Set failure threshold to be the following calculation:
          #
          # 1. Take the `timeout` from the CF manifest, use 60 if unset.
          # 2. Divide by 2.
          # 3. Round up to the nearest integer.
          failureThreshold: 1
          successThreshold: 1
          periodSeconds: 2
        # If `health-check-type` is "process" or "none", delete the livenessProbe section.
        livenessProbe:
          # If `health-check-type` is "port" or blank, delete the httpGet section.
          httpGet:
            # Set to be the value of `health-check-http-endpoint` or / if blank.
            path: CHECK_PATH
            port: 8080
          # If `health-check-type` is "http", delete the tcpSocket section.
          tcpSocket:
            port: 8080
          # Set to the value of `health-check-invocation-timeout` or 1.
          timeoutSeconds: 1
          failureThreshold: 1
          successThreshold: 1
          periodSeconds: 30
  traffic:
  - percent: 100
    latestRevision: true
バッキング サービスを接続する
Spring や Steeltoe などの Cloud Foundry アプリケーションによるサービス インジェクションと検出を可能にするため、VCAP_SERVICES 環境変数を作成する必要があります。この操作は、移行するアプリケーションごとに行う必要があります。詳細については、Cloud Foundry VCAP_SERVICES のドキュメントをご覧ください。
アプリケーションがすでに Cloud Foundry で実行されており、Cloud Run で同じサービスに接続する必要がある場合は、既存の環境変数を使用できます。それ以外の場合は、新しい VCAP_SERVICES を作成する必要があります。
VCAP_SERVICES 環境変数を構成するには:
既存の
VCAP_SERVICESの場合:cf env APP_NAMEを実行して、VCAP_SERVICES環境変数を取得します。- うまくいかない場合:
- Cloud Foundry でアプリケーションに接続します(
cf ssh APP_NAMEを実行)。 envコマンドを実行して、VCAP_SERVICESの出力を取得します。exitを実行して SSH セッションを終了します。
 - Cloud Foundry でアプリケーションに接続します(
 VCAP_SERVICES値をvcap.jsonという名前の新しいファイルに保存します。
サービスを追加する場合、または Cloud Foundry とは異なるサービスに接続する場合は、新しい
VCAP_SERVICESを作成します。- テキスト エディタで、空の JSON マップ(
{})を作成します。 - 追加するサービスごとに、次の操作を行います。
 - アプリがバインディングを検出する方法については、追加するタイプの 
VCAP_SERVICESを解析するためにアプリが使用するライブラリのドキュメントを参照してください。 - サービス プロバイダの名前がまだ存在しない場合は、
mysql、postgresql、elasticsearchなどの名前を使用してキーをマップに追加します。値を空の配列に設定します。 次のプロパティを持つオブジェクトを配列に追加します。
通常、サービスの検出やバインドに使用されないメタデータ:
binding_name。サービスでアプリケーションの権限を付与するリソースを表す文字列。これは、データベースのユーザー名、ファイアウォール ルール、サービス アカウント名などです。instance_name。バッキング サービスの名前を表す文字列。データベースの名前、ランダムな値、グローバル サービスの標識値などを指定できます。name。存在する場合はbinding_name、存在しない場合はinstance_name。通常、この値は重要ではありません。label。このバインディングがネストされているVCAP_SERVICESマップ内のキーの値。plan。サービスプランの名前。例: user-provided、high-availability など。
サービスの検出やバインドによく使用される値:
tags。ライブラリが互換性のあるサービスを見つける際に役立つタグのリスト。多くの場合、サービスの共通名(MySQL と MariaDB ではmysql、Redis や Cloud Memorystore の場合はredis、Postgres と互換性のあるデータベースの場合はpostgresなど)が含まれます。credentials。クライアント ライブラリが接続を実行するために使用する認証情報を含むオブジェクト。ほとんどのクライアント ライブラリは、サービスの標準 URI または JDBC 形式を含むuriフィールドに依存しています。
内容を
vcap.jsonとして保存します。
- テキスト エディタで、空の JSON マップ(
 
Cloud Run リソースに認証情報を適用する
認証情報を適用するには:
シークレットを作成して
VCAP_SERVICES環境変数の内容を保持し、次のコマンドで出力されたバージョンをメモします。gcloud secrets create APP_NAME-vcap \ --replication-policy="automatic" \ --data-file=vcap.jsonアプリケーションのサービス アカウントにシークレットの読み取り権限を付与します。
gcloud secrets add-iam-policy-binding APP_NAME-vcap \ --member="serviceaccount:app-service-account" \ --role="roles/secretmanager.secretAccessor"次の環境変数を
spec.template.spec.containers[0].env array内のアプリケーションservice.yamlに追加します。- name: VCAP_SERVICES valueFrom: secretKeyRef: key: Version output by step 1 name: APP_NAME-vcap
一般的なバッキング サービスのテンプレート
以降のセクションでは、よく使用されるバッキング サービスについて説明します。
MySQL
通常、MySQL ライブラリでは mysql タグが使用されます。credentials に次のキーを含めるのが一般的です。
uriテンプレート:mysql://username:password@host:port/dbname。URI 文字列の作成については、MySQL のドキュメントをご覧ください。通常、ポートは3306です。username: 接続のユーザー名。uriに含まれている場合でも、一部のライブラリでは必須です。password: 接続のパスワード。uriに含まれている場合でも、一部のライブラリでは必須です。
Redis
通常、Redis ライブラリでは redis タグが使用されます。credentials に次のキーを含めるのが一般的です。
uriテンプレート:redis://:password@host:port/dbunumber。
URI 文字列の作成については、IANA Redis URI のドキュメントをご覧ください。通常、ポートは 6379 です。
RabbitMQ
通常、RabbitMQ ライブラリでは rabbitmq タグと credentials の次のキーが使用されます。
uriテンプレート:amqp://username:password@host:port/vhost?query。
URI 文字列の作成については、RabbitMQ のドキュメントをご覧ください。通常、ポートは 5672 です。
ユーザー提供のサービス
ユーザー提供のサービスは Cloud Foundry の特別なタイプのサービスで、任意の認証情報を挿入できます。ラベルは常に user-provided です。タグは -t フラグを使用して cf create-user-provided-service に渡される値で、認証情報は -p フラグの内容です。
アプリケーションのデプロイ
完全に移行された Cloud Foundry アプリケーションを Cloud Run サービスにデプロイするには:
Cloud Run 環境を設定します(まだ設定していない場合)。
次のコマンドを実行します。
gcloud run services replace service.yamlデプロイが完了するまで少しお待ちください。正常に完了すると、コマンドラインにサービス URL が表示されます。
このサービス URL をウェブブラウザで開き、デプロイしたコンテナにアクセスします。
これで完了です。Cloud Foundry アプリケーションが Cloud Run に移行されました。