Terraform - 如何创建 GKE 集群并安装 Helm Charts?

Terraform - How to Create a GKE Cluster and Install Helm Charts?

目标

我有一个特定的工作流程来在 Google 云上设置一个新的 Kubernetes 集群。我想用 Terraform 自动化这个过程。这些是步骤:

  1. 创建集群
    gcloud beta container --project "my-google-project" clusters create "cluster-name" --zone "europe-west3-b"
    
  2. 设置 Helm 回购
    helm repo add stable https://kubernetes-charts.storage.googleapis.com/
    helm repo add jetstack https://charts.jetstack.io/
    helm repo update
    
  3. 安装 NGINX Ingress
    kubectl create clusterrolebinding cluster-admin-binding --clusterrole cluster-admin --user $(gcloud config get-value account)
    helm install nginx-ingress stable/nginx-ingress
    
  4. 安装证书管理器
    kubectl apply --validate=false -f https://raw.githubusercontent.com/jetstack/cert-manager/v0.13.0/deploy/manifests/00-crds.yaml
    kubectl create namespace cert-manager
    helm install cert-manager jetstack/cert-manager --namespace cert-manager
    

想法

第一步可能是这样的:

resource "google_container_cluster" "primary" {
  name               = "cluster-name"
  location           = "europe-west3-b"
  initial_node_count = 3

  master_auth {
    username = ""
    password = ""

    client_certificate_config {
      issue_client_certificate = false
    }
  }

  node_config {
    oauth_scopes = [
      "https://www.googleapis.com/auth/logging.write",
      "https://www.googleapis.com/auth/monitoring",
    ]

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

但我不知道如何处理步骤 2 - 4。

虽然 Terraform 对于为 Kubernetes 等 运行 构建和配置云基础设施很有意义,但在部署后用于配置所述基础设施并不一定有意义。我认为大多数基础架构设计会将部署到已配置集群上的应用程序视为对所述集群的配置。这里的语义肯定有点细微差别,但我认为像 Ansible 这样的工具更适合在配置后将应用程序部署到集群。

所以我的建议是定义一些 Ansible 角色。也许:

create_cluster
deploy_helm
install_nginx_ingress
install_cert_manager

在每个角色中,根据 Ansible 用于 includeimport 这些角色的 Galaxy schema. Lastly, define a Playbook 定义需要使用的任务和变量。这将允许您配置您的基础架构并在一个命令中部署所有必需的应用程序:

ansible-playbook playbook.yml