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.kubernetes_cluster.dev paths = ["./k8s/deployment.yaml"] wait_until_ready = true}Full Syntax
Section titled “Full Syntax”resource "kubernetes_config" "name" { cluster = resource.kubernetes_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 | ✓ | Reference to the Kubernetes cluster to apply configuration to |
paths | list(string) | ✓ | Paths to Kubernetes manifest files or directories to apply |
wait_ | bool | ✓ | Wait for all resources to be ready before proceeding |
health_ | block | — | Health check configuration for applied resources |
Health Check
Health check configuration for applied resources
| Field | Type | Required | Description |
|---|---|---|---|
timeout | string | ✓ | Maximum time to wait for health check (e.g. 120s) ConstraintsMust be a valid duration (e.g. 30s, 5m) |
pods | list(string) | — | Pod name patterns to check for readiness |
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.kubernetes_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.kubernetes_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.kubernetes_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.kubernetes_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.kubernetes_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.kubernetes_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.kubernetes_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.kubernetes_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.kubernetes_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
