静的 IP アドレスを使用したドメイン名の構成

コレクションでコンテンツを整理 必要に応じて、コンテンツの保存と分類を行います。

このチュートリアルでは、Google Kubernetes Engine(GKE)を使用して、静的外部 IP アドレスでウェブ アプリケーションをインターネットに公開し、そのアプリケーションを指すドメイン名を構成する方法を説明します。

このチュートリアルでは、登録済みドメイン名(example.com など)を所有していることを前提とします。ドメインをお持ちでない場合は、Google Domains または他のお好きなドメイン登録事業者を通じてドメイン名を登録できます。

目標

このチュートリアルでは次の手順について説明します。

  • アプリケーション用の静的外部 IP アドレスを予約する
  • 静的 IP を使用するように Service リソースまたは Ingress リソースを構成する
  • アプリケーションを指すようにドメイン名の DNS レコードを更新する

費用

このチュートリアルでは、課金対象である次の Google Cloud コンポーネントを使用します。

料金計算ツールを使うと、予想使用量に基づいて費用の見積もりを生成できます。 新しい Google Cloud ユーザーは無料トライアルをご利用いただける場合があります。

このチュートリアルを終了した後、作成したリソースを削除すると、それ以上の請求は発生しません。詳細については、クリーンアップをご覧ください。

始める前に

次の手順で Kubernetes Engine API を有効にします。
  1. Google Cloud Console で Kubernetes Engine ページにアクセスします。
  2. プロジェクトを作成または選択します。
  3. API と関連サービスが有効になるのを待ちます。 これには数分かかることがあります。
  4. Cloud プロジェクトに対して課金が有効になっていることを確認します。詳しくは、プロジェクトで課金が有効になっているかどうかを確認する方法をご覧ください。

このチュートリアルで使用されている以下のコマンドライン ツールをインストールします。

  • gcloud は、Kubernetes Engine クラスタの作成と削除に使用されます。gcloudgcloud CLI に含まれています。
  • kubectl は、Kubernetes Engine で使用されるクラスタ オーケストレーション システムである Kubernetes の管理に使用されます。gcloud を使用して kubectl をインストールできます。
    gcloud components install kubectl

GitHub からサンプルコードのクローンを作成します。

git clone https://github.com/GoogleCloudPlatform/kubernetes-engine-samples
cd kubernetes-engine-samples/hello-app/manifests

gcloud コマンドライン ツールのデフォルトの設定

次のようにしてデフォルト値を設定しておくと、gcloud コマンドライン ツールでプロジェクト ID および Compute Engine ゾーン オプションを入力する時間が節約されます。
gcloud config set project project-id
gcloud config set compute/zone compute-zone

クラスタの作成

domain-test という名前のコンテナ クラスタを作成して、ウェブ アプリケーションをデプロイします。

gcloud container clusters create domain-test

ウェブ アプリケーションのデプロイ

以下のマニフェストは、サンプルのウェブ アプリケーション コンテナ イメージを実行する Deployment を説明しています。


# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: helloweb
  labels:
    app: hello
spec:
  selector:
    matchLabels:
      app: hello
      tier: web
  template:
    metadata:
      labels:
        app: hello
        tier: web
    spec:
      containers:
      - name: hello-app
        image: us-docker.pkg.dev/google-samples/containers/gke/hello-app:1.0
        ports:
        - containerPort: 8080
        resources:
          requests:
            cpu: 200m
---

次のコマンドを実行して Deployment を作成します。

kubectl apply -f helloweb-deployment.yaml

アプリケーションの公開

次のいずれかの方法を使用して、アプリケーションを GKE に公開できます。

各メソッドの長所と短所については、Ingress による HTTP(S) 負荷分散の設定をご覧ください。

Service の使用

アプリケーションに静的パブリック IP アドレスが割り当てられていることを保証するには、静的 IP アドレスを予約する必要があります。

Service を使用してアプリケーションを公開する場合は、リージョン IP アドレスを作成する必要があります。次のセクションで説明するように、グローバル IP アドレスは Ingress リソースタイプでのみ機能します。

Service を使用するには、リージョン us-central1helloweb-ip という名前の静的 IP アドレスを作成します。

gcloud

gcloud compute addresses create helloweb-ip --region us-central1

作成した静的 IP アドレスを見つけるには、次のコマンドを実行します。

gcloud compute addresses describe helloweb-ip --region us-central1
出力:
address: 203.0.113.32
...

Config Connector

注: この手順には Config Connector が必要です。Config Connector をクラスタにインストールするには、インストール手順を実施してください。

apiVersion: compute.cnrm.cloud.google.com/v1beta1
kind: ComputeAddress
metadata:
  name: helloweb-ip
spec:
  location: us-central1
このマニフェストをデプロイするには、マニフェストを compute-address.yaml というファイル名でマシンにダウンロードしてから、次のコマンドを実行します。
kubectl apply -f compute-address-regional.yaml
静的 IP アドレスを確認するには、次のコマンドを実行します。
kubectl get computeaddress helloweb-ip -o jsonpath='{.spec.address}'

次のマニフェストでは、LoadBalancer タイプの Service について説明します。この Service は、パブリック IP を持つ Pod を公開するネットワーク ロードバランサを作成します。

YOUR.IP.ADDRESS.HERE を静的 IP アドレスで置き換えます。


# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

apiVersion: v1
kind: Service
metadata:
  name: helloweb
  labels:
    app: hello
spec:
  selector:
    app: hello
    tier: web
  ports:
  - port: 80
    targetPort: 8080
  type: LoadBalancer
  loadBalancerIP: "YOUR.IP.ADDRESS.HERE"
---

次に、Service を作成します。

kubectl apply -f helloweb-service-static-ip.yaml

