簡體   English   中英

如何在 Terraform 中自動添加 kube.netes 客戶端密碼作為文件掛載?

[英]How can I automatically add a kubernetes client secret as a file mount in Terraform?

我正在使用 Terraform 設置外部 DNS。 根據文檔,我必須手動創建一個azure.json文件並將其掛載為秘密卷。 方向也是state:

默認情況下,Azure DNS 提供程序期望配置文件位於 /etc/kube.netes/azure.json

{
  "tenantId": "01234abc-de56-ff78-abc1-234567890def",
  "subscriptionId": "01234abc-de56-ff78-abc1-234567890def",
  "resourceGroup": "MyDnsResourceGroup",
  "aadClientId": "01234abc-de56-ff78-abc1-234567890def",
  "aadClientSecret": "uKiuXeiwui4jo9quae9o"
}

然后,我運行kubectl create secret generic azure-config-file --from-file=/local/path/to/azure.json以將密鑰掛載為文件。

問題是這些值是動態的,我需要根據 CI/CD 管道自動執行此操作。 我正在使用 Terraform Kube.netes 資源,在這里我使用了kube.netes_secret資源。

resource "kubernetes_secret" "azure_config_file" {
  metadata {
    name = "azure-config-file"
  }

  data = {
    tenantId = data.azurerm_subscription.current.tenant_id
    subscriptionId = data.azurerm_subscription.current.subscription_id
    resourceGroup = azurerm_resource_group.k8s.name
    aadClientId = azuread_application.sp_externaldns_connect_to_dns_zone.application_id
    aadClientSecret = azuread_application_password.sp_externaldns_connect_to_dns_zone.value
  }

  depends_on = [
    kubernetes_namespace.external_dns,
  ]
}

秘密被掛載,但 pod 永遠看不到它,這會導致 crashLoopBackoff。 這可能不是最好的方向。

如何使用 Terraform 自動執行此過程並正確安裝它?

作為參考,這是 YAML 清單的相關部分

...

       volumeMounts:
        - name: azure-config-file
          mountPath: /etc/kubernetes
          readOnly: true
      volumes:
      - name: azure-config-file
        secret:
          secretName: azure-config-file
          items:
          - key: externaldns-config.json
            path: azure.json

這是將 --from --from-file標志與 kubectl 一起使用的 Terraform 版本。

基本上,您將按照下面data塊的結構添加文件名及其內容。

resource "kubernetes_secret" "azure_config_file" {
  metadata {
    name = "azure-config-file"
  }

  data = { "azure.json" = jsonencode({
    tenantId        = data.azurerm_subscription.current.tenant_id
    subscriptionId  = data.azurerm_subscription.current.subscription_id
    resourceGroup   = data.azurerm_resource_group.rg.name
    aadClientId     = azuread_application.sp_externaldns_connect_to_dns_zone.application_id
    aadClientSecret = azuread_application_password.sp_externaldns_connect_to_dns_zone.value
    })

  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM