|
| 1 | +# Database Connection Configuration |
| 2 | + |
| 3 | +The balancer application supports connecting to PostgreSQL databases via two methods: |
| 4 | + |
| 5 | +1. **CloudNativePG** - Kubernetes-managed PostgreSQL cluster (within cluster) |
| 6 | +2. **AWS RDS** - External PostgreSQL database (AWS managed) |
| 7 | + |
| 8 | +The application automatically detects the connection type based on the `SQL_HOST` environment variable format. |
| 9 | + |
| 10 | +## Connection Type Detection |
| 11 | + |
| 12 | +The application determines the connection type by analyzing the `SQL_HOST` value: |
| 13 | + |
| 14 | +- **CloudNativePG**: |
| 15 | + - Contains `.svc.cluster.local` (Kubernetes service DNS) |
| 16 | + - Short service name (e.g., `balancer-postgres-rw`) |
| 17 | + - Typically no SSL required within cluster |
| 18 | + |
| 19 | +- **AWS RDS**: |
| 20 | + - Full domain name (e.g., `balancer-db.xxxxx.us-east-1.rds.amazonaws.com`) |
| 21 | + - External IP address |
| 22 | + - Typically requires SSL |
| 23 | + |
| 24 | +## Configuration |
| 25 | + |
| 26 | +### Environment Variables |
| 27 | + |
| 28 | +All database configuration is done via environment variables: |
| 29 | + |
| 30 | +- `SQL_ENGINE`: Database engine (default: `django.db.backends.postgresql`) |
| 31 | +- `SQL_DATABASE`: Database name |
| 32 | +- `SQL_USER`: Database username |
| 33 | +- `SQL_PASSWORD`: Database password |
| 34 | +- `SQL_HOST`: Database host (see examples below) |
| 35 | +- `SQL_PORT`: Database port (default: `5432`) |
| 36 | +- `SQL_SSL_MODE`: Optional SSL mode (see SSL Configuration below) |
| 37 | + |
| 38 | +### CloudNativePG Configuration |
| 39 | + |
| 40 | +When using CloudNativePG, the application connects to the Kubernetes service created by the operator. |
| 41 | + |
| 42 | +**Example Configuration:** |
| 43 | +```bash |
| 44 | +SQL_ENGINE=django.db.backends.postgresql |
| 45 | +SQL_DATABASE=balancer |
| 46 | +SQL_USER=balancer |
| 47 | +SQL_PASSWORD=<password-from-secret> |
| 48 | +SQL_HOST=balancer-postgres-rw |
| 49 | +SQL_PORT=5432 |
| 50 | +``` |
| 51 | + |
| 52 | +**Service Names:** |
| 53 | +- `{cluster-name}-rw`: Read-write service (primary instance) |
| 54 | +- `{cluster-name}-r`: Read service (replicas) |
| 55 | +- `{cluster-name}-ro`: Read-only service |
| 56 | + |
| 57 | +**Full DNS Name:** |
| 58 | +```bash |
| 59 | +SQL_HOST=balancer-postgres-rw.balancer.svc.cluster.local |
| 60 | +``` |
| 61 | + |
| 62 | +### AWS RDS Configuration |
| 63 | + |
| 64 | +When using AWS RDS, the application connects to the external RDS endpoint. |
| 65 | + |
| 66 | +**Example Configuration:** |
| 67 | +```bash |
| 68 | +SQL_ENGINE=django.db.backends.postgresql |
| 69 | +SQL_DATABASE=balancer |
| 70 | +SQL_USER=balancer |
| 71 | +SQL_PASSWORD=<rds-password> |
| 72 | +SQL_HOST=balancer-db.xxxxx.us-east-1.rds.amazonaws.com |
| 73 | +SQL_PORT=5432 |
| 74 | +SQL_SSL_MODE=require |
| 75 | +``` |
| 76 | + |
| 77 | +## SSL Configuration |
| 78 | + |
| 79 | +### CloudNativePG |
| 80 | + |
| 81 | +SSL is typically **not required** for connections within the Kubernetes cluster. The application will not use SSL by default for CloudNativePG connections. |
| 82 | + |
| 83 | +### AWS RDS |
| 84 | + |
| 85 | +SSL is typically **required** for AWS RDS connections. The application defaults to `require` mode for external hosts, but you can override this: |
| 86 | + |
| 87 | +**SSL Mode Options:** |
| 88 | +- `disable`: No SSL |
| 89 | +- `allow`: Try non-SSL first, then SSL |
| 90 | +- `prefer`: Try SSL first, then non-SSL (default for external) |
| 91 | +- `require`: Require SSL |
| 92 | +- `verify-ca`: Require SSL and verify CA |
| 93 | +- `verify-full`: Require SSL and verify CA and hostname |
| 94 | + |
| 95 | +**Example:** |
| 96 | +```bash |
| 97 | +SQL_SSL_MODE=require |
| 98 | +``` |
| 99 | + |
| 100 | +## Migration Guide |
| 101 | + |
| 102 | +### From AWS RDS to CloudNativePG |
| 103 | + |
| 104 | +1. Update the `SQL_HOST` environment variable in your SealedSecret: |
| 105 | + ```bash |
| 106 | + # Old (AWS RDS) |
| 107 | + SQL_HOST=balancer-db.xxxxx.us-east-1.rds.amazonaws.com |
| 108 | + |
| 109 | + # New (CloudNativePG) |
| 110 | + SQL_HOST=balancer-postgres-rw |
| 111 | + ``` |
| 112 | + |
| 113 | +2. Update database credentials to match CloudNativePG secret |
| 114 | + |
| 115 | +3. Remove or set `SQL_SSL_MODE` to `disable` (optional, as it's auto-detected) |
| 116 | + |
| 117 | +4. Restart the application pods |
| 118 | + |
| 119 | +### From CloudNativePG to AWS RDS |
| 120 | + |
| 121 | +1. Update the `SQL_HOST` environment variable: |
| 122 | + ```bash |
| 123 | + # Old (CloudNativePG) |
| 124 | + SQL_HOST=balancer-postgres-rw |
| 125 | + |
| 126 | + # New (AWS RDS) |
| 127 | + SQL_HOST=balancer-db.xxxxx.us-east-1.rds.amazonaws.com |
| 128 | + ``` |
| 129 | + |
| 130 | +2. Update database credentials to match RDS credentials |
| 131 | + |
| 132 | +3. Set `SQL_SSL_MODE=require` (or appropriate mode) |
| 133 | + |
| 134 | +4. Ensure network connectivity (VPC peering, security groups, etc.) |
| 135 | + |
| 136 | +5. Restart the application pods |
| 137 | + |
| 138 | +## Troubleshooting |
| 139 | + |
| 140 | +### Connection Issues |
| 141 | + |
| 142 | +1. **Verify host format**: Check that `SQL_HOST` matches the expected format for your connection type |
| 143 | + |
| 144 | +2. **Check network connectivity**: |
| 145 | + - CloudNativePG: Ensure pods are in the same namespace |
| 146 | + - AWS RDS: Verify VPC peering, security groups, and network ACLs |
| 147 | + |
| 148 | +3. **Verify credentials**: Ensure username, password, and database name are correct |
| 149 | + |
| 150 | +4. **Check SSL configuration**: For AWS RDS, ensure SSL is properly configured |
| 151 | + |
| 152 | +### Common Errors |
| 153 | + |
| 154 | +**"Connection refused"** |
| 155 | +- Verify the host and port are correct |
| 156 | +- Check if the database service is running |
| 157 | +- Verify network connectivity |
| 158 | + |
| 159 | +**"SSL required"** |
| 160 | +- Add `SQL_SSL_MODE=require` for AWS RDS connections |
| 161 | +- Verify SSL certificates are available |
| 162 | + |
| 163 | +**"Authentication failed"** |
| 164 | +- Verify username and password |
| 165 | +- Check database user permissions |
| 166 | +- Ensure the database exists |
| 167 | + |
| 168 | +## References |
| 169 | + |
| 170 | +- [Django Database Configuration](https://docs.djangoproject.com/en/4.2/ref/settings/#databases) |
| 171 | +- [CloudNativePG Documentation](https://cloudnative-pg.io/) |
| 172 | +- [AWS RDS PostgreSQL](https://docs.aws.amazon.com/rds/latest/userguide/CHAP_PostgreSQL.html) |
| 173 | +- [PostgreSQL SSL Configuration](https://www.postgresql.org/docs/current/libpq-ssl.html) |
| 174 | + |
0 commit comments