Kubernetes Config
The kubernetes_config resource applies Kubernetes configuration files to a Kubernetes cluster. It supports individual files or directories containing YAML/JSON manifests, with optional health checks to ensure successful deployment.
Use Cases
Section titled “Use Cases”As a lab author, you can use kubernetes_config resources to:
- Application Deployment: Deploy applications using Kubernetes manifests (Deployments, Services, ConfigMaps)
- Infrastructure Setup: Apply cluster infrastructure components like RBAC, NetworkPolicies, and PersistentVolumes
- Cluster Management: Configure cluster-wide settings, operators, and administrative resources
Kubernetes config resources enable declarative infrastructure deployment with health verification and dependency management.
HCL Syntax
Section titled “HCL Syntax”Basic Syntax
Section titled “Basic Syntax”resource "kubernetes_config" "name" { cluster = resource.k8s_cluster.dev paths = ["./k8s/deployment.yaml"] wait_until_ready = true}Full Syntax
Section titled “Full Syntax”resource "kubernetes_config" "name" { cluster = resource.k8s_cluster.production paths = [ "./k8s/namespace.yaml", "./k8s/deployments/", "./k8s/services.yaml" ] wait_until_ready = true
health_check { timeout = "120s" pods = [ "app=web-server", "app.kubernetes.io/name=api" ] }}Fields
| Field | Type | Required | Description |
|---|---|---|---|
cluster | referencek8s_cluster | ✓ | Select the Kubernetes cluster in your sandbox that these manifests are applied to |
paths | list(string) | ✓ | Paths to the manifest files or directories to apply, relative to the lab repository. Directories are applied in full. |
wait_ | bool | ✓ | Hold the sandbox build until every applied resource reports ready. Turn this off if the manifests deploy something that never becomes ready on its own. |
health_ | block | — | Wait for specific pods to become ready before the sandbox is considered built |
Health Check
Health check configuration for applied resources
| Field | Type | Required | Description |
|---|---|---|---|
timeout | string | ✓ | The maximum duration to wait before marking the health check as failed. Expressed as a Go duration, e.g. 1s = 1 second, 100ms = 100 milliseconds. ConstraintsMust be a valid duration (e.g. 30s, 5m) |
pods | list(string) | — | Pods to wait for, given as label selectors such as `app=nginx`. The check passes once every match is running. |
Computed Attributes
These attributes are set by the system and are read-only.
| Attribute | Type | Description |
|---|---|---|
job_checksums | map(string) | Checksums of applied Kubernetes manifests for change detection |
Examples
Section titled “Examples”Simple Application Deployment
Section titled “Simple Application Deployment”resource "kubernetes_config" "webapp" { cluster = resource.k8s_cluster.dev paths = ["./k8s/webapp-deployment.yaml"] wait_until_ready = true
health_check { timeout = "60s" pods = ["app=webapp"] }}Multi-File Configuration
Section titled “Multi-File Configuration”resource "kubernetes_config" "microservices" { cluster = resource.k8s_cluster.app paths = [ "./k8s/namespace.yaml", "./k8s/configmaps/", "./k8s/deployments/", "./k8s/services/" ] wait_until_ready = true
health_check { timeout = "180s" pods = [ "app=api-service", "app=web-frontend", "app=worker" ] }}Database Setup
Section titled “Database Setup”resource "kubernetes_config" "database" { cluster = resource.k8s_cluster.data paths = [ "./k8s/postgres-secret.yaml", "./k8s/postgres-pvc.yaml", "./k8s/postgres-deployment.yaml", "./k8s/postgres-service.yaml" ] wait_until_ready = true
health_check { timeout = "120s" pods = ["app=postgresql"] }}Development Environment
Section titled “Development Environment”resource "kubernetes_config" "dev_stack" { cluster = resource.k8s_cluster.development paths = ["./k8s/dev-environment/"] wait_until_ready = true
health_check { timeout = "300s" pods = [ "app=redis", "app=postgres", "app=rabbitmq", "tier=development" ] }}RBAC and Security Configuration
Section titled “RBAC and Security Configuration”resource "kubernetes_config" "security" { cluster = resource.k8s_cluster.secure paths = [ "./k8s/rbac/service-accounts.yaml", "./k8s/rbac/cluster-roles.yaml", "./k8s/rbac/role-bindings.yaml", "./k8s/network-policies/" ] wait_until_ready = true}Monitoring Stack
Section titled “Monitoring Stack”resource "kubernetes_config" "monitoring" { cluster = resource.k8s_cluster.production paths = [ "./k8s/monitoring/namespace.yaml", "./k8s/monitoring/prometheus/", "./k8s/monitoring/grafana/", "./k8s/monitoring/alertmanager/" ] wait_until_ready = true
health_check { timeout = "240s" pods = [ "app.kubernetes.io/name=prometheus", "app.kubernetes.io/name=grafana", "app.kubernetes.io/name=alertmanager" ] }}Configuration File Examples
Section titled “Configuration File Examples”Basic Deployment
Section titled “Basic Deployment”apiVersion: apps/v1kind: Deploymentmetadata: name: webapp labels: app: webappspec: replicas: 2 selector: matchLabels: app: webapp template: metadata: labels: app: webapp spec: containers: - name: webapp image: nginx:1.21 ports: - containerPort: 80---apiVersion: v1kind: Servicemetadata: name: webappspec: selector: app: webapp ports: - port: 80 targetPort: 80 type: ClusterIPIntegration with Dependencies
Section titled “Integration with Dependencies”Sequential Deployment
Section titled “Sequential Deployment”# Deploy infrastructure firstresource "kubernetes_config" "infrastructure" { cluster = resource.k8s_cluster.app paths = [ "./k8s/namespaces.yaml", "./k8s/rbac/", "./k8s/secrets/" ] wait_until_ready = true}
# Deploy databases after infrastructureresource "kubernetes_config" "databases" { depends_on = [resource.kubernetes_config.infrastructure]
cluster = resource.k8s_cluster.app paths = ["./k8s/databases/"] wait_until_ready = true
health_check { timeout = "120s" pods = ["tier=database"] }}
# Deploy applications after databasesresource "kubernetes_config" "applications" { depends_on = [resource.kubernetes_config.databases]
cluster = resource.k8s_cluster.app paths = ["./k8s/apps/"] wait_until_ready = true
health_check { timeout = "180s" pods = ["tier=application"] }}Best Practices
Section titled “Best Practices”- Organized Structure: Organize configuration files in logical directories (namespaces, apps, infrastructure)
- Health Checks: Always configure health checks for critical application components
- Dependency Management: Use
depends_onto ensure proper deployment order - Wait Strategy: Set
wait_until_ready = truefor resources that other components depend on - Realistic Timeouts: Set appropriate health check timeouts based on application startup time
- Label Consistency: Use consistent labeling across resources for effective health checking
- Path Organization: Use directories for related configurations and individual files for standalone resources
