Skip to content

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.

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.

resource "kubernetes_config" "name" {
cluster = resource.kubernetes_cluster.dev
paths = ["./k8s/deployment.yaml"]
wait_until_ready = true
}
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"
]
}
}
FieldRequiredTypeDescription
clusterreference to kubernetes_clusterReference to the Kubernetes cluster resource
pathslist(string)Paths to Kubernetes config files or directories to apply
wait_until_readyboolWait until all resources are created and running
health_checkblockHealth check configuration for deployed resources

kubernetes_config → health_check

Configures health verification for resources deployed by the configuration.

FieldRequiredTypeDescription
timeoutstringMaximum duration to wait for health check (Go duration format)
podslist(string)Kubernetes label selectors for pods to monitor

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
  • 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”)
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"]
}
}
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"
]
}
}
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"]
}
}
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"
]
}
}
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
}
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"
]
}
}
./k8s/webapp-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: webapp
labels:
app: webapp
spec:
replicas: 2
selector:
matchLabels:
app: webapp
template:
metadata:
labels:
app: webapp
spec:
containers:
- name: webapp
image: nginx:1.21
ports:
- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: webapp
spec:
selector:
app: webapp
ports:
- port: 80
targetPort: 80
type: ClusterIP
# Deploy infrastructure first
resource "kubernetes_config" "infrastructure" {
cluster = resource.kubernetes_cluster.app
paths = [
"./k8s/namespaces.yaml",
"./k8s/rbac/",
"./k8s/secrets/"
]
wait_until_ready = true
}
# Deploy databases after infrastructure
resource "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 databases
resource "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"]
}
}
  1. Organized Structure: Organize configuration files in logical directories (namespaces, apps, infrastructure)
  2. Health Checks: Always configure health checks for critical application components
  3. Dependency Management: Use depends_on to ensure proper deployment order
  4. Wait Strategy: Set wait_until_ready = true for resources that other components depend on
  5. Realistic Timeouts: Set appropriate health check timeouts based on application startup time
  6. Label Consistency: Use consistent labeling across resources for effective health checking
  7. Path Organization: Use directories for related configurations and individual files for standalone resources