2020/07 時点での Serverless NEG のハンズオン資料です。

複数の記事に跨って作成しており、4番目の記事になります。


構成

この記事の内容は長いため複数の記事に跨って作成しています。

適宜、興味のある記事を参照して下さい。

事前準備

gcloud auth login -q
### New Setting
export _pj_id='Your GCP Project ID'
export _common='check-serverless-neg'
gcloud config set project ${_pj_id}
cd [Your WorkSpace]
git clone https://github.com/iganari/package-gcp.git
cd package-gcp/compute/networkendpointgroups/serverless

やること

External HTTP(S) Load Balancer の作成

各種の Serverless NEG を作成する

gcloud beta compute network-endpoint-groups create ${_common}-serverless-neg-run \
    --region=asia-northeast1 \
    --network-endpoint-type=SERVERLESS  \
    --cloud-run-service=${_common}-run
gcloud beta compute network-endpoint-groups create ${_common}-serverless-neg-app \
    --region=asia-northeast1 \
    --network-endpoint-type=SERVERLESS  \
    --app-engine-service=${_common}-app
gcloud beta compute network-endpoint-groups create ${_common}-serverless-neg-func \
    --region=asia-northeast1 \
    --network-endpoint-type=SERVERLESS  \
    --cloud-function-name=func
gcloud beta compute network-endpoint-groups list
### 例

# gcloud beta compute network-endpoint-groups list
NAME                                     LOCATION         ENDPOINT_TYPE  SIZE
check-serverless-neg-serverless-neg-app   asia-northeast1  SERVERLESS     0
check-serverless-neg-serverless-neg-func  asia-northeast1  SERVERLESS     0
check-serverless-neg-serverless-neg-run   asia-northeast1  SERVERLESS     0

各種の Backend Service を作成する

gcloud compute backend-services create ${_common}-backend-service-run \
    --global
gcloud compute backend-services create ${_common}-backend-service-app \
    --global
gcloud compute backend-services create ${_common}-backend-service-func \
    --global
gcloud compute backend-services list
### 例

# gcloud compute backend-services list
NAME                                      BACKENDS  PROTOCOL
check-serverless-neg-backend-service-app            HTTP
check-serverless-neg-backend-service-func           HTTP
check-serverless-neg-backend-service-run            HTTP

Backend Service に Cloud Run 用の Serverless NEG を設定する

gcloud beta compute backend-services add-backend ${_common}-backend-service-run \
    --global \
    --network-endpoint-group=${_common}-serverless-neg-run \
    --network-endpoint-group-region=asia-northeast1
gcloud beta compute backend-services add-backend ${_common}-backend-service-app \
    --global \
    --network-endpoint-group=${_common}-serverless-neg-app \
    --network-endpoint-group-region=asia-northeast1
gcloud beta compute backend-services add-backend ${_common}-backend-service-func \
    --global \
    --network-endpoint-group=${_common}-serverless-neg-func \
    --network-endpoint-group-region=asia-northeast1
gcloud compute backend-services list
### Ex.

# gcloud compute backend-services list
NAME                                       BACKENDS                                                                        PROTOCOL
check-serverless-neg-backend-service-app   asia-northeast1/networkEndpointGroups/check-serverless-neg-serverless-neg-app   HTTP
check-serverless-neg-backend-service-func  asia-northeast1/networkEndpointGroups/check-serverless-neg-serverless-neg-func  HTTP
check-serverless-neg-backend-service-run   asia-northeast1/networkEndpointGroups/check-serverless-neg-serverless-neg-run   HTTP

URL map の作成

gcloud compute url-maps create ${_common}-url-map \
    --default-service ${_common}-backend-service-run
gcloud compute url-maps add-path-matcher ${_common}-url-map \
    --path-matcher-name=${_common}-path-matcher \
    --path-rules "/app=check-serverless-neg-backend-service-app,/func=check-serverless-neg-backend-service-func" \
    --default-service=check-serverless-neg-backend-service-run
gcloud compute url-maps list
### Ex.

# gcloud compute url-maps list
NAME                          DEFAULT_SERVICE
check-serverless-neg-url-map  backendServices/check-serverless-neg-backend-service-run

マネージド SSL を設定する

export _my_domain=$(echo ${_common}.hejda.org)
gcloud compute ssl-certificates create ${_common}-www-ssl-cert \
    --domains ${_my_domain}
gcloud compute ssl-certificates list
### Ex.

# gcloud compute ssl-certificates list
NAME                               TYPE     CREATION_TIMESTAMP             EXPIRE_TIME  MANAGED_STATUS
check-serverless-neg-www-ssl-cert  MANAGED  2020-07-26T00:35:54.246-07:00               PROVISIONING
    check-serverless-neg.hejda.org: PROVISIONING

Load Balancer で使用する Forwarding Rule の作成

gcloud compute forwarding-rules create ${_common}-https-content-rule \
    --address=${_common}-example-ip \
    --target-https-proxy=${_common}-https-proxy \
    --global \
    --ports=443
gcloud compute forwarding-rules list
### 例

# gcloud compute forwarding-rules list
NAME                                     REGION  IP_ADDRESS      IP_PROTOCOL  TARGET
check-serverless-neg-https-content-rule          34.107.216.140  TCP          check-serverless-neg-https-proxy

—> これで、目的の Serverless NEG を使った External HTTP(S) Load Balancer の作成が完了しました!!

Web ブラウザで確認

ここまで作ってきたリソースを Web ブラウザで確認していきます。

Check the resources with a Web browser.

まとめ

これで Serverless NEG を使った External HTTP(S) Load Balancer の作成が出来ました!!

最後に リソースの削除 をします。

Have fun! :)

comments powered by Disqus