|
| 1 | +# PostgreSQL on Kubernetes |
| 2 | + |
| 3 | +This directory contains the Kubernetes manifests and scripts to deploy PostgreSQL on your cluster. |
| 4 | + |
| 5 | +## Quick Start |
| 6 | + |
| 7 | +1. **Set up secrets:** |
| 8 | + ```bash |
| 9 | + cp secrets.sh.example secrets.sh |
| 10 | + # Edit secrets.sh with your actual password |
| 11 | + ``` |
| 12 | + |
| 13 | +2. **Deploy PostgreSQL:** |
| 14 | + ```bash |
| 15 | + chmod +x deploy.sh |
| 16 | + ./deploy.sh |
| 17 | + ``` |
| 18 | + |
| 19 | +3. **Connect to PostgreSQL:** |
| 20 | + - From within the cluster: `psql -h postgres-service.postgres.svc.cluster.local -U postgres -d postgres` |
| 21 | + - From outside (port-forward): `kubectl port-forward service/postgres-service 5432:5432 -n postgres` |
| 22 | + |
| 23 | +4. **Clean up:** |
| 24 | + ```bash |
| 25 | + chmod +x cleanup.sh |
| 26 | + ./cleanup.sh |
| 27 | + ``` |
| 28 | + |
| 29 | +## Files |
| 30 | + |
| 31 | +- `secrets.sh.example` - Template for secrets configuration (copy to `secrets.sh`) |
| 32 | +- `secrets.sh` - Your actual secrets (gitignored, create from example) |
| 33 | +- `.gitignore` - Ensures secrets don't get committed |
| 34 | +- `postgres-pvc.yaml` - Persistent Volume Claim for data storage (10Gi) |
| 35 | +- `postgres-deployment.yaml` - PostgreSQL deployment with health checks |
| 36 | +- `postgres-service.yaml` - Service to expose PostgreSQL within the cluster |
| 37 | +- `deploy.sh` - Script to deploy all resources |
| 38 | +- `cleanup.sh` - Script to remove all resources |
| 39 | + |
| 40 | +## Configuration |
| 41 | + |
| 42 | +### Default Credentials |
| 43 | + |
| 44 | +Credentials are set in your local `secrets.sh` file: |
| 45 | +- **Host:** postgres-service.postgres.svc.cluster.local |
| 46 | +- **Port:** 5432 |
| 47 | +- **Database:** Set in `secrets.sh` (default: postgres) |
| 48 | +- **Username:** Set in `secrets.sh` (default: postgres) |
| 49 | +- **Password:** Set in `secrets.sh` |
| 50 | + |
| 51 | +⚠️ **Security Note:** Never commit `secrets.sh` to version control! |
| 52 | + |
| 53 | +### Storage |
| 54 | +- Default storage request: 10Gi |
| 55 | +- Access mode: ReadWriteOnce |
| 56 | +- You may need to specify a `storageClassName` in `postgres-pvc.yaml` depending on your cluster setup |
| 57 | + |
| 58 | +### Resource Limits |
| 59 | +- Memory: 256Mi request, 512Mi limit |
| 60 | +- CPU: 250m request, 500m limit |
| 61 | + |
| 62 | +## Customization |
| 63 | + |
| 64 | +### Changing Credentials |
| 65 | +Edit your local `secrets.sh` file: |
| 66 | +```bash |
| 67 | +export POSTGRES_PASSWORD="your-new-secure-password" |
| 68 | +export POSTGRES_DB="your-database-name" |
| 69 | +export POSTGRES_USER="your-username" |
| 70 | +``` |
| 71 | + |
| 72 | +### Storage Class |
| 73 | +If your cluster uses a specific storage class, uncomment and modify the `storageClassName` in `postgres-pvc.yaml`. |
| 74 | + |
| 75 | +### PostgreSQL Version |
| 76 | +To use a different PostgreSQL version, modify the `image` field in `postgres-deployment.yaml`: |
| 77 | +```yaml |
| 78 | +image: postgres:14 # or postgres:13, postgres:16, etc. |
| 79 | +``` |
| 80 | +
|
| 81 | +## Connection Examples |
| 82 | +
|
| 83 | +### From a Pod in the Cluster |
| 84 | +```yaml |
| 85 | +apiVersion: v1 |
| 86 | +kind: Pod |
| 87 | +metadata: |
| 88 | + name: postgres-client |
| 89 | + namespace: postgres |
| 90 | +spec: |
| 91 | + containers: |
| 92 | + - name: postgres-client |
| 93 | + image: postgres:17 |
| 94 | + command: ['sh', '-c', 'sleep 3600'] |
| 95 | + env: |
| 96 | + - name: PGPASSWORD |
| 97 | + value: "postgres123" |
| 98 | +``` |
| 99 | +
|
| 100 | +Then exec into the pod: |
| 101 | +```bash |
| 102 | +kubectl exec -it postgres-client -n postgres -- psql -h postgres-service.postgres.svc.cluster.local -U postgres -d postgres |
| 103 | +``` |
| 104 | + |
| 105 | +### Environment Variables for Applications |
| 106 | +```yaml |
| 107 | +env: |
| 108 | +- name: DATABASE_URL |
| 109 | + value: "postgresql://postgres:postgres123@postgres-service.postgres.svc.cluster.local:5432/postgres" |
| 110 | +- name: POSTGRES_HOST |
| 111 | + value: "postgres-service.postgres.svc.cluster.local" |
| 112 | +- name: POSTGRES_PORT |
| 113 | + value: "5432" |
| 114 | +- name: POSTGRES_DB |
| 115 | + value: "postgres" |
| 116 | +- name: POSTGRES_USER |
| 117 | + value: "postgres" |
| 118 | +- name: POSTGRES_PASSWORD |
| 119 | + valueFrom: |
| 120 | + secretKeyRef: |
| 121 | + name: postgres-secret |
| 122 | + key: postgres-password |
| 123 | +``` |
| 124 | +
|
| 125 | +## Troubleshooting |
| 126 | +
|
| 127 | +### Check Pod Status |
| 128 | +```bash |
| 129 | +kubectl get pods -l app=postgres -n postgres |
| 130 | +kubectl describe pod -l app=postgres -n postgres |
| 131 | +``` |
| 132 | + |
| 133 | +### Check Logs |
| 134 | +```bash |
| 135 | +kubectl logs -l app=postgres -n postgres |
| 136 | +``` |
| 137 | + |
| 138 | +### Check PVC Status |
| 139 | +```bash |
| 140 | +kubectl get pvc postgres-pvc -n postgres |
| 141 | +``` |
| 142 | + |
| 143 | +### Test Connection |
| 144 | +```bash |
| 145 | +kubectl run postgres-client --rm -i --tty --image postgres:17 -n postgres -- psql -h postgres-service.postgres.svc.cluster.local -U postgres -d postgres |
| 146 | +``` |
| 147 | + |
| 148 | +## High Availability Notes |
| 149 | + |
| 150 | +This setup deploys a single PostgreSQL instance. For production environments, consider: |
| 151 | + |
| 152 | +1. **PostgreSQL Operator** - Use operators like Zalando's PostgreSQL Operator or Crunchy Data PostgreSQL Operator |
| 153 | +2. **Backup Strategy** - Implement automated backups using tools like pgBackRest or Barman |
| 154 | +3. **Monitoring** - Add monitoring with tools like pg_stat_statements and exporters for Prometheus |
| 155 | +4. **Resource Tuning** - Adjust memory and CPU limits based on your workload |
| 156 | + |
| 157 | +## Security Considerations |
| 158 | + |
| 159 | +1. **Change Default Password** - Always change the default password in production |
| 160 | +2. **Network Policies** - Consider implementing Kubernetes Network Policies to restrict access |
| 161 | +3. **TLS** - Configure SSL/TLS for encrypted connections |
| 162 | +4. **RBAC** - Use Kubernetes RBAC to control access to PostgreSQL resources |
0 commit comments