# k8s **Repository Path**: grow-project/k8s ## Basic Information - **Project Name**: k8s - **Description**: 一个基于Golang、Docker和Kubernetes的完整实战项目,用于学习和实践K8s核心概念与部署流程。 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2026-07-01 - **Last Updated**: 2026-09-15 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # K8s Demo App - Golang微服务实战项目 一个基于Golang、Docker和Kubernetes的完整实战项目,用于学习和实践K8s核心概念与部署流程。 ## 项目功能 - **用户管理API**: 提供完整的CRUD操作 - 创建用户 - 查询用户列表 - 查询单个用户 - 更新用户信息 - 删除用户 ## 技术架构 ``` ┌─────────────────────────────────────────────────────────────┐ │ Ingress │ │ k8s-demo-app-ingress │ └─────────────────────────┬───────────────────────────────────┘ │ ┌─────────────────────────▼───────────────────────────────────┐ │ Service │ │ k8s-demo-app-service (ClusterIP) │ └─────────────────────────┬───────────────────────────────────┘ │ Load Balancing ┌─────────────────┼─────────────────┐ ▼ ▼ ▼ ┌───────────────┐ ┌───────────────┐ ┌───────────────┐ │ Pod (app) │ │ Pod (app) │ │ Pod (app) │ │ k8s-demo-app │ │ k8s-demo-app │ │ k8s-demo-app │ └───────────────┘ └───────────────┘ └───────────────┘ │ │ │ └─────────────────┼─────────────────┘ ▼ ┌─────────────────────────────────────────────────────────────┐ │ StatefulSet │ │ postgres │ │ (PersistentVolume: postgres-data) │ └─────────────────────────────────────────────────────────────┘ ``` ## 目录结构 ``` k8s-demo-app/ ├── app/ # Golang应用代码 │ ├── cmd/api/ # 应用入口 │ │ └── main.go │ ├── internal/ # 内部模块 │ │ ├── handler/ # HTTP处理器 │ │ │ └── user_handler.go │ │ ├── service/ # 业务逻辑层 │ │ │ └── user_service.go │ │ ├── repository/ # 数据访问层 │ │ │ └── user_repository.go │ │ └── model/ # 数据模型 │ │ └── user.go │ ├── pkg/ # 公共包 │ │ ├── config/ # 配置管理 │ │ │ └── config.go │ │ └── database/ # 数据库连接 │ │ └── postgres.go │ ├── Dockerfile # Docker镜像构建 │ ├── .dockerignore │ ├── go.mod │ ├── go.sum │ └── .env.example # 环境变量示例 ├── k8s/manifests/ # K8s资源配置 │ ├── 00-namespace.yaml # 命名空间 │ ├── 01-configmap.yaml # 配置管理 │ ├── 02-secret.yaml # 密钥管理 │ ├── 03-postgres-statefulset.yaml # PostgreSQL状态集 │ ├── 04-postgres-service.yaml # PostgreSQL服务 │ ├── 05-app-deployment.yaml # 应用部署 │ ├── 06-app-service.yaml # 应用服务 │ ├── 07-ingress.yaml # 入口配置 │ └── 08-hpa.yaml # 水平自动伸缩 ├── scripts/ # 部署脚本 │ ├── build-image.sh # 构建镜像 │ ├── deploy.sh # 部署应用 │ └── cleanup.sh # 清理资源 └── README.md ``` ## 环境要求 - Go 1.26+ - Docker - Kubernetes集群 (推荐使用Docker Desktop或Minikube) - kubectl ## 快速开始 ### 1. 构建Docker镜像 ```bash ./scripts/build-image.sh ``` ### 2. 部署到Kubernetes ```bash ./scripts/deploy.sh ``` ### 3. 验证部署 ```bash # 查看Pod状态 kubectl get pods -n k8s-demo # 查看服务状态 kubectl get services -n k8s-demo # 查看Ingress kubectl get ingress -n k8s-demo # 查看HPA kubectl get hpa -n k8s-demo ``` ### 4. 测试API ```bash # 健康检查 curl http://localhost:8080/health # 创建用户 curl -X POST http://localhost:8080/api/v1/users \ -H "Content-Type: application/json" \ -d '{"username":"testuser","email":"test@example.com","password":"password123"}' # 查询用户列表 curl http://localhost:8080/api/v1/users # 查询单个用户 curl http://localhost:8080/api/v1/users/1 # 更新用户 curl -X PUT http://localhost:8080/api/v1/users/1 \ -H "Content-Type: application/json" \ -d '{"username":"updateduser"}' # 删除用户 curl -X DELETE http://localhost:8080/api/v1/users/1 ``` ### 5. 清理资源 ```bash ./scripts/cleanup.sh ``` ## K8s核心概念实践 ### 1. 容器化实践 - 使用Dockerfile构建多阶段镜像 - 最小化镜像体积(Alpine基础镜像) - 非root用户运行 ### 2. 资源配置实践 - **Deployment**: 管理无状态应用副本 - **StatefulSet**: 管理有状态数据库服务 - **Service**: 服务发现与负载均衡 - **Ingress**: 外部流量入口 - **ConfigMap**: 配置管理 - **Secret**: 敏感信息管理 - **PersistentVolume**: 数据持久化 - **HPA**: 水平自动伸缩 ### 3. 滚动更新与回滚 ```bash # 查看更新历史 kubectl rollout history deployment/k8s-demo-app -n k8s-demo # 执行回滚 kubectl rollout undo deployment/k8s-demo-app -n k8s-demo # 回滚到特定版本 kubectl rollout undo deployment/k8s-demo-app -n k8s-demo --to-revision=2 ``` ### 4. 健康检查 - **LivenessProbe**: 存活探针 - 检测应用是否健康运行 - **ReadinessProbe**: 就绪探针 - 检测应用是否准备好接收流量 ### 5. 资源限制与请求 - `resources.requests`: 资源请求(保证分配) - `resources.limits`: 资源限制(最大使用量) ### 6. 服务发现与负载均衡 - ClusterIP服务实现内部服务发现 - Ingress实现外部访问和路径路由 ### 7. 配置管理与密钥管理 - ConfigMap存储非敏感配置 - Secret存储敏感信息(自动base64编码) ## API接口文档 | 方法 | 路径 | 描述 | |------|------|------| | GET | /health | 健康检查 | | POST | /api/v1/users | 创建用户 | | GET | /api/v1/users | 查询用户列表 | | GET | /api/v1/users/:id | 查询单个用户 | | PUT | /api/v1/users/:id | 更新用户信息 | | DELETE | /api/v1/users/:id | 删除用户 | ### 请求示例 **创建用户** ```json POST /api/v1/users { "username": "string (3-50字符)", "email": "string (邮箱格式)", "password": "string (至少6字符)" } ``` **更新用户** ```json PUT /api/v1/users/:id { "username": "string (可选)", "email": "string (可选)", "password": "string (可选)" } ``` ### 响应示例 **成功响应** ```json { "id": 1, "username": "testuser", "email": "test@example.com", "created_at": "2024-01-01T00:00:00Z", "updated_at": "2024-01-01T00:00:00Z" } ``` **错误响应** ```json { "error": "error message" } ``` ## 学习路径规划 ### 阶段一: 基础环境准备 1. 安装Go开发环境 2. 安装Docker 3. 安装Kubernetes集群(Docker Desktop/Minikube) 4. 配置kubectl ### 阶段二: 应用开发 1. 理解项目目录结构 2. 学习Golang分层架构(Handler-Service-Repository) 3. 实现CRUD API 4. 配置数据库连接 ### 阶段三: 容器化 1. 编写Dockerfile 2. 构建和测试镜像 3. 理解多阶段构建 ### 阶段四: K8s部署 1. 创建Namespace 2. 配置ConfigMap和Secret 3. 部署PostgreSQL(StatefulSet) 4. 部署应用(Deployment) 5. 配置Service和Ingress ### 阶段五: 高级特性 1. 配置健康检查 2. 设置资源限制 3. 实现滚动更新 4. 配置自动伸缩 5. 学习服务发现 ### 阶段六: 运维实践 1. Pod管理和日志查看 2. 应用更新和回滚 3. 故障排查 4. 性能监控 ## 命令参考 ```bash # 查看Pod日志 kubectl logs -n k8s-demo # 进入Pod kubectl exec -it -n k8s-demo -- /bin/sh # 查看Deployment状态 kubectl describe deployment k8s-demo-app -n k8s-demo # 手动扩缩容 kubectl scale deployment k8s-demo-app -n k8s-demo --replicas=3 # 查看事件 kubectl get events -n k8s-demo # 查看资源使用情况 kubectl top pods -n k8s-demo ``` ## License MIT