ネットワークで隔離されたネットワークを構成すること


Google KubernetesエンジンとクラウドSQLの間の接続性を確保するために実装できるセキュリティパターンを紹介しました.この部分では、以下のGCPリソースを展開してネットワーク分離を実装します.
2サブネットをもつ
  • VPC
  • GKEのウェブサブネット.
  • 1データサブネット.
  • 雲のNATは、Webサブネットに接続されています.
  • ファイアウォール規則は、認可されたネットワークだけへのサブネットへのアクセスを制限します.
  • It is recommended to group similar applications into fewer, more manageable and larger subnets.



    If you have multiples GKE clusters per environment, Google Cloud recommends to use Shared VPC to reduce management and topology complexity.


    VPC

    Virtual Private Cloudから始めましょう.
    地形ファイルinfra/plan/vpc.tfを作成します
  • 単純なVPCリソース
  • ウェブサブネット.それは、我々のGoogle Kubernetesエンジン
  • を主催します
  • データサブネット.それはあなたのクラウドデータフロージョブ、クラウドコンポーザー環境などをホストすることができました.
  • resource "google_compute_network" "custom" {
      name                    = "custom"
      auto_create_subnetworks = "false" 
      routing_mode            = "GLOBAL"
    }
    
    resource "google_compute_subnetwork" "web" {
      name          = "web"
      ip_cidr_range = "10.10.10.0/24"
      network       = google_compute_network.custom.id
      region        = var.region
    
      secondary_ip_range  = [
        {
            range_name    = "services"
            ip_cidr_range = "10.10.11.0/24"
        },
        {
            range_name    = "pods"
            ip_cidr_range = "10.1.0.0/20"
        }
      ]
    
      private_ip_google_access = true
    }
    
    resource "google_compute_subnetwork" "data" {
      name          = "data"
      ip_cidr_range = "10.20.10.0/24"
      network       = google_compute_network.custom.id
      region        = var.region
    
      private_ip_google_access = true
    }
    

    クラウドナット


    GKEによってインターネットにアクセスするのに我々のウェブサブネットを許すために、我々は雲ナットをつくる必要があります.クラウド・ナットをクラウド・ルータを使ってサブネットと関連づける.
    地形ファイルを作成
    resource "google_compute_address" "web" {
      name    = "web"
      region  = var.region
    }
    
    resource "google_compute_router" "web" {
      name    = "web"
      network = google_compute_network.custom.id
    }
    
    resource "google_compute_router_nat" "web" {
      name                               = "web"
      router                             = google_compute_router.web.name
      nat_ip_allocate_option             = "MANUAL_ONLY"
      nat_ips                            = [ google_compute_address.web.self_link ]
      source_subnetwork_ip_ranges_to_nat = "LIST_OF_SUBNETWORKS" 
      subnetwork {
        name                    = google_compute_subnetwork.web.id
        source_ip_ranges_to_nat = ["ALL_IP_RANGES"]
      }
      depends_on                         = [ google_compute_address.web ]
    }
    

    ファイアウォール


    ファイアウォールを使用すると、VMインスタンス、ネットワークタグまたはサービスアカウントへの、およびインバウンドとアウトバウンドのネットワークトラフィックを制限できます.この場合、以下の規則を実装できます.
  • 規則のGSQLノードへのアクセスを制限する規則.
  • 規則的なネットワークだけへのネットワークアクセスを制限する規則.
  • 地形ファイルを作成
    resource "google_compute_firewall" "mysql" {
      name    = "allow-only-gke-cluster"
      network = google_compute_network.custom.name
    
      allow {
        protocol = "tcp"
        ports    = ["3306"]
      }
    
      priority = 1000
    
      source_ranges = ["10.10.10.0/24"]
    }
    
    resource "google_compute_firewall" "web" {
      name    = "allow-only-authorized-networks"
      network = google_compute_network.custom.name
    
      allow {
        protocol = "tcp"
      }
    
      priority = 1000
    
      source_ranges = var.authorized_source_ranges
    }
    
    地形を整えましょう.
    地形ファイルを作成
    variable "region" {
      type = string
      default = "europe-west1"
    }
    
    variable "authorized_source_ranges" {
      type        = list(string)
      description = "Addresses or CIDR blocks which are allowed to connect to GKE API Server."
    }
    
    ファイルを追加する
    terraform {
      required_providers {
        google = {
          source = "hashicorp/google"
          version = "3.71.0"
        }
      }
    }
    
    ファイルを追加する
    provider "google" {
      region  = "europe-west1"
    }
    
    infra/plan/nat.tf
    terraform {
      backend "gcs" {
      }
    }
    
    さて、次の変数をエクスポートし、あなたのterraform状態を保存するためにバケツを作成します.
    export PROJECT_ID=<PROJECT_ID>
    export REGION=<REGION>
    export TERRAFORM_BUCKET_NAME=<BUCKET_NAME>
    
    gcloud config set project ${PROJECT_ID}
    
    gsutil mb -c standard -l ${REGION} gs://${TERRAFORM_BUCKET_NAME}
    gsutil versioning set on gs://${TERRAFORM_BUCKET_NAME}
    
    infra/plan/firewall.tfを作成し、インフラストラクチャを展開します.
    authorized_source_ranges    = ["<AUTHORIZED_NETWORK>"]
    
    cd infra/plan
    
    sed -i "s,<AUTHORIZED_NETWORK>,$AUTHORIZED_NETWORK,g" terraform.tfvars
    
    terraform init \
      -backend-config="bucket=${TERRAFORM_BUCKET_NAME}" \
      -backend-config="prefix=state"
    
    terraform apply
    
    すべてのリソースが正しく作成されているか確認しましょう
    VPCとサブネット

    クラウドナット

    ファイアウォール

    結論


    我々のネットワークは、現在我々のGCP資源を主催する準備ができています.で、我々はGKEのオートパイロットの設定に焦点を当てます.