- Create infrastructure for PostgreSQL StatefulSet in Kubernetes - Implement environment-specific configurations (staging/production) - Add Kustomize overlays for deterministic deployments - Document deployment process in AGENTS.md and infra/k8s/README.md - Include Makefile and helper scripts for easy management New directories: - infra/k8s/base/ - Shared resources (StatefulSet, Service, PVC, Namespace) - infra/k8s/staging/ - Staging environment overlay - infra/k8s/production/ - Production environment overlay Key files: - infra/k8s/base/kustomization.yaml - infra/k8s/staging/kustomization.yaml - infra/k8s/production/kustomization.yaml - infra/k8s/README.md Resources are prefixed by environment (staging-, production-) and deployed to the postgres namespace.
5.9 KiB
Testing PostgreSQL Kubernetes Deployment
This guide covers testing the PostgreSQL k8s deployment when you already have Terraform infrastructure in place.
Prerequisites
- Terraform infrastructure deployed (
terraform applycompleted) kubectlconfigured to connect to your LKE cluster- Environment variable set:
export ENVIRONMENT=staging
Step 1: Get Your Kubeconfig
Since you already have Terraform infrastructure deployed, you need to get the kubeconfig from your LKE cluster:
Option A: From Terraform outputs
# Get the kubeconfig from Terraform state
terraform output -json postgres | jq -r '.kubeconfig' > ~/.kube/config
# Or if you want to save it directly
terraform output -raw postgres | jq -r '.kubeconfig' > ~/.kube/config
Option B: From Linode CLI
# Install linode-cli if not already installed
# Get the cluster ID from terraform state or console
CLUSTER_ID=$(terraform state show module.postgres.linode_lke_cluster.postgres | grep id | awk '{print $2}')
# Get kubeconfig
linode lke cluster-kubeconfig $CLUSTER_ID --raw > ~/.kube/config
Option C: From Linode Dashboard
- Go to Kubernetes in the Linode Dashboard
- Select your cluster (should be named something like
hapi-stagingorhapi-production) - Click Kubeconfig tab
- Copy the config and save to
~/.kube/config
Verify your setup:
kubectl cluster-info
kubectl get nodes
Option 1: Deploy with Kustomize (Recommended for quick testing)
cd infra/k8s
# Set environment
export ENVIRONMENT=staging
# Apply the kustomize configuration
kubectl apply -k .
Verify the deployment:
# Check StatefulSet
kubectl get statefulset postgres-statefulset -n postgres
# Check Pod status
kubectl get pod -l app=postgres -n postgres
# Check PVC (standalone)
kubectl get pvc postgres-pvc -n postgres
# Check PVC from volumeClaimTemplates
kubectl get pvc -l app=postgres -n postgres
# Check Service
kubectl get service postgres-service -n postgres
# Test PostgreSQL connectivity
kubectl exec -it -l app=postgres -n postgres -- pg_isready -U postgres
Fix for CommonLabels Deprecation
If you see Warning: 'commonLabels' is deprecated. Please use 'labels' instead, run:
# Fix the kustomization file
kustomize edit fix
This updates commonLabels/commonAnnotations to labels/annotations per the latest Kustomize spec.
Troubleshooting
OpenAPI Validation Error
If you see failed to download openapi: Get "http://localhost:8080/openapi/v2": connection refused, this means kubectl is trying to connect to a local cluster but can't find one.
Solutions:
- Ensure kubeconfig is set (see Step 1 above)
- Use
--validate=falseto skip OpenAPI validation:kubectl apply -k . --validate=false - Use
--server-dry-runfor validation without connecting to API server:kubectl apply -k . --server-dry-run
Option 2: Deploy with Terraform Module
Add the k8s module to your Terraform configuration:
1. Update infra/main.tf:
module "k8s" {
source = "./k8s"
environment = var.environment
postgres_image = "postgres:16.3"
postgres_db = "hapi_${var.environment}"
storage_size = "50Gi"
cpu_request = "500m"
cpu_limit = "1000m"
memory_request = "512Mi"
memory_limit = "1Gi"
}
2. Run Terraform:
cd infra
terraform init # if new module
terraform apply
Option 3: Use the Makefile
The k8s directory includes a Makefile with convenient commands:
cd infra/k8s
# Deploy to staging
make apply
# Deploy to production
make apply ENVIRONMENT=production
# Check status
make status
# View logs
make logs
# Test connectivity
make test
# Open psql shell
make psql
Option 4: Manual kubectl Apply
If you prefer more control:
cd infra/k8s
# Create namespace first (if not using kustomization.yaml)
kubectl create namespace postgres --dry-run=client -o yaml | kubectl apply -f -
# Apply resources in order
kubectl apply -f pvc.yaml
kubectl apply -f service.yaml
kubectl apply -f statefulset.yaml
Verification Steps
1. Check all resources are created:
kubectl get all -n postgres -l app=postgres
2. Verify PostgreSQL is ready:
# Wait for pod to be ready
kubectl wait --for=condition=Ready pod -l app=postgres -n postgres --timeout=300s
# Test connection
kubectl exec -n postgres -l app=postgres -- pg_isready -U postgres
3. Create test database:
kubectl exec -n postgres -l app=postgres -- psql -U postgres -c "CREATE DATABASE hapi_test;"
4. Check logs:
kubectl logs -n postgres -l app=postgres --tail=50
Troubleshooting
Pod stuck in Pending:
kubectl describe pod -l app=postgres -n postgres
kubectl get events -n postgres --sort-by='.lastTimestamp'
PVC bound but pod can't mount:
kubectl describe pvc postgres-pvc -n postgres
kubectl describe pod -l app=postgres -n postgres
PostgreSQL connection issues:
# Get PostgreSQL logs
kubectl logs -n postgres -l app=postgres
# Check PostgreSQL config
kubectl exec -n postgres -l app=postgres -- cat /var/lib/postgresql/data/postgresql.conf
Testing with Terraform-Managed Cluster
If your Terraform creates the LKE cluster, ensure:
- Kubernetes credentials are configured (Terraform can output kubeconfig)
- Network policies allow traffic (firewall rules in
infra/network) - Storage class exists (
linode-block-storageis default)
Example: Get kubeconfig from Terraform
# In postgres module outputs
output "kubeconfig" {
description = "Kubeconfig for PostgreSQL cluster"
value = linode_lke_cluster.postgres[var.environment].kubeconfig
}
Next Steps
After testing:
- Configure backups to Object Storage
- Set up monitoring with Prometheus
- Add secrets management (Kubernetes Secrets or external secrets)
- Deploy to production after staging validation