100 lines
2.1 KiB
Markdown
100 lines
2.1 KiB
Markdown
# PostgreSQL 16 部署说明
|
|
|
|
## 配置信息
|
|
|
|
- **命名空间**: postgresql
|
|
- **版本**: PostgreSQL 16 (Alpine)
|
|
- **存储**: 使用 Longhorn 提供 10Gi 持久化存储
|
|
- **内存限制**: 2GB
|
|
- **访问地址**: postgresql-service.postgresql.svc.cluster.local:5432
|
|
|
|
## 默认凭证
|
|
|
|
- **用户名**: postgres
|
|
- **密码**: postgres123
|
|
- **数据库**: postgres
|
|
|
|
⚠️ **安全提示**: 生产环境请修改默认密码!
|
|
|
|
## 部署方式
|
|
|
|
```bash
|
|
bash deploy.sh
|
|
```
|
|
|
|
## 数据库配置
|
|
|
|
### 连接设置
|
|
- 最大连接数: 100
|
|
- 监听地址: 所有接口 (*)
|
|
|
|
### 内存配置
|
|
- shared_buffers: 256MB
|
|
- effective_cache_size: 1GB
|
|
- work_mem: 4MB
|
|
|
|
### WAL 配置
|
|
- wal_level: replica (支持主从复制)
|
|
- max_wal_size: 1GB
|
|
|
|
### 日志配置
|
|
- 记录所有 SQL 语句
|
|
- 记录执行时间
|
|
|
|
## 连接测试
|
|
|
|
在集群内部测试连接:
|
|
|
|
```bash
|
|
kubectl run pg-test --rm -it --image=postgres:16-alpine --env="PGPASSWORD=postgres123" -- psql -h postgresql-service.postgresql.svc.cluster.local -U postgres -c "SELECT version();"
|
|
```
|
|
|
|
## 数据持久化
|
|
|
|
PostgreSQL 数据存储在 Longhorn 卷上:
|
|
- 数据目录: /var/lib/postgresql/data/pgdata
|
|
- 可以通过 Longhorn UI 创建快照和备份到 S3
|
|
|
|
## 常用操作
|
|
|
|
### 查看日志
|
|
```bash
|
|
kubectl logs -n postgresql postgresql-0 -f
|
|
```
|
|
|
|
### 进入数据库
|
|
```bash
|
|
kubectl exec -it -n postgresql postgresql-0 -- psql -U postgres
|
|
```
|
|
|
|
### 创建新数据库
|
|
```bash
|
|
kubectl exec -n postgresql postgresql-0 -- psql -U postgres -c "CREATE DATABASE myapp;"
|
|
```
|
|
|
|
### 创建新用户
|
|
```bash
|
|
kubectl exec -n postgresql postgresql-0 -- psql -U postgres -c "CREATE USER myuser WITH PASSWORD 'mypassword';"
|
|
kubectl exec -n postgresql postgresql-0 -- psql -U postgres -c "GRANT ALL PRIVILEGES ON DATABASE myapp TO myuser;"
|
|
```
|
|
|
|
## 备份与恢复
|
|
|
|
### 手动备份
|
|
```bash
|
|
kubectl exec -n postgresql postgresql-0 -- pg_dump -U postgres postgres > backup.sql
|
|
```
|
|
|
|
### 恢复备份
|
|
```bash
|
|
cat backup.sql | kubectl exec -i -n postgresql postgresql-0 -- psql -U postgres postgres
|
|
```
|
|
|
|
## 监控
|
|
|
|
查看数据库状态:
|
|
|
|
```bash
|
|
kubectl exec -n postgresql postgresql-0 -- psql -U postgres -c "SELECT * FROM pg_stat_activity;"
|
|
```
|