SohamB1810/devops-project
GitHub: SohamB1810/devops-project
一个生产级 DevOps 示例项目,演示如何以 Docker、Kubernetes 与 GitHub Actions 构建端到端 CI/CD 并集成监控与安全扫描。
Stars: 0 | Forks: 0
# 🚀 DevOps 项目 — 生产级 Kubernetes 流水线
一个完全自动化的生产级 DevOps 项目,采用 Docker、Kubernetes、GitHub Actions、Prometheus、Grafana、MongoDB 和 Trivy 构建。涵盖从代码到部署、监控到安全的完整 DevOps 生命周期。
## 🏗️ 架构概述
```
Developer → GitHub Push
↓
GitHub Actions CI/CD
├── Build Docker Image
├── Run Health Check Tests
├── Trivy Security Scan
└── Deploy to Kubernetes
↓
Kubernetes Cluster (Minikube / GKE)
├── devops-app (2 replicas)
├── user-service
├── order-service
└── MongoDB (with PersistentVolume)
↓
Prometheus + Grafana Monitoring
├── CPU / Memory / Disk metrics
├── Pod health dashboards
└── Node Exporter metrics
```
## ✅ 功能特性
| 功能 | 技术 |
|---|---|
| 容器化 | Docker |
| 编排 | Kubernetes (Minikube) |
| CI/CD 流水线 | GitHub Actions |
| 监控 | Prometheus + Grafana |
| 数据库 | MongoDB(集群内) |
| 密钥管理 | Kubernetes Secrets + ConfigMaps |
| 安全扫描 | Trivy |
| 微服务 | user-service + order-service |
| 入口 | NGINX Ingress Controller |
| 公开部署 | Render |
## 📁 项目结构
```
devops-project/
├── app/
│ ├── index.js # Main Express app
│ ├── test.js # Health check test
│ ├── package.json
│ ├── Dockerfile
│ └── public/
│ └── index.html
├── user-service/
│ ├── index.js
│ ├── Dockerfile
│ └── package.json
├── order-service/
│ ├── index.js
│ ├── Dockerfile
│ └── package.json
├── .github/
│ └── workflows/
│ └── deploy.yml # CI/CD pipeline
├── deployment.yaml # Main app deployment
├── service.yaml # NodePort service
├── ingress.yaml # NGINX ingress
├── mongo-deployment.yaml # MongoDB deployment
├── mongo-service.yaml # MongoDB service
└── mongo-pvc.yaml # Persistent volume claim
```
## ⚙️ CI/CD 流水线
每次推送到 `main` 分支都会自动触发完整流水线:
```
Push to main
│
├── Job 1: build-and-push
│ ├── Checkout code
│ ├── Login to Docker Hub
│ ├── Build Docker image
│ ├── Push to Docker Hub
│ ├── Run health check test (curl /health)
│ └── Trivy vulnerability scan
│
└── Job 2: deploy
├── Setup kubectl
├── Configure kubeconfig from secret
├── kubectl set image (rolling update)
└── Verify rollout status
```
## 📊 监控栈
Prometheus 和 Grafana 通过 Helm 在集群内部署:
```
helm install monitoring prometheus-community/kube-prometheus-stack \
--namespace monitoring --create-namespace
```
**可用的仪表盘:**
- Node Exporter Full(ID: 15661)——CPU、内存、磁盘、网络
- Prometheus Overview —— 抓取目标、查询统计
- Pod 级指标 —— 每容器资源使用量
**访问 Grafana:**
```
kubectl --namespace monitoring port-forward service/monitoring-grafana 3000:80
# 打开 http://localhost:3000
# 用户名:admin
```
## 🔐 密钥与配置
敏感值绝不会硬编码。所有配置均通过 Kubernetes 原语注入:
```
# Secrets(静态加密)
kubectl create secret generic devops-app-secret \
--from-literal=APP_ENV=production \
--from-literal=APP_PORT=3000
# ConfigMaps(非敏感配置)
kubectl create configmap devops-app-config \
--from-literal=LOG_LEVEL=info \
--from-literal=APP_NAME=devops-app \
--from-literal=VERSION=1.0.0
```
Pod 通过 `secretKeyRef` 和 `configMapKeyRef` 在 `deployment.yaml` 中将这些值作为环境变量接收。
## 🛡️ 安全 — Trivy 扫描
每次构建在部署前都会进行漏洞扫描:
```
- name: Trivy Security Scan
uses: aquasecurity/trivy-action@master
with:
image-ref: soham1810/test-image:latest
format: table
exit-code: 1
severity: CRITICAL,HIGH
```
如果发现 CRITICAL 或 HIGH 漏洞,流水线会自动失败。
## 🧩 微服务
应用被拆分为独立服务,每个服务拥有自己的 Deployment 和 Docker 镜像:
| 服务 | 端口 | 职责 |
|---|---|---|
| devops-app | 3000 | 主应用 + 健康检查端点 |
| user-service | 4000 | 用户管理 API |
| order-service | 5000 | 订单管理 API |
| MongoDB | 27017 | 持久化数据库 |
## 🚀 本地环境准备
**前提条件:** Docker、Minikube、kubectl、Helm
```
# 启动 Minikube
minikube start
# 部署应用
kubectl apply -f deployment.yaml
kubectl apply -f service.yaml
kubectl apply -f ingress.yaml
# 部署 MongoDB
kubectl apply -f mongo-pvc.yaml
kubectl apply -f mongo-deployment.yaml
kubectl apply -f mongo-service.yaml
# 访问应用
minikube service devops-app --url
# 安装监控
helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
helm repo update
helm install monitoring prometheus-community/kube-prometheus-stack \
--namespace monitoring --create-namespace
# 访问 Grafana
kubectl --namespace monitoring port-forward service/monitoring-grafana 3000:80
```
## 🔑 所需的 GitHub 密钥
| 密钥 | 描述 |
|---|---|
| `DOCKER_USER` | Docker Hub 用户名 |
| `DOCKER_PASS` | Docker Hub 密码 |
| `KUBECONFIG_B64` | 用于集群访问的 base64 编码 kubeconfig |
## 📈 本项目演示内容
- **Docker** —— 多阶段容器化与镜像优化
- **Kubernetes** —— 部署、服务、入口、探针、滚动更新
- **CI/CD** —— 完整的自动构建、测试、扫描、部署流水线
- **监控** —— 使用 Prometheus 和 Grafana 的实时可观测性
- **安全** —— 镜像扫描、Kubernetes 密钥、最小权限配置
- **微服务** —— 服务拆分与服务间通信
- **数据库** —— Kubernetes 中的有状态工作负载与持久卷
- **GitOps** —— 基础设施即代码,所有内容均版本控制
## 🌐 在线演示
- **公开地址:** 部署在 Render 上
- **Docker Hub:** `soham1810/test-image:latest`
- **GitHub:** [SohamB1810/devops-project](https://github.com/SohamB1810/devops-project)
## 👤 作者
**Soham** —— DevOps 工程师
作为一个生产级项目构建,涵盖完整的 DevOps 生命周期。
标签:API集成, ConfigMap, Docker, GitHub Actions, Grafana, Health Check, Ingress, Kubernetes Secrets, Microservices, MongoDB, Monitoring, NGINX Ingress Controller, NIDS, Node Exporter, PersistentVolume, Render, 可观测性, 子域名突变, 安全合规, 安全扫描, 安全防御评估, 容器化, 开源框架, 弹性伸缩, 持久化存储, 持续交付, 持续集成, 数据库, 时序注入, 特权提升, 生产环境, 生产级, 监控, 网络代理, 自动化部署, 自动笔记, 自定义脚本, 自定义请求头, 请求拦截, 隐蔽技术