【问题标题】:Handle multiple apply Kubernetes deployment with Terraform & GitlabCI使用 Terraform 和 Gitlab CI 处理多个应用 Kubernetes 部署
【发布时间】:2021-10-01 05:38:13
【问题描述】:

我部署了一个具有两个不同 terraform apply 的 Kubernetes 环境。第一个用于部署 EKS 集群本身,第二个用于部署不同的服务。

这里建议:https://registry.terraform.io/providers/hashicorp/kubernetes/latest/docs#stacking-with-managed-kubernetes-cluster-resources

The most reliable way to configure the Kubernetes provider is to ensure that the cluster
itself and the Kubernetes provider resources can be managed with separate apply
operations. Data-sources can be used to convey values between the two stages as needed.

我开始构建 GitlabCI 管道,并且只想尝试使用 terraform plan 命令来提交合并请求。

如果没有应用第一个模块(未创建 EKS 集群),第二个 terraform plan 将无效,有没有一种方法可以执行第二个模块的持续集成而无需部署集群?

【问题讨论】:

    标签: kubernetes terraform gitlab-ci amazon-eks


    【解决方案1】:

    如果您的“服务”模块依赖于“集群”模块中的值(它肯定会这样做,因为它需要知道将资源部署到哪里),那么不,您不能运行 terraform plan,因为它会尝试加载数据源时失败。

    如果给定某个变量,您可以使用条件来消除对“服务”模块的依赖:

    data "cluster" "my_cluster" {
      count = var.my_flag ? 0 : 1
      id = "myclusterid"
    }
    
    resource "my_resource_type" "my_resource" {
      cluster_id = var.my_flag ? null : data.cluster.my_cluster[0]
    }
    

    这将阻止它尝试读取不存在的数据源,但 terraform plan 几乎肯定会失败,因为必填字段不会接受 null 作为值。

    【讨论】:

      猜你喜欢
      • 2019-10-20
      • 2018-06-01
      • 2021-10-09
      • 1970-01-01
      • 2021-07-27
      • 1970-01-01
      • 1970-01-01
      • 2023-02-11
      • 2018-11-08
      相关资源
      最近更新 更多