Files
k3s/完整部署总结.md
2026-02-05 00:11:05 +08:00

345 lines
9.5 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# K3s 基础设施完整部署总结
部署完成时间: 2026-01-30
## ✅ 所有已部署服务
### 基础设施层
| 服务 | 访问地址 | 说明 |
|------|---------|------|
| **Longhorn** | https://longhorn.u6.net3w.com | 分布式块存储管理 |
| **Grafana** | https://grafana.u6.net3w.com | 监控仪表板 (admin/prom-operator) |
| **Prometheus** | https://prometheus.u6.net3w.com | 指标监控系统 |
| **Alertmanager** | https://alertmanager.u6.net3w.com | 告警管理系统 |
### 中间件层
| 服务 | 访问地址 | 说明 |
|------|---------|------|
| **Redis** | redis.redis.svc.cluster.local:6379 | 内存数据库 |
| **PostgreSQL** | postgresql-service.postgresql.svc.cluster.local:5432 | 关系型数据库 (postgres/postgres123) |
### 对象存储
| 服务 | 访问地址 | 说明 |
|------|---------|------|
| **MinIO S3 API** | https://s3.u6.net3w.com | 对象存储 API |
| **MinIO Console** | https://console.s3.u6.net3w.com | MinIO 管理控制台 |
### 导航页面
| 服务 | 访问地址 | 说明 |
|------|---------|------|
| **服务导航** | https://dh.u6.net3w.com | 🚀 所有服务的导航页面 |
## 架构图
```
Internet (*.u6.net3w.com)
Caddy (前端反向代理 + 自动 SSL)
Traefik Ingress Controller
┌─────────────────────────────────────────────────────┐
│ Kubernetes Services │
├─────────────────────────────────────────────────────┤
│ 基础设施层 │
│ - Longhorn (存储) │
│ - Prometheus + Grafana (监控) │
│ - Alertmanager (告警) │
├─────────────────────────────────────────────────────┤
│ 中间件层 │
│ - Redis (缓存/队列) │
│ - PostgreSQL (数据库) │
├─────────────────────────────────────────────────────┤
│ 对象存储 │
│ - MinIO (S3 兼容存储) │
├─────────────────────────────────────────────────────┤
│ 导航服务 │
│ - Navigation (服务导航页面) │
└─────────────────────────────────────────────────────┘
```
## 快速访问
### 🏠 从导航页面开始
访问 **https://dh.u6.net3w.com** 可以看到所有服务的导航页面,点击即可访问各个服务。
### 📊 监控和管理
- **Grafana**: https://grafana.u6.net3w.com
- 用户名: `admin`
- 密码: `prom-operator`
- 预装多个监控仪表板
- **Longhorn**: https://longhorn.u6.net3w.com
- 管理所有持久化存储
- 查看卷状态、创建快照、备份到 S3
- **Prometheus**: https://prometheus.u6.net3w.com
- 查看原始指标数据
- 执行 PromQL 查询
### 🗄️ 数据存储
- **MinIO Console**: https://console.s3.u6.net3w.com
- 管理对象存储桶
- 上传/下载文件
- **Redis**: 内部访问 `redis.redis.svc.cluster.local:6379`
- **PostgreSQL**: 内部访问 `postgresql-service.postgresql.svc.cluster.local:5432`
## 存储配置
### Longhorn 卷
| 服务 | 存储大小 | 用途 |
|------|---------|------|
| Redis | 5Gi | 数据持久化 |
| PostgreSQL | 10Gi | 数据库文件 |
| Prometheus | 20Gi | 监控数据 (15天保留) |
| Grafana | 5Gi | 仪表板配置 |
| Alertmanager | 5Gi | 告警数据 |
| MinIO | 50Gi | 对象存储 |
| **总计** | **95Gi** | - |
### S3 备份
- **备份目标**: s3://longhorn-backup@us-east-1/
- **MinIO 端点**: https://s3.u6.net3w.com
- **自动备份**: Longhorn 可配置自动备份策略
## 目录结构
```
/home/fei/k3s/
├── 009-基础设施/
│ ├── 003-helm/ # Helm 安装配置
│ ├── 004-longhorn/ # Longhorn 存储配置
│ ├── 005-ingress/ # Ingress 和 Caddy 配置
│ ├── 006-monitoring/ # Prometheus + Grafana
│ ├── 部署总结.md
│ └── 域名绑定配置.md
├── 010-中间件/
│ ├── 001-redis/ # Redis 配置
│ ├── 002-postgresql/ # PostgreSQL 配置
│ └── 003-navigation/ # 导航页面
└── 011-skills/
└── 003-s3-specs.md # S3 配置信息
```
## 网络配置
### DNS 配置
确保以下 DNS 记录已配置:
```
*.u6.net3w.com → <服务器IP>
```
或单独配置:
```
longhorn.u6.net3w.com → <服务器IP>
grafana.u6.net3w.com → <服务器IP>
prometheus.u6.net3w.com → <服务器IP>
alertmanager.u6.net3w.com → <服务器IP>
s3.u6.net3w.com → <服务器IP>
console.s3.u6.net3w.com → <服务器IP>
dh.u6.net3w.com → <服务器IP>
```
### SSL 证书
Caddy 自动为所有域名申请和管理 Let's Encrypt SSL 证书。
## 常用命令
### 查看所有服务状态
```bash
# 查看所有 Pods
kubectl get pods -A
# 查看所有 Ingress
kubectl get ingress -A
# 查看所有 PVC
kubectl get pvc -A
# 查看存储类
kubectl get storageclass
```
### 测试服务连接
```bash
# 测试 Redis
kubectl exec -n redis $(kubectl get pod -n redis -l app=redis -o jsonpath='{.items[0].metadata.name}') -- redis-cli ping
# 测试 PostgreSQL
kubectl exec -n postgresql postgresql-0 -- psql -U postgres -c "SELECT version();"
```
### 查看日志
```bash
# Caddy 日志
kubectl logs -n default -l app=caddy -f
# Traefik 日志
kubectl logs -n kube-system -l app.kubernetes.io/name=traefik -f
# Grafana 日志
kubectl logs -n monitoring -l app.kubernetes.io/name=grafana -f
```
## 备份策略
### 1. Longhorn 卷备份
- 所有持久化数据存储在 Longhorn 卷上
- 通过 Longhorn UI 创建快照
- 自动备份到 MinIO S3
### 2. 数据库备份
**Redis**:
```bash
# 手动触发 RDB 快照
kubectl exec -n redis $(kubectl get pod -n redis -l app=redis -o jsonpath='{.items[0].metadata.name}') -- redis-cli BGSAVE
```
**PostgreSQL**:
```bash
# 导出数据库
kubectl exec -n postgresql postgresql-0 -- pg_dump -U postgres postgres > backup.sql
# 恢复数据库
cat backup.sql | kubectl exec -i -n postgresql postgresql-0 -- psql -U postgres postgres
```
### 3. 配置备份
定期备份 `/home/fei/k3s/` 目录中的所有配置文件。
## 安全建议
### 立即执行
1. ✅ 修改 PostgreSQL 默认密码
2. ✅ 为敏感服务添加认证Prometheus、Alertmanager
3. ✅ 配置防火墙规则
4. ✅ 定期更新系统和容器镜像
### 可选增强
- 配置 RBAC 权限控制
- 启用 Pod Security Policies
- 配置网络策略 (NetworkPolicy)
- 添加入侵检测系统
## 监控告警
### 配置告警通知
编辑 Alertmanager 配置,添加通知渠道:
```bash
kubectl edit secret alertmanager-kube-prometheus-stack-alertmanager -n monitoring
```
支持的通知方式:
- 邮件
- Slack
- 钉钉
- 企业微信
- Webhook
### 推荐的 Grafana 仪表板
1. **Node Exporter Full** (ID: 1860) - 节点详细监控
2. **Kubernetes Cluster Monitoring** (ID: 7249) - 集群概览
3. **Longhorn** (ID: 13032) - 存储监控
4. **Redis** (ID: 11835) - Redis 监控
5. **PostgreSQL** (ID: 9628) - PostgreSQL 监控
## 下一步建议
### 短期 (1-2周)
1. ✅ 配置告警规则和通知
2. ✅ 为 Redis 和 PostgreSQL 添加监控
3. ✅ 测试备份和恢复流程
4. ✅ 优化资源限制
### 中期 (1-2月)
1. ✅ 部署日志收集系统 (Loki)
2. ✅ 配置 CI/CD 流程
3. ✅ 实现高可用 (多节点)
4. ✅ 添加更多应用服务
### 长期 (3-6月)
1. ✅ 实现多集群管理
2. ✅ 配置灾备方案
3. ✅ 性能优化和调优
4. ✅ 安全加固和审计
## 故障排查
### 服务无法访问
1. 检查 Pod 状态: `kubectl get pods -A`
2. 检查 Ingress: `kubectl get ingress -A`
3. 检查 Caddy: `kubectl logs -n default -l app=caddy`
4. 检查 DNS 解析: `nslookup <domain>`
### 存储问题
1. 检查 PVC: `kubectl get pvc -A`
2. 访问 Longhorn UI: https://longhorn.u6.net3w.com
3. 检查节点磁盘空间: `df -h`
### 监控数据缺失
1. 检查 Prometheus targets: https://prometheus.u6.net3w.com/targets
2. 检查 ServiceMonitor: `kubectl get servicemonitor -A`
3. 查看 Prometheus 日志
## 文档和资源
### 本地文档
- 部署总结: `/home/fei/k3s/009-基础设施/部署总结.md`
- 域名配置: `/home/fei/k3s/009-基础设施/域名绑定配置.md`
- 各服务 readme: 每个服务目录下的 `readme.md`
### 官方文档
- K3s: https://docs.k3s.io/
- Longhorn: https://longhorn.io/docs/
- Prometheus: https://prometheus.io/docs/
- Grafana: https://grafana.com/docs/
---
## 🎉 恭喜!
您的 K3s 集群已完整部署,包含:
- ✅ 分布式存储 (Longhorn)
- ✅ 监控系统 (Prometheus + Grafana)
- ✅ 数据库 (Redis + PostgreSQL)
- ✅ 对象存储 (MinIO)
- ✅ 服务导航 (Navigation)
- ✅ 自动 SSL (Caddy)
- ✅ Ingress 路由 (Traefik)
**访问导航页面开始使用**: https://dh.u6.net3w.com 🚀