跳到內容

CloudBuild GKE Autopilot Private Cluster on Original VPC

此方法為使用既有 GKE cluster VPC 與 Cloud Build Private Pool 共用同 VPC 進行實作 ,也就是下圖兩個紅框使用同一個 VPC。

CloudBuild GKE Autopilot Private Cluster 建立新 VPC 架構圖

Scenario

流程與說明

  1. 建立 VPC 網路並開啟 Subnet PGA 與 NAT Gateway,而 GKE 內的 pod 對外網路,也會透過 NAT Gateway 連外
  2. 建立 GKE autopilot Private Cluster
  3. (Optional) 使用 GCE 作為跳板,service account 需具備 Kubernetes Engine Developer 權限,才能取得 GKE credential
  4. 建立 Artifact Registry,讓 GKE Private Cluster 能夠透過 PGA 進行直連
  5. 建立 CloudBuild 專用 VPC 網路 (上圖紅框處) 與所需 route
  • 建立 Cloud Build 並使用 Private Pool 確保資料流在 private network 與固定 ip 中,因此需建立一台獨立 VM 作為 CloudBuild -> GKE 之跳板
    • 流程會變成 CloudBuild 連接至 internal ip -> VM (static internal ip & static public ip) -> GKE(public ip)
    • CloudBuild 建立 private pool 時不可勾選對外 ip,確保流量在 VPC 內,但相對就無法連到外網,因此如有安裝套件相關作業,建議分開 private pool 執行
  1. 保留以下兩個網段不被任何服務所使用
  • CloudBuild 保留 192.168.10.0/24
  • Docker bridge 保留 172.17.0.0/16 (GCP 務必避開使用此網段)

執行步驟

建立 GKE VPC、 建立 Artifact Registry、建立 GKE autopilot Private Cluster、與 (Optional) 建立跳板 VM 與 CloudBuild GKE Autopilot Private Cluster on New VPC 相同

建立 CloudBuild

設定 Cloud Build 網路環境

點我開啟必要 API,本內容參考此文件進行實作

建立 PSA

此處就使用原本的 VPC 不用建立新的,但仍須建立 PSA,開啟 VPC 建立 IP RANGE

建立 VPC PSA

分配 ip range

建立完成後,請留意防火牆不要阻擋到此網段

設定 VPC ip range

建立 Private connections to services

點選 Create connection

PSA設定

選擇 Google Cloud Platform 後,再勾選剛剛所建立的網段

建立private connection

建立 Private Pool

參考此文件進行建立,建立前先點我開啟 API

建立 worker pool

建立worker pool

  1. 輸入名稱與選擇區域
  2. 選擇 Private network
  3. 選擇前面 VPC 網路與提供 ip 網段
  4. 不可勾選分配外部 ip,確保連線都透過私有 VPC 出去,但若需要對外下載相關套件行為,會無法下載
  5. 建立,預設使用 e2-medium 與提供 100GB 硬碟空間

設定worker pool

建立橋接 VM

本內容參考此文件進行實作,目的為讓 CloudBuild 連線到 GKE 時使用固定 ip

建立 VM 輸入名稱,並選擇區域與合適大小,這邊選最小的 e2-micro,開機硬碟選擇 Ubuntu 22.04 LTS

建立橋接 VM

  1. 展開進階設定以設定網路
  2. 輸入網路 tag nat-gw,後面建立 route 會用到
  3. 開啟 IP forwarding

設定橋接 VM 網路

  1. 選擇所建立之 VPC 網路 (build-network) 與其 subnetwork
  2. 保留固定 ip
  3. 完成

設定橋接 VM 網路-保留固定 ip

  1. 輸入保留 ip 名稱
  2. 可自訂 ip 或自動分配
  3. 保留

設定橋接 VM 網路-設定保留內部固定 ip

一樣保留一組對外 ip

設定橋接 VM 網路-設定保留外部固定 ip

貼上以下啟動腳本於 startup script 中

橋接 VM startup script
1
#! /bin/bash
2
set -e
3
4
sysctl -w net.ipv4.ip_forward=1
5
IFACE=$(ip -brief link | tail -1 | awk {'print $1'})
6
iptables -t nat -A POSTROUTING -o "$IFACE" -j MASQUERADE

設定橋接 VM startup script

建立 VPC 路由

為能讓所有連線至 GKE public ip 都使用固定 ip 因此需額外建立 2 條路由

  • CloudBuild private pool -> 橋接 GKE VM
  • 橋接 GKE VM -> default-internet-gateway

開啟 VPC Route 並建立

  1. 選擇 Routes
  2. 點選 ROUTE MANAGEMENT
  3. 建立 ROUTE

建立route

建立第一條路由,由 Cloudbuild 至 VM

  1. 輸入名稱,如: cloudbuild-to-vm
  2. 選擇與 GKE 所在之同一 VPC
  3. 輸入 GKE public ip
  4. 設定優先度,這邊設定 100
  5. 指定往 GKE public ip 的 next hop 要怎走,輸入前面所建立之 VM internal static ip

最後往下點選建立

設定進入route

建立第二條路由,透過 VM ip 連線至 GKE public ip

  1. 輸入名稱如: vm-to-internet-gw
  2. 選擇與 GKE 所在之同一 VPC
  3. 輸入 GKE public ip
  4. 設定優先度,數值相較於第一條低即可,這邊設定 10
  5. 輸入前面 VM 所輸入之 tag:nat-gw
  6. Next hop 為 Default internet gateway 最後點選建立即可

設定流出route

設定 GKE Authorized Network

設定後使前面所建立之橋接 VM 可連線,點選所建立之 GKE Cluster

設GKE

點選 Control plane authorized networks 編輯

編輯 control plane

  1. 新增
  2. 輸入名稱與橋接 VM public static ip
  3. 儲存

編輯 control plane authorized network

後續其他部分可參考與 CloudBuild GKE Autopilot Private Cluster on New VPC