Terraformed 私有 GKE 集群自动化访问

Posted

技术标签:

【中文标题】Terraformed 私有 GKE 集群自动化访问【英文标题】:Terraformed private GKE cluster automation access 【发布时间】:2019-04-05 23:15:40 【问题描述】:

我使用以下.tf 在 Google Kubernetes Engine (GKE) 上构建了一个私有 Kubernetes 集群(Terraform 版本 11.10):

module "nat" 
  source     = "GoogleCloudPlatform/nat-gateway/google"
  region     = "europe-west1"
  network    = "default"
  subnetwork = "default"


resource "google_container_node_pool" "cluster_1_np" 
  name               = "cluster-1-np"
  region             = "europe-west1"
  cluster            = "$google_container_cluster.cluster_1.name"
  initial_node_count = 1

  lifecycle 
    ignore_changes = ["node_count"]
  

  autoscaling 
    min_node_count = 1
    max_node_count = 50
  

  management 
    auto_repair  = true
    auto_upgrade = true
  

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

    tags = ["$module.nat.routing_tag_regional"]
  


resource "google_container_cluster" "cluster_1" 
  provider                 = "google-beta"
  name                     = "cluster-1"
  region                   = "europe-west1"
  remove_default_node_pool = true

  private_cluster_config 
    enable_private_endpoint = false
    enable_private_nodes    = true
    master_ipv4_cidr_block  = "172.16.0.0/28"
  

  ip_allocation_policy 
    create_subnetwork = true
  

  lifecycle 
    ignore_changes = ["initial_node_count", "network_policy", "node_config", "node_pool"]
  

  node_pool 
    name = "default-pool"
  

  addons_config 
    http_load_balancing 
      disabled = false
    

    horizontal_pod_autoscaling 
      disabled = false
    
  

  master_authorized_networks_config 
    cidr_blocks = [
      
        cidr_block   = "<MY_OFFICE_CIDR>"
        display_name = "Office"
      ,
    ]
  

这很好用,给了我一个私有集群(而 NAT 工作,让节点可以访问互联网),我办公室的机器可以运行kubectl 命令与它进行交互,而无需麻烦。

我现在面临的问题是集成任何基于 Web 的持续集成 (CI) 或持续部署 (CD)。私有集群是谷歌云平台(GCP)的一个新特性,这方面的文档有点欠缺。

到目前为止,我的尝试完全失败了,我的网络知识根本不够。我试过this solution,但似乎自动化机器必须与代理在同一网络上。

我找到了this similar SO question(几乎完全一样,但他是特定于 Cloud Build 的)。在对该问题的其中一个答案的 cmets 中,OP 提到他找到了一种解决方法,他暂时修改了构建机器的主授权网络,但他没有确切说明他是如何执行此操作的。

我试图复制他的解决方法,但相关的gcloud 命令似乎能够update 网络列表,或者完全删除所有网络,而不是像他的评论建议的那样一次添加/删除一个。

非常感谢网络向导的帮助。

【问题讨论】:

【参考方案1】:

这是与公共云中的CircleCI 或Travis 等CI 系统交互时的常见问题。您可以使用此命令更新您的master authorized networks

gcloud container clusters update [CLUSTER_NAME] \
  --enable-master-authorized-networks \
  --master-authorized-networks=<MY_OFFICE_CIDR>,<NEW-CIDR-FROM-CI> \
  --zone=<your-zone>

要删除 CI 系统网络,您可以执行以下操作(只需从 cli 中删除网络):

gcloud container clusters update [CLUSTER_NAME] \
  --enable-master-authorized-networks \
  --master-authorized-networks=<MY_OFFICE_CIDR> \
  --zone=<your-zone>

彻底删除所有授权网络(禁用):

gcloud container clusters update [CLUSTER_NAME] \
  --no-enable-master-authorized-networks 

您也可以从 UI 中执行此操作:

实际上记录在here。

【讨论】:

请注意,根据答案中链接的文档,--no-enable-master-authorized-networks 具有允许从任何 IP 访问的效果。

以上是关于Terraformed 私有 GKE 集群自动化访问的主要内容,如果未能解决你的问题,请参考以下文章