基本的な本番環境クラスタ用にネットワークを構成する


このチュートリアルは、ウェブ アプリケーションを Google Kubernetes Engine(GKE)クラスタにデプロイし、HTTPS ロードバランサで公開することに関心があるクラウド アーキテクトと運用管理者を対象としています。

目標

このチュートリアルの学習内容は次のとおりです。

  • GKE クラスタを作成する。
  • Terraform を使用してグローバル IP アドレスと Cloud DNS ゾーンを作成する。
  • HTTPS ロード バランシングを構成する。
  • サンプルのウェブ アプリケーションをデプロイする。

費用

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

料金計算ツールを使うと、予想使用量に基づいて費用の見積もりを生成できます。

新規の Google Cloud ユーザーは無料トライアルをご利用いただける場合があります。

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

始める前に

プロジェクトを設定する

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. In the Google Cloud console, on the project selector page, click Create project to begin creating a new Google Cloud project.

    Go to project selector

  3. Make sure that billing is enabled for your Google Cloud project.

  4. Enable the Google Kubernetes Engine, Cloud DNS APIs.

    Enable the APIs

  5. In the Google Cloud console, on the project selector page, click Create project to begin creating a new Google Cloud project.

    Go to project selector

  6. Make sure that billing is enabled for your Google Cloud project.

  7. Enable the Google Kubernetes Engine, Cloud DNS APIs.

    Enable the APIs

    • ドメイン名を所有している必要があります。ドメイン名は 63 文字以下にする必要があります。Google Domains または他の登録事業者を使用できます。

    環境の設定

    このチュートリアルでは、Cloud Shell を使用してGoogle Cloudでホストされているリソースを管理します。Cloud Shell には、このチュートリアルに必要な、Terraformkubectlgcloud CLI などのソフトウェアがプリインストールされています。

    1. 環境変数を設定します。

      PROJECT_ID=$(gcloud config get-value project)
      gcloud config set project $PROJECT_ID
      gcloud config set compute/region us-central1
      
    2. コード リポジトリのクローンを作成します。

      git clone https://github.com/GoogleCloudPlatform/kubernetes-engine-samples.git
      
    3. 作業ディレクトリを変更します。

      cd kubernetes-engine-samples/autopilot/networking-tutorial
      

    GKE クラスタを作成する

    次の Terraform ファイルによって GKE クラスタが作成されます。

    
    terraform {
      required_version = "~> 1.3"
    }
    
    provider "google" {}
    
    variable "region" {
      type        = string
      description = "Region where the cluster will be created."
      default     = "us-central1"
    }
    
    variable "cluster_name" {
      type        = string
      description = "Name of the cluster"
      default     = "networking-cluster"
    }
    
    resource "google_container_cluster" "default" {
      name             = var.cluster_name
      description      = "Cluster for sample web application"
      location         = var.region
      enable_autopilot = true
    
      ip_allocation_policy {}
    }
    
    output "region" {
      value       = var.region
      description = "Compute region"
    }
    
    output "cluster_name" {
      value       = google_container_cluster.default.name
      description = "Cluster name"
    }
    

    次の Terraform ファイルによってグローバル IP アドレスと Cloud DNS ゾーンが作成されます。

    
    terraform {
      required_version = "~> 1.3"
    }
    
    variable "base_domain" {
      type        = string
      description = "Your base domain"
    }
    
    variable "name" {
      type        = string
      description = "Name of resources"
      default     = "networking-tutorial"
    }
    
    data "google_client_config" "current" {}
    
    resource "google_compute_global_address" "default" {
      name = var.name
    }
    
    resource "google_dns_managed_zone" "default" {
      name        = var.name
      dns_name    = "${var.name}.${var.base_domain}."
      description = "DNS Zone for web application"
    }
    
    resource "google_dns_record_set" "a" {
      name         = google_dns_managed_zone.default.dns_name
      type         = "A"
      ttl          = 300
      managed_zone = google_dns_managed_zone.default.name
    
      rrdatas = [google_compute_global_address.default.address]
    }
    
    resource "google_dns_record_set" "cname" {
      name         = join(".", compact(["www", google_dns_record_set.a.name]))
      type         = "CNAME"
      ttl          = 300
      managed_zone = google_dns_managed_zone.default.name
    
      rrdatas = [google_dns_record_set.a.name]
    }
    
    output "dns_zone_name_servers" {
      value       = google_dns_managed_zone.default.name_servers
      description = "Write these virtual name servers in your base domain."
    }
    
    output "domain" {
      value = trim(google_dns_record_set.a.name, ".")
    }
    
    1. Terraform を初期化します。

      terraform init
      
    2. インフラストラクチャの変更を表示します。

      terraform plan
      

      プロンプトが表示されたら、ドメインを入力します(my-domain.net など)。

    3. Terraform 構成を適用します。

      terraform apply --auto-approve
      

      プロンプトが表示されたら、ドメインを入力します(my-domain.net など)。

      出力は次のようになります。

      Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
      
      Outputs:
      
      cluster_name = "networking-cluster"
      region = "us-central1"
      

    外部アプリケーション ロードバランサを作成する

    1. 次のマニフェストには、ManagedCertificate、FrontendConfig、Deployment、Service、Ingress が記述されています。

      ---
      apiVersion: networking.gke.io/v1
      kind: ManagedCertificate
      metadata:
        name: networking-managed-cert
      spec:
        domains:
          - DOMAIN_NAME
          - www.DOMAIN_NAME
      ---
      apiVersion: networking.gke.io/v1beta1
      kind: FrontendConfig
      metadata:
        name: networking-fc
      spec:
        redirectToHttps:
          enabled: true
          responseCodeName: MOVED_PERMANENTLY_DEFAULT
      ---
      apiVersion: apps/v1
      kind: Deployment
      metadata:
        name: frontend
      spec:
        selector:
          matchLabels:
            app: frontend
        replicas: 2
        template:
          metadata:
            labels:
              app: frontend
          spec:
            containers:
            - name: echo-amd64
              image: us-docker.pkg.dev/google-samples/containers/gke/hello-app-cdn:1.0
      ---
      apiVersion: v1
      kind: Service
      metadata:
        name: frontend
      spec:
        type: LoadBalancer
        selector:
          app: frontend
        ports:
        - name: http
          port: 80
          targetPort: 8080
      ---
      apiVersion: networking.k8s.io/v1
      kind: Ingress
      metadata:
        name: frontend
        annotations:
          networking.gke.io/managed-certificates: networking-managed-cert
          networking.gke.io/v1beta1.FrontendConfig: networking-fc
          kubernetes.io/ingress.global-static-ip-name: networking-tutorial
          kubernetes.io/ingress.class: gce
        labels:
          app: frontend
      spec:
        defaultBackend:
          service:
            name: frontend
            port:
              number: 80

      DOMAIN_NAMEmy-domain.net などのドメイン名に置き換えます。

      このマニフェストには次のプロパティがあります。

      • networking.gke.io/managed-certificates: ManagedCertificate の名前。
      • networking.gke.io/v1beta1.FrontendConfig: FrontendConfig リソースの名前。
      • kubernetes.io/ingress.global-static-ip-name: IP アドレスの名前。
      • kubernetes.io/ingress.class: 外部アプリケーション ロードバランサを作成するように GKE Ingress コントローラに指示します。
    2. マニフェストをクラスタに適用します。

      kubectl apply -f kubernetes-manifests.yaml
      
    3. Ingress が作成されたことを確認します。

      kubectl describe ingress frontend
      

      出力は次のようになります。

      ...
        Events:
          Type    Reason  Age   From                     Message
          ----    ------  ----  ----                     -------
          Normal  ADD     2m    loadbalancer-controller  default/frontend
          Normal  CREATE  1m    loadbalancer-controller  ip: 203.0.113.2
      ...
      

      Ingress のプロビジョニングには数分かかる場合があります。

    アプリケーションをテストする

    1. SSL 証明書のステータスを確認します。

      kubectl get managedcertificates.networking.gke.io networking-managed-cert
      

      SSL 証明書のプロビジョニングには最長で 30 分ほどかかる場合があります。次の出力は、SSL 証明書の準備ができていることを示しています。

      NAME                      AGE   STATUS
      networking-managed-cert   28m   Active
      
    2. curl コマンドを実行します。

      curl -Lv https://DOMAIN_NAME
      

      出力は次のようになります。

      *   Trying 34.160.115.33:443...
      * Connected to DOMAIN_NAME (34.160.115.33) port 443 (#0)
      ...
      * TLSv1.3 (IN), TLS handshake, Certificate (11):
      ...
      * Server certificate:
      *  subject: CN=DOMAIN_NAME
      ...
      > Host: DOMAIN_NAME
      

    クリーンアップ

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

    プロジェクトを削除する

    1. In the Google Cloud console, go to the Manage resources page.

      Go to Manage resources

    2. In the project list, select the project that you want to delete, and then click Delete.
    3. In the dialog, type the project ID, and then click Shut down to delete the project.

    リソースを個別に削除する

    1. Kubernetes リソースを削除します。

      kubectl delete -f kubernetes-manifests.yaml
      
    2. Terraform リソースを削除します。

      terraform destroy --auto-approve
      

      プロンプトが表示されたら、ドメインを入力します(my-domain.net など)。

    次のステップ