首次提交:初始化项目
This commit is contained in:
25
010-中间件/002-postgresql/deploy.sh
Normal file
25
010-中间件/002-postgresql/deploy.sh
Normal file
@@ -0,0 +1,25 @@
|
||||
#!/bin/bash
|
||||
|
||||
# 创建命名空间
|
||||
kubectl create namespace postgresql
|
||||
|
||||
# 部署 PostgreSQL
|
||||
kubectl apply -f postgresql-deployment.yaml
|
||||
|
||||
# 等待 PostgreSQL 启动
|
||||
echo "等待 PostgreSQL 启动..."
|
||||
kubectl wait --for=condition=ready pod -l app=postgresql -n postgresql --timeout=300s
|
||||
|
||||
# 显示状态
|
||||
echo "PostgreSQL 部署完成!"
|
||||
kubectl get pods -n postgresql
|
||||
kubectl get pvc -n postgresql
|
||||
kubectl get svc -n postgresql
|
||||
|
||||
echo ""
|
||||
echo "连接信息:"
|
||||
echo " 主机: postgresql-service.postgresql.svc.cluster.local"
|
||||
echo " 端口: 5432"
|
||||
echo " 用户: postgres"
|
||||
echo " 密码: postgres123"
|
||||
echo " 数据库: postgres"
|
||||
167
010-中间件/002-postgresql/postgresql-deployment.yaml
Normal file
167
010-中间件/002-postgresql/postgresql-deployment.yaml
Normal file
@@ -0,0 +1,167 @@
|
||||
apiVersion: v1
|
||||
kind: PersistentVolumeClaim
|
||||
metadata:
|
||||
name: postgresql-pvc
|
||||
namespace: postgresql
|
||||
spec:
|
||||
accessModes:
|
||||
- ReadWriteOnce
|
||||
storageClassName: longhorn
|
||||
resources:
|
||||
requests:
|
||||
storage: 10Gi
|
||||
---
|
||||
apiVersion: v1
|
||||
kind: Secret
|
||||
metadata:
|
||||
name: postgresql-secret
|
||||
namespace: postgresql
|
||||
type: Opaque
|
||||
stringData:
|
||||
POSTGRES_PASSWORD: "postgres123"
|
||||
POSTGRES_USER: "postgres"
|
||||
POSTGRES_DB: "postgres"
|
||||
---
|
||||
apiVersion: v1
|
||||
kind: ConfigMap
|
||||
metadata:
|
||||
name: postgresql-config
|
||||
namespace: postgresql
|
||||
data:
|
||||
postgresql.conf: |
|
||||
# 连接设置
|
||||
listen_addresses = '*'
|
||||
max_connections = 100
|
||||
|
||||
# 内存设置
|
||||
shared_buffers = 256MB
|
||||
effective_cache_size = 1GB
|
||||
maintenance_work_mem = 64MB
|
||||
work_mem = 4MB
|
||||
|
||||
# WAL 设置
|
||||
wal_level = replica
|
||||
max_wal_size = 1GB
|
||||
min_wal_size = 80MB
|
||||
|
||||
# 日志设置
|
||||
logging_collector = on
|
||||
log_directory = 'log'
|
||||
log_filename = 'postgresql-%Y-%m-%d_%H%M%S.log'
|
||||
log_statement = 'all'
|
||||
log_duration = on
|
||||
|
||||
# 性能优化
|
||||
random_page_cost = 1.1
|
||||
effective_io_concurrency = 200
|
||||
|
||||
pg_hba.conf: |
|
||||
# TYPE DATABASE USER ADDRESS METHOD
|
||||
local all all trust
|
||||
host all all 0.0.0.0/0 md5
|
||||
host all all ::0/0 md5
|
||||
---
|
||||
apiVersion: apps/v1
|
||||
kind: StatefulSet
|
||||
metadata:
|
||||
name: postgresql
|
||||
namespace: postgresql
|
||||
spec:
|
||||
serviceName: postgresql
|
||||
replicas: 1
|
||||
selector:
|
||||
matchLabels:
|
||||
app: postgresql
|
||||
template:
|
||||
metadata:
|
||||
labels:
|
||||
app: postgresql
|
||||
spec:
|
||||
containers:
|
||||
- name: postgresql
|
||||
image: postgres:16-alpine
|
||||
ports:
|
||||
- containerPort: 5432
|
||||
name: postgresql
|
||||
env:
|
||||
- name: POSTGRES_USER
|
||||
valueFrom:
|
||||
secretKeyRef:
|
||||
name: postgresql-secret
|
||||
key: POSTGRES_USER
|
||||
- name: POSTGRES_PASSWORD
|
||||
valueFrom:
|
||||
secretKeyRef:
|
||||
name: postgresql-secret
|
||||
key: POSTGRES_PASSWORD
|
||||
- name: POSTGRES_DB
|
||||
valueFrom:
|
||||
secretKeyRef:
|
||||
name: postgresql-secret
|
||||
key: POSTGRES_DB
|
||||
- name: PGDATA
|
||||
value: /var/lib/postgresql/data/pgdata
|
||||
volumeMounts:
|
||||
- name: data
|
||||
mountPath: /var/lib/postgresql/data
|
||||
- name: config
|
||||
mountPath: /etc/postgresql
|
||||
resources:
|
||||
requests:
|
||||
memory: "512Mi"
|
||||
cpu: "250m"
|
||||
limits:
|
||||
memory: "2Gi"
|
||||
cpu: "1000m"
|
||||
livenessProbe:
|
||||
exec:
|
||||
command:
|
||||
- pg_isready
|
||||
- -U
|
||||
- postgres
|
||||
initialDelaySeconds: 30
|
||||
periodSeconds: 10
|
||||
readinessProbe:
|
||||
exec:
|
||||
command:
|
||||
- pg_isready
|
||||
- -U
|
||||
- postgres
|
||||
initialDelaySeconds: 5
|
||||
periodSeconds: 5
|
||||
volumes:
|
||||
- name: data
|
||||
persistentVolumeClaim:
|
||||
claimName: postgresql-pvc
|
||||
- name: config
|
||||
configMap:
|
||||
name: postgresql-config
|
||||
---
|
||||
apiVersion: v1
|
||||
kind: Service
|
||||
metadata:
|
||||
name: postgresql
|
||||
namespace: postgresql
|
||||
spec:
|
||||
selector:
|
||||
app: postgresql
|
||||
ports:
|
||||
- port: 5432
|
||||
targetPort: 5432
|
||||
protocol: TCP
|
||||
type: ClusterIP
|
||||
clusterIP: None
|
||||
---
|
||||
apiVersion: v1
|
||||
kind: Service
|
||||
metadata:
|
||||
name: postgresql-service
|
||||
namespace: postgresql
|
||||
spec:
|
||||
selector:
|
||||
app: postgresql
|
||||
ports:
|
||||
- port: 5432
|
||||
targetPort: 5432
|
||||
protocol: TCP
|
||||
type: ClusterIP
|
||||
99
010-中间件/002-postgresql/readme.md
Normal file
99
010-中间件/002-postgresql/readme.md
Normal file
@@ -0,0 +1,99 @@
|
||||
# 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;"
|
||||
```
|
||||
Reference in New Issue
Block a user