Kubernetes Backend
The Kubernetes backend stores state in Kubernetes Secrets and uses Lease objects for state locking.Implementation
Location:/internal/backend/remote-state/kubernetes/backend.go
Use Cases
- Managing Kubernetes infrastructure from within the cluster
- GitOps workflows with ArgoCD or Flux
- Kubernetes-native CI/CD pipelines
- Multi-tenancy with namespace isolation
Basic Configuration
Required Configuration
secret_suffix
- Type: String
- Required: Yes
- Description: Suffix used when creating the secret
tfstate-{workspace}-{secret_suffix}
Important: The suffix must not end with -<number>. The backend appends numeric indices when chunking large state files.
Optional Configuration
namespace
- Type: String
- Optional: Yes
- Default:
"default" - Environment Variable:
KUBE_NAMESPACE - Description: Namespace where the secret will be stored
labels
- Type: Map of strings
- Optional: Yes
- Description: Additional labels to apply to the secret
Authentication Methods
The Kubernetes backend supports multiple authentication methods:1. In-Cluster Configuration
For pods running inside Kubernetes:KUBE_IN_CLUSTER_CONFIG
2. Kubeconfig File
KUBE_LOAD_CONFIG_FILE(default:true)KUBE_CONFIG_PATHKUBE_CONFIG_PATHS(colon-separated list)
3. Multiple Config Files
KUBE_CONFIG_PATHS
4. Direct Configuration
5. Client Certificate Authentication
KUBE_CLIENT_CERT_DATAKUBE_CLIENT_KEY_DATA
6. Username/Password Authentication
KUBE_USERKUBE_PASSWORD
Advanced Configuration
Context Selection
KUBE_CTXKUBE_CTX_CLUSTERKUBE_CTX_AUTH_INFO
Exec Plugin Authentication
Insecure TLS
KUBE_INSECURE (default: false)
Warning: Only use in development environments.
Proxy Configuration
KUBE_PROXY_URL
State Storage
State is stored in Kubernetes Secrets:- Secret name:
tfstate-{workspace}-{secret_suffix} - Default workspace:
tfstate-default-{secret_suffix} - Other workspaces:
tfstate-{workspace}-{secret_suffix}
Chunking
Large state files are automatically chunked into multiple secrets:tfstate-{workspace}-{secret_suffix}-0tfstate-{workspace}-{secret_suffix}-1tfstate-{workspace}-{secret_suffix}-2
secret_suffix cannot end with -<number>.
State Locking
The backend uses Kubernetes Lease objects for locking:- Lease name: Matches the secret name
- Namespace: Same as the secret
- Automatic renewal: Leases are renewed during long operations
Configuration Options Summary
| Option | Type | Required | Default | Description |
|---|---|---|---|---|
secret_suffix | string | Yes | - | Suffix for secret name |
namespace | string | No | default | Kubernetes namespace |
labels | map(string) | No | {} | Labels for the secret |
in_cluster_config | bool | No | false | Use in-cluster config |
load_config_file | bool | No | true | Load kubeconfig file |
config_path | string | No | - | Path to kubeconfig |
config_paths | list(string) | No | - | Multiple kubeconfig paths |
config_context | string | No | - | Kubeconfig context |
config_context_auth_info | string | No | - | Auth info name |
config_context_cluster | string | No | - | Cluster name |
host | string | No | - | Kubernetes API server URL |
username | string | No | - | Basic auth username |
password | string | No | - | Basic auth password |
token | string | No | - | Service account token |
client_certificate | string | No | - | Client certificate PEM |
client_key | string | No | - | Client key PEM |
cluster_ca_certificate | string | No | - | CA certificate PEM |
insecure | bool | No | false | Skip TLS verification |
proxy_url | string | No | - | Proxy server URL |
Example: In-Cluster with Service Account
Example: EKS with AWS IAM Authenticator
RBAC Requirements
The service account or user needs these permissions:Best Practices
- Use namespaces to isolate different teams or environments
- Apply labels for organization and filtering
- RBAC policies with minimal required permissions
- In-cluster config for pods, kubeconfig for external access
- Service accounts instead of user credentials
- Monitor secret size - Kubernetes secrets have a 1MB limit
- Descriptive suffixes to identify state purposes
Limitations
- Secret size limit - 1MB per secret (handled via chunking)
- No built-in encryption - Use encryption at rest in etcd
- RBAC required - Must have proper permissions
- Namespace scoped - Cannot share across namespaces easily