Search code examples
kubernetesterraformterraform-provider-kubernetes

sidecar with kubernetes provider for terraform


I have to add 2 containers as sidecars in a k8s deployment. I am using terraform Kubernetes provider. Is it available in terraform Kubernetes provider? If yes, any example would be helpful.

    resource "kubernetes_deployment" "test_deployment" {
      metadata {
        name = test_nginx
        namespace = test
        labels = {
          app = test_nginx
        }
      }
    
      spec {
        replicas = "2"
    
        selector {
          match_labels = {
            app = test_nginx
          }
        }
    
        template {
          metadata {
            labels = {
              app = test_nginx
            }
          }
          spec {
             container {
              image             = nginx
              name              = local_nginx
              .
              .
              .
              image             = logrotate
              name              = local_logrotate
              .
              .
              .
                    }
                }
            }
        }
    }

Error:

Error: Attribute redefined │ │ On deployment\deployment.tf line 84: The argument "image" was already set │ at deployment\deployment.tf:28,11-16. Each argument may be set only once.


Solution

  • You need to use another container block:

    resource "kubernetes_deployment" "test_deployment" {
      metadata {
        name = test_nginx
        namespace = test
        labels = {
          app = test_nginx
        }
      }
    
      spec {
        replicas = "2"
    
        selector {
          match_labels = {
            app = test_nginx
          }
        }
    
        template {
          metadata {
            labels = {
              app = test_nginx
            }
          }
          spec {
             container {
              image             = nginx
              name              = local_nginx
              .
              .
              .
              }
    
             # new container block
             container {
              image             = logrotate
              name              = local_logrotate
              .
              .
              .
                    }
                }
            }
        }
    }
    

    I would also suggest moving to a new version of deployments [1], i.e., kubernetes_deployment_v1.


    [1] https://registry.terraform.io/providers/hashicorp/kubernetes/latest/docs/resources/deployment_v1