- 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.
266 lines
5.9 KiB
Markdown
266 lines
5.9 KiB
Markdown
# 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 apply` completed)
|
|
- `kubectl` configured 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
|
|
|
|
```bash
|
|
# 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
|
|
|
|
```bash
|
|
# 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
|
|
|
|
1. Go to **Kubernetes** in the Linode Dashboard
|
|
2. Select your cluster (should be named something like `hapi-staging` or `hapi-production`)
|
|
3. Click **Kubeconfig** tab
|
|
4. Copy the config and save to `~/.kube/config`
|
|
|
|
### Verify your setup:
|
|
|
|
```bash
|
|
kubectl cluster-info
|
|
kubectl get nodes
|
|
```
|
|
|
|
## Option 1: Deploy with Kustomize (Recommended for quick testing)
|
|
|
|
```bash
|
|
cd infra/k8s
|
|
|
|
# Set environment
|
|
export ENVIRONMENT=staging
|
|
|
|
# Apply the kustomize configuration
|
|
kubectl apply -k .
|
|
```
|
|
|
|
### Verify the deployment:
|
|
|
|
```bash
|
|
# 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:
|
|
|
|
```bash
|
|
# 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:**
|
|
|
|
1. **Ensure kubeconfig is set** (see Step 1 above)
|
|
2. **Use `--validate=false`** to skip OpenAPI validation:
|
|
```bash
|
|
kubectl apply -k . --validate=false
|
|
```
|
|
3. **Use `--server-dry-run`** for validation without connecting to API server:
|
|
```bash
|
|
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`:
|
|
|
|
```hcl
|
|
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:
|
|
|
|
```bash
|
|
cd infra
|
|
terraform init # if new module
|
|
terraform apply
|
|
```
|
|
|
|
## Option 3: Use the Makefile
|
|
|
|
The k8s directory includes a Makefile with convenient commands:
|
|
|
|
```bash
|
|
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:
|
|
|
|
```bash
|
|
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:
|
|
|
|
```bash
|
|
kubectl get all -n postgres -l app=postgres
|
|
```
|
|
|
|
### 2. Verify PostgreSQL is ready:
|
|
|
|
```bash
|
|
# 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:
|
|
|
|
```bash
|
|
kubectl exec -n postgres -l app=postgres -- psql -U postgres -c "CREATE DATABASE hapi_test;"
|
|
```
|
|
|
|
### 4. Check logs:
|
|
|
|
```bash
|
|
kubectl logs -n postgres -l app=postgres --tail=50
|
|
```
|
|
|
|
## Troubleshooting
|
|
|
|
### Pod stuck in Pending:
|
|
|
|
```bash
|
|
kubectl describe pod -l app=postgres -n postgres
|
|
kubectl get events -n postgres --sort-by='.lastTimestamp'
|
|
```
|
|
|
|
### PVC bound but pod can't mount:
|
|
|
|
```bash
|
|
kubectl describe pvc postgres-pvc -n postgres
|
|
kubectl describe pod -l app=postgres -n postgres
|
|
```
|
|
|
|
### PostgreSQL connection issues:
|
|
|
|
```bash
|
|
# 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:
|
|
|
|
1. **Kubernetes credentials** are configured (Terraform can output kubeconfig)
|
|
2. **Network policies** allow traffic (firewall rules in `infra/network`)
|
|
3. **Storage class** exists (`linode-block-storage` is default)
|
|
|
|
### Example: Get kubeconfig from Terraform
|
|
|
|
```hcl
|
|
# In postgres module outputs
|
|
output "kubeconfig" {
|
|
description = "Kubeconfig for PostgreSQL cluster"
|
|
value = linode_lke_cluster.postgres[var.environment].kubeconfig
|
|
}
|
|
```
|
|
|
|
## Next Steps
|
|
|
|
After testing:
|
|
|
|
1. **Configure backups** to Object Storage
|
|
2. **Set up monitoring** with Prometheus
|
|
3. **Add secrets management** (Kubernetes Secrets or external secrets)
|
|
4. **Deploy to production** after staging validation
|