Files
brunoandClaude 2848a75aa2 feat: 配置完整 CI/CD 流程和 Kubernetes 部署
## 🚀 CI/CD 流程

### Gitea Actions Workflow
- 自动构建 Backend 和 Frontend Docker 镜像
- 推送镜像到 Harbor (192.168.1.83:8080)
- 自动部署到 Kubernetes learn namespace
- 支持滚动更新和健康检查

### Docker 镜像
- **Backend**: Python 3.11 + FastAPI
  - 多阶段构建优化
  - 健康检查探针
  - 生产环境优化
- **Frontend**: Node 18 + Vue 3 + Nginx
  - 多阶段构建
  - Gzip 压缩
  - 静态资源缓存优化

## ☸️ Kubernetes 部署

### 基础设施
- Namespace: learn
- PostgreSQL 15 (持久化存储)
- ConfigMap 和 Secret 管理
- Service 和 Ingress 配置

### 应用部署
- Backend: 2 副本,资源限制
- Frontend: 2 副本,Nginx 服务
- 健康检查:Liveness + Readiness Probes
- 滚动更新策略

### 访问配置
- Ingress: pager.learn.local
- Frontend: http://pager.learn.local
- API: http://pager.learn.local/api
- API Docs: http://pager.learn.local/api/docs

## 📊 Grafana 监控

### ServiceMonitor
- Prometheus 自动服务发现
- Backend、Frontend、PostgreSQL 指标采集
- 30s 采集间隔

### Dashboard 面板
- HTTP 请求速率和响应时间
- Pod CPU 和内存使用
- 数据库连接数
- 错误率 (4xx/5xx)
- Pod 重启次数
- 磁盘使用率

## 📦 新增文件

### Docker
- `backend/Dockerfile` - 后端镜像构建
- `frontend/Dockerfile` - 前端镜像构建
- `frontend/nginx.conf` - Nginx 配置
- `.dockerignore` - 构建优化

### CI/CD
- `.gitea/workflows/ci-cd.yaml` - Gitea Actions 配置

### Kubernetes
- `k8s/namespace.yaml` - Namespace 定义
- `k8s/configmap.yaml` - 应用配置
- `k8s/secret.yaml` - 敏感信息
- `k8s/postgres.yaml` - PostgreSQL 部署
- `k8s/deployment.yaml` - 应用部署
- `k8s/service.yaml` - Service 配置
- `k8s/ingress.yaml` - Ingress 路由

### 监控
- `k8s/monitoring/servicemonitor.yaml` - Prometheus 采集
- `k8s/monitoring/grafana-dashboard.json` - Dashboard 定义
- `k8s/monitoring/install-dashboard.sh` - 安装脚本

### 文档和脚本
- `CICD_DEPLOY.md` - 完整部署文档
- `deploy.sh` - 快速部署脚本

## 🔧 配置要求

### Gitea Secrets
- `HARBOR_PASSWORD`: Harbor 密码
- `KUBECONFIG`: K8s 配置 (base64 编码)

### Harbor
- 项目: pager
- 认证: admin/admin123

### Kubernetes
- 版本: 1.24+
- Namespace: learn
- Ingress Controller: nginx

## 📝 使用说明

### 自动部署
```bash
git push origin master
# 自动触发 CI/CD 流程
```

### 手动部署
```bash
./deploy.sh
```

### 查看状态
```bash
kubectl get pods -n learn
kubectl logs -f -l app=pager-backend -n learn
```

### 监控
1. 部署 ServiceMonitor: `kubectl apply -f k8s/monitoring/`
2. 安装 Grafana Dashboard: `cd k8s/monitoring && ./install-dashboard.sh`

## 🎯 特性

- ✅ 自动化 CI/CD 流程
- ✅ Docker 多阶段构建优化
- ✅ Kubernetes 高可用部署
- ✅ 滚动更新零停机
- ✅ 健康检查和自动恢复
- ✅ Prometheus + Grafana 监控
- ✅ 完整的故障排查文档

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
2025-11-22 22:56:31 +08:00

158 lines
4.0 KiB
YAML

apiVersion: apps/v1
kind: Deployment
metadata:
name: pager-backend
namespace: learn
labels:
app: pager-backend
component: api
spec:
replicas: 2
selector:
matchLabels:
app: pager-backend
template:
metadata:
labels:
app: pager-backend
component: api
version: IMAGE_TAG
annotations:
prometheus.io/scrape: "true"
prometheus.io/port: "8888"
prometheus.io/path: "/metrics"
spec:
imagePullSecrets:
- name: harbor-registry
containers:
- name: backend
image: 192.168.1.83:8080/pager/pager-backend:IMAGE_TAG
imagePullPolicy: Always
ports:
- containerPort: 8888
name: http
protocol: TCP
env:
- name: DATABASE_URL
valueFrom:
configMapKeyRef:
name: pager-config
key: DATABASE_URL
- name: SECRET_KEY
valueFrom:
secretKeyRef:
name: pager-secrets
key: SECRET_KEY
- name: ALGORITHM
valueFrom:
secretKeyRef:
name: pager-secrets
key: ALGORITHM
- name: ACCESS_TOKEN_EXPIRE_MINUTES
valueFrom:
configMapKeyRef:
name: pager-config
key: ACCESS_TOKEN_EXPIRE_MINUTES
- name: OPENROUTER_API_KEY
valueFrom:
secretKeyRef:
name: pager-secrets
key: OPENROUTER_API_KEY
- name: AI_MODEL
valueFrom:
configMapKeyRef:
name: pager-config
key: AI_MODEL
- name: USE_CLAUDE_PARSER
valueFrom:
configMapKeyRef:
name: pager-config
key: USE_CLAUDE_PARSER
- name: ENVIRONMENT
valueFrom:
configMapKeyRef:
name: pager-config
key: ENVIRONMENT
resources:
requests:
memory: "512Mi"
cpu: "500m"
limits:
memory: "1Gi"
cpu: "1000m"
livenessProbe:
httpGet:
path: /health
port: 8888
initialDelaySeconds: 30
periodSeconds: 10
timeoutSeconds: 5
failureThreshold: 3
readinessProbe:
httpGet:
path: /health
port: 8888
initialDelaySeconds: 10
periodSeconds: 5
timeoutSeconds: 3
failureThreshold: 3
volumeMounts:
- name: uploads
mountPath: /app/uploads
volumes:
- name: uploads
emptyDir: {}
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: pager-frontend
namespace: learn
labels:
app: pager-frontend
component: web
spec:
replicas: 2
selector:
matchLabels:
app: pager-frontend
template:
metadata:
labels:
app: pager-frontend
component: web
version: IMAGE_TAG
annotations:
prometheus.io/scrape: "true"
prometheus.io/port: "80"
spec:
imagePullSecrets:
- name: harbor-registry
containers:
- name: frontend
image: 192.168.1.83:8080/pager/pager-frontend:IMAGE_TAG
imagePullPolicy: Always
ports:
- containerPort: 80
name: http
protocol: TCP
resources:
requests:
memory: "128Mi"
cpu: "100m"
limits:
memory: "256Mi"
cpu: "200m"
livenessProbe:
httpGet:
path: /
port: 80
initialDelaySeconds: 10
periodSeconds: 10
readinessProbe:
httpGet:
path: /
port: 80
initialDelaySeconds: 5
periodSeconds: 5