【问题标题】:Change Public GKE to Private GKE cluster using terraform使用 terraform 将公共 GKE 更改为私有 GKE 集群
【发布时间】:2021-05-01 02:56:02
【问题描述】:

如何将现有的 GKE 集群更改为 GKE 私有集群?我是否能够根据防火墙规则从 Internet 连接到 Kubectl API,还是应该有一个堡垒主机?我不想实现Cloud Natnat gateway。我有一个 squid 代理 VM,可以处理 pod 的 Internet 访问。我只需要能够连接到 Kubectl 即可应用或修改任何内容。

我不确定如何修改我编写的现有模块以使节点成为私有节点,并且我不确定如果我尝试应用与私有 gke 集群相关的新更改,集群是否会被删除。

resource "google_container_cluster" "primary" {
  name     = "prod"
  network  = "prod"
  subnetwork = "private-subnet-a"
  location               = "us-west1-a"
  remove_default_node_pool = true
  initial_node_count = 1

  depends_on = [var.depends_on_vpc]
}

resource "google_container_node_pool" "primary_nodes" {
  depends_on = [var.depends_on_vpc]

  name       = "prod-node-pool"
  location   = "us-west1-a"
  cluster    = google_container_cluster.primary.name
  node_count = 2

  node_config {
    preemptible  = false
    machine_type = "n1-standard-2"

    metadata = {
      disable-legacy-endpoints = "true"
    }

    oauth_scopes = [
      "https://www.googleapis.com/auth/logging.write",
      "https://www.googleapis.com/auth/monitoring",
      "https://www.googleapis.com/auth/devstorage.read_only",
      "https://www.googleapis.com/auth/compute",
    ]
  }
}

【问题讨论】:

    标签: kubernetes terraform google-kubernetes-engine


    【解决方案1】:

    回答部分问题:

    如何将现有的 GKE 集群更改为 GKE 私有集群?

    GKE 设置:Private cluster 是不可变的。此设置只能在GKE 集群配置期间设置。

    要将您的集群创建为私有集群,您可以:

    • 创建一个新的GKE 专用集群。
    • 复制现有集群并将其设置为私有:
      • 此设置在GCP Cloud Console -> Kubernetes Engine -> CLUSTER-NAME -> Duplicate 中可用
      • 此设置将克隆您之前集群的基础架构配置,但不会克隆工作负载(PodsDeployments 等)

    我是否能够根据防火墙规则从 Internet 连接到 Kubectl API,或者我应该拥有一个堡垒主机?

    是的,您可以,但这在很大程度上取决于您在GKE 集群创建过程中选择的配置。

    关于连接到您的GKE 私有集群的能力,有一个专门的文档:


    关于如何使用 Terraform 创建私有集群,有一个专门的站点,其中包含特定于 GKE 的配置选项。还有一些参数负责配置private 集群:

    关于使用 Terraform 创建私有 GKE 集群的基本示例:

    • main.tf
    provider "google" {
      project = "INSERT_PROJECT_HERE" 
      region  = "europe-west3"
      zone    = "europe-west3-c"
    }
    
    • gke.tf
    resource "google_container_cluster" "primary-cluster" {
      name               = "gke-private"
      location           = "europe-west3-c"
      initial_node_count = 1
    
      private_cluster_config {
        enable_private_nodes = "true"
        enable_private_endpoint = "false" # this option will make your cluster available through public endpoint 
        master_ipv4_cidr_block = "172.16.0.0/28"
      }
    
      ip_allocation_policy {
        cluster_secondary_range_name = "" 
        services_secondary_range_name = ""
      }
    
      
      node_config {
        machine_type = "e2-medium"
      }
    }
    

    附注!

    我创建了一个公共GKE 集群,修改了负责创建它的.tf 以支持私有集群。运行后:$ terraform plan Terraform 响应信息表明集群将被重新创建

    【讨论】:

    • 在 atm 测试它。创建新集群
    【解决方案2】:
    1. 您必须重新创建集群,因为私有/公共选项是不可变的。 Terraform 将重新创建集群。

    2. 要访问私有集群端点,您可以选择appropriate methods

      1) Public endpoint access disabled:- creates a private cluster with no client access to the public endpoint.
      2) Public endpoint access enabled, authorized networks enabled:- creates a private cluster with limited access to the public endpoint.
      3) Public endpoint access enabled, authorized networks disabled:- creates a private cluster with unrestricted access to the public endpoint.
      
    3. 要从授权网络 ssh 进入节点/pod,您可以通过IAP 设置访问权限。

    我正在使用this terraform module 使用第二个选项管理多个集群,它是完全可配置的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-31
      • 2020-01-30
      • 2020-07-24
      • 1970-01-01
      • 2019-12-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多