Containers

Container blocks manage containerized applications and services. Facilitating the definition of container deployments, jobs, configurations, and associated Kubernetes resources like services, probes, and volumes.

Syntax

resource "name" {
  containers {
    [container_type] "[container_name]" {
      type = [k8s_resource_type]
      [attribute] = [value]
    }
  }
}

(Note: the "[container_type]" has no effect on functionality)

Defining Containers

Each container is defined by its type, name, and relevant attributes. Attributes vary based on the container type.

Common Attributes:

  • image: The container image to use.

  • type: The type of Kubernetes resource (e.g., Deployment, CronJob).

  • replicas: Number of pod replicas.

  • command/args: Commands and arguments to run inside the container.

  • resources: Resource limits and requests.

  • ports: Port configurations.

  • service: Service-related settings.

  • auto_scaling: Defines auto-scaling parameters.

  • node_selector: Specifies node selection criteria.

Auto Scaling

Defines the auto-scaling behavior for container deployments based on CPU utilization or other metrics.

Attributes:

  • min_replicas: Minimum number of replicas.

  • max_replicas: Maximum number of replicas.

  • target_cpu_utilization_percentage: Target CPU usage for scaling.

Services

Configures Kubernetes Services to expose containers. Supports different service types like LoadBalancer, ClusterIP, etc.

Attributes:

  • type: Type of service (e.g., LoadBalancer, ClusterIP).

  • annotations: Key-value pairs for service annotations.

  • ports: Port configurations for the service.

Probes and Resources

  • readiness_probe: Defines how Kubernetes determines if a container is ready to receive traffic.

  • resources: Sets resource limits and requests for containers.

Example

service "name" {
 containers {
    app "web_app" {
      image             = "nginx:latest"
      type              = "Deployment"
      replicas          = 3
      command           = ["/bin/sh"]
      args              = ["-c", "nginx -g 'daemon off;'"]
      working_dir       = "/usr/share/nginx/html"

      readiness_probe = {
        http_get = {
          path = "/healthz"
          port = 80
        }
        initial_delay_seconds = 5
        period_seconds        = 10
      }

      resources = {
        limits = {
          cpu    = "500m"
          memory = "512Mi"
        }
        requests = {
          cpu    = "250m"
          memory = "256Mi"
        }
      }

      empty_dir_volumes = [
        {
          name       = "cache"
          size_limit = "1Gi"
        }
      ]

      volume_mounts = [
        {
          name      = "cache"
          mountPath = "/cache"
        }
      ]

      ports = [
        {
          container_port = 80
          service_port   = 80
        }
      ]

      service = {
        type        = "LoadBalancer"
        annotations = {
          "service.beta.kubernetes.io/aws-load-balancer-type" = "nlb"
        }
      }

      node_selector = {
        "kubernetes.io/os" = "linux"
        "node-type"        = "web"
      }

      auto_scaling = {
        min_replicas                      = 2
        max_replicas                      = 10
        target_cpu_utilization_percentage = 80
      }
    }
  }
}

Last updated