使用 terraform 将公共 GKE 更改为私有 GKE 集群

时间:2021-01-27 09:33:09

标签: kubernetes terraform google-kubernetes-engine

如何将现有的 GKE 集群更改为 GKE 私有集群?我能否根据防火墙规则从 Internet 连接到 Kubectl API,还是应该拥有堡垒主机?我不想实现 Cloud Natnat gateway。我有一个鱿鱼代理虚拟机,可以处理豆荚的互联网访问。我只需要能够连接到 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",
    ]
  }
}

2 个答案:

答案 0 :(得分:1)

回答部分问题:

<块引用>

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

GKE Private cluster network settings

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 响应了将重新创建集群的信息

答案 1 :(得分:0)

  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 通过第二个选项管理多个集群,它是完全可配置的。