跳到主要內容

發表文章

目前顯示的是有「k8s」標籤的文章

如何使用 Cloud VPN 跨專案存取私有 GKE 叢集 - 使用 Classic VPN

  如何使用 Cloud VPN 跨專案存取私有 GKE 叢集 - 使用 Classic VPN Classic VPN 使用 Route Based 的方式, 跟據 Routing Table 裡設定的路由規則, 將封包轉送到指定的目的地 以下示範在 project 01, project 02 各自建立 GKE, VM, 接著透過 Classic VPN 讓 us-east4 的 VM 可以 access project 01 下 us-central1 的 private GKE 叢集 Step 1. 建立 VPN Gateway 需要建立一個 VPN Gateway 作為 VPC 網路對外溝通的橋樑 variable "p1_project_id" {     default = "project-01" } variable "p1_region" {     default = "us-central1" } resource "google_compute_vpn_gateway" "p1_vpn_gw" {   project   = var.p1_project_id   region    = var.p1_region   name      = "vpn-gw"   network = "projects/ ${var.p1_ project_id } /global/networks/default" } Step 2. 建立 Routing 規則 需要一個對外 IP, 讓外部流量可以流進 VPN Gateway resource "google_compute_address" "p1_vpn_ip" {   project      = var.p1_project_id   region       = var.p1_region   name         = "p1-vpn-ip"   address_type = ...

私有 GKE 叢集整合 ArgoCD 實現 GitOps 自動部署

  私有 GKE 叢集整合 ArgoCD 實現 GitOps 自動部署 前言 為了提升安全性, 企業通常會採用 Private GKE 的叢集架構, 只允許在內網裡存取 Control Plane, 在這種情況下, 若要部署資源到叢集裡, 除了額外再架一個跳板機使用 kubectl 去操作之外, 另一個方式是走 Pull-based 的 GitOps 流程, 安裝一個 Agent 在 GKE 叢集裡,  定時地去掃指定Git Repository 內的 k8s 資源檔, 並將差異同步到 GKE 叢集中 以下介紹如何使用 ArgoCD 來快速地實現以上的流程 Step 1. 安裝 ArgoCD $gcloud container clusters get-credentials cluster --zone us-central1 --project=stage $kubectl create ns argocd $kubectl apply -n argocd -f https://raw.githubusercontent.com/argoproj/argo-cd/stable/manifests/install.yaml Step 2. Patch ArgoCD Server Service 指派外部 IP 因為在接下來的步驟會需要連線到 ArgoCD Server 上操作, 所以必須先  patch 一個 Load Balancer type 的 Service 給 ArgoCD Server 使它能對外溝通 $kubectl patch svc argocd-server -n argocd -p '{"spec": {"type": "LoadBalancer","loadBalancerIP":"10.51.0.12"},"metadata":{"annotations":{"networking.gke.io/load-balancer-type":"Internal"}}}' 以上範例使用 Internal Load Balancer 的 Service, ...

如何在 local 端使用 kubectl 操作 private GKE 叢集

如何在 local 端使用 kubectl 操作 private GKE 叢集 Private GKE cluster set up with bastion vm   基於安全考量,  通常在創建 GKE 叢集時, 會選擇 Private Cluster 的部署方式, 將 Control Plan 藏在內網裡, 若想要在 local 端使用 kubectl 來操作 GKE 的話, 會須要一個跳板機(Bastion Host) 將請求 轉送給內網的 Control Plan, 架構如下 然而這個做法有個安全疑慮, 就是當對外 IP 不小心被洩露了, 駭客就能透過它連進跳板機來進行惡意的操作 比較好的做法是,  Cloud IAP + Private 的 Bastion Host  透過 IAP 的TCP 轉送功能, 就不需要分配對外 IP 給 Bastion Host 實際做法如下 環境建立 Step 1. 建立一個新的 Subnet 給 GKE Cluster 使用 gcloud compute networks create k8s-proxy \   --subnet-mode=custom \   --project=backend gcloud compute networks subnets create subnet-cluster \     --network=k8s-proxy \     --range=10.50.0.0/16 \     --region=us-central1 gcloud compute firewall-rules create k8s-proxy-ssh \     --network k8s-proxy \     --allow tcp:22 因為 Cloud IAP 會從  35.235.240.0/20 這個網段連進內網, 所以防火牆需要開放從從 35.235.240.0/20 過來的流量 gcloud compute firewall-rules create allow-ssh-ingress-from-iap...

如何在 GKE 上部署 Gitlab Runner (使用 Helm) 並整合 Workload Identity

  如何在 GKE 上部署  Gitlab Runner (使用 Helm) 並整合 Workload Identity Gitlab Runner + Helm + workload Identity to deploy Application on GKE 建立 Service Account 給 Gitlab Runner 使用 如果想讓 Gitlab Runner 可以在 GCP  上部署任何應用程式,  必須給他一組有特定權限的 Service Account Gitlab Runner 要使用這個 Service Account, 比較 Hardcore 一點的做法是, 直接下載 Service Account 的 Credential,  再透過 gcloud 指令拿 Credential 去做認證 gcloud auth activate-service-account $SERVICE_ACCOUNT_EMAIL \     --key-file=$CRED  \     --project=$PROJECT    這個做法的缺點是, 需要煩惱如何安全地保管 Credential  比較安全一點的做法是 透過 GKE Workload Identity 將 GKE 裡的 Service Account 與 GCP 上的 Service Account 連結在一起 假設 Gitlab Runner 架構規劃如下: 在 GCP 上總共有三個 Project (cicd/dev/prod), cicd 的 Project 裡有一座 GKE 用來跑 Gitlab Runner, Runner 會將應用程式部署到 dev, 以及 prod 的 App Engine 上 要實現以上的架構 首先 Step 1. 建立 Google Service Account  # Parameters KSA_NAME="gitlab-runner-pd" RUNNER_GSA_PROJECT="cicd" NAMESPACE="gitlab" RUNNER_NAME="gitlab-runner-pd" TOKEN="eZH45H6uQ...

GPU-based workloads as a part of Airflow DAGs

如何讓在 Airflow 上跑的任務使用 GPU 資源 Step 1. 在既有的 Airflow 下新增有 GPU 的 Pool 使用參數 --accelerator 指定 GPU 的規格 gcloud beta container   node-pools create "gpu-pool" \ --cluster "mlworkflow-24aeef46-gke" \ --zone "us-east1-b" \ --machine-type "n1-standard-4" \ --accelerator "type=nvidia-tesla-p100,count=1" \ --image-type "COS_CONTAINERD" \ --num-nodes "3"  Step 2. 測試 GPU 的功能是否正常 部署一個 Pod 來跑 nvidia-smi 指令 注意: GCP 在建立 GPU 的 Pool 時會自動加上 Taint 資訊如下  spec:   taints:   - effect: NoSchedule     key: nvidia.com/gpu     value: present Pod  的 deployment 要加上 Tolerations cat <<EOF | kubectl apply -f - apiVersion: v1 kind: Pod metadata:   labels:     run: mig-none-example   name: mig-none-example spec:   tolerations:   - key: "nvidia.com/gpu"     operator: "Exists"     effect: "NoSchedule"   containers:   - image: nvidia/cuda:11.0-base     name: mig-none-example     resou...