Merge pull request 'feat: PostgreSQL Kubernetes deployment with Kustomize' (#36) from feat/01b-kubernetes-postgresql-statefulset into main
This commit was merged in pull request #36.
This commit is contained in:
commit
741f6e61e9
26 files changed
+1407
-14
No files matched your search
@@ -21,12 +21,46 @@ Single-context layout: `GLOSSARY.md` at repo root and `docs/adr/` for architectu
|
||||
The `infra/` directory contains Terraform configurations with these modules:
|
||||
|
||||
- `backend/` - Terraform state backend (Object Storage S3-compatible)
|
||||
- `k8s/` - PostgreSQL StatefulSet with persistent storage and Kubernetes manifests
|
||||
- `network/` - Firewall rules for API (8000), PostgreSQL (5432), Adminer (8080)
|
||||
- `postgres/` - LKE clusters for staging/production
|
||||
- `storage/` - Object Storage buckets for uploads and state
|
||||
- `network/` - Firewall rules for API (8000), PostgreSQL (5432), Adminer (8080)
|
||||
|
||||
See `infra/README.md` and `docs/adr/0002-linode-provider-v2-migration.md` for details.
|
||||
|
||||
### Kubernetes Deployments
|
||||
|
||||
PostgreSQL is deployed using Kustomize with environment-specific configurations:
|
||||
|
||||
- **Base** (`infra/k8s/base/`) - Shared resources (StatefulSet, Service, PVC, Namespace)
|
||||
- **Staging** (`infra/k8s/staging/`) - Staging environment overlay with `staging-` prefix
|
||||
- **Production** (`infra/k8s/production/`) - Production environment overlay with `production-` prefix
|
||||
|
||||
**Deployment commands:**
|
||||
|
||||
```bash
|
||||
# Staging
|
||||
kubectl apply -k infra/k8s/staging
|
||||
|
||||
# Production
|
||||
kubectl apply -k infra/k8s/production
|
||||
|
||||
# Dry run
|
||||
kubectl apply -k infra/k8s/staging --dry-run=client
|
||||
```
|
||||
|
||||
**Resource naming:**
|
||||
|
||||
- Staging resources are prefixed with `staging-`
|
||||
- Production resources are prefixed with `production-`
|
||||
- All resources go to the `postgres` namespace
|
||||
|
||||
**Secrets required:**
|
||||
|
||||
- `postgres-secrets` with `username` and `password` keys
|
||||
|
||||
See `infra/k8s/README.md` for more details.
|
||||
|
||||
## Provider Versions
|
||||
|
||||
### Linode Terraform Provider
|
||||
|
||||
@@ -11,9 +11,10 @@ This implementation creates the Linode infrastructure for the HAPI project using
|
||||
| Module | Location | Description |
|
||||
|--------|----------|-------------|
|
||||
| **Backend** | `infra/backend/` | Terraform state stored in Linode Object Storage |
|
||||
| **Kubernetes** | `infra/k8s/` | PostgreSQL StatefulSet with persistent storage |
|
||||
| **Network** | `infra/network/` | Firewall rules for API, PostgreSQL, and Adminer |
|
||||
| **PostgreSQL** | `infra/postgres/` | Database instances for staging and production |
|
||||
| **Storage** | `infra/storage/` | Object Storage buckets for uploads and state |
|
||||
| **Network** | `infra/network/` | Firewall rules for API, PostgreSQL, and Adminer |
|
||||
|
||||
### Directory Structure
|
||||
|
||||
@@ -23,6 +24,17 @@ infra/
|
||||
│ ├── main.tf
|
||||
│ ├── variables.tf
|
||||
│ └── terraform.tfvars.example
|
||||
├── k8s/ # PostgreSQL Kubernetes manifests
|
||||
│ ├── kustomization.yaml
|
||||
│ ├── Makefile
|
||||
│ ├── pvc.yaml
|
||||
│ ├── README.md
|
||||
│ ├── service.yaml
|
||||
│ └── statefulset.yaml
|
||||
├── network/ # Firewall configuration
|
||||
│ ├── main.tf
|
||||
│ ├── variables.tf
|
||||
│ └── terraform.tfvars.example
|
||||
├── postgres/ # PostgreSQL database instances
|
||||
│ ├── main.tf
|
||||
│ ├── variables.tf
|
||||
@@ -32,10 +44,6 @@ infra/
|
||||
│ ├── main.tf
|
||||
│ ├── variables.tf
|
||||
│ └── terraform.tfvars.example
|
||||
├── network/ # Firewall configuration
|
||||
│ ├── main.tf
|
||||
│ ├── variables.tf
|
||||
│ └── terraform.tfvars.example
|
||||
├── main.tf # Orchestration file
|
||||
├── providers.tf # Provider configurations
|
||||
├── variables.tf # Common variables
|
||||
@@ -43,7 +51,8 @@ infra/
|
||||
├── .gitignore # Terraform ignores
|
||||
├── .env.example # Environment variable template
|
||||
├── Makefile # Common commands
|
||||
└── README.md # Infrastructure documentation
|
||||
├── README.md # Infrastructure documentation
|
||||
└── IMPLEMENTATION-SUMMARY.md # This file
|
||||
```
|
||||
|
||||
## Implementation Details
|
||||
@@ -60,7 +69,19 @@ infra/
|
||||
- Separate database names (`hapi_staging`, `hapi_production`)
|
||||
- Random password generation for database access
|
||||
|
||||
### 3. Object Storage (`infra/storage/`)
|
||||
### 2.5. Kubernetes (`infra/k8s/`) (New)
|
||||
- PostgreSQL StatefulSet with 50Gi persistent storage
|
||||
- ClusterIP Service for internal access
|
||||
- Kustomize for environment-specific customization
|
||||
- Health checks and resource limits configured
|
||||
|
||||
### 4. Kubernetes (`infra/k8s/`) (New)
|
||||
- PostgreSQL StatefulSet with 50Gi persistent storage
|
||||
- ClusterIP Service for internal access
|
||||
- Kustomize for environment-specific customization
|
||||
- Health checks and resource limits configured
|
||||
|
||||
### 5. Object Storage (`infra/storage/`)
|
||||
- Upload buckets for staging and production
|
||||
- Terraform state buckets for each environment
|
||||
- S3-compatible API configuration
|
||||
|
||||
+14
-1
@@ -17,9 +17,10 @@ Key differences in v2:
|
||||
```
|
||||
infra/
|
||||
├── backend/ # Terraform state backend (Object Storage S3-compatible)
|
||||
├── k8s/ # Kubernetes manifests for PostgreSQL StatefulSet
|
||||
├── network/ # Firewall rules
|
||||
├── postgres/ # PostgreSQL database instances (LKE clusters)
|
||||
├── storage/ # Object Storage buckets
|
||||
├── network/ # Firewall rules
|
||||
├── main.tf # Main orchestration file
|
||||
├── providers.tf # Provider configurations
|
||||
├── variables.tf # Common variables
|
||||
@@ -87,6 +88,18 @@ terraform apply -var="environment=production"
|
||||
|
||||
## Resources Provisioned
|
||||
|
||||
### Kubernetes (New)
|
||||
- StatefulSet for PostgreSQL with persistent storage
|
||||
- PersistentVolumeClaim for 50Gi of block storage
|
||||
- Service for internal cluster access
|
||||
- Kustomize for environment-specific customization
|
||||
|
||||
### Kubernetes (New)
|
||||
- StatefulSet for PostgreSQL with persistent storage
|
||||
- PersistentVolumeClaim for 50Gi of block storage
|
||||
- Service for internal cluster access
|
||||
- Kustomize for environment-specific customization
|
||||
|
||||
### PostgreSQL
|
||||
- LKE (Linode Kubernetes Engine) cluster for staging and production
|
||||
- Database instances within clusters (`hapi_staging`, `hapi_production`)
|
||||
|
||||
@@ -4,9 +4,9 @@ terraform {
|
||||
backend "s3" {
|
||||
# State stored in Object Storage
|
||||
bucket = "hapi-terraform-state-${var.environment}"
|
||||
key = "hapi/terraform.tfstate"
|
||||
region = var.linode_object_storage_cluster
|
||||
endpoint = "${var.linode_object_storage_cluster}.linodeobjects.com"
|
||||
key = "hapi/terraform-${var.environment}.tfstate"
|
||||
region = var.linode_object_storage_region
|
||||
endpoint = "${var.linode_object_storage_region}.linodeobjects.com"
|
||||
access_key = var.object_storage_access_key
|
||||
secret_key = var.object_storage_secret_key
|
||||
skip_region = true
|
||||
|
||||
@@ -10,8 +10,8 @@ variable "environment" {
|
||||
}
|
||||
}
|
||||
|
||||
variable "linode_object_storage_cluster" {
|
||||
description = "Linode Object Storage cluster for backend state storage"
|
||||
variable "linode_object_storage_region" {
|
||||
description = "Linode Object Storage region for backend state storage"
|
||||
type = string
|
||||
default = "us-east-1"
|
||||
}
|
||||
|
||||
Executable
+53
@@ -0,0 +1,53 @@
|
||||
#!/bin/bash
|
||||
# Script to get kubeconfig from Terraform or Linode CLI
|
||||
# Usage: ./get-kubeconfig.sh [environment]
|
||||
|
||||
set -e
|
||||
|
||||
ENVIRONMENT="${1:-staging}"
|
||||
OUTPUT_FILE="${2:-/home/bobby/.kube/config}"
|
||||
|
||||
echo "Getting kubeconfig for $ENVIRONMENT environment..."
|
||||
|
||||
# Check if Terraform is available and state exists
|
||||
if command -v terraform &> /dev/null && [ -f "infra/terraform.tfstate" ]; then
|
||||
echo "📦 Getting kubeconfig from Terraform state..."
|
||||
|
||||
# Get kubeconfig from postgres module output
|
||||
KUBECONFIG=$(terraform output -json | jq -r '.postgres.value.kubeconfig // empty')
|
||||
|
||||
if [ -n "$KUBECONFIG" ]; then
|
||||
echo "$KUBECONFIG" > "$OUTPUT_FILE"
|
||||
echo "✅ Kubeconfig saved to $OUTPUT_FILE"
|
||||
else
|
||||
echo "⚠️ No kubeconfig found in Terraform state"
|
||||
echo "You may need to run 'terraform apply' first to get the cluster kubeconfig"
|
||||
fi
|
||||
elif command -v linode-cli &> /dev/null; then
|
||||
echo "📦 Getting kubeconfig from Linode CLI..."
|
||||
|
||||
# List clusters to find the right one
|
||||
CLUSTERS=$(linode lke clusters-list --json)
|
||||
CLUSTER_ID=$(echo "$CLUSTERS" | jq -r ".[] | select(.label | contains(\"$ENVIRONMENT\")) | .id" 2>/dev/null | head -1)
|
||||
|
||||
if [ -n "$CLUSTER_ID" ]; then
|
||||
echo "Found cluster $CLUSTER_ID"
|
||||
linode lke cluster-kubeconfig "$CLUSTER_ID" --raw > "$OUTPUT_FILE"
|
||||
echo "✅ Kubeconfig saved to $OUTPUT_FILE"
|
||||
else
|
||||
echo "⚠️ No Linode cluster found for $ENVIRONMENT"
|
||||
echo "Available clusters:"
|
||||
linode lke clusters
|
||||
fi
|
||||
else
|
||||
echo "ℹ️ No Terraform or Linode CLI found"
|
||||
echo "Please manually get your kubeconfig from the Linode Dashboard:"
|
||||
echo "1. Go to Kubernetes in Linode Dashboard"
|
||||
echo "2. Select your cluster (hapi-$ENVIRONMENT)"
|
||||
echo "3. Click 'Kubeconfig' tab and copy the config"
|
||||
echo "4. Save to $OUTPUT_FILE"
|
||||
fi
|
||||
|
||||
echo ""
|
||||
echo "Verify your setup with:"
|
||||
echo " kubectl cluster-info"
|
||||
@@ -0,0 +1,66 @@
|
||||
# Makefile for PostgreSQL Kubernetes deployment
|
||||
# Provides convenient commands for deploying and managing PostgreSQL
|
||||
|
||||
SHELL := /bin/bash
|
||||
ENVIRONMENT ?= staging
|
||||
POSTGRES_DB ?= hapi_$(ENVIRONMENT)
|
||||
|
||||
# Colors for output
|
||||
GREEN := \033[0;32m
|
||||
YELLOW := \033[0;33m
|
||||
RED := \033[0;31m
|
||||
NC := \033[0m
|
||||
|
||||
NAMESPACE ?= postgres
|
||||
|
||||
.PHONY: all apply delete status logs test psql
|
||||
|
||||
all: apply
|
||||
|
||||
## Apply the Kubernetes configuration
|
||||
apply:
|
||||
@echo "$(GREEN)Applying PostgreSQL configuration for $(ENVIRONMENT)...$(NC)"
|
||||
kubectl apply -k . --namespace=$(NAMESPACE) --context=$(CLUSTER_CONTEXT)
|
||||
|
||||
## Delete the PostgreSQL deployment
|
||||
delete:
|
||||
@echo "$(RED)Deleting PostgreSQL deployment for $(ENVIRONMENT)...$(NC)"
|
||||
kubectl delete -k . --namespace=$(NAMESPACE) --context=$(CLUSTER_CONTEXT)
|
||||
|
||||
## Show current status of all resources
|
||||
status:
|
||||
@echo "$(YELLOW)PostgreSQL Status for $(ENVIRONMENT):$(NC)"
|
||||
@echo "StatefulSet:"
|
||||
kubectl get statefulset postgres-statefulset --namespace=$(NAMESPACE)
|
||||
@echo ""
|
||||
@echo "Pods:"
|
||||
kubectl get pod -l app=postgres --namespace=$(NAMESPACE)
|
||||
@echo ""
|
||||
@echo "PVC:"
|
||||
kubectl get pvc postgres-pvc --namespace=$(NAMESPACE)
|
||||
@echo ""
|
||||
@echo "Service:"
|
||||
kubectl get service postgres-service --namespace=$(NAMESPACE)
|
||||
|
||||
## Show PostgreSQL logs
|
||||
logs:
|
||||
@echo "$(YELLOW)PostgreSQL Logs for $(ENVIRONMENT):$(NC)"
|
||||
kubectl logs -l app=postgres --namespace=$(NAMESPACE) --tail=100
|
||||
|
||||
## Test PostgreSQL connectivity
|
||||
test:
|
||||
@echo "$(GREEN)Testing PostgreSQL connectivity...$(NC)"
|
||||
POD_NAME=$$(kubectl get pod -l app=postgres -n $(NAMESPACE) -o jsonpath='{.items[0].metadata.name}') && \
|
||||
kubectl exec -it $${POD_NAME} -n $(NAMESPACE) -- pg_isready -U postgres -d $(POSTGRES_DB)
|
||||
|
||||
## Run psql shell in PostgreSQL pod
|
||||
psql:
|
||||
@echo "$(YELLOW)Opening psql shell...$(NC)"
|
||||
kubectl exec -it -l app=postgres -n $(NAMESPACE) -- psql -U postgres -d $(POSTGRES_DB)
|
||||
|
||||
## Show help
|
||||
help:
|
||||
@echo "$(GREEN)PostgreSQL Kubernetes Deployment$(NC)"
|
||||
@echo ""
|
||||
@echo "Available targets:"
|
||||
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf " $(YELLOW)%-15s$(NC) %s\n", $$1, $$2}'
|
||||
@@ -0,0 +1,71 @@
|
||||
# PostgreSQL Kubernetes Deployments
|
||||
|
||||
This directory contains Kustomize configurations for deploying PostgreSQL to different environments.
|
||||
|
||||
## Directory Structure
|
||||
|
||||
```
|
||||
k8s/
|
||||
├── base/ # Base resources shared across environments
|
||||
│ ├── kustomization.yaml
|
||||
│ ├── namespace.yaml
|
||||
│ ├── statefulset.yaml
|
||||
│ ├── service.yaml
|
||||
│ └── pvc.yaml
|
||||
├── staging/ # Staging environment configuration
|
||||
│ └── kustomization.yaml
|
||||
├── production/ # Production environment configuration
|
||||
│ └── kustomization.yaml
|
||||
└── README.md
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
### Staging Environment
|
||||
|
||||
```bash
|
||||
kubectl apply -k infra/k8s/staging
|
||||
```
|
||||
|
||||
### Production Environment
|
||||
|
||||
```bash
|
||||
kubectl apply -k infra/k8s/production
|
||||
```
|
||||
|
||||
### Dry Run
|
||||
|
||||
```bash
|
||||
kubectl apply -k infra/k8s/staging --dry-run=client
|
||||
```
|
||||
|
||||
## Resource Naming
|
||||
|
||||
Resources are prefixed with the environment name:
|
||||
|
||||
- **Staging**: `staging-postgres-*`
|
||||
- **Production**: `production-postgres-*`
|
||||
|
||||
## Environment Variables
|
||||
|
||||
The `ENVIRONMENT` variable is embedded in ConfigMaps:
|
||||
|
||||
- **Staging**: `ENVIRONMENT=staging`
|
||||
- **Production**: `ENVIRONMENT=production`
|
||||
|
||||
## Secrets
|
||||
|
||||
The following secrets are expected in the `postgres` namespace:
|
||||
|
||||
- `postgres-secrets` with keys:
|
||||
- `username`: PostgreSQL username
|
||||
- `password`: PostgreSQL password
|
||||
|
||||
## Configuration
|
||||
|
||||
Each environment's `kustomization.yaml` customizes:
|
||||
|
||||
- `namePrefix`: Environment-specific prefix
|
||||
- `commonLabels`: Environment label applied to all resources
|
||||
- `patches`: Adds environment label to Service, PVC, and StatefulSet
|
||||
- `configMapGenerator`: Creates ConfigMap with `ENVIRONMENT` value
|
||||
@@ -0,0 +1,265 @@
|
||||
# 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
|
||||
Executable
+17
@@ -0,0 +1,17 @@
|
||||
#!/bin/bash
|
||||
# Wrapper script for applying kustomize with environment variable substitution
|
||||
|
||||
set -e
|
||||
|
||||
# Use ENVIRONMENT from environment or default to 'staging'
|
||||
ENVIRONMENT="${ENVIRONMENT:-staging}"
|
||||
export ENVIRONMENT
|
||||
|
||||
# Set POSTGRES_DB if not set
|
||||
POSTGRES_DB="${POSTGRES_DB:-hapi_${ENVIRONMENT}}"
|
||||
export POSTGRES_DB
|
||||
|
||||
# Run kubectl apply with environment variables substituted
|
||||
envsubst '${ENVIRONMENT} ${POSTGRES_DB}' < kustomization.yaml > kustomization.subst.yaml
|
||||
kubectl apply -f kustomization.subst.yaml --dry-run=client
|
||||
rm kustomization.subst.yaml
|
||||
@@ -0,0 +1,10 @@
|
||||
# Base Kustomize configuration for PostgreSQL
|
||||
|
||||
apiVersion: kustomize.config.k8s.io/v1beta1
|
||||
kind: Kustomization
|
||||
|
||||
resources:
|
||||
- namespace.yaml
|
||||
- statefulset.yaml
|
||||
- service.yaml
|
||||
- pvc.yaml
|
||||
@@ -0,0 +1,10 @@
|
||||
# Namespace for PostgreSQL resources
|
||||
apiVersion: v1
|
||||
kind: Namespace
|
||||
metadata:
|
||||
name: postgres
|
||||
labels:
|
||||
app: postgres
|
||||
managedBy: terraform
|
||||
annotations:
|
||||
description: "Namespace for PostgreSQL StatefulSet and related resources"
|
||||
@@ -0,0 +1,19 @@
|
||||
# Standalone PersistentVolumeClaim for PostgreSQL
|
||||
# Note: This PVC is separate from the StatefulSet's volumeClaimTemplates.
|
||||
# It is currently unused in the active deployment but can be used for
|
||||
# backup, migration, or testing purposes. The primary storage is provided
|
||||
# by volumeClaimTemplates in statefulset.yaml.
|
||||
|
||||
apiVersion: v1
|
||||
kind: PersistentVolumeClaim
|
||||
metadata:
|
||||
name: postgres-pvc
|
||||
labels:
|
||||
app: postgres
|
||||
spec:
|
||||
accessModes:
|
||||
- ReadWriteOnce
|
||||
storageClassName: linode-block-storage
|
||||
resources:
|
||||
requests:
|
||||
storage: 50Gi
|
||||
@@ -0,0 +1,17 @@
|
||||
# Service for PostgreSQL StatefulSet
|
||||
# Provides stable network identity and load balancing
|
||||
|
||||
apiVersion: v1
|
||||
kind: Service
|
||||
metadata:
|
||||
name: postgres-service
|
||||
labels:
|
||||
app: postgres
|
||||
spec:
|
||||
type: ClusterIP
|
||||
ports:
|
||||
- port: 5432
|
||||
targetPort: 5432
|
||||
name: postgres
|
||||
selector:
|
||||
app: postgres
|
||||
@@ -0,0 +1,90 @@
|
||||
# PostgreSQL StatefulSet for HAPI
|
||||
# Provides production-ready PostgreSQL with persistent storage
|
||||
|
||||
apiVersion: apps/v1
|
||||
kind: StatefulSet
|
||||
metadata:
|
||||
name: postgres-statefulset
|
||||
labels:
|
||||
app: postgres
|
||||
spec:
|
||||
serviceName: postgres-service
|
||||
replicas: 1
|
||||
selector:
|
||||
matchLabels:
|
||||
app: postgres
|
||||
template:
|
||||
metadata:
|
||||
labels:
|
||||
app: postgres
|
||||
spec:
|
||||
securityContext:
|
||||
fsGroup: 999
|
||||
containers:
|
||||
- name: postgres
|
||||
image: postgres:16.3
|
||||
ports:
|
||||
- containerPort: 5432
|
||||
name: postgres
|
||||
env:
|
||||
- name: POSTGRES_USER
|
||||
valueFrom:
|
||||
secretKeyRef:
|
||||
name: postgres-secrets
|
||||
key: username
|
||||
- name: POSTGRES_PASSWORD
|
||||
valueFrom:
|
||||
secretKeyRef:
|
||||
name: postgres-secrets
|
||||
key: password
|
||||
- name: POSTGRES_DB
|
||||
value: ${POSTGRES_DB}
|
||||
- name: PGDATA
|
||||
value: /var/lib/postgresql/data/pgdata
|
||||
volumeMounts:
|
||||
- name: postgres-data
|
||||
mountPath: /var/lib/postgresql/data
|
||||
- name: postgres-config
|
||||
mountPath: /etc/postgresql/postgresql.conf
|
||||
subPath: postgresql.conf
|
||||
resources:
|
||||
requests:
|
||||
cpu: "500m"
|
||||
memory: "512Mi"
|
||||
limits:
|
||||
cpu: "1000m"
|
||||
memory: "1Gi"
|
||||
securityContext:
|
||||
runAsUser: 999
|
||||
runAsNonRoot: true
|
||||
readinessProbe:
|
||||
exec:
|
||||
command:
|
||||
- pg_isready
|
||||
- -U
|
||||
- postgres
|
||||
initialDelaySeconds: 5
|
||||
periodSeconds: 10
|
||||
livenessProbe:
|
||||
exec:
|
||||
command:
|
||||
- pg_isready
|
||||
- -U
|
||||
- postgres
|
||||
initialDelaySeconds: 15
|
||||
periodSeconds: 20
|
||||
volumes:
|
||||
- name: postgres-data
|
||||
persistentVolumeClaim:
|
||||
claimName: postgres-data
|
||||
- name: postgres-config
|
||||
configMap:
|
||||
name: postgres-config
|
||||
volumeClaimTemplates:
|
||||
- metadata:
|
||||
name: postgres-data
|
||||
spec:
|
||||
accessModes: ["ReadWriteOnce"]
|
||||
resources:
|
||||
requests:
|
||||
storage: 10Gi
|
||||
@@ -0,0 +1,224 @@
|
||||
# PostgreSQL StatefulSet for HAPI
|
||||
# Provides production-ready PostgreSQL with persistent storage
|
||||
|
||||
resource "kubernetes_stateful_set_v1" "postgres" {
|
||||
metadata {
|
||||
name = "postgres-statefulset"
|
||||
namespace = "postgres"
|
||||
labels = {
|
||||
app = "postgres"
|
||||
environment = var.environment
|
||||
}
|
||||
}
|
||||
|
||||
spec {
|
||||
service_name = "postgres-service"
|
||||
replicas = 1
|
||||
|
||||
selector {
|
||||
match_labels = {
|
||||
app = "postgres"
|
||||
environment = var.environment
|
||||
}
|
||||
}
|
||||
|
||||
template {
|
||||
metadata {
|
||||
labels = {
|
||||
app = "postgres"
|
||||
environment = var.environment
|
||||
}
|
||||
}
|
||||
|
||||
spec {
|
||||
container {
|
||||
name = "postgres"
|
||||
image = var.postgres_image
|
||||
|
||||
port {
|
||||
container_port = 5432
|
||||
name = "postgres"
|
||||
}
|
||||
|
||||
env {
|
||||
name = "POSTGRES_USER"
|
||||
|
||||
value_from {
|
||||
secret_key_ref {
|
||||
name = "postgres-secrets"
|
||||
key = "username"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
env {
|
||||
name = "POSTGRES_PASSWORD"
|
||||
|
||||
value_from {
|
||||
secret_key_ref {
|
||||
name = "postgres-secrets"
|
||||
key = "password"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
env {
|
||||
name = "POSTGRES_DB"
|
||||
value = var.postgres_db
|
||||
}
|
||||
|
||||
env {
|
||||
name = "PGDATA"
|
||||
value = "/var/lib/postgresql/data/pgdata"
|
||||
}
|
||||
|
||||
volume_mount {
|
||||
name = "postgres-data"
|
||||
mount_path = "/var/lib/postgresql/data"
|
||||
}
|
||||
|
||||
volume_mount {
|
||||
name = "postgres-config"
|
||||
mount_path = "/etc/postgresql/postgresql.conf"
|
||||
sub_path = "postgresql.conf"
|
||||
}
|
||||
|
||||
resources {
|
||||
requests = {
|
||||
cpu = var.cpu_request
|
||||
memory = var.memory_request
|
||||
}
|
||||
|
||||
limits = {
|
||||
cpu = var.cpu_limit
|
||||
memory = var.memory_limit
|
||||
}
|
||||
}
|
||||
|
||||
readiness_probe {
|
||||
exec {
|
||||
command = [
|
||||
"pg_isready",
|
||||
"-U",
|
||||
"postgres",
|
||||
"-d",
|
||||
var.postgres_db,
|
||||
]
|
||||
}
|
||||
|
||||
initial_delay_seconds = 30
|
||||
period_seconds = 10
|
||||
}
|
||||
|
||||
liveness_probe {
|
||||
exec {
|
||||
command = [
|
||||
"pg_isready",
|
||||
"-U",
|
||||
"postgres",
|
||||
"-d",
|
||||
var.postgres_db,
|
||||
]
|
||||
}
|
||||
|
||||
initial_delay_seconds = 60
|
||||
period_seconds = 10
|
||||
}
|
||||
|
||||
security_context {
|
||||
run_as_user = 999
|
||||
run_as_group = 999
|
||||
fs_group = 999
|
||||
}
|
||||
}
|
||||
|
||||
volume {
|
||||
name = "postgres-data"
|
||||
|
||||
persistent_volume_claim {
|
||||
claim_name = "postgres-data"
|
||||
}
|
||||
}
|
||||
|
||||
volume {
|
||||
name = "postgres-config"
|
||||
|
||||
config_map {
|
||||
name = "postgres-config"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
volume_claim_template {
|
||||
metadata {
|
||||
name = "postgres-data"
|
||||
}
|
||||
|
||||
spec {
|
||||
access_modes = ["ReadWriteOnce"]
|
||||
|
||||
storage_class_name = "linode-block-storage"
|
||||
|
||||
resources {
|
||||
requests = {
|
||||
storage = var.storage_size
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pod_management_policy = "Parallel"
|
||||
}
|
||||
|
||||
# PostgreSQL Service
|
||||
resource "kubernetes_service_v1" "postgres" {
|
||||
metadata {
|
||||
name = "postgres-service"
|
||||
namespace = "postgres"
|
||||
labels = {
|
||||
app = "postgres"
|
||||
environment = var.environment
|
||||
}
|
||||
}
|
||||
|
||||
spec {
|
||||
selector = {
|
||||
app = "postgres"
|
||||
environment = var.environment
|
||||
}
|
||||
|
||||
port {
|
||||
name = "postgres"
|
||||
port = 5432
|
||||
target_port = 5432
|
||||
}
|
||||
|
||||
type = "ClusterIP"
|
||||
}
|
||||
}
|
||||
|
||||
# PersistentVolumeClaim for PostgreSQL
|
||||
resource "kubernetes_persistent_volume_claim_v1" "postgres" {
|
||||
metadata {
|
||||
name = "postgres-pvc"
|
||||
namespace = "postgres"
|
||||
labels = {
|
||||
app = "postgres"
|
||||
environment = var.environment
|
||||
}
|
||||
}
|
||||
|
||||
spec {
|
||||
access_modes = ["ReadWriteOnce"]
|
||||
|
||||
storage_class_name = "linode-block-storage"
|
||||
|
||||
resources {
|
||||
requests = {
|
||||
storage = var.storage_size
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
# Kustomize configuration for PostgreSQL Kubernetes resources
|
||||
# Enables environment-specific customization and easy deployment
|
||||
|
||||
apiVersion: kustomize.config.k8s.io/v1beta1
|
||||
kind: Kustomization
|
||||
|
||||
resources:
|
||||
- namespace.yaml
|
||||
- pvc.yaml
|
||||
- service.yaml
|
||||
- statefulset.yaml
|
||||
|
||||
commonLabels:
|
||||
app: postgres
|
||||
managedBy: terraform
|
||||
|
||||
commonAnnotations:
|
||||
description: "PostgreSQL StatefulSet for HAPI"
|
||||
|
||||
# Environment-specific configurations
|
||||
namePrefix: staging-
|
||||
|
||||
# Environment variable ConfigMap for substitutions
|
||||
configMapGenerator:
|
||||
- name: env-vars
|
||||
literals:
|
||||
- ENVIRONMENT=${ENVIRONMENT:-staging}
|
||||
|
||||
# Apply environment label to resources
|
||||
patches:
|
||||
- target:
|
||||
kind: Service
|
||||
patch: |-
|
||||
- op: add
|
||||
path: /metadata/labels/environment
|
||||
valueFrom:
|
||||
configMapKeyRef:
|
||||
name: env-vars
|
||||
key: ENVIRONMENT
|
||||
- op: add
|
||||
path: /spec/selector/environment
|
||||
valueFrom:
|
||||
configMapKeyRef:
|
||||
name: env-vars
|
||||
key: ENVIRONMENT
|
||||
- target:
|
||||
kind: PersistentVolumeClaim
|
||||
patch: |-
|
||||
- op: add
|
||||
path: /metadata/labels/environment
|
||||
valueFrom:
|
||||
configMapKeyRef:
|
||||
name: env-vars
|
||||
key: ENVIRONMENT
|
||||
- target:
|
||||
kind: StatefulSet
|
||||
patch: |-
|
||||
- op: add
|
||||
path: /metadata/labels/environment
|
||||
valueFrom:
|
||||
configMapKeyRef:
|
||||
name: env-vars
|
||||
key: ENVIRONMENT
|
||||
- op: add
|
||||
path: /spec/selector/matchLabels/environment
|
||||
valueFrom:
|
||||
configMapKeyRef:
|
||||
name: env-vars
|
||||
key: ENVIRONMENT
|
||||
- op: add
|
||||
path: /spec/template/metadata/labels/environment
|
||||
valueFrom:
|
||||
configMapKeyRef:
|
||||
name: env-vars
|
||||
key: ENVIRONMENT
|
||||
|
||||
# Secrets can be injected via external Secret Manager or inline
|
||||
# See docs/adr/0003-secrets-management.md for details
|
||||
@@ -0,0 +1,78 @@
|
||||
# Kustomize configuration for PostgreSQL Kubernetes resources
|
||||
# Enables environment-specific customization and easy deployment
|
||||
|
||||
apiVersion: kustomize.config.k8s.io/v1beta1
|
||||
kind: Kustomization
|
||||
|
||||
resources:
|
||||
- namespace.yaml
|
||||
- pvc.yaml
|
||||
- service.yaml
|
||||
- statefulset.yaml
|
||||
|
||||
commonLabels:
|
||||
app: postgres
|
||||
managedBy: terraform
|
||||
|
||||
commonAnnotations:
|
||||
description: "PostgreSQL StatefulSet for HAPI"
|
||||
|
||||
# Environment-specific configurations
|
||||
namePrefix: ${ENVIRONMENT}-
|
||||
|
||||
# Environment variable ConfigMap for substitutions
|
||||
configMapGenerator:
|
||||
- name: env-vars
|
||||
literals:
|
||||
- ENVIRONMENT=${ENVIRONMENT:-staging}
|
||||
|
||||
# Apply environment label to resources
|
||||
patches:
|
||||
- target:
|
||||
kind: Service
|
||||
patch: |-
|
||||
- op: add
|
||||
path: /metadata/labels/environment
|
||||
valueFrom:
|
||||
configMapKeyRef:
|
||||
name: env-vars
|
||||
key: ENVIRONMENT
|
||||
- op: add
|
||||
path: /spec/selector/environment
|
||||
valueFrom:
|
||||
configMapKeyRef:
|
||||
name: env-vars
|
||||
key: ENVIRONMENT
|
||||
- target:
|
||||
kind: PersistentVolumeClaim
|
||||
patch: |-
|
||||
- op: add
|
||||
path: /metadata/labels/environment
|
||||
valueFrom:
|
||||
configMapKeyRef:
|
||||
name: env-vars
|
||||
key: ENVIRONMENT
|
||||
- target:
|
||||
kind: StatefulSet
|
||||
patch: |-
|
||||
- op: add
|
||||
path: /metadata/labels/environment
|
||||
valueFrom:
|
||||
configMapKeyRef:
|
||||
name: env-vars
|
||||
key: ENVIRONMENT
|
||||
- op: add
|
||||
path: /spec/selector/matchLabels/environment
|
||||
valueFrom:
|
||||
configMapKeyRef:
|
||||
name: env-vars
|
||||
key: ENVIRONMENT
|
||||
- op: add
|
||||
path: /spec/template/metadata/labels/environment
|
||||
valueFrom:
|
||||
configMapKeyRef:
|
||||
name: env-vars
|
||||
key: ENVIRONMENT
|
||||
|
||||
# Secrets can be injected via external Secret Manager or inline
|
||||
# See docs/adr/0003-secrets-management.md for details
|
||||
@@ -0,0 +1,53 @@
|
||||
# PostgreSQL Kubernetes module for HAPI
|
||||
# Manages Kubernetes resources for PostgreSQL deployment
|
||||
|
||||
terraform {
|
||||
required_providers {
|
||||
kubernetes = {
|
||||
source = "hashicorp/kubernetes"
|
||||
version = "~> 2.0"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
# Data source to get Kubernetes cluster info
|
||||
data "kubernetes_namespace" "postgres" {
|
||||
metadata {
|
||||
name = "postgres"
|
||||
}
|
||||
}
|
||||
|
||||
# Kubernetes resources
|
||||
module "k8s_resources" {
|
||||
source = "./k8s"
|
||||
|
||||
environment = var.environment
|
||||
postgres_image = var.postgres_image
|
||||
postgres_db = var.postgres_db
|
||||
storage_size = var.storage_size
|
||||
cpu_request = var.cpu_request
|
||||
cpu_limit = var.cpu_limit
|
||||
memory_request = var.memory_request
|
||||
memory_limit = var.memory_limit
|
||||
}
|
||||
|
||||
# Outputs
|
||||
output "statefulset_name" {
|
||||
description = "Name of the PostgreSQL StatefulSet"
|
||||
value = module.k8s_resources.statefulset_name
|
||||
}
|
||||
|
||||
output "service_name" {
|
||||
description = "Name of the PostgreSQL Service"
|
||||
value = module.k8s_resources.service_name
|
||||
}
|
||||
|
||||
output "service_endpoint" {
|
||||
description = "Endpoint for the PostgreSQL Service"
|
||||
value = module.k8s_resources.service_endpoint
|
||||
}
|
||||
|
||||
output "pvc_name" {
|
||||
description = "Name of the PostgreSQL PVC"
|
||||
value = module.k8s_resources.pvc_name
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
# Outputs for PostgreSQL Kubernetes module
|
||||
|
||||
output "statefulset_name" {
|
||||
description = "Name of the PostgreSQL StatefulSet"
|
||||
value = kubernetes_stateful_set_v1.postgres.metadata[0].name
|
||||
}
|
||||
|
||||
output "service_name" {
|
||||
description = "Name of the PostgreSQL Service"
|
||||
value = kubernetes_service_v1.postgres.metadata[0].name
|
||||
}
|
||||
|
||||
output "service_endpoint" {
|
||||
description = "Endpoint for the PostgreSQL Service"
|
||||
value = kubernetes_service_v1.postgres.status[0].load_balancer[0].ingress[0].ip
|
||||
}
|
||||
|
||||
output "pvc_name" {
|
||||
description = "Name of the PostgreSQL PVC"
|
||||
value = kubernetes_persistent_volume_claim_v1.postgres.metadata[0].name
|
||||
}
|
||||
|
||||
output "namespace" {
|
||||
description = "Namespace where PostgreSQL resources are deployed"
|
||||
value = "postgres"
|
||||
}
|
||||
|
||||
output "postgres_image" {
|
||||
description = "PostgreSQL Docker image used"
|
||||
value = var.postgres_image
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
# Kustomize configuration for PostgreSQL in production environment
|
||||
|
||||
apiVersion: kustomize.config.k8s.io/v1beta1
|
||||
kind: Kustomization
|
||||
|
||||
resources:
|
||||
- ../base
|
||||
|
||||
namespace: postgres
|
||||
|
||||
commonLabels:
|
||||
environment: production
|
||||
app: postgres
|
||||
managedBy: terraform
|
||||
|
||||
commonAnnotations:
|
||||
description: "PostgreSQL StatefulSet for production environment"
|
||||
|
||||
namePrefix: production-
|
||||
|
||||
configMapGenerator:
|
||||
- name: env-vars
|
||||
literals:
|
||||
- ENVIRONMENT=production
|
||||
|
||||
patches:
|
||||
- target:
|
||||
kind: Service
|
||||
patch: |-
|
||||
- op: add
|
||||
path: /metadata/labels/environment
|
||||
value: production
|
||||
- op: add
|
||||
path: /spec/selector/environment
|
||||
value: production
|
||||
- target:
|
||||
kind: PersistentVolumeClaim
|
||||
patch: |-
|
||||
- op: add
|
||||
path: /metadata/labels/environment
|
||||
value: production
|
||||
- target:
|
||||
kind: StatefulSet
|
||||
patch: |-
|
||||
- op: add
|
||||
path: /metadata/labels/environment
|
||||
value: production
|
||||
- op: add
|
||||
path: /spec/selector/matchLabels/environment
|
||||
value: production
|
||||
- op: add
|
||||
path: /spec/template/metadata/labels/environment
|
||||
value: production
|
||||
Executable
+53
@@ -0,0 +1,53 @@
|
||||
#!/bin/bash
|
||||
# Quickstart script for testing PostgreSQL k8s deployment
|
||||
# Usage: ./quickstart.sh [environment]
|
||||
|
||||
set -e
|
||||
|
||||
ENVIRONMENT="${1:-staging}"
|
||||
NAMESPACE="postgres"
|
||||
|
||||
echo "🚀 Starting PostgreSQL k8s deployment for $ENVIRONMENT..."
|
||||
|
||||
# Set environment variable
|
||||
export ENVIRONMENT
|
||||
export NAMESPACE
|
||||
|
||||
# Create namespace if not exists
|
||||
echo "📝 Creating namespace $NAMESPACE..."
|
||||
kubectl create namespace "$NAMESPACE" --dry-run=client -o yaml | kubectl apply -f -
|
||||
|
||||
# Apply Kustomize configuration
|
||||
echo "🛠 Applying Kustomize configuration..."
|
||||
kubectl apply -k .
|
||||
|
||||
# Wait for StatefulSet
|
||||
echo "⏳ Waiting for StatefulSet to be ready..."
|
||||
kubectl wait --for=condition=Ready pod -l app=postgres -n "$NAMESPACE" --timeout=300s
|
||||
|
||||
# Show status
|
||||
echo "📊 Deployment status:"
|
||||
echo ""
|
||||
echo "StatefulSet:"
|
||||
kubectl get statefulset postgres-statefulset -n "$NAMESPACE"
|
||||
echo ""
|
||||
echo "Pods:"
|
||||
kubectl get pod -l app=postgres -n "$NAMESPACE"
|
||||
echo ""
|
||||
echo "PVC:"
|
||||
kubectl get pvc -n "$NAMESPACE"
|
||||
echo ""
|
||||
echo "Service:"
|
||||
kubectl get service postgres-service -n "$NAMESPACE"
|
||||
|
||||
# Test connectivity
|
||||
echo ""
|
||||
echo "🔌 Testing PostgreSQL connectivity..."
|
||||
POD_NAME=$(kubectl get pod -l app=postgres -n "$NAMESPACE" -o jsonpath='{.items[0].metadata.name}')
|
||||
kubectl exec -n "$NAMESPACE" "$POD_NAME" -- pg_isready -U postgres
|
||||
|
||||
echo ""
|
||||
echo "✅ PostgreSQL deployment complete!"
|
||||
echo ""
|
||||
echo "To connect to PostgreSQL:"
|
||||
echo " kubectl exec -n $NAMESPACE -l app=postgres -- psql -U postgres"
|
||||
@@ -0,0 +1,53 @@
|
||||
# Kustomize configuration for PostgreSQL in staging environment
|
||||
|
||||
apiVersion: kustomize.config.k8s.io/v1beta1
|
||||
kind: Kustomization
|
||||
|
||||
resources:
|
||||
- ../base
|
||||
|
||||
namespace: postgres
|
||||
|
||||
commonLabels:
|
||||
environment: staging
|
||||
app: postgres
|
||||
managedBy: terraform
|
||||
|
||||
commonAnnotations:
|
||||
description: "PostgreSQL StatefulSet for staging environment"
|
||||
|
||||
namePrefix: staging-
|
||||
|
||||
configMapGenerator:
|
||||
- name: env-vars
|
||||
literals:
|
||||
- ENVIRONMENT=staging
|
||||
|
||||
patches:
|
||||
- target:
|
||||
kind: Service
|
||||
patch: |-
|
||||
- op: add
|
||||
path: /metadata/labels/environment
|
||||
value: staging
|
||||
- op: add
|
||||
path: /spec/selector/environment
|
||||
value: staging
|
||||
- target:
|
||||
kind: PersistentVolumeClaim
|
||||
patch: |-
|
||||
- op: add
|
||||
path: /metadata/labels/environment
|
||||
value: staging
|
||||
- target:
|
||||
kind: StatefulSet
|
||||
patch: |-
|
||||
- op: add
|
||||
path: /metadata/labels/environment
|
||||
value: staging
|
||||
- op: add
|
||||
path: /spec/selector/matchLabels/environment
|
||||
value: staging
|
||||
- op: add
|
||||
path: /spec/template/metadata/labels/environment
|
||||
value: staging
|
||||
@@ -0,0 +1,21 @@
|
||||
# Example Terraform variables for PostgreSQL Kubernetes deployment
|
||||
|
||||
# Kubernetes cluster connection
|
||||
kube_config_path = "~/.kube/config"
|
||||
kube_context = "linode-us-east"
|
||||
|
||||
# PostgreSQL configuration
|
||||
postgres_image = "postgres:16.3"
|
||||
postgres_db = "hapi_staging"
|
||||
postgres_user = "postgres"
|
||||
postgres_password = "changeme"
|
||||
|
||||
# Resource configuration
|
||||
storage_size = "50Gi"
|
||||
cpu_request = "500m"
|
||||
cpu_limit = "1000m"
|
||||
memory_request = "512Mi"
|
||||
memory_limit = "1Gi"
|
||||
|
||||
# Environment
|
||||
environment = "staging"
|
||||
@@ -0,0 +1,58 @@
|
||||
# Variables for PostgreSQL Kubernetes module
|
||||
|
||||
variable "environment" {
|
||||
description = "Deployment environment (staging or production)"
|
||||
type = string
|
||||
validation {
|
||||
condition = contains(["staging", "production"], var.environment)
|
||||
error_message = "Environment must be 'staging' or 'production'."
|
||||
}
|
||||
}
|
||||
|
||||
variable "linode_region" {
|
||||
description = "Linode region for PostgreSQL"
|
||||
type = string
|
||||
default = "us-east"
|
||||
}
|
||||
|
||||
variable "postgres_image" {
|
||||
description = "PostgreSQL Docker image"
|
||||
type = string
|
||||
default = "postgres:16.3"
|
||||
}
|
||||
|
||||
variable "postgres_db" {
|
||||
description = "PostgreSQL database name"
|
||||
type = string
|
||||
default = "hapi_staging"
|
||||
}
|
||||
|
||||
variable "storage_size" {
|
||||
description = "Storage size for PostgreSQL PVC"
|
||||
type = string
|
||||
default = "50Gi"
|
||||
}
|
||||
|
||||
variable "cpu_request" {
|
||||
description = "CPU request for PostgreSQL pod"
|
||||
type = string
|
||||
default = "500m"
|
||||
}
|
||||
|
||||
variable "cpu_limit" {
|
||||
description = "CPU limit for PostgreSQL pod"
|
||||
type = string
|
||||
default = "1000m"
|
||||
}
|
||||
|
||||
variable "memory_request" {
|
||||
description = "Memory request for PostgreSQL pod"
|
||||
type = string
|
||||
default = "512Mi"
|
||||
}
|
||||
|
||||
variable "memory_limit" {
|
||||
description = "Memory limit for PostgreSQL pod"
|
||||
type = string
|
||||
default = "1Gi"
|
||||
}
|
||||
@@ -87,3 +87,8 @@ output "state_bucket_name" {
|
||||
description = "Name of the state bucket in Object Storage"
|
||||
value = "hapi-postgres-state-${var.environment}"
|
||||
}
|
||||
|
||||
output "kubeconfig" {
|
||||
description = "Kubeconfig for the PostgreSQL LKE cluster"
|
||||
value = linode_lke_cluster.postgres[var.environment].kubeconfig
|
||||
}
|
||||
Reference in new issue
Block a user