在 k8s pod 的字符串转换为 yaml 时,用 terraform 编写的 Kubernetes pod 出错

Kubernetes pod written in terraform error in string conversion to yaml for k8s pod

提问人:Naman_sharma 提问时间:11/15/2023 更新时间:11/15/2023 访问量:24

问:

这是我的 Terraform 文件:


provider "kubernetes" {
    config_path    = "~/.kube/config"
  config_context = "minikube"
}

resource "kubernetes_service_v1" "nginx_service" {
  metadata {
    name = "nginx-service"
  }

  spec {
    selector = {
      app = "mypod"
    }

    port {
      protocol   = "TCP"
      port       = 80
    }
  }
}

resource "kubernetes_config_map" "nginx_config" {
  metadata {
    name = "nginx-config"
  }

  data = {
    "default.conf" = <<-EOT
      server {
        listen 80;
        autoindex on;

        location / {
          root /usr/share/nginx/html;
          index index.html;
        }
        location /secure {
          alias /usr/share/nginx/html/secure;
          index index.html;
          try_files $uri $uri/ =404;
        }
        location /insecure {
          alias /usr/share/nginx/html/insecure;
          index index.html;
        }
      }
    EOT
  }
}

resource "kubernetes_pod" "mypod" {
  metadata {
    name = "mypod"
    labels = {
      app = "mypod"
    }
  }

  spec {
    volume {
      name = "html-files"
      empty_dir {}
    }

    volume {
      name = "nginx-config"
      config_map {
        name = "nginx-config"
      }
    }

    container {
      name  = "nginx-container"
      image = "nginx"

      volume_mount {
        name       = "html-files"
        mount_path = "/usr/share/nginx/html"
      }

      volume_mount {
        name       = "nginx-config"
        mount_path = "/etc/nginx/conf.d"
      }
    }

    container {
      name  = "ubuntu-container"
      image = "ubuntu"

      volume_mount {
        name       = "html-files"
        mount_path = "/usr/share/nginx/html"
      }

      command = ["/bin/sh", "-c"]

      args = [
            "echo '<html><body><h1>This is the HOME page</h1></body></html>' > /usr/share/nginx/html/index.html ",
            "mkdir -p /usr/share/nginx/html/insecure /usr/share/nginx/html/secure" ,
            "echo '<html><body><h1>This is an insecure page</h1></body></html>' > /usr/share/nginx/html/insecure/index.html",
            "echo '<html><body><h1>This is a secure page</h1></body></html>' > /usr/share/nginx/html/secure/index.html ",
            "sleep infinity ",
          ]
    }
  }
}

resource "kubernetes_ingress_v1" "nginx_ingress" {
  metadata {
    name = "nginx-ingress"
    annotations = {
      "kubernetes.io/ingress.class"            = "nginx"
      "nginx.ingress.kubernetes.io/auth-type" = "basic"
      "nginx.ingress.kubernetes.io/auth-secret" = "basic-auth"
      "nginx.ingress.kubernetes.io/auth-realm" = "Authentication required"
    }
  }

  spec {
    rule {
      host = "naman.training.app"

      http {
        path {
          path     = "/secure"
          backend {
            service {
              name = kubernetes_service_v1.nginx_service.metadata[0].name
              port {
                number = kubernetes_service_v1.nginx_service.spec[0].port[0].port
              }
            }
          }
        }

        path {
          path     = "/insecure"
          backend {
            service { 
              name = kubernetes_service_v1.nginx_service.metadata[0].name
              port {
                number = kubernetes_service_v1.nginx_service.spec[0].port[0].port
              }
            }
          }
        }
      }
    }

    tls {
      hosts = ["naman.training.app"]
      secret_name = "my-tls-secret"
    }
  }
}


resource "kubernetes_secret" "my_tls_secret" {
  metadata {
    name = "my-tls-secret"
  }

  data = {
    "tls.crt" =  file("naman.training.app.crt")   
    "tls.key" = file("naman.training.app-key.pem")  

  }
}

resource "kubernetes_secret" "basic_auth" {
  metadata {
    name = "basic-auth"
  }

  data = {
    auth = file("./auth")
  }
}

在这种情况下,每次我执行时 Ubuntu 容器都会失败terraform apply

但是如果我使用此 yaml 单独部署 pod,它可以工作 -

apiVersion: v1
kind: Pod
metadata:
  name: mypod
  labels:
    app: mypod
spec:
  volumes:
    - name: html-files
      emptyDir: {}
    - name: nginx-config
      configMap:
        name: nginx-config
  containers:
    - name: nginx-container
      image: nginx
      volumeMounts:
        - name: html-files
          mountPath: /usr/share/nginx/html
        - name: nginx-config
          mountPath: /etc/nginx/conf.d
    - name: ubuntu-container
      image: ubuntu
      volumeMounts:
        - name: html-files
          mountPath: /usr/share/nginx/html
      command: ["/bin/sh", "-c"]
      args:
        - |
          echo '<html><body><h1>This is the HOME page</h1></body></html>' > /usr/share/nginx/html/index.html &&
          mkdir -p /usr/share/nginx/html/insecure /usr/share/nginx/html/secure && 
          echo '<html><body><h1>This is an insecure page</h1></body></html>' > /usr/share/nginx/html/insecure/index.html &&
          echo '<html><body><h1>This is a secure page</h1></body></html>' > /usr/share/nginx/html/secure/index.html &&
          sleep infinity

我尝试过一些字符串格式,例如-

args = <<-EOT
  - mkdir -p /usr/share/nginx/html/insecure /usr/share/nginx/html/secure
  - >-
    echo '<html><body><h1>This is an insecure page</h1></body></html>' >
    /usr/share/nginx/html/insecure/index.html
  - >-
    echo '<html><body><h1>This is a secure page</h1></body></html>' >
    /usr/share/nginx/html/secure/index.html
  - sleep infinity
EOT

args = [ <<-EOT
    echo '<html><body><h1>This is the HOME page</h1></body></html>' > /usr/share/nginx/html/index.html &&
    mkdir -p /usr/share/nginx/html/insecure /usr/share/nginx/html/secure &&
    echo '<html><body><h1>This is an insecure page</h1></body></html>' > /usr/share/nginx/html/insecure/index.html &&
    echo '<html><body><h1>This is a secure page</h1></body></html>' > /usr/share/nginx/html/secure/index.html &&
    sleep infinity
  EOT
]

当我单独使用 yaml 文件进行部署时,pod 内的 yaml 是这样的 -

args:
        - >
          echo '<html><body><h1>This is the HOME page</h1></body></html>' >
          /usr/share/nginx/html/index.html &&

          mkdir -p /usr/share/nginx/html/insecure /usr/share/nginx/html/secure
          && 

          echo '<html><body><h1>This is an insecure page</h1></body></html>' >
          /usr/share/nginx/html/insecure/index.html &&

          echo '<html><body><h1>This is a secure page</h1></body></html>' >
          /usr/share/nginx/html/secure/index.html &&

          sleep infinity

它工作得很好,但正因为如此,我必须单独部署 pod,我想使用 terraform 完成这一切,我找不到问题所在。

Kubernetes Terraform YAML 参数 HCL

评论

1赞 Marko E 11/15/2023
“container fails every time I execute ” 是什么意思?是否有任何错误?如果有的话,日志怎么说?terraform apply
0赞 Naman_sharma 11/16/2023
好吧,终端中的 terraform 没有显示错误,但是当我观察 pod 的状态时 - 在 cli 上使用或 in ,ubuntu 容器一次又一次地失败。然后,它使用消息重新启动它 - “Back-off restarting failed container ubuntu-container in pod mypod_default”。后来当我看到这与直接从 yaml 文件创建的 pod 之间的区别时,我看到从 terraform hcl 转换的 args 在转换为 yaml 的过程中确实格式不正确,这就是为什么从 terraform 执行此操作会抛出错误。kubectl get podminikube dashboard
0赞 Naman_sharma 11/16/2023
因此,作为一种解决方案,我尝试使用 EOT,以便 terraform 可以正确格式化它,但它没有这样做,我找不到任何解决方案,或者不知道如果有任何其他问题可能是什么......
0赞 Marko E 11/16/2023
您可以在 Pod 规范中看到的格式是什么?你能把它添加到问题中吗?
0赞 Naman_sharma 11/18/2023
我是在最后添加的。

答: 暂无答案