ロードバランサに関連付けられている予約済み IP アドレスを確認するには、次のコマンドを実行します。

kubectl get service
出力:
NAME               CLUSTER-IP      EXTERNAL-IP      PORT(S)          AGE
helloweb           10.31.254.176   203.0.113.32     80:30690/TCP     54s

Ingress の使用

HTTP(S) ロードバランサを作成する Ingress を使ってアプリケーションを公開する場合は、グローバル静的 IP アドレスを予約する必要があります。リージョン IP アドレスは Ingress では機能しません。

Ingress を使用してアプリケーションをインターネットに公開する方法については、Ingress による HTTP(S) 負荷分散の設定のチュートリアルをご覧ください。

helloweb-ip という名前のグローバル静的 IP アドレスを作成するには、次のコマンドを実行します。

gcloud

gcloud compute addresses create helloweb-ip --global

作成した静的 IP アドレスを見つけるには:

gcloud compute addresses describe helloweb-ip --global
出力:
address: 203.0.113.32
...

Config Connector

注: この手順には Config Connector が必要です。Config Connector をクラスタにインストールするには、インストール手順を実施してください。

apiVersion: compute.cnrm.cloud.google.com/v1beta1
kind: ComputeAddress
metadata:
  name: helloweb-ip
spec:
  location: global
このマニフェストをデプロイするには、マニフェストを compute-address-global.yaml というファイル名でマシンにダウンロードしてから、次のコマンドを実行します。
kubectl apply -f compute-address-global.yaml

以下のマニフェストに記述されている Ingress は、次の 2 つのリソースを持つ静的 IP でウェブ アプリケーションを公開します。

  • type:NodePort を使用した Service
  • サービス名と静的 IP アノテーションで構成された Ingress

# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: helloweb
  annotations:
    kubernetes.io/ingress.global-static-ip-name: helloweb-ip
  labels:
    app: hello
spec:
  defaultBackend:
    service:
      name: helloweb-backend
      port:
        number: 8080
---
apiVersion: v1
kind: Service
metadata:
  name: helloweb-backend
  labels:
    app: hello
spec:
  type: NodePort
  selector:
    app: hello
    tier: web
  ports:
  - port: 8080
    targetPort: 8080
---

kubernetes.io/ingress.global-static-ip-name アノテーションは、HTTP(S) ロードバランサに関連付けるグローバル IP アドレス リソースの名前を指定します。

リソースをクラスタに適用します。

kubectl apply -f helloweb-ingress-static-ip.yaml
出力:
ingress "helloweb" created
service "helloweb-backend" created

ロードバランサに関連付けられた予約済みの IP アドレスを確認するには、次のようにします。

kubectl get ingress
出力:
NAME       HOSTS     ADDRESS          PORTS     AGE
helloweb   *         203.0.113.32     80        4m

予約済みの静的 IP アドレスへのアクセス

ロードバランサが正しく構成されていることを確認するには、ウェブブラウザを使用して IP アドレスにアクセスするか、次のように curl を使用します。

curl http://203.0.113.32/
出力:
Hello, world!
Hostname: helloweb-3766687455-8lvqv

ドメイン名レコードの構成

ドメイン名(example.com など)またはサブドメイン名(blog.example.com など)を問い合わせるブラウザが、予約した静的 IP アドレスを参照するようにするには、ドメイン名の DNS(ドメイン ネームサーバー)レコードを更新する必要があります。

ドメイン名またはサブドメイン名の A(アドレス)タイプ DNS レコードを作成し、予約済みの IP アドレスでその値を構成する必要があります。

ドメインの DNS レコードはネームサーバーによって管理されます。ネームサーバーは、ドメインを登録した「登録事業者」、Cloud DNS などの DNS サービス、または別のサードパーティ プロバイダの場合があります。

  • ネームサーバーが Cloud DNS の場合: Cloud DNS クイックスタート ガイドに沿って、アプリケーションの予約済み IP アドレスを使用してドメイン名の DNS A レコードを構成します。

  • ネームサーバーが他のプロバイダの場合: DNS A レコードの設定に関する DNS サービスのドキュメントを参照して、ドメイン名を構成します。Google Cloud DNS を使用する場合は、Cloud DNS に移行するをご覧ください。

ドメイン名へのアクセス

ドメイン名の DNS A レコードが予約した IP アドレスに解決されることを確認するには、そのドメイン名にアクセスします。

ドメイン名の A レコードに対して DNS クエリを発行するには、次の host コマンドを実行します。

host example.com
出力:
example.com has address 203.0.113.32

これで、ウェブブラウザにドメイン名を指定して、ウェブサイトにアクセスできます。

クリーンアップ

このチュートリアルで使用したリソースについて、Google Cloud アカウントに課金されないようにするには、リソースを含むプロジェクトを削除するか、プロジェクトを維持して個々のリソースを削除します。

  1. 負荷分散リソースを削除します。

    kubectl delete ingress,service -l app=hello

  2. 予約済みの静的 IP を解放します。ロードバランサを削除すると、未使用だが予約済みである IP アドレスが無料ではなくなり、未使用の IP アドレスの料金に従って課金されます。静的 IP リソースを解放するには、次のコマンドを実行します。

    • Service を使用する場合:

      gcloud compute addresses delete helloweb-ip --region us-central1
    • Ingress を使用する場合:

      gcloud compute addresses delete helloweb-ip --global
  3. サンプル アプリケーションを削除します。

    kubectl delete -f helloweb-deployment.yaml

  4. 次のコマンドの出力を監視して、ロードバランサが削除されるまで待ちます。出力された名前に、"helloweb" を含む転送ルールが表示されなくなるはずです。

    gcloud compute forwarding-rules list

  5. コンテナ クラスタを削除します。

    gcloud container clusters delete domain-test

次のステップ