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
Section titled “Fields”Field | Required | Type | Description |
---|---|---|---|
cluster | ✓ | reference to kubernetes_ | Reference to the Kubernetes cluster resource |
paths | ✓ | list(string) | Paths to Kubernetes config files or directories to apply |
wait_ | ✓ | bool | Wait until all resources are created and running |
health_check | block | Health check configuration for deployed resources |
Health Check Block
Section titled “Health Check Block”kubernetes_config → health_check
Configures health verification for resources deployed by the configuration.
Field | Required | Type | Description |
---|---|---|---|
timeout | ✓ | string | Maximum duration to wait for health check (Go duration format) |
pods | ✓ | list(string) | Kubernetes label selectors for pods to monitor |
Computed Attributes
Section titled “Computed Attributes”These attributes are set by the system after configuration is applied:
Field | Type | Description |
---|---|---|
job_checksums |
map(string) | Checksums of applied configuration files for change detection |
Validation Rules
Section titled “Validation Rules”- Path resolution: All paths are made absolute relative to the config file location
- Cluster dependency: Referenced cluster must exist and be healthy before applying configuration
- File existence: All specified files and directories must exist
- Health check selectors: Must use valid Kubernetes label selector syntax
- Timeout format: Must be valid Go duration strings (e.g., ”60s”, “5m”, “1h”)
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: ClusterIP
Integration 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_on
to ensure proper deployment order - Wait Strategy: Set
wait_until_ready = true
for 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