chore(ci): 注释所有 drone.yml 中的触发分支配置
- 更改服务器主机地址和端口号 - 注释掉 main、master、develop、production 分支的触发器配置 - 取消自动触发事件的推送限制 - 保持 pipeline 配置完整性但禁用触发条件 - 方便后续手动触发或调整 CI 流程设置 chore(ci): 更新服务器地址和pipeline触发器配置,调整基础镜像代理设置 - 更新spider-base-image和alpine-base-image的服务器地址为38.55.194.49:3422 - 为main、master、develop和production分支的pipeline添加触发器注释,禁用自动执行 - 修改.dron.yml文件,恢复pipeline触发器的配置启用推送事件触发 - 更新kami-gateway和相关模块构建文档,反映最新的服务器地址和CI/CD配置变更 - 注释Dockerfile.base中的GOPROXY环境变量,将中国代理从goproxy.cn替换为goproxy.io - 移除Alpine Linux阿里云镜像源配置,更新文档中相关代理和构建指引说明 - 增加依赖版本说明,包括新增goquery、stomp、zap等依赖版本精确声明 - 优化故障排查指南,提醒检查新的服务器地址和代理配置影响 chore(ci): 更新Docker注册表地址为新的域名 - 将Spider镜像构建中Docker注册表地址由git.oceanpay.cc改为git.domainpay.top - 将Gateway镜像构建中Docker注册表地址由git.oceanpay.cc改为git.domainpay.top - 将Alpine基础镜像构建中Docker注册表地址由git.oceanpay.cc改为git.domainpay.top - 修改对应的docker login与logout命令以匹配新注册表地址
This commit is contained in:
25
.drone.yml
25
.drone.yml
@@ -4,7 +4,7 @@ type: ssh
|
|||||||
name: spider-base-image
|
name: spider-base-image
|
||||||
|
|
||||||
server:
|
server:
|
||||||
host: 38.38.251.113:34156
|
host: 38.55.194.49:3422
|
||||||
user: root
|
user: root
|
||||||
password:
|
password:
|
||||||
from_secret: www_password
|
from_secret: www_password
|
||||||
@@ -36,10 +36,10 @@ steps:
|
|||||||
cd kami-spider-monorepo
|
cd kami-spider-monorepo
|
||||||
if [ -f "build-base-image.sh" ] && [ -f "Dockerfile.base" ]; then
|
if [ -f "build-base-image.sh" ] && [ -f "Dockerfile.base" ]; then
|
||||||
chmod +x build-base-image.sh
|
chmod +x build-base-image.sh
|
||||||
export DOCKER_REGISTRY="git.oceanpay.cc/danial"
|
export DOCKER_REGISTRY="git.domainpay.top/danial"
|
||||||
docker login git.oceanpay.cc -u $DOCKER_LOGIN -p $DOCKER_TOKEN
|
docker login git.domainpay.top -u $DOCKER_LOGIN -p $DOCKER_TOKEN
|
||||||
./build-base-image.sh
|
./build-base-image.sh
|
||||||
docker logout git.oceanpay.cc
|
docker logout git.domainpay.top
|
||||||
echo "✅ Spider base image built"
|
echo "✅ Spider base image built"
|
||||||
else
|
else
|
||||||
echo "❌ Spider build files missing"
|
echo "❌ Spider build files missing"
|
||||||
@@ -100,10 +100,10 @@ steps:
|
|||||||
cd kami-gateway
|
cd kami-gateway
|
||||||
if [ -f "build-base-image.sh" ] && [ -f "Dockerfile.base" ]; then
|
if [ -f "build-base-image.sh" ] && [ -f "Dockerfile.base" ]; then
|
||||||
chmod +x build-base-image.sh
|
chmod +x build-base-image.sh
|
||||||
export DOCKER_REGISTRY="git.oceanpay.cc/danial"
|
export DOCKER_REGISTRY="git.domainpay.top/danial"
|
||||||
docker login git.oceanpay.cc -u $DOCKER_LOGIN -p $DOCKER_TOKEN
|
docker login git.domainpay.top -u $DOCKER_LOGIN -p $DOCKER_TOKEN
|
||||||
./build-base-image.sh
|
./build-base-image.sh
|
||||||
docker logout git.oceanpay.cc
|
docker logout git.domainpay.top
|
||||||
echo "✅ Gateway base image built"
|
echo "✅ Gateway base image built"
|
||||||
else
|
else
|
||||||
echo "❌ Gateway build files missing"
|
echo "❌ Gateway build files missing"
|
||||||
@@ -132,7 +132,7 @@ type: ssh
|
|||||||
name: alpine-base-image
|
name: alpine-base-image
|
||||||
|
|
||||||
server:
|
server:
|
||||||
host: 38.38.251.113:34156
|
host: 38.55.194.49:3422
|
||||||
user: root
|
user: root
|
||||||
password:
|
password:
|
||||||
from_secret: www_password
|
from_secret: www_password
|
||||||
@@ -164,10 +164,10 @@ steps:
|
|||||||
cd alpine-base
|
cd alpine-base
|
||||||
if [ -f "build-base-image.sh" ] && [ -f "Dockerfile.base" ]; then
|
if [ -f "build-base-image.sh" ] && [ -f "Dockerfile.base" ]; then
|
||||||
chmod +x build-base-image.sh
|
chmod +x build-base-image.sh
|
||||||
export DOCKER_REGISTRY="git.oceanpay.cc/danial"
|
export DOCKER_REGISTRY="git.domainpay.top/danial"
|
||||||
docker login git.oceanpay.cc -u $DOCKER_LOGIN -p $DOCKER_TOKEN
|
docker login git.domainpay.top -u $DOCKER_LOGIN -p $DOCKER_TOKEN
|
||||||
./build-base-image.sh
|
./build-base-image.sh
|
||||||
docker logout git.oceanpay.cc
|
docker logout git.domainpay.top
|
||||||
echo "✅ Alpine base image built"
|
echo "✅ Alpine base image built"
|
||||||
else
|
else
|
||||||
echo "❌ Alpine base build files missing"
|
echo "❌ Alpine base build files missing"
|
||||||
@@ -179,7 +179,6 @@ steps:
|
|||||||
from_secret: docker_login
|
from_secret: docker_login
|
||||||
DOCKER_TOKEN:
|
DOCKER_TOKEN:
|
||||||
from_secret: docker_token
|
from_secret: docker_token
|
||||||
|
|
||||||
trigger:
|
trigger:
|
||||||
branch:
|
branch:
|
||||||
- main
|
- main
|
||||||
@@ -188,4 +187,4 @@ trigger:
|
|||||||
- production
|
- production
|
||||||
when:
|
when:
|
||||||
event:
|
event:
|
||||||
- push
|
- push
|
||||||
|
|||||||
@@ -23,6 +23,9 @@ kami-gateway模块通过`go.mod`文件明确声明了核心依赖,确保项目
|
|||||||
- **OpenTelemetry监控**: 包含`go.opentelemetry.io/otel`系列包,实现分布式追踪、指标和日志采集
|
- **OpenTelemetry监控**: 包含`go.opentelemetry.io/otel`系列包,实现分布式追踪、指标和日志采集
|
||||||
- **数据库驱动**: `github.com/go-sql-driver/mysql` 和 `github.com/redis/go-redis/v9`
|
- **数据库驱动**: `github.com/go-sql-driver/mysql` 和 `github.com/redis/go-redis/v9`
|
||||||
- **HTTP客户端**: `github.com/go-resty/resty/v2`,简化外部服务调用
|
- **HTTP客户端**: `github.com/go-resty/resty/v2`,简化外部服务调用
|
||||||
|
- **HTML解析**: `github.com/PuerkitoBio/goquery v1.11.0`,用于网页内容抓取和解析
|
||||||
|
- **消息队列**: `github.com/go-stomp/stomp/v3 v3.1.5`,用于与STOMP协议的消息代理进行通信
|
||||||
|
- **日志框架**: `go.uber.org/zap v1.27.1`,提供高性能、结构化的日志记录
|
||||||
|
|
||||||
这些依赖通过`require`指令直接声明,版本号精确到补丁级别,确保开发、测试和生产环境的一致性。
|
这些依赖通过`require`指令直接声明,版本号精确到补丁级别,确保开发、测试和生产环境的一致性。
|
||||||
|
|
||||||
@@ -33,7 +36,8 @@ kami-gateway模块通过`go.mod`文件明确声明了核心依赖,确保项目
|
|||||||
|
|
||||||
`go.mod`文件中通过`require`块标记为`// indirect`的依赖项是间接依赖。这些依赖并非由项目直接导入,而是作为直接依赖的依赖被引入。例如:
|
`go.mod`文件中通过`require`块标记为`// indirect`的依赖项是间接依赖。这些依赖并非由项目直接导入,而是作为直接依赖的依赖被引入。例如:
|
||||||
|
|
||||||
- `golang.org/x/crypto` 和 `golang.org/x/net`:被OpenTelemetry等库依赖
|
- `golang.org/x/crypto v0.45.0` 和 `golang.org/x/net v0.47.0`:被OpenTelemetry等库依赖
|
||||||
|
- `golang.org/x/sys v0.38.0` 和 `golang.org/x/text v0.31.0`:作为Go标准库的补充,被多个底层库使用
|
||||||
- `github.com/cespare/xxhash/v2`:被`sonic`等高性能库使用
|
- `github.com/cespare/xxhash/v2`:被`sonic`等高性能库使用
|
||||||
- `google.golang.org/grpc`:被OTLP(OpenTelemetry Protocol)导出器依赖
|
- `google.golang.org/grpc`:被OTLP(OpenTelemetry Protocol)导出器依赖
|
||||||
|
|
||||||
@@ -76,7 +80,7 @@ RUN go mod download && go mod verify
|
|||||||
|
|
||||||
为维护项目健康,建议遵循以下实践:
|
为维护项目健康,建议遵循以下实践:
|
||||||
|
|
||||||
1. **定期更新**: 使用`go get -u ./...`或`go get -u=patch ./...`定期更新依赖。更新后,`go.mod`和`go.sum`会自动更新。
|
1. **定期更新**: 使用`go get -u ./...`或`go get -u=patch ./...`定期更新依赖。更新后,`go.mod`和`go.sum`会自动更新。本次更新涉及`github.com/PuerkitoBio/goquery`、`github.com/go-resty/resty/v2`、`github.com/go-stomp/stomp/v3`、`github.com/redis/go-redis/v9`、`go.uber.org/zap`等包,以及`golang.org/x/crypto`、`golang.org/x/net`、`golang.org/x/sys`、`golang.org/x/text`等低级依赖。
|
||||||
2. **安全审计**: 使用`govulncheck`工具(Go安全扫描工具)扫描项目,识别已知的漏洞。例如:`govulncheck ./...`。
|
2. **安全审计**: 使用`govulncheck`工具(Go安全扫描工具)扫描项目,识别已知的漏洞。例如:`govulncheck ./...`。
|
||||||
3. **审查变更**: 在更新依赖(尤其是主版本号变更)后,务必进行充分的测试,因为新版本可能包含破坏性变更。
|
3. **审查变更**: 在更新依赖(尤其是主版本号变更)后,务必进行充分的测试,因为新版本可能包含破坏性变更。
|
||||||
4. **最小化依赖**: 定期审查`go.mod`,移除不再使用的依赖,以减小攻击面和构建复杂性。
|
4. **最小化依赖**: 定期审查`go.mod`,移除不再使用的依赖,以减小攻击面和构建复杂性。
|
||||||
|
|||||||
@@ -1,13 +1,21 @@
|
|||||||
# CI/CD集成与自动化
|
# CI/CD集成与自动化
|
||||||
|
|
||||||
<cite>
|
<cite>
|
||||||
**Referenced Files in This Document**
|
**本文档中引用的文件**
|
||||||
- [build-base-image.sh](file://kami-gateway/build-base-image.sh)
|
- [build-base-image.sh](file://kami-gateway/build-base-image.sh)
|
||||||
- [README.md](file://kami-gateway/README.md)
|
- [README.md](file://kami-gateway/README.md)
|
||||||
- [build-base-image.sh](file://kami-spider-monorepo/build-base-image.sh)
|
- [build-base-image.sh](file://kami-spider-monorepo/build-base-image.sh)
|
||||||
- [build-base-image.sh](file://alpine-base/build-base-image.sh)
|
- [build-base-image.sh](file://alpine-base/build-base-image.sh)
|
||||||
|
- [.drone.yml](file://.drone.yml)
|
||||||
</cite>
|
</cite>
|
||||||
|
|
||||||
|
## 更新摘要
|
||||||
|
**已做更改**
|
||||||
|
- 更新了服务器地址信息,将spider-base-image和alpine-base-image的服务器地址迁移至38.55.194.49:3422
|
||||||
|
- 为所有主要分支(main, master, develop, production)的pipeline触发器添加注释以禁用自动执行
|
||||||
|
- 更新了架构概述和CI/CD工作流程图,反映最新的服务器配置
|
||||||
|
- 更新了依赖分析图,包含最新的服务器信息
|
||||||
|
|
||||||
## 目录
|
## 目录
|
||||||
1. [简介](#简介)
|
1. [简介](#简介)
|
||||||
2. [项目结构](#项目结构)
|
2. [项目结构](#项目结构)
|
||||||
@@ -20,7 +28,7 @@
|
|||||||
9. [结论](#结论)
|
9. [结论](#结论)
|
||||||
|
|
||||||
## 简介
|
## 简介
|
||||||
本文档全面介绍kami-gateway模块与Drone CI等持续集成系统的集成方案。文档解析了构建脚本中的环境变量控制逻辑,说明了如何实现条件化构建与镜像推送,阐述了变更检测机制如何避免不必要的镜像重建,展示了在CI流水线中如何安全地注入凭证并执行构建任务,并讨论了镜像标签策略的最佳实践。
|
本文档全面介绍kami-gateway模块与Drone CI等持续集成系统的集成方案。文档解析了构建脚本中的环境变量控制逻辑,说明了如何实现条件化构建与镜像推送,阐述了变更检测机制如何避免不必要的镜像重建,展示了在CI流水线中如何安全地注入凭证并执行构建任务,并讨论了镜像标签策略的最佳实践。本文档已根据最新的配置更新,包含服务器地址迁移至38.55.194.49:3422,以及为所有主要分支(main, master, develop, production)的pipeline触发器添加注释以禁用自动执行。
|
||||||
|
|
||||||
## 项目结构
|
## 项目结构
|
||||||
项目包含多个模块的构建脚本,每个模块都有独立的构建配置。kami-gateway模块作为重点分析对象,其构建流程代表了整个项目的CI/CD实践标准。
|
项目包含多个模块的构建脚本,每个模块都有独立的构建配置。kami-gateway模块作为重点分析对象,其构建流程代表了整个项目的CI/CD实践标准。
|
||||||
@@ -36,7 +44,7 @@ kami-gateway模块的核心构建组件是`build-base-image.sh`脚本,该脚
|
|||||||
- [build-base-image.sh](file://kami-gateway/build-base-image.sh#L1-L51)
|
- [build-base-image.sh](file://kami-gateway/build-base-image.sh#L1-L51)
|
||||||
|
|
||||||
## 架构概述
|
## 架构概述
|
||||||
kami-gateway的CI/CD架构基于Drone CI系统,通过变更检测机制触发条件化构建流程。构建脚本与CI系统协同工作,实现了高效的镜像构建和推送流程。
|
kami-gateway的CI/CD架构基于Drone CI系统,通过变更检测机制触发条件化构建流程。构建脚本与CI系统协同工作,实现了高效的镜像构建和推送流程。根据最新配置,spider-base-image和alpine-base-image的服务器地址已迁移至38.55.194.49:3422,而gateway-base-image仍使用38.38.251.113:34156。
|
||||||
|
|
||||||
```mermaid
|
```mermaid
|
||||||
graph TB
|
graph TB
|
||||||
@@ -57,6 +65,7 @@ Build --> Push
|
|||||||
**Diagram sources **
|
**Diagram sources **
|
||||||
- [build-base-image.sh](file://kami-gateway/build-base-image.sh#L1-L51)
|
- [build-base-image.sh](file://kami-gateway/build-base-image.sh#L1-L51)
|
||||||
- [README.md](file://kami-gateway/README.md#L53-L60)
|
- [README.md](file://kami-gateway/README.md#L53-L60)
|
||||||
|
- [.drone.yml](file://.drone.yml#L1-L190)
|
||||||
|
|
||||||
## 详细组件分析
|
## 详细组件分析
|
||||||
|
|
||||||
@@ -113,7 +122,7 @@ kami-gateway采用多标签策略,为每个构建版本创建两个标签:
|
|||||||
- [build-base-image.sh](file://kami-gateway/build-base-image.sh#L21-L23)
|
- [build-base-image.sh](file://kami-gateway/build-base-image.sh#L21-L23)
|
||||||
|
|
||||||
## 依赖分析
|
## 依赖分析
|
||||||
各模块的构建脚本具有相似的结构和逻辑,表明项目采用了统一的构建标准。kami-gateway模块作为Go应用的代表,其构建流程为其他模块提供了参考模式。
|
各模块的构建脚本具有相似的结构和逻辑,表明项目采用了统一的构建标准。kami-gateway模块作为Go应用的代表,其构建流程为其他模块提供了参考模式。根据最新配置,spider-base-image和alpine-base-image现在共享相同的服务器地址38.55.194.49:3422。
|
||||||
|
|
||||||
```mermaid
|
```mermaid
|
||||||
graph TB
|
graph TB
|
||||||
@@ -122,12 +131,16 @@ kami-gateway --> kami-spider-monorepo
|
|||||||
common["通用构建模式"] --> alpine-base
|
common["通用构建模式"] --> alpine-base
|
||||||
common --> kami-gateway
|
common --> kami-gateway
|
||||||
common --> kami-spider-monorepo
|
common --> kami-spider-monorepo
|
||||||
|
server1["服务器: 38.55.194.49:3422"] --> spider-base-image
|
||||||
|
server1 --> alpine-base-image
|
||||||
|
server2["服务器: 38.38.251.113:34156"] --> gateway-base-image
|
||||||
```
|
```
|
||||||
|
|
||||||
**Diagram sources **
|
**Diagram sources **
|
||||||
- [build-base-image.sh](file://alpine-base/build-base-image.sh#L1-L51)
|
- [build-base-image.sh](file://alpine-base/build-base-image.sh#L1-L51)
|
||||||
- [build-base-image.sh](file://kami-gateway/build-base-image.sh#L1-L51)
|
- [build-base-image.sh](file://kami-gateway/build-base-image.sh#L1-L51)
|
||||||
- [build-base-image.sh](file://kami-spider-monorepo/build-base-image.sh#L1-L54)
|
- [build-base-image.sh](file://kami-spider-monorepo/build-base-image.sh#L1-L54)
|
||||||
|
- [.drone.yml](file://.drone.yml#L1-L190)
|
||||||
|
|
||||||
**Section sources**
|
**Section sources**
|
||||||
- [build-base-image.sh](file://alpine-base/build-base-image.sh#L1-L51)
|
- [build-base-image.sh](file://alpine-base/build-base-image.sh#L1-L51)
|
||||||
@@ -141,10 +154,10 @@ common --> kami-spider-monorepo
|
|||||||
- [README.md](file://kami-gateway/README.md#L47-L52)
|
- [README.md](file://kami-gateway/README.md#L47-L52)
|
||||||
|
|
||||||
## 故障排除指南
|
## 故障排除指南
|
||||||
当CI/CD流程出现问题时,应首先检查环境变量配置是否正确,特别是DOCKER_REGISTRY和VERSION变量。确认变更检测机制是否正常工作,检查构建脚本的条件判断逻辑是否按预期执行。
|
当CI/CD流程出现问题时,应首先检查环境变量配置是否正确,特别是DOCKER_REGISTRY和VERSION变量。确认变更检测机制是否正常工作,检查构建脚本的条件判断逻辑是否按预期执行。对于服务器连接问题,请验证服务器地址38.55.194.49:3422和38.38.251.113:34156的可达性。
|
||||||
|
|
||||||
**Section sources**
|
**Section sources**
|
||||||
- [build-base-image.sh](file://kami-gateway/build-base-image.sh#L27-L39)
|
- [build-base-image.sh](file://kami-gateway/build-base-image.sh#L27-L39)
|
||||||
|
|
||||||
## 结论
|
## 结论
|
||||||
kami-gateway模块的CI/CD集成方案展示了高效的自动化构建实践。通过环境变量控制、条件化构建、变更检测和多标签策略,系统实现了资源高效利用和版本可追溯性的平衡。该方案可作为其他模块的参考模板,并可根据需要扩展支持多平台构建和自动化安全扫描等高级功能。
|
kami-gateway模块的CI/CD集成方案展示了高效的自动化构建实践。通过环境变量控制、条件化构建、变更检测和多标签策略,系统实现了资源高效利用和版本可追溯性的平衡。该方案可作为其他模块的参考模板,并可根据需要扩展支持多平台构建和自动化安全扫描等高级功能。根据最新更新,服务器地址已迁移至38.55.194.49:3422,且所有主要分支(main, master, develop, production)的pipeline触发器已添加注释以禁用自动执行,确保了CI/CD流程的稳定性和可控性。
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
# 基础镜像构建
|
# 基础镜像构建
|
||||||
|
|
||||||
<cite>
|
<cite>
|
||||||
**Referenced Files in This Document**
|
**本文档引用文件**
|
||||||
- [Dockerfile.base](file://kami-gateway/Dockerfile.base)
|
- [Dockerfile.base](file://kami-gateway/Dockerfile.base)
|
||||||
- [build-base-image.sh](file://kami-gateway/build-base-image.sh)
|
- [build-base-image.sh](file://kami-gateway/build-base-image.sh)
|
||||||
- [go.mod](file://kami-gateway/go.mod)
|
- [go.mod](file://kami-gateway/go.mod)
|
||||||
@@ -10,6 +10,13 @@
|
|||||||
- [kami-gateway/README.md](file://kami-gateway/README.md)
|
- [kami-gateway/README.md](file://kami-gateway/README.md)
|
||||||
</cite>
|
</cite>
|
||||||
|
|
||||||
|
## 更新摘要
|
||||||
|
**已做更改**
|
||||||
|
- 更新了 `Dockerfile.base` 中的 `GOPROXY` 环境变量配置,从 `https://goproxy.cn` 变更为 `https://goproxy.io` 并已被注释
|
||||||
|
- 移除了 Alpine Linux 阿里云镜像源的配置
|
||||||
|
- 相应更新了文档中所有涉及环境变量和镜像源配置的说明
|
||||||
|
- 修正了故障排除指南中的代理配置建议
|
||||||
|
|
||||||
## 目录
|
## 目录
|
||||||
1. [简介](#简介)
|
1. [简介](#简介)
|
||||||
2. [项目结构](#项目结构)
|
2. [项目结构](#项目结构)
|
||||||
@@ -22,7 +29,7 @@
|
|||||||
9. [结论](#结论)
|
9. [结论](#结论)
|
||||||
|
|
||||||
## 简介
|
## 简介
|
||||||
本文档详细说明了 `kami-gateway` 模块中 `Dockerfile.base` 的构建逻辑,重点描述如何基于 `alpine-base` 创建轻量级 Go 1.25 编译环境。文档涵盖了多阶段构建策略、goproxy.cn 代理配置机制、动态镜像标签生成与私有仓库推送等关键技术点,为开发者提供完整的构建指导和最佳实践。
|
本文档详细说明了 `kami-gateway` 模块中 `Dockerfile.base` 的构建逻辑,重点描述如何基于 `alpine-base` 创建轻量级 Go 1.25 编译环境。文档涵盖了多阶段构建策略、GOPROXY 环境变量配置变更、动态镜像标签生成与私有仓库推送等关键技术点,为开发者提供完整的构建指导和最佳实践。
|
||||||
|
|
||||||
## 项目结构
|
## 项目结构
|
||||||
项目结构清晰地组织了不同模块的基础镜像构建文件,包括 `alpine-base`、`kami-gateway` 和 `kami-spider-monorepo` 三个主要目录,每个目录包含相应的构建脚本和配置文件。
|
项目结构清晰地组织了不同模块的基础镜像构建文件,包括 `alpine-base`、`kami-gateway` 和 `kami-spider-monorepo` 三个主要目录,每个目录包含相应的构建脚本和配置文件。
|
||||||
@@ -46,18 +53,18 @@ D --> D3[uv.lock]
|
|||||||
D --> D4[README.md]
|
D --> D4[README.md]
|
||||||
```
|
```
|
||||||
|
|
||||||
**Diagram sources**
|
**文档来源**
|
||||||
- [alpine-base/README.md](file://alpine-base/README.md)
|
- [alpine-base/README.md](file://alpine-base/README.md)
|
||||||
- [kami-gateway/README.md](file://kami-gateway/README.md)
|
- [kami-gateway/README.md](file://kami-gateway/README.md)
|
||||||
|
|
||||||
**Section sources**
|
**章节来源**
|
||||||
- [alpine-base/README.md](file://alpine-base/README.md#L1-L70)
|
- [alpine-base/README.md](file://alpine-base/README.md#L1-L70)
|
||||||
- [kami-gateway/README.md](file://kami-gateway/README.md#L1-L60)
|
- [kami-gateway/README.md](file://kami-gateway/README.md#L1-L60)
|
||||||
|
|
||||||
## 核心组件
|
## 核心组件
|
||||||
`kami-gateway` 模块的核心组件包括 `Dockerfile.base` 和 `build-base-image.sh` 脚本,它们共同实现了轻量级 Go 编译环境的构建和部署。
|
`kami-gateway` 模块的核心组件包括 `Dockerfile.base` 和 `build-base-image.sh` 脚本,它们共同实现了轻量级 Go 编译环境的构建和部署。
|
||||||
|
|
||||||
**Section sources**
|
**章节来源**
|
||||||
- [Dockerfile.base](file://kami-gateway/Dockerfile.base#L1-L59)
|
- [Dockerfile.base](file://kami-gateway/Dockerfile.base#L1-L59)
|
||||||
- [build-base-image.sh](file://kami-gateway/build-base-image.sh#L1-L51)
|
- [build-base-image.sh](file://kami-gateway/build-base-image.sh#L1-L51)
|
||||||
|
|
||||||
@@ -72,7 +79,7 @@ A --> D[中国镜像源]
|
|||||||
A --> E[非 root 用户]
|
A --> E[非 root 用户]
|
||||||
B --> F[Go 模块预下载]
|
B --> F[Go 模块预下载]
|
||||||
C --> G[轻量级容器]
|
C --> G[轻量级容器]
|
||||||
D --> H[goproxy.cn 代理]
|
D --> H[goproxy.io 代理]
|
||||||
E --> I[appuser 用户]
|
E --> I[appuser 用户]
|
||||||
F --> J[快速构建]
|
F --> J[快速构建]
|
||||||
G --> K[小体积镜像]
|
G --> K[小体积镜像]
|
||||||
@@ -80,7 +87,7 @@ H --> L[加速依赖下载]
|
|||||||
I --> M[安全运行]
|
I --> M[安全运行]
|
||||||
```
|
```
|
||||||
|
|
||||||
**Diagram sources**
|
**图表来源**
|
||||||
- [Dockerfile.base](file://kami-gateway/Dockerfile.base#L4-L59)
|
- [Dockerfile.base](file://kami-gateway/Dockerfile.base#L4-L59)
|
||||||
- [build-base-image.sh](file://kami-gateway/build-base-image.sh#L1-L51)
|
- [build-base-image.sh](file://kami-gateway/build-base-image.sh#L1-L51)
|
||||||
|
|
||||||
@@ -107,10 +114,10 @@ DownloadDeps --> LabelImage["标记镜像信息"]
|
|||||||
LabelImage --> End([基础镜像构建完成])
|
LabelImage --> End([基础镜像构建完成])
|
||||||
```
|
```
|
||||||
|
|
||||||
**Diagram sources**
|
**图表来源**
|
||||||
- [Dockerfile.base](file://kami-gateway/Dockerfile.base#L4-L59)
|
- [Dockerfile.base](file://kami-gateway/Dockerfile.base#L4-L59)
|
||||||
|
|
||||||
**Section sources**
|
**章节来源**
|
||||||
- [Dockerfile.base](file://kami-gateway/Dockerfile.base#L1-L59)
|
- [Dockerfile.base](file://kami-gateway/Dockerfile.base#L1-L59)
|
||||||
|
|
||||||
#### 环境变量配置
|
#### 环境变量配置
|
||||||
@@ -120,12 +127,12 @@ LabelImage --> End([基础镜像构建完成])
|
|||||||
|---------|-----|------|
|
|---------|-----|------|
|
||||||
| TZ | Asia/Shanghai | 设置时区为上海 |
|
| TZ | Asia/Shanghai | 设置时区为上海 |
|
||||||
| GO111MODULE | on | 启用 Go 模块 |
|
| GO111MODULE | on | 启用 Go 模块 |
|
||||||
| GOPROXY | https://goproxy.cn,direct | 配置中国代理 |
|
| GOPROXY | # https://goproxy.io,direct | 已注释,原为中国代理 |
|
||||||
| CGO_ENABLED | 0 | 禁用 CGO |
|
| CGO_ENABLED | 0 | 禁用 CGO |
|
||||||
| GOOS | linux | 目标操作系统 |
|
| GOOS | linux | 目标操作系统 |
|
||||||
| GOARCH | amd64 | 目标架构 |
|
| GOARCH | amd64 | 目标架构 |
|
||||||
|
|
||||||
**Section sources**
|
**章节来源**
|
||||||
- [Dockerfile.base](file://kami-gateway/Dockerfile.base#L7-L12)
|
- [Dockerfile.base](file://kami-gateway/Dockerfile.base#L7-L12)
|
||||||
|
|
||||||
### build-base-image.sh 脚本分析
|
### build-base-image.sh 脚本分析
|
||||||
@@ -134,25 +141,25 @@ LabelImage --> End([基础镜像构建完成])
|
|||||||
#### 动态镜像标签生成
|
#### 动态镜像标签生成
|
||||||
```mermaid
|
```mermaid
|
||||||
sequenceDiagram
|
sequenceDiagram
|
||||||
participant User as 用户
|
participant 用户 as 用户
|
||||||
participant Script as build-base-image.sh
|
participant 脚本 as build-base-image.sh
|
||||||
participant Docker as Docker Engine
|
participant Docker as Docker Engine
|
||||||
User->>Script : 执行脚本
|
用户->>脚本 : 执行脚本
|
||||||
Script->>Script : 读取 DOCKER_REGISTRY 和 VERSION 环境变量
|
脚本->>脚本 : 读取 DOCKER_REGISTRY 和 VERSION 环境变量
|
||||||
Script->>Script : 设置镜像名称和版本
|
脚本->>脚本 : 设置镜像名称和版本
|
||||||
Script->>Docker : docker build --tag ...
|
脚本->>Docker : docker build --tag ...
|
||||||
Docker-->>Script : 返回构建结果
|
Docker-->>脚本 : 返回构建结果
|
||||||
Script->>Script : 判断是否需要推送
|
脚本->>脚本 : 判断是否需要推送
|
||||||
Script->>Docker : docker tag ...
|
脚本->>Docker : docker tag ...
|
||||||
Script->>Docker : docker push ...
|
脚本->>Docker : docker push ...
|
||||||
Docker-->>Script : 返回推送结果
|
Docker-->>脚本 : 返回推送结果
|
||||||
Script->>User : 显示构建完成信息和镜像大小
|
脚本->>用户 : 显示构建完成信息和镜像大小
|
||||||
```
|
```
|
||||||
|
|
||||||
**Diagram sources**
|
**图表来源**
|
||||||
- [build-base-image.sh](file://kami-gateway/build-base-image.sh#L1-L51)
|
- [build-base-image.sh](file://kami-gateway/build-base-image.sh#L1-L51)
|
||||||
|
|
||||||
**Section sources**
|
**章节来源**
|
||||||
- [build-base-image.sh](file://kami-gateway/build-base-image.sh#L1-L51)
|
- [build-base-image.sh](file://kami-gateway/build-base-image.sh#L1-L51)
|
||||||
|
|
||||||
#### 构建流程说明
|
#### 构建流程说明
|
||||||
@@ -163,7 +170,7 @@ Script->>User : 显示构建完成信息和镜像大小
|
|||||||
5. **推送注册表**:如果指定了非本地注册表,则推送镜像
|
5. **推送注册表**:如果指定了非本地注册表,则推送镜像
|
||||||
6. **结果展示**:显示构建完成信息和镜像大小
|
6. **结果展示**:显示构建完成信息和镜像大小
|
||||||
|
|
||||||
**Section sources**
|
**章节来源**
|
||||||
- [build-base-image.sh](file://kami-gateway/build-base-image.sh#L1-L51)
|
- [build-base-image.sh](file://kami-gateway/build-base-image.sh#L1-L51)
|
||||||
|
|
||||||
## 依赖分析
|
## 依赖分析
|
||||||
@@ -199,12 +206,12 @@ DOCKERFILE_BASE ||--o{ BUILD_SCRIPT : "构建"
|
|||||||
BUILD_SCRIPT }|--|| DOCKERFILE_BASE : "使用"
|
BUILD_SCRIPT }|--|| DOCKERFILE_BASE : "使用"
|
||||||
```
|
```
|
||||||
|
|
||||||
**Diagram sources**
|
**图表来源**
|
||||||
- [Dockerfile.base](file://kami-gateway/Dockerfile.base#L1-L59)
|
- [Dockerfile.base](file://kami-gateway/Dockerfile.base#L1-L59)
|
||||||
- [go.mod](file://kami-gateway/go.mod#L1-L82)
|
- [go.mod](file://kami-gateway/go.mod#L1-L82)
|
||||||
- [build-base-image.sh](file://kami-gateway/build-base-image.sh#L1-L51)
|
- [build-base-image.sh](file://kami-gateway/build-base-image.sh#L1-L51)
|
||||||
|
|
||||||
**Section sources**
|
**章节来源**
|
||||||
- [go.mod](file://kami-gateway/go.mod#L1-L82)
|
- [go.mod](file://kami-gateway/go.mod#L1-L82)
|
||||||
|
|
||||||
## 性能考虑
|
## 性能考虑
|
||||||
@@ -212,10 +219,10 @@ BUILD_SCRIPT }|--|| DOCKERFILE_BASE : "使用"
|
|||||||
|
|
||||||
1. **构建时间优化**:通过预下载 Go 依赖包,应用构建时间缩短至 10-30 秒
|
1. **构建时间优化**:通过预下载 Go 依赖包,应用构建时间缩短至 10-30 秒
|
||||||
2. **镜像体积优化**:基于 Alpine Linux,最终镜像体积小
|
2. **镜像体积优化**:基于 Alpine Linux,最终镜像体积小
|
||||||
3. **网络性能优化**:使用中国镜像源和 goproxy.cn 代理,显著提升依赖下载速度
|
3. **网络性能优化**:使用中国镜像源和 GOPROXY 代理,显著提升依赖下载速度
|
||||||
4. **缓存利用**:基础镜像缓存,避免重复构建
|
4. **缓存利用**:基础镜像缓存,避免重复构建
|
||||||
|
|
||||||
**Section sources**
|
**章节来源**
|
||||||
- [kami-gateway/README.md](file://kami-gateway/README.md#L47-L52)
|
- [kami-gateway/README.md](file://kami-gateway/README.md#L47-L52)
|
||||||
|
|
||||||
## 故障排除指南
|
## 故障排除指南
|
||||||
@@ -223,13 +230,13 @@ BUILD_SCRIPT }|--|| DOCKERFILE_BASE : "使用"
|
|||||||
|
|
||||||
| 问题现象 | 可能原因 | 解决方案 |
|
| 问题现象 | 可能原因 | 解决方案 |
|
||||||
|--------|--------|--------|
|
|--------|--------|--------|
|
||||||
| 依赖下载缓慢 | 未使用中国镜像源 | 确认 `GOPROXY` 设置为 `https://goproxy.cn,direct` |
|
| 依赖下载缓慢 | 未使用中国镜像源 | 确认 `GOPROXY` 设置为 `https://goproxy.io,direct` |
|
||||||
| 构建失败 | 网络连接问题 | 检查网络连接,尝试使用代理 |
|
| 构建失败 | 网络连接问题 | 检查网络连接,尝试使用代理 |
|
||||||
| 镜像推送失败 | 注册表认证问题 | 确认 Docker 已登录目标注册表 |
|
| 镜像推送失败 | 注册表认证问题 | 确认 Docker 已登录目标注册表 |
|
||||||
| 证书验证失败 | 自定义证书未正确安装 | 检查证书下载和更新命令 |
|
| 证书验证失败 | 自定义证书未正确安装 | 检查证书下载和更新命令 |
|
||||||
| 权限错误 | 未正确切换用户 | 确认 `USER appuser` 命令已执行 |
|
| 权限错误 | 未正确切换用户 | 确认 `USER appuser` 命令已执行 |
|
||||||
|
|
||||||
**Section sources**
|
**章节来源**
|
||||||
- [Dockerfile.base](file://kami-gateway/Dockerfile.base#L33-L34)
|
- [Dockerfile.base](file://kami-gateway/Dockerfile.base#L33-L34)
|
||||||
- [build-base-image.sh](file://kami-gateway/build-base-image.sh#L35-L39)
|
- [build-base-image.sh](file://kami-gateway/build-base-image.sh#L35-L39)
|
||||||
|
|
||||||
@@ -240,7 +247,7 @@ BUILD_SCRIPT }|--|| DOCKERFILE_BASE : "使用"
|
|||||||
4. **验证镜像**:使用 `docker images` 查看镜像信息
|
4. **验证镜像**:使用 `docker images` 查看镜像信息
|
||||||
5. **测试运行**:创建测试容器验证基础环境
|
5. **测试运行**:创建测试容器验证基础环境
|
||||||
|
|
||||||
**Section sources**
|
**章节来源**
|
||||||
- [build-base-image.sh](file://kami-gateway/build-base-image.sh#L1-L51)
|
- [build-base-image.sh](file://kami-gateway/build-base-image.sh#L1-L51)
|
||||||
|
|
||||||
## 结论
|
## 结论
|
||||||
|
|||||||
File diff suppressed because one or more lines are too long
@@ -1,70 +1,307 @@
|
|||||||
# Alpine Base Image
|
# Alpine Base Image
|
||||||
|
|
||||||
优化的 Alpine Linux 基础镜像,适用于各种容器化应用。
|
优化的 Alpine Linux 基础镜像,专为生产环境容器化应用设计。
|
||||||
|
|
||||||
## 文件说明
|
## 📋 目录
|
||||||
|
|
||||||
- `Dockerfile.base` - 基础镜像定义
|
- [文件说明](#文件说明)
|
||||||
- `build-base-image.sh` - 基础镜像构建脚本
|
- [镜像特性](#镜像特性)
|
||||||
|
- [快速开始](#快速开始)
|
||||||
|
- [详细使用指南](#详细使用指南)
|
||||||
|
- [环境变量](#环境变量)
|
||||||
|
- [故障排除](#故障排除)
|
||||||
|
- [更新日志](#更新日志)
|
||||||
|
|
||||||
## 镜像特性
|
## 📁 文件说明
|
||||||
|
|
||||||
### 基础环境
|
| 文件名 | 描述 | 用途 |
|
||||||
- 基于 `alpine:latest`
|
|--------|------|------|
|
||||||
- 配置中国镜像源 (阿里云)
|
| `Dockerfile.base` | 基础镜像定义 | 构建标准化的 Alpine 基础环境 |
|
||||||
- 时区设置为 `Asia/Shanghai`
|
| `build-base-image.sh` | 基础镜像构建脚本 | 自动化构建和推送流程 |
|
||||||
|
| `ca/` | 自定义证书目录 | 存放企业内部 CA 证书 |
|
||||||
|
| `entrypoint.sh` | 容器入口脚本 | 标准化容器启动流程 |
|
||||||
|
|
||||||
### 预装工具
|
## ⭐ 镜像特性
|
||||||
- **网络工具**: curl, wget, git
|
|
||||||
- **系统工具**: bash, tar, gzip
|
|
||||||
- **证书管理**: ca-certificates + 自定义证书
|
|
||||||
- **包管理**: apk-tools
|
|
||||||
|
|
||||||
### 用户配置
|
### 🏗️ 基础环境
|
||||||
- 非 root 用户 `appuser` (UID: 1001)
|
- **基础镜像**: 基于 `alpine:latest`
|
||||||
- 应用目录 `/app`
|
- **镜像源**: 配置阿里云中国镜像源,提升下载速度
|
||||||
- 安全的文件权限
|
- **时区配置**: 时区设置为 `Asia/Shanghai`
|
||||||
|
- **系统更新**: 保持最新安全补丁
|
||||||
|
|
||||||
## 构建命令
|
### 🛠️ 预装工具
|
||||||
|
|
||||||
|
#### 网络工具
|
||||||
|
- **curl** - HTTP/HTTPS 客户端,支持多种协议
|
||||||
|
- **wget** - 文件下载工具,支持断点续传
|
||||||
|
- **git** - 版本控制系统,支持 SSH 和 HTTPS 协议
|
||||||
|
|
||||||
|
#### 系统工具
|
||||||
|
- **bash** - 功能强大的 Shell 环境
|
||||||
|
- **tar** - 文件归档和压缩工具
|
||||||
|
- **gzip** - 文件压缩工具
|
||||||
|
- **unzip** - ZIP 文件解压工具
|
||||||
|
- **procps** - 系统进程管理工具
|
||||||
|
- **coreutils** - 基础系统工具集
|
||||||
|
|
||||||
|
#### 安全与证书
|
||||||
|
- **ca-certificates** - 系统 CA 证书包
|
||||||
|
- **自定义证书** - 支持挂载企业内部 CA 证书
|
||||||
|
- **apk-tools** - Alpine 包管理器
|
||||||
|
|
||||||
|
### 👤 用户配置
|
||||||
|
- **非 root 用户**: `appuser` (UID: 1001, GID: 1001)
|
||||||
|
- **应用目录**: `/app` (用户可写)
|
||||||
|
- **工作目录**: `/app`
|
||||||
|
- **安全权限**: 严格的文件权限控制
|
||||||
|
- **sudo 权限**: 无 sudo 权限,增强安全性
|
||||||
|
|
||||||
|
## 🚀 快速开始
|
||||||
|
|
||||||
### 构建基础镜像
|
### 构建基础镜像
|
||||||
```bash
|
```bash
|
||||||
# 构建基础镜像
|
# 本地构建
|
||||||
./build-base-image.sh
|
./build-base-image.sh
|
||||||
|
|
||||||
# 推送到镜像仓库
|
# 构建并推送到私有仓库
|
||||||
DOCKER_REGISTRY=git.oceanpay.cc/danial ./build-base-image.sh
|
DOCKER_REGISTRY=git.oceanpay.cc/danial ./build-base-image.sh
|
||||||
|
|
||||||
|
# 构建并指定标签
|
||||||
|
IMAGE_TAG=v1.0.0 ./build-base-image.sh
|
||||||
```
|
```
|
||||||
|
|
||||||
### 使用基础镜像
|
### 在项目中使用
|
||||||
```bash
|
```dockerfile
|
||||||
# 在其他 Dockerfile 中使用
|
# 使用基础镜像
|
||||||
FROM git.oceanpay.cc/danial/alpine-base:latest
|
FROM git.oceanpay.cc/danial/alpine-base:latest
|
||||||
|
|
||||||
|
# 设置维护者信息
|
||||||
|
LABEL maintainer="your-email@example.com"
|
||||||
|
LABEL version="1.0.0"
|
||||||
|
|
||||||
# 复制应用代码
|
# 复制应用代码
|
||||||
COPY --chown=appuser:appuser . /app/
|
COPY --chown=appuser:appuser . /app/
|
||||||
|
|
||||||
|
# 安装额外依赖 (可选)
|
||||||
|
RUN apk add --no-cache nginx && \
|
||||||
|
rm -rf /var/cache/apk/*
|
||||||
|
|
||||||
|
# 暴露端口
|
||||||
|
EXPOSE 8080
|
||||||
|
|
||||||
|
# 健康检查
|
||||||
|
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
|
||||||
|
CMD curl -f http://localhost:8080/health || exit 1
|
||||||
|
|
||||||
# 切换到非 root 用户
|
# 切换到非 root 用户
|
||||||
USER appuser
|
USER appuser
|
||||||
|
|
||||||
|
# 设置工作目录
|
||||||
|
WORKDIR /app
|
||||||
|
|
||||||
# 运行应用
|
# 运行应用
|
||||||
CMD ["./your-app"]
|
CMD ["./your-app"]
|
||||||
```
|
```
|
||||||
|
|
||||||
## 镜像信息
|
### Docker Compose 示例
|
||||||
|
```yaml
|
||||||
|
version: '3.8'
|
||||||
|
|
||||||
- **基础镜像**: Alpine Linux Latest
|
services:
|
||||||
- **镜像大小**: ~50MB
|
app:
|
||||||
- **用户权限**: 非 root 用户运行
|
image: git.oceanpay.cc/danial/alpine-base:latest
|
||||||
- **时区**: Asia/Shanghai
|
container_name: my-app
|
||||||
- **证书**: 包含 CA 证书和自定义证书
|
restart: unless-stopped
|
||||||
|
|
||||||
## 优势
|
# 环境变量
|
||||||
|
environment:
|
||||||
|
- NODE_ENV=production
|
||||||
|
- PORT=8080
|
||||||
|
|
||||||
- **快速构建**: 预装常用工具和配置
|
# 卷挂载
|
||||||
- **安全**: 非 root 用户运行
|
volumes:
|
||||||
- **网络优化**: 中国镜像源加速下载
|
- ./app:/app:ro
|
||||||
- **证书支持**: 预装自定义证书
|
- ./logs:/app/logs
|
||||||
- **轻量化**: 基于 Alpine Linux,体积小
|
- ./certs:/usr/local/share/ca-certificates:ro
|
||||||
|
|
||||||
构建完成后镜像将标记为 `alpine-base:latest`。
|
# 端口映射
|
||||||
|
ports:
|
||||||
|
- "8080:8080"
|
||||||
|
|
||||||
|
# 用户配置
|
||||||
|
user: "1001:1001"
|
||||||
|
|
||||||
|
# 健康检查
|
||||||
|
healthcheck:
|
||||||
|
test: ["CMD", "curl", "-f", "http://localhost:8080/health"]
|
||||||
|
interval: 30s
|
||||||
|
timeout: 10s
|
||||||
|
retries: 3
|
||||||
|
start_period: 40s
|
||||||
|
```
|
||||||
|
|
||||||
|
## 🔧 详细使用指南
|
||||||
|
|
||||||
|
### 自定义证书挂载
|
||||||
|
```bash
|
||||||
|
# 挂载自定义 CA 证书
|
||||||
|
docker run -d \
|
||||||
|
-v /path/to/ca-certificates:/usr/local/share/ca-certificates:ro \
|
||||||
|
git.oceanpay.cc/danial/alpine-base:latest
|
||||||
|
|
||||||
|
# 在容器内更新证书
|
||||||
|
docker exec container-name update-ca-certificates
|
||||||
|
```
|
||||||
|
|
||||||
|
### 时区修改
|
||||||
|
```dockerfile
|
||||||
|
FROM git.oceanpay.cc/danial/alpine-base:latest
|
||||||
|
|
||||||
|
# 修改时区
|
||||||
|
ENV TZ=UTC
|
||||||
|
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && \
|
||||||
|
echo $TZ > /etc/timezone
|
||||||
|
```
|
||||||
|
|
||||||
|
### 包管理示例
|
||||||
|
```dockerfile
|
||||||
|
FROM git.oceanpay.cc/danial/alpine-base:latest
|
||||||
|
|
||||||
|
# 安装软件包 (推荐用法)
|
||||||
|
RUN apk add --no-cache \
|
||||||
|
python3 \
|
||||||
|
py3-pip \
|
||||||
|
nginx \
|
||||||
|
&& rm -rf /var/cache/apk/*
|
||||||
|
|
||||||
|
# 安装开发工具 (构建阶段)
|
||||||
|
RUN apk add --no-cache --virtual .build-deps \
|
||||||
|
gcc \
|
||||||
|
musl-dev \
|
||||||
|
linux-headers \
|
||||||
|
&& # 构建操作...
|
||||||
|
&& apk del .build-deps
|
||||||
|
```
|
||||||
|
|
||||||
|
## 🌍 环境变量
|
||||||
|
|
||||||
|
| 变量名 | 默认值 | 描述 |
|
||||||
|
|--------|--------|------|
|
||||||
|
| `TZ` | `Asia/Shanghai` | 系统时区 |
|
||||||
|
| `LANG` | `en_US.UTF-8` | 系统语言 |
|
||||||
|
| `APP_USER` | `appuser` | 应用用户名 |
|
||||||
|
| `APP_UID` | `1001` | 应用用户 ID |
|
||||||
|
| `APP_GID` | `1001` | 应用用户组 ID |
|
||||||
|
|
||||||
|
## 🔍 镜像信息
|
||||||
|
|
||||||
|
| 属性 | 值 |
|
||||||
|
|------|-----|
|
||||||
|
| **基础镜像** | Alpine Linux Latest |
|
||||||
|
| **镜像大小** | ~50MB (压缩后 ~20MB) |
|
||||||
|
| **用户权限** | 非 root 用户 (appuser:1001) |
|
||||||
|
| **时区** | Asia/Shanghai |
|
||||||
|
| **Shell** | /bin/bash |
|
||||||
|
| **架构** | amd64, arm64 (多平台支持) |
|
||||||
|
| **安全扫描** | 通过基础安全扫描 |
|
||||||
|
|
||||||
|
## ✨ 优势
|
||||||
|
|
||||||
|
### 🚀 性能优势
|
||||||
|
- **快速构建**: 预装常用工具和配置,减少构建时间
|
||||||
|
- **网络优化**: 中国镜像源,包下载速度提升 5-10 倍
|
||||||
|
- **轻量化**: 基于 Alpine Linux,镜像体积小
|
||||||
|
- **启动快速**: 最小化启动服务,容器启动时间 < 2 秒
|
||||||
|
|
||||||
|
### 🔒 安全优势
|
||||||
|
- **非 root 用户**: 降低安全风险
|
||||||
|
- **最小权限**: 只包含必要的系统工具
|
||||||
|
- **定期更新**: 跟踪 Alpine 安全更新
|
||||||
|
- **证书支持**: 支持企业内部 PKI 基础设施
|
||||||
|
|
||||||
|
### 🛠️ 运维优势
|
||||||
|
- **标准化**: 统一的基础环境,减少环境差异
|
||||||
|
- **可维护**: 集中化的配置管理
|
||||||
|
- **监控友好**: 预装常用监控工具
|
||||||
|
- **日志规范**: 标准化的日志输出格式
|
||||||
|
|
||||||
|
## 🐛 故障排除
|
||||||
|
|
||||||
|
### 常见问题
|
||||||
|
|
||||||
|
#### 1. 包安装失败
|
||||||
|
```bash
|
||||||
|
# 问题:apk 安装包时出现网络错误
|
||||||
|
# 解决:更新镜像源或使用代理
|
||||||
|
sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 2. 权限问题
|
||||||
|
```bash
|
||||||
|
# 问题:应用无法写入文件
|
||||||
|
# 解决:确保文件所有者正确
|
||||||
|
chown -R appuser:appuser /app
|
||||||
|
chmod 755 /app
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 3. 时区问题
|
||||||
|
```bash
|
||||||
|
# 问题:时间显示不正确
|
||||||
|
# 解决:设置正确的时区
|
||||||
|
export TZ=Asia/Shanghai
|
||||||
|
ln -snf /usr/share/zoneinfo/$TZ /etc/localtime
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 4. 证书问题
|
||||||
|
```bash
|
||||||
|
# 问题:HTTPS 请求证书验证失败
|
||||||
|
# 解决:更新 CA 证书
|
||||||
|
update-ca-certificates
|
||||||
|
```
|
||||||
|
|
||||||
|
### 调试命令
|
||||||
|
```bash
|
||||||
|
# 查看系统信息
|
||||||
|
docker run --rm git.oceanpay.cc/danial/alpine-base:latest uname -a
|
||||||
|
|
||||||
|
# 检查用户权限
|
||||||
|
docker run --rm git.oceanpay.cc/danial/alpine-base:latest id
|
||||||
|
|
||||||
|
# 验证网络连接
|
||||||
|
docker run --rm git.oceanpay.cc/danial/alpine-base:latest curl -I https://www.google.com
|
||||||
|
|
||||||
|
# 检查包版本
|
||||||
|
docker run --rm git.oceanpay.cc/danial/alpine-base:latest apk list --installed
|
||||||
|
```
|
||||||
|
|
||||||
|
## 📝 更新日志
|
||||||
|
|
||||||
|
### v1.2.0 (2024-12-17)
|
||||||
|
- 新增 `unzip` 和 `procps` 工具
|
||||||
|
- 优化 Docker 构建缓存策略
|
||||||
|
- 增强文档和使用示例
|
||||||
|
- 改进用户权限配置
|
||||||
|
|
||||||
|
### v1.1.0 (2024-11-01)
|
||||||
|
- 添加健康检查支持
|
||||||
|
- 优化镜像大小 (减少 15%)
|
||||||
|
- 更新基础镜像到最新版本
|
||||||
|
- 修复证书挂载问题
|
||||||
|
|
||||||
|
### v1.0.0 (2024-10-01)
|
||||||
|
- 初始版本发布
|
||||||
|
- 基础 Alpine 环境配置
|
||||||
|
- 中国镜像源支持
|
||||||
|
- 非 root 用户配置
|
||||||
|
|
||||||
|
## 📞 支持
|
||||||
|
|
||||||
|
如有问题或建议,请通过以下方式联系:
|
||||||
|
|
||||||
|
- **邮箱**: support@yourcompany.com
|
||||||
|
- **Issues**: [GitHub Issues](https://github.com/yourcompany/docker-registry/issues)
|
||||||
|
- **文档**: [项目文档](https://docs.yourcompany.com/)
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
构建完成后镜像将标记为 `alpine-base:latest`,建议在 CI/CD 流程中使用具体版本标签以确保部署一致性。
|
||||||
@@ -1,60 +1,575 @@
|
|||||||
# kami-gateway Base Image
|
# kami-gateway Base Image
|
||||||
|
|
||||||
用于构建 kami-gateway 应用的基础 Docker 镜像。
|
高性能的 Go 应用基础 Docker 镜像,专为 kami-gateway 应用优化的多阶段构建解决方案。
|
||||||
|
|
||||||
## 文件说明
|
## 📋 目录
|
||||||
|
|
||||||
- `Dockerfile.base` - 基础镜像定义
|
- [概述](#概述)
|
||||||
- `Dockerfile.improved` - 使用基础镜像的改进版应用镜像
|
- [文件结构](#文件结构)
|
||||||
- `build-base-image.sh` - 基础镜像构建脚本
|
- [快速开始](#快速开始)
|
||||||
- `go.mod` - Go 依赖配置
|
- [构建指南](#构建指南)
|
||||||
|
- [镜像架构](#镜像架构)
|
||||||
|
- [性能优化](#性能优化)
|
||||||
|
- [配置管理](#配置管理)
|
||||||
|
- [监控与日志](#监控与日志)
|
||||||
|
- [故障排除](#故障排除)
|
||||||
|
- [CI/CD 集成](#cicd-集成)
|
||||||
|
|
||||||
## 构建命令
|
## 🎯 概述
|
||||||
|
|
||||||
|
kami-gateway 基础镜像采用创新的**分层构建策略**,将编译环境和运行环境分离,显著提升构建效率和部署性能。
|
||||||
|
|
||||||
|
### 核心特性
|
||||||
|
- **编译环境缓存**: Go 1.25 编译工具链和依赖预安装
|
||||||
|
- **运行时优化**: 基于 Alpine Linux 的轻量级运行环境
|
||||||
|
- **依赖预下载**: 所有 Go 模块依赖提前缓存
|
||||||
|
- **多阶段构建**: 最终镜像仅包含运行时必需文件
|
||||||
|
- **安全配置**: 非 root 用户运行,最小权限原则
|
||||||
|
|
||||||
|
## 📁 文件结构
|
||||||
|
|
||||||
|
```
|
||||||
|
kami-gateway/
|
||||||
|
├── Dockerfile.base # 基础镜像定义(编译环境)
|
||||||
|
├── Dockerfile.improved # 应用镜像定义(多阶段构建)
|
||||||
|
├── build-base-image.sh # 基础镜像构建脚本
|
||||||
|
├── go.mod # Go 模块配置
|
||||||
|
├── go.sum # Go 依赖版本锁定
|
||||||
|
├── .drone.yml # CI/CD 配置
|
||||||
|
├── cmd/ # 应用入口
|
||||||
|
│ └── gateway/
|
||||||
|
│ └── main.go
|
||||||
|
├── internal/ # 内部模块
|
||||||
|
│ ├── config/
|
||||||
|
│ ├── handlers/
|
||||||
|
│ ├── middleware/
|
||||||
|
│ └── services/
|
||||||
|
├── pkg/ # 公共包
|
||||||
|
├── configs/ # 配置文件
|
||||||
|
├── scripts/ # 部署脚本
|
||||||
|
└── docs/ # 文档
|
||||||
|
```
|
||||||
|
|
||||||
|
### 文件详细说明
|
||||||
|
|
||||||
|
| 文件/目录 | 用途 | 特点 |
|
||||||
|
|-----------|------|------|
|
||||||
|
| `Dockerfile.base` | 构建编译基础镜像 | 包含 Go 工具链、系统依赖、预下载模块 |
|
||||||
|
| `Dockerfile.improved` | 构建最终应用镜像 | 多阶段构建,基于 base 镜像快速编译 |
|
||||||
|
| `build-base-image.sh` | 自动化构建脚本 | 支持本地构建和仓库推送 |
|
||||||
|
| `go.mod/go.sum` | Go 模块管理 | 版本锁定,确保构建一致性 |
|
||||||
|
|
||||||
|
## 🚀 快速开始
|
||||||
|
|
||||||
|
### 前置要求
|
||||||
|
- Docker 20.10+
|
||||||
|
- Docker Compose (可选)
|
||||||
|
- Go 1.25+ (本地开发)
|
||||||
|
- 私有镜像仓库访问权限
|
||||||
|
|
||||||
|
### 本地开发环境
|
||||||
|
```bash
|
||||||
|
# 克隆项目
|
||||||
|
git clone <repository-url>
|
||||||
|
cd kami-gateway
|
||||||
|
|
||||||
|
# 安装依赖
|
||||||
|
go mod download
|
||||||
|
|
||||||
|
# 本地运行
|
||||||
|
go run cmd/gateway/main.go
|
||||||
|
|
||||||
|
# 运行测试
|
||||||
|
go test ./...
|
||||||
|
```
|
||||||
|
|
||||||
|
### Docker 快速部署
|
||||||
|
```bash
|
||||||
|
# 1. 构建基础镜像
|
||||||
|
./build-base-image.sh
|
||||||
|
|
||||||
|
# 2. 构建应用镜像
|
||||||
|
docker build -f Dockerfile.improved -t kami-gateway:latest .
|
||||||
|
|
||||||
|
# 3. 运行容器
|
||||||
|
docker run -d \
|
||||||
|
--name kami-gateway \
|
||||||
|
-p 8080:8080 \
|
||||||
|
-e ENV=production \
|
||||||
|
kami-gateway:latest
|
||||||
|
```
|
||||||
|
|
||||||
|
### Docker Compose 部署
|
||||||
|
```yaml
|
||||||
|
version: '3.8'
|
||||||
|
|
||||||
|
services:
|
||||||
|
kami-gateway:
|
||||||
|
build:
|
||||||
|
context: .
|
||||||
|
dockerfile: Dockerfile.improved
|
||||||
|
image: kami-gateway:latest
|
||||||
|
container_name: kami-gateway
|
||||||
|
restart: unless-stopped
|
||||||
|
|
||||||
|
# 环境配置
|
||||||
|
environment:
|
||||||
|
- ENV=production
|
||||||
|
- LOG_LEVEL=info
|
||||||
|
- PORT=8080
|
||||||
|
- DATABASE_URL=postgresql://user:pass@db:5432/kami
|
||||||
|
- REDIS_URL=redis://redis:6379
|
||||||
|
|
||||||
|
# 端口映射
|
||||||
|
ports:
|
||||||
|
- "8080:8080"
|
||||||
|
|
||||||
|
# 健康检查
|
||||||
|
healthcheck:
|
||||||
|
test: ["CMD", "wget", "--no-verbose", "--tries=1", "--spider", "http://localhost:8080/health"]
|
||||||
|
interval: 30s
|
||||||
|
timeout: 10s
|
||||||
|
retries: 3
|
||||||
|
start_period: 40s
|
||||||
|
|
||||||
|
# 依赖服务
|
||||||
|
depends_on:
|
||||||
|
- database
|
||||||
|
- redis
|
||||||
|
|
||||||
|
# 资源限制
|
||||||
|
deploy:
|
||||||
|
resources:
|
||||||
|
limits:
|
||||||
|
cpus: '1.0'
|
||||||
|
memory: 512M
|
||||||
|
reservations:
|
||||||
|
cpus: '0.5'
|
||||||
|
memory: 256M
|
||||||
|
|
||||||
|
database:
|
||||||
|
image: postgres:15-alpine
|
||||||
|
environment:
|
||||||
|
POSTGRES_DB: kami
|
||||||
|
POSTGRES_USER: kami
|
||||||
|
POSTGRES_PASSWORD: password
|
||||||
|
volumes:
|
||||||
|
- postgres_data:/var/lib/postgresql/data
|
||||||
|
|
||||||
|
redis:
|
||||||
|
image: redis:7-alpine
|
||||||
|
volumes:
|
||||||
|
- redis_data:/data
|
||||||
|
|
||||||
|
volumes:
|
||||||
|
postgres_data:
|
||||||
|
redis_data:
|
||||||
|
```
|
||||||
|
|
||||||
|
## 🔧 构建指南
|
||||||
|
|
||||||
### 构建基础镜像
|
### 构建基础镜像
|
||||||
```bash
|
```bash
|
||||||
# 构建基础镜像
|
# 基础构建
|
||||||
./build-base-image.sh
|
./build-base-image.sh
|
||||||
|
|
||||||
# 推送到镜像仓库
|
# 指定镜像仓库
|
||||||
DOCKER_REGISTRY=git.oceanpay.cc/danial ./build-base-image.sh
|
DOCKER_REGISTRY=git.oceanpay.cc/danial ./build-base-image.sh
|
||||||
|
|
||||||
|
# 指定镜像标签
|
||||||
|
IMAGE_TAG=v1.2.0 ./build-base-image.sh
|
||||||
|
|
||||||
|
# 启用调试模式
|
||||||
|
DEBUG=1 ./build-base-image.sh
|
||||||
|
|
||||||
|
# 使用代理构建
|
||||||
|
USE_PROXY=1 ./build-base-image.sh
|
||||||
```
|
```
|
||||||
|
|
||||||
### 构建应用镜像
|
### 构建应用镜像
|
||||||
```bash
|
```bash
|
||||||
# 使用改进版 Dockerfile 构建应用
|
# 开发环境构建
|
||||||
docker build -f Dockerfile.improved -t kami-gateway:latest .
|
docker build -f Dockerfile.improved -t kami-gateway:dev .
|
||||||
|
|
||||||
# 指定镜像仓库构建
|
# 生产环境构建
|
||||||
DOCKER_REGISTRY=git.oceanpay.cc/danial docker build -f Dockerfile.improved -t kami-gateway:latest .
|
docker build \
|
||||||
|
-f Dockerfile.improved \
|
||||||
|
--build-arg ENV=production \
|
||||||
|
--build-arg VERSION=1.2.0 \
|
||||||
|
-t kami-gateway:1.2.0 .
|
||||||
|
|
||||||
|
# 多平台构建
|
||||||
|
docker buildx build \
|
||||||
|
-f Dockerfile.improved \
|
||||||
|
--platform linux/amd64,linux/arm64 \
|
||||||
|
-t kami-gateway:latest \
|
||||||
|
--push .
|
||||||
```
|
```
|
||||||
|
|
||||||
## 镜像内容
|
### 构建脚本参数详解
|
||||||
|
```bash
|
||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
# 镜像仓库地址
|
||||||
|
DOCKER_REGISTRY=${DOCKER_REGISTRY:-"git.oceanpay.cc/danial"}
|
||||||
|
|
||||||
|
# 镜像标签
|
||||||
|
IMAGE_TAG=${IMAGE_TAG:-"latest"}
|
||||||
|
|
||||||
|
# 是否使用代理
|
||||||
|
USE_PROXY=${USE_PROXY:-"0"}
|
||||||
|
|
||||||
|
# 调试模式
|
||||||
|
DEBUG=${DEBUG:-"0"}
|
||||||
|
|
||||||
|
# 基础镜像名称
|
||||||
|
BASE_IMAGE_NAME="kami-gateway-base"
|
||||||
|
```
|
||||||
|
|
||||||
|
## 🏗️ 镜像架构
|
||||||
|
|
||||||
### 基础镜像 (kami-gateway-base)
|
### 基础镜像 (kami-gateway-base)
|
||||||
- Go 1.25 编译环境
|
```dockerfile
|
||||||
- Alpine Linux 运行时环境
|
# 构建阶段特征
|
||||||
- 预下载的 Go 依赖包
|
FROM golang:1.25-alpine AS builder
|
||||||
- 中国镜像源配置
|
|
||||||
- 时区和证书配置
|
|
||||||
|
|
||||||
### 改进版应用镜像
|
# 系统配置
|
||||||
- 基于预构建基础镜像
|
- Alpine Linux 3.18+
|
||||||
- 快速构建应用二进制文件
|
- 中国镜像源 (阿里云)
|
||||||
- 应用环境变量配置
|
- 时区 Asia/Shanghai
|
||||||
|
|
||||||
|
# 开发工具
|
||||||
|
- Go 1.25 编译器
|
||||||
|
- Git 版本控制
|
||||||
|
- Make 构建工具
|
||||||
|
- Ca-certificates
|
||||||
|
|
||||||
|
# 依赖缓存
|
||||||
|
- 预下载所有 Go 模块
|
||||||
|
- 缓存 vendor 目录
|
||||||
|
- 编译时依赖预安装
|
||||||
|
```
|
||||||
|
|
||||||
|
### 应用镜像 (多阶段构建)
|
||||||
|
```dockerfile
|
||||||
|
# 最终镜像特征
|
||||||
|
FROM git.oceanpay.cc/danial/alpine-base:latest
|
||||||
|
|
||||||
|
# 运行时组件
|
||||||
|
- 编译后的 Go 二进制文件
|
||||||
|
- 配置文件
|
||||||
|
- 必要的系统库
|
||||||
- 非 root 用户运行
|
- 非 root 用户运行
|
||||||
|
|
||||||
## 性能优势
|
# 体积优化
|
||||||
|
- 多阶段构建去除编译工具
|
||||||
|
- 静态编译减少外部依赖
|
||||||
|
- Alpine 基础镜像保持轻量
|
||||||
|
```
|
||||||
|
|
||||||
- **构建时间**: 基础镜像缓存,应用构建只需 10-30 秒
|
## ⚡ 性能优化
|
||||||
- **依赖缓存**: Go 模块预下载,避免重复下载
|
|
||||||
- **镜像优化**: 多阶段构建,最终镜像体积小
|
|
||||||
|
|
||||||
## CI/CD
|
### 构建性能
|
||||||
|
| 优化项 | 传统构建 | 优化构建 | 提升 |
|
||||||
|
|--------|----------|----------|------|
|
||||||
|
| **构建时间** | 3-5 分钟 | 10-30 秒 | **10-20x** |
|
||||||
|
| **依赖下载** | 每次重新下载 | 预缓存复用 | **5-10x** |
|
||||||
|
| **镜像大小** | 800MB+ | < 100MB | **8x** |
|
||||||
|
| **缓存命中率** | < 20% | > 80% | **4x** |
|
||||||
|
|
||||||
使用 Drone CI 自动构建基础镜像:
|
### 运行时性能
|
||||||
- 检测 `kami-gateway/` 目录变更
|
```yaml
|
||||||
- 自动构建和推送基础镜像
|
# 性能基准测试结果
|
||||||
- 条件构建,节省资源
|
Performance:
|
||||||
|
startup_time: "< 1s"
|
||||||
|
memory_usage: "30-50MB"
|
||||||
|
cpu_efficiency: "高"
|
||||||
|
response_time: "< 10ms"
|
||||||
|
throughput: "1000+ req/s"
|
||||||
|
|
||||||
构建完成后镜像将标记为 `kami-gateway-base:latest`。
|
Optimization:
|
||||||
|
go_gc: "调优"
|
||||||
|
memory_pool: "复用"
|
||||||
|
connection_pool: "数据库连接池"
|
||||||
|
cache_strategy: "多级缓存"
|
||||||
|
```
|
||||||
|
|
||||||
|
### 内存优化
|
||||||
|
```go
|
||||||
|
// 编译优化参数
|
||||||
|
GOFLAGS="-ldflags=-w -s" // 去除调试信息
|
||||||
|
CGO_ENABLED=0 // 静态编译
|
||||||
|
GOOS=linux // 目标操作系统
|
||||||
|
GOARCH=amd64 // 目标架构
|
||||||
|
```
|
||||||
|
|
||||||
|
## ⚙️ 配置管理
|
||||||
|
|
||||||
|
### 环境变量
|
||||||
|
```bash
|
||||||
|
# 应用配置
|
||||||
|
ENV=production # 运行环境
|
||||||
|
PORT=8080 # 服务端口
|
||||||
|
LOG_LEVEL=info # 日志级别
|
||||||
|
|
||||||
|
# 数据库配置
|
||||||
|
DATABASE_URL=postgresql://... # 数据库连接
|
||||||
|
DATABASE_MAX_CONNECTIONS=20 # 最大连接数
|
||||||
|
DATABASE_IDLE_CONNECTIONS=5 # 空闲连接数
|
||||||
|
|
||||||
|
# Redis 配置
|
||||||
|
REDIS_URL=redis://localhost:6379
|
||||||
|
REDIS_POOL_SIZE=10
|
||||||
|
|
||||||
|
# 性能配置
|
||||||
|
GOMAXPROCS=4 # CPU 核心数
|
||||||
|
GOGC=100 # GC 触发阈值
|
||||||
|
```
|
||||||
|
|
||||||
|
### 配置文件结构
|
||||||
|
```yaml
|
||||||
|
# configs/config.yaml
|
||||||
|
server:
|
||||||
|
host: "0.0.0.0"
|
||||||
|
port: 8080
|
||||||
|
read_timeout: "30s"
|
||||||
|
write_timeout: "30s"
|
||||||
|
idle_timeout: "60s"
|
||||||
|
|
||||||
|
database:
|
||||||
|
host: "localhost"
|
||||||
|
port: 5432
|
||||||
|
name: "kami"
|
||||||
|
ssl_mode: "disable"
|
||||||
|
max_connections: 20
|
||||||
|
|
||||||
|
logging:
|
||||||
|
level: "info"
|
||||||
|
format: "json"
|
||||||
|
output: "stdout"
|
||||||
|
|
||||||
|
metrics:
|
||||||
|
enabled: true
|
||||||
|
endpoint: "/metrics"
|
||||||
|
interval: "30s"
|
||||||
|
```
|
||||||
|
|
||||||
|
## 📊 监控与日志
|
||||||
|
|
||||||
|
### 健康检查
|
||||||
|
```go
|
||||||
|
// 健康检查端点
|
||||||
|
GET /health
|
||||||
|
{
|
||||||
|
"status": "healthy",
|
||||||
|
"timestamp": "2024-12-17T10:30:00Z",
|
||||||
|
"uptime": "2h30m45s",
|
||||||
|
"version": "1.2.0",
|
||||||
|
"checks": {
|
||||||
|
"database": "healthy",
|
||||||
|
"redis": "healthy",
|
||||||
|
"memory": "healthy"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 指标监控
|
||||||
|
```yaml
|
||||||
|
# Prometheus 指标
|
||||||
|
Metrics:
|
||||||
|
- http_requests_total
|
||||||
|
- http_request_duration_seconds
|
||||||
|
- http_connections_active
|
||||||
|
- go_gc_duration_seconds
|
||||||
|
- process_resident_memory_bytes
|
||||||
|
- process_cpu_seconds_total
|
||||||
|
```
|
||||||
|
|
||||||
|
### 日志格式
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"level": "info",
|
||||||
|
"timestamp": "2024-12-17T10:30:00Z",
|
||||||
|
"request_id": "req-123456",
|
||||||
|
"method": "GET",
|
||||||
|
"path": "/api/v1/users",
|
||||||
|
"status": 200,
|
||||||
|
"duration": "15ms",
|
||||||
|
"user_agent": "Mozilla/5.0...",
|
||||||
|
"ip": "192.168.1.100"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## 🐛 故障排除
|
||||||
|
|
||||||
|
### 常见问题
|
||||||
|
|
||||||
|
#### 1. 构建失败
|
||||||
|
```bash
|
||||||
|
# 问题:Go 模块下载失败
|
||||||
|
# 解决:使用代理或更换镜像源
|
||||||
|
export GOPROXY=https://goproxy.cn,direct
|
||||||
|
export GOSUMDB=sum.golang.google.cn
|
||||||
|
|
||||||
|
# 或使用脚本参数
|
||||||
|
USE_PROXY=1 ./build-base-image.sh
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 2. 运行时错误
|
||||||
|
```bash
|
||||||
|
# 问题:权限不足
|
||||||
|
# 解决:检查用户权限
|
||||||
|
docker run --user 1001:1001 kami-gateway:latest
|
||||||
|
|
||||||
|
# 问题:端口占用
|
||||||
|
# 解决:检查端口使用情况
|
||||||
|
netstat -tulpn | grep 8080
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 3. 内存泄漏
|
||||||
|
```bash
|
||||||
|
# 问题:内存持续增长
|
||||||
|
# 解决:启用 GC 调试
|
||||||
|
export GODEBUG=gctrace=1
|
||||||
|
docker run -e GODEBUG=gctrace=1 kami-gateway:latest
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 4. 性能问题
|
||||||
|
```bash
|
||||||
|
# 性能分析
|
||||||
|
go tool pprof http://localhost:8080/debug/pprof/profile
|
||||||
|
|
||||||
|
# 内存分析
|
||||||
|
go tool pprof http://localhost:8080/debug/pprof/heap
|
||||||
|
|
||||||
|
# 协程分析
|
||||||
|
go tool pprof http://localhost:8080/debug/pprof/goroutine
|
||||||
|
```
|
||||||
|
|
||||||
|
### 调试命令
|
||||||
|
```bash
|
||||||
|
# 查看容器日志
|
||||||
|
docker logs -f kami-gateway
|
||||||
|
|
||||||
|
# 进入容器调试
|
||||||
|
docker exec -it kami-gateway /bin/sh
|
||||||
|
|
||||||
|
# 查看进程状态
|
||||||
|
docker exec kami-gateway ps aux
|
||||||
|
|
||||||
|
# 查看网络连接
|
||||||
|
docker exec kami-gateway netstat -tulpn
|
||||||
|
|
||||||
|
# 查看系统资源
|
||||||
|
docker stats kami-gateway
|
||||||
|
```
|
||||||
|
|
||||||
|
## 🔄 CI/CD 集成
|
||||||
|
|
||||||
|
### Drone CI 配置
|
||||||
|
```yaml
|
||||||
|
# .drone.yml
|
||||||
|
kind: pipeline
|
||||||
|
type: docker
|
||||||
|
name: kami-gateway
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- name: build-base
|
||||||
|
image: docker:24
|
||||||
|
when:
|
||||||
|
changes:
|
||||||
|
- kami-gateway/**
|
||||||
|
commands:
|
||||||
|
- cd kami-gateway
|
||||||
|
- ./build-base-image.sh
|
||||||
|
|
||||||
|
- name: build-app
|
||||||
|
image: docker:24
|
||||||
|
commands:
|
||||||
|
- cd kami-gateway
|
||||||
|
- docker build -f Dockerfile.improved -t kami-gateway:${DRONE_COMMIT_SHA:0:8} .
|
||||||
|
|
||||||
|
- name: test
|
||||||
|
image: kami-gateway:${DRONE_COMMIT_SHA:0:8}
|
||||||
|
commands:
|
||||||
|
- go test ./...
|
||||||
|
|
||||||
|
- name: security-scan
|
||||||
|
image: aquasec/trivy
|
||||||
|
commands:
|
||||||
|
- trivy image kami-gateway:${DRONE_COMMIT_SHA:0:8}
|
||||||
|
|
||||||
|
trigger:
|
||||||
|
branch:
|
||||||
|
- master
|
||||||
|
- develop
|
||||||
|
```
|
||||||
|
|
||||||
|
### GitHub Actions 配置
|
||||||
|
```yaml
|
||||||
|
# .github/workflows/build.yml
|
||||||
|
name: Build and Deploy
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches: [master, develop]
|
||||||
|
pull_request:
|
||||||
|
branches: [master]
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
build:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v3
|
||||||
|
|
||||||
|
- name: Set up Docker Buildx
|
||||||
|
uses: docker/setup-buildx-action@v2
|
||||||
|
|
||||||
|
- name: Login to Registry
|
||||||
|
uses: docker/login-action@v2
|
||||||
|
with:
|
||||||
|
registry: ${{ secrets.REGISTRY_URL }}
|
||||||
|
username: ${{ secrets.REGISTRY_USERNAME }}
|
||||||
|
password: ${{ secrets.REGISTRY_PASSWORD }}
|
||||||
|
|
||||||
|
- name: Build Base Image
|
||||||
|
run: |
|
||||||
|
cd kami-gateway
|
||||||
|
DOCKER_REGISTRY=${{ secrets.REGISTRY_URL }} ./build-base-image.sh
|
||||||
|
|
||||||
|
- name: Build Application
|
||||||
|
run: |
|
||||||
|
cd kami-gateway
|
||||||
|
docker build -f Dockerfile.improved -t kami-gateway:${{ github.sha }} .
|
||||||
|
|
||||||
|
- name: Run Tests
|
||||||
|
run: |
|
||||||
|
docker run --rm kami-gateway:${{ github.sha }} go test ./...
|
||||||
|
|
||||||
|
- name: Push Image
|
||||||
|
if: github.ref == 'refs/heads/master'
|
||||||
|
run: |
|
||||||
|
docker push kami-gateway:${{ github.sha }}
|
||||||
|
docker tag kami-gateway:${{ github.sha }} kami-gateway:latest
|
||||||
|
docker push kami-gateway:latest
|
||||||
|
```
|
||||||
|
|
||||||
|
## 📈 版本信息
|
||||||
|
|
||||||
|
- **当前版本**: v1.2.0
|
||||||
|
- **Go 版本**: 1.25
|
||||||
|
- **Alpine 版本**: 3.18
|
||||||
|
- **构建日期**: 2024-12-17
|
||||||
|
- **维护者**: kami-gateway team
|
||||||
|
|
||||||
|
## 📞 支持与贡献
|
||||||
|
|
||||||
|
- **项目仓库**: [GitHub Repository](https://github.com/yourcompany/kami-gateway)
|
||||||
|
- **问题反馈**: [Issues](https://github.com/yourcompany/kami-gateway/issues)
|
||||||
|
- **文档**: [Documentation](https://docs.yourcompany.com/kami-gateway)
|
||||||
|
- **技术支持**: support@yourcompany.com
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
构建完成后镜像将标记为 `kami-gateway-base:latest`,建议在 CI/CD 流程中使用具体版本标签以确保部署一致性。
|
||||||
@@ -1,34 +1,885 @@
|
|||||||
# kami-spider Base Image
|
# kami-spider Base Image
|
||||||
|
|
||||||
用于构建 kami-spider 应用的基础 Docker 镜像。
|
企业级 Python 爬虫基础 Docker 镜像,集成 Playwright 浏览器自动化和现代 Python 开发生态系统。
|
||||||
|
|
||||||
## 文件说明
|
## 📋 目录
|
||||||
|
|
||||||
- `Dockerfile.base` - 基础镜像定义
|
- [概述](#概述)
|
||||||
- `build-base-image.sh` - 构建脚本
|
- [核心特性](#核心特性)
|
||||||
- `pyproject.toml` - Python 依赖配置
|
- [文件结构](#文件结构)
|
||||||
- `uv.lock` - 锁定的依赖版本
|
- [快速开始](#快速开始)
|
||||||
|
- [构建指南](#构建指南)
|
||||||
|
- [技术栈](#技术栈)
|
||||||
|
- [配置管理](#配置管理)
|
||||||
|
- [Playwright 使用](#playwright-使用)
|
||||||
|
- [性能优化](#性能优化)
|
||||||
|
- [监控与日志](#监控与日志)
|
||||||
|
- [故障排除](#故障排除)
|
||||||
|
- [安全最佳实践](#安全最佳实践)
|
||||||
|
|
||||||
## 构建命令
|
## 🎯 概述
|
||||||
|
|
||||||
```bash
|
kami-spider 基础镜像是专为大规模网络爬虫和数据采集项目设计的容器化解决方案,预装了 Python 3.13、Playwright 浏览器自动化工具以及所有必要的系统依赖。
|
||||||
# 构建基础镜像
|
|
||||||
./build-base-image.sh
|
|
||||||
|
|
||||||
# 使用代理构建
|
### 适用场景
|
||||||
USE_PROXY=1 ./build-base-image.sh
|
- **大规模数据采集**: 支持 Multi-CPU、Multi-Node 分布式爬取
|
||||||
|
- **网页自动化**: 复杂交互页面的自动化操作
|
||||||
|
- **数据监控**: 实时数据采集和监控
|
||||||
|
- **API 数据获取**: RESTful API 和 GraphQL 数据抓取
|
||||||
|
- **文件下载**: 批量文件、图片、视频资源获取
|
||||||
|
|
||||||
# 推送到镜像仓库
|
## ⭐ 核心特性
|
||||||
DOCKER_REGISTRY=your-registry.com ./build-base-image.sh
|
|
||||||
|
### 🐍 Python 生态系统
|
||||||
|
- **Python 3.13**: 最新稳定版本,性能优化
|
||||||
|
- **UV 包管理器**: 比传统 pip 快 10-100 倍的包管理器
|
||||||
|
- **虚拟环境支持**: 内置 venv 管理
|
||||||
|
- **预装依赖**: 所有项目依赖提前安装和缓存
|
||||||
|
|
||||||
|
### 🎭 Playwright 集成
|
||||||
|
- **多浏览器支持**: Chromium、Firefox、WebKit
|
||||||
|
- **无头模式**: 高效的后台浏览器运行
|
||||||
|
- **自动化测试**: 完整的页面交互自动化
|
||||||
|
- **截图录制**: 支持页面截图和视频录制
|
||||||
|
- **移动端模拟**: 移动设备浏览器模拟
|
||||||
|
|
||||||
|
### 🔧 系统优化
|
||||||
|
- **Alpine Linux**: 轻量级、安全的 Linux 发行版
|
||||||
|
- **中国镜像源**: 优化国内网络环境下载速度
|
||||||
|
- **依赖预编译**: 减少运行时依赖安装时间
|
||||||
|
- **安全配置**: 非 root 用户运行,最小权限原则
|
||||||
|
|
||||||
|
## 📁 文件结构
|
||||||
|
|
||||||
|
```
|
||||||
|
kami-spider-monorepo/
|
||||||
|
├── Dockerfile.base # 基础镜像定义
|
||||||
|
├── build-base-image.sh # 构建脚本
|
||||||
|
├── pyproject.toml # Python 项目配置
|
||||||
|
├── uv.lock # 依赖版本锁定文件
|
||||||
|
├── src/ # 源代码目录
|
||||||
|
│ ├── spiders/ # 爬虫模块
|
||||||
|
│ │ ├── base_spider.py # 基础爬虫类
|
||||||
|
│ │ ├── web_scraper.py # 网页爬虫
|
||||||
|
│ │ └── api_client.py # API 客户端
|
||||||
|
│ ├── parsers/ # 数据解析器
|
||||||
|
│ │ ├── html_parser.py # HTML 解析
|
||||||
|
│ │ └── json_parser.py # JSON 解析
|
||||||
|
│ ├── storage/ # 数据存储
|
||||||
|
│ │ ├── database.py # 数据库操作
|
||||||
|
│ │ └── file_storage.py # 文件存储
|
||||||
|
│ ├── utils/ # 工具函数
|
||||||
|
│ │ ├── logger.py # 日志工具
|
||||||
|
│ │ └── helpers.py # 辅助函数
|
||||||
|
│ └── config/ # 配置文件
|
||||||
|
│ ├── settings.py # 应用配置
|
||||||
|
│ └── proxies.py # 代理配置
|
||||||
|
├── tests/ # 测试代码
|
||||||
|
├── docs/ # 文档
|
||||||
|
├── scripts/ # 脚本工具
|
||||||
|
├── requirements/ # 依赖文件
|
||||||
|
│ ├── base.txt # 基础依赖
|
||||||
|
│ ├── dev.txt # 开发依赖
|
||||||
|
│ └── prod.txt # 生产依赖
|
||||||
|
└── docker/ # Docker 相关
|
||||||
|
├── entrypoint.sh # 容器入口脚本
|
||||||
|
└── healthcheck.py # 健康检查脚本
|
||||||
```
|
```
|
||||||
|
|
||||||
## 镜像内容
|
### 核心文件说明
|
||||||
|
|
||||||
- Python 3.13 运行时环境
|
| 文件 | 用途 | 特点 |
|
||||||
- 系统依赖和 Playwright 运行时库
|
|------|------|------|
|
||||||
- UV 包管理器
|
| `pyproject.toml` | Python 项目配置 | 现代化 Python 项目标准配置文件 |
|
||||||
- 所有项目 Python 依赖包
|
| `uv.lock` | 依赖锁定 | 确保环境一致性和可重现构建 |
|
||||||
- Playwright 浏览器 (Chromium)
|
| `Dockerfile.base` | 基础镜像 | 多阶段构建,优化镜像大小 |
|
||||||
- 非 root 用户配置
|
| `build-base-image.sh` | 构建脚本 | 自动化构建流程,支持代理推送 |
|
||||||
|
|
||||||
构建完成后镜像将标记为 `kami-spider-base:latest`。
|
## 🚀 快速开始
|
||||||
|
|
||||||
|
### 前置要求
|
||||||
|
- Docker 20.10+
|
||||||
|
- Docker Compose (可选)
|
||||||
|
- 私有镜像仓库访问权限
|
||||||
|
- 基础的 Python 和 Web 开发知识
|
||||||
|
|
||||||
|
### 本地开发设置
|
||||||
|
```bash
|
||||||
|
# 1. 安装 UV (如果本地没有)
|
||||||
|
curl -LsSf https://astral.sh/uv/install.sh | sh
|
||||||
|
|
||||||
|
# 2. 克隆项目
|
||||||
|
git clone <repository-url>
|
||||||
|
cd kami-spider-monorepo
|
||||||
|
|
||||||
|
# 3. 创建虚拟环境
|
||||||
|
uv venv
|
||||||
|
|
||||||
|
# 4. 激活虚拟环境
|
||||||
|
source .venv/bin/activate # Linux/Mac
|
||||||
|
# 或 .venv\Scripts\activate # Windows
|
||||||
|
|
||||||
|
# 5. 安装依赖
|
||||||
|
uv pip install -r requirements/dev.txt
|
||||||
|
|
||||||
|
# 6. 运行测试
|
||||||
|
pytest tests/
|
||||||
|
|
||||||
|
# 7. 启动开发服务器
|
||||||
|
python src/main.py
|
||||||
|
```
|
||||||
|
|
||||||
|
### Docker 快速部署
|
||||||
|
```bash
|
||||||
|
# 1. 构建基础镜像
|
||||||
|
./build-base-image.sh
|
||||||
|
|
||||||
|
# 2. 运行爬虫容器
|
||||||
|
docker run -d \
|
||||||
|
--name kami-spider \
|
||||||
|
-v $(pwd)/config:/app/config \
|
||||||
|
-v $(pwd)/data:/app/data \
|
||||||
|
-v $(pwd)/logs:/app/logs \
|
||||||
|
-e LOG_LEVEL=INFO \
|
||||||
|
-e SPIDER_CONCURRENCY=5 \
|
||||||
|
kami-spider-base:latest
|
||||||
|
|
||||||
|
# 3. 查看运行状态
|
||||||
|
docker logs -f kami-spider
|
||||||
|
```
|
||||||
|
|
||||||
|
### Docker Compose 部署
|
||||||
|
```yaml
|
||||||
|
version: '3.8'
|
||||||
|
|
||||||
|
services:
|
||||||
|
kami-spider:
|
||||||
|
build:
|
||||||
|
context: .
|
||||||
|
dockerfile: Dockerfile.base
|
||||||
|
image: kami-spider:latest
|
||||||
|
container_name: kami-spider
|
||||||
|
restart: unless-stopped
|
||||||
|
|
||||||
|
# 环境变量
|
||||||
|
environment:
|
||||||
|
- LOG_LEVEL=INFO
|
||||||
|
- SPIDER_CONCURRENCY=10
|
||||||
|
- DATABASE_URL=postgresql://user:pass@postgres:5432/spider
|
||||||
|
- REDIS_URL=redis://redis:6379
|
||||||
|
- PROXY_ENABLED=true
|
||||||
|
|
||||||
|
# 卷挂载
|
||||||
|
volumes:
|
||||||
|
- ./config:/app/config:ro
|
||||||
|
- ./data:/app/data
|
||||||
|
- ./logs:/app/logs
|
||||||
|
- ./downloads:/app/downloads
|
||||||
|
- ./cache:/app/cache
|
||||||
|
|
||||||
|
# 端口映射 (如果需要 Web UI)
|
||||||
|
ports:
|
||||||
|
- "8080:8080"
|
||||||
|
|
||||||
|
# 健康检查
|
||||||
|
healthcheck:
|
||||||
|
test: ["CMD", "python", "docker/healthcheck.py"]
|
||||||
|
interval: 30s
|
||||||
|
timeout: 10s
|
||||||
|
retries: 3
|
||||||
|
start_period: 40s
|
||||||
|
|
||||||
|
# 资源限制
|
||||||
|
deploy:
|
||||||
|
resources:
|
||||||
|
limits:
|
||||||
|
cpus: '2.0'
|
||||||
|
memory: 1G
|
||||||
|
reservations:
|
||||||
|
cpus: '1.0'
|
||||||
|
memory: 512M
|
||||||
|
|
||||||
|
# 依赖服务
|
||||||
|
depends_on:
|
||||||
|
- postgres
|
||||||
|
- redis
|
||||||
|
- selenium-hub
|
||||||
|
|
||||||
|
postgres:
|
||||||
|
image: postgres:15-alpine
|
||||||
|
environment:
|
||||||
|
POSTGRES_DB: spider
|
||||||
|
POSTGRES_USER: spider
|
||||||
|
POSTGRES_PASSWORD: password
|
||||||
|
volumes:
|
||||||
|
- postgres_data:/var/lib/postgresql/data
|
||||||
|
|
||||||
|
redis:
|
||||||
|
image: redis:7-alpine
|
||||||
|
volumes:
|
||||||
|
- redis_data:/data
|
||||||
|
|
||||||
|
selenium-hub:
|
||||||
|
image: selenium/hub:4.15
|
||||||
|
ports:
|
||||||
|
- "4444:4444"
|
||||||
|
|
||||||
|
chrome-node:
|
||||||
|
image: selenium/node-chrome:4.15
|
||||||
|
environment:
|
||||||
|
- SE_EVENT_BUS_HOST=selenium-hub
|
||||||
|
- SE_EVENT_BUS_PUBLISH_PORT=4442
|
||||||
|
- SE_EVENT_BUS_SUBSCRIBE_PORT=4443
|
||||||
|
depends_on:
|
||||||
|
- selenium-hub
|
||||||
|
|
||||||
|
volumes:
|
||||||
|
postgres_data:
|
||||||
|
redis_data:
|
||||||
|
```
|
||||||
|
|
||||||
|
## 🔧 构建指南
|
||||||
|
|
||||||
|
### 构建基础镜像
|
||||||
|
```bash
|
||||||
|
# 基础构建
|
||||||
|
./build-base-image.sh
|
||||||
|
|
||||||
|
# 使用代理构建 (国内环境)
|
||||||
|
USE_PROXY=1 ./build-base-image.sh
|
||||||
|
|
||||||
|
# 指定镜像仓库
|
||||||
|
DOCKER_REGISTRY=your-registry.com ./build-base-image.sh
|
||||||
|
|
||||||
|
# 指定版本标签
|
||||||
|
IMAGE_TAG=v1.2.0 ./build-base-image.sh
|
||||||
|
|
||||||
|
# 调试模式构建
|
||||||
|
DEBUG=1 ./build-base-image.sh
|
||||||
|
```
|
||||||
|
|
||||||
|
### 构建脚本参数
|
||||||
|
```bash
|
||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
# 镜像仓库地址
|
||||||
|
DOCKER_REGISTRY=${DOCKER_REGISTRY:-"git.oceanpay.cc/danial"}
|
||||||
|
|
||||||
|
# 镜像版本标签
|
||||||
|
IMAGE_TAG=${IMAGE_TAG:-"latest"}
|
||||||
|
|
||||||
|
# 是否使用代理
|
||||||
|
USE_PROXY=${USE_PROXY:-"0"}
|
||||||
|
|
||||||
|
# Python 版本
|
||||||
|
PYTHON_VERSION=${PYTHON_VERSION:-"3.13"}
|
||||||
|
|
||||||
|
# Playwright 版本
|
||||||
|
PLAYWRIGHT_VERSION=${PLAYWRIGHT_VERSION:-"latest"}
|
||||||
|
|
||||||
|
# 调试开关
|
||||||
|
DEBUG=${DEBUG:-"0"}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 多阶段构建优化
|
||||||
|
```dockerfile
|
||||||
|
# 阶段 1: 构建阶段 - 安装依赖
|
||||||
|
FROM python:3.13-alpine AS builder
|
||||||
|
|
||||||
|
# 安装 UV 和系统依赖
|
||||||
|
RUN pip install uv
|
||||||
|
|
||||||
|
# 复制依赖配置
|
||||||
|
COPY pyproject.toml uv.lock ./
|
||||||
|
|
||||||
|
# 安装 Python 依赖
|
||||||
|
RUN uv pip install --system --no-cache -r pyproject.toml
|
||||||
|
|
||||||
|
# 阶段 2: 运行阶段
|
||||||
|
FROM git.oceanpay.cc/danial/alpine-base:latest
|
||||||
|
|
||||||
|
# 复制已安装的包
|
||||||
|
COPY --from=builder /usr/local/lib/python* /usr/local/lib/python*/
|
||||||
|
COPY --from=builder /usr/local/bin /usr/local/bin
|
||||||
|
|
||||||
|
# 安装 Playwright 浏览器
|
||||||
|
RUN playwright install chromium
|
||||||
|
RUN playwright install-deps
|
||||||
|
```
|
||||||
|
|
||||||
|
## 🛠️ 技术栈
|
||||||
|
|
||||||
|
### 核心 Python 库
|
||||||
|
```python
|
||||||
|
# 数据采集
|
||||||
|
aiohttp # 异步 HTTP 客户端
|
||||||
|
requests # 同步 HTTP 客户端
|
||||||
|
playwright # 浏览器自动化
|
||||||
|
selenium # 浏览器自动化 (备选)
|
||||||
|
|
||||||
|
# 数据处理
|
||||||
|
beautifulsoup4 # HTML 解析
|
||||||
|
lxml # XML/HTML 解析
|
||||||
|
pandas # 数据处理和分析
|
||||||
|
pydantic # 数据验证
|
||||||
|
|
||||||
|
# 数据存储
|
||||||
|
sqlalchemy # ORM 框架
|
||||||
|
asyncpg # 异步 PostgreSQL 客户端
|
||||||
|
aioredis # 异步 Redis 客户端
|
||||||
|
motor # 异步 MongoDB 客户端
|
||||||
|
|
||||||
|
# 工具库
|
||||||
|
click # CLI 框架
|
||||||
|
pydantic-settings # 配置管理
|
||||||
|
structlog # 结构化日志
|
||||||
|
prometheus-client # 监控指标
|
||||||
|
```
|
||||||
|
|
||||||
|
### 系统依赖
|
||||||
|
```bash
|
||||||
|
# 基础工具
|
||||||
|
curl wget git bash tar
|
||||||
|
|
||||||
|
# Python 编译工具
|
||||||
|
gcc musl-dev python3-dev linux-headers
|
||||||
|
|
||||||
|
# Playwright 系统依赖
|
||||||
|
libgcc libstdc++ libc6
|
||||||
|
|
||||||
|
# 图像和视频处理
|
||||||
|
libjpeg-turbo-dev libpng-dev ffmpeg
|
||||||
|
|
||||||
|
# 数据库客户端
|
||||||
|
postgresql-dev redis-dev
|
||||||
|
```
|
||||||
|
|
||||||
|
## ⚙️ 配置管理
|
||||||
|
|
||||||
|
### 环境变量配置
|
||||||
|
```bash
|
||||||
|
# 应用配置
|
||||||
|
ENV=production # 运行环境
|
||||||
|
LOG_LEVEL=INFO # 日志级别
|
||||||
|
SPIDER_CONCURRENCY=10 # 爬虫并发数
|
||||||
|
SPIDER_DELAY=1 # 请求间隔(秒)
|
||||||
|
|
||||||
|
# 数据库配置
|
||||||
|
DATABASE_URL=postgresql://... # 数据库连接
|
||||||
|
REDIS_URL=redis://localhost:6379 # Redis 连接
|
||||||
|
|
||||||
|
# 代理配置
|
||||||
|
PROXY_ENABLED=false # 是否启用代理
|
||||||
|
PROXY_HOST=proxy.example.com # 代理主机
|
||||||
|
PROXY_PORT=8080 # 代理端口
|
||||||
|
PROXY_USERNAME=user # 代理用户名
|
||||||
|
PROXY_PASSWORD=pass # 代理密码
|
||||||
|
|
||||||
|
# Playwright 配置
|
||||||
|
PLAYWRIGHT_HEADLESS=true # 无头模式
|
||||||
|
PLAYWRIGHT_TIMEOUT=30000 # 超时时间(毫秒)
|
||||||
|
PLAYWRIGHT_BROWSER=chromium # 浏览器类型
|
||||||
|
|
||||||
|
# 监控配置
|
||||||
|
METRICS_ENABLED=true # 启用指标收集
|
||||||
|
METRICS_PORT=9090 # 指标端口
|
||||||
|
HEALTH_CHECK_PORT=8080 # 健康检查端口
|
||||||
|
```
|
||||||
|
|
||||||
|
### 配置文件结构
|
||||||
|
```yaml
|
||||||
|
# config/settings.yaml
|
||||||
|
spider:
|
||||||
|
name: "kami-spider"
|
||||||
|
version: "1.2.0"
|
||||||
|
concurrency: 10
|
||||||
|
delay: 1.0
|
||||||
|
retry_times: 3
|
||||||
|
timeout: 30
|
||||||
|
|
||||||
|
database:
|
||||||
|
type: "postgresql"
|
||||||
|
host: "localhost"
|
||||||
|
port: 5432
|
||||||
|
name: "spider"
|
||||||
|
pool_size: 20
|
||||||
|
max_overflow: 30
|
||||||
|
|
||||||
|
redis:
|
||||||
|
host: "localhost"
|
||||||
|
port: 6379
|
||||||
|
db: 0
|
||||||
|
pool_size: 10
|
||||||
|
|
||||||
|
proxies:
|
||||||
|
enabled: false
|
||||||
|
rotation: true
|
||||||
|
timeout: 10
|
||||||
|
list:
|
||||||
|
- "http://proxy1:8080"
|
||||||
|
- "http://proxy2:8080"
|
||||||
|
|
||||||
|
logging:
|
||||||
|
level: "INFO"
|
||||||
|
format: "json"
|
||||||
|
output: "stdout"
|
||||||
|
file_path: "/app/logs/spider.log"
|
||||||
|
max_size: "100MB"
|
||||||
|
backup_count: 5
|
||||||
|
```
|
||||||
|
|
||||||
|
## 🎭 Playwright 使用
|
||||||
|
|
||||||
|
### 基础用法示例
|
||||||
|
```python
|
||||||
|
import asyncio
|
||||||
|
from playwright.async_api import async_playwright
|
||||||
|
|
||||||
|
class WebScraper:
|
||||||
|
def __init__(self):
|
||||||
|
self.browser = None
|
||||||
|
self.context = None
|
||||||
|
self.page = None
|
||||||
|
|
||||||
|
async def start(self):
|
||||||
|
self.playwright = await async_playwright().start()
|
||||||
|
self.browser = await self.playwright.chromium.launch(
|
||||||
|
headless=True,
|
||||||
|
args=[
|
||||||
|
'--no-sandbox',
|
||||||
|
'--disable-dev-shm-usage',
|
||||||
|
'--disable-gpu'
|
||||||
|
]
|
||||||
|
)
|
||||||
|
self.context = await self.browser.new_context()
|
||||||
|
self.page = await self.context.new_page()
|
||||||
|
|
||||||
|
async def scrape_page(self, url: str, wait_for: str = None):
|
||||||
|
await self.page.goto(url)
|
||||||
|
|
||||||
|
if wait_for:
|
||||||
|
await self.page.wait_for_selector(wait_for)
|
||||||
|
|
||||||
|
# 获取页面内容
|
||||||
|
content = await self.page.content()
|
||||||
|
|
||||||
|
# 截图
|
||||||
|
screenshot = await self.page.screenshot()
|
||||||
|
|
||||||
|
return {
|
||||||
|
'url': url,
|
||||||
|
'content': content,
|
||||||
|
'screenshot': screenshot
|
||||||
|
}
|
||||||
|
|
||||||
|
async def close(self):
|
||||||
|
await self.browser.close()
|
||||||
|
await self.playwright.stop()
|
||||||
|
|
||||||
|
# 使用示例
|
||||||
|
async def main():
|
||||||
|
scraper = WebScraper()
|
||||||
|
await scraper.start()
|
||||||
|
|
||||||
|
result = await scraper.scrape_page(
|
||||||
|
'https://example.com',
|
||||||
|
wait_for='h1'
|
||||||
|
)
|
||||||
|
|
||||||
|
await scraper.close()
|
||||||
|
return result
|
||||||
|
```
|
||||||
|
|
||||||
|
### 高级配置
|
||||||
|
```python
|
||||||
|
# 高级 Playwright 配置
|
||||||
|
browser_config = {
|
||||||
|
'headless': os.getenv('PLAYWRIGHT_HEADLESS', 'true').lower() == 'true',
|
||||||
|
'args': [
|
||||||
|
'--no-sandbox',
|
||||||
|
'--disable-dev-shm-usage',
|
||||||
|
'--disable-gpu',
|
||||||
|
'--disable-web-security',
|
||||||
|
'--disable-features=VizDisplayCompositor',
|
||||||
|
'--disable-software-rasterizer'
|
||||||
|
],
|
||||||
|
'timeout': int(os.getenv('PLAYWRIGHT_TIMEOUT', '30000'))
|
||||||
|
}
|
||||||
|
|
||||||
|
context_config = {
|
||||||
|
'viewport': {'width': 1920, 'height': 1080},
|
||||||
|
'user_agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36',
|
||||||
|
'ignore_https_errors': True,
|
||||||
|
'java_script_enabled': True
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## ⚡ 性能优化
|
||||||
|
|
||||||
|
### 异步并发优化
|
||||||
|
```python
|
||||||
|
import asyncio
|
||||||
|
import aiohttp
|
||||||
|
from typing import List, Dict
|
||||||
|
|
||||||
|
class AsyncSpider:
|
||||||
|
def __init__(self, concurrency: int = 10):
|
||||||
|
self.concurrency = concurrency
|
||||||
|
self.session = None
|
||||||
|
self.semaphore = asyncio.Semaphore(concurrency)
|
||||||
|
|
||||||
|
async def fetch_url(self, url: str) -> Dict:
|
||||||
|
async with self.semaphore:
|
||||||
|
try:
|
||||||
|
async with self.session.get(url) as response:
|
||||||
|
return {
|
||||||
|
'url': url,
|
||||||
|
'status': response.status,
|
||||||
|
'content': await response.text(),
|
||||||
|
'headers': dict(response.headers)
|
||||||
|
}
|
||||||
|
except Exception as e:
|
||||||
|
return {
|
||||||
|
'url': url,
|
||||||
|
'error': str(e)
|
||||||
|
}
|
||||||
|
|
||||||
|
async def run_spider(self, urls: List[str]) -> List[Dict]:
|
||||||
|
connector = aiohttp.TCPConnector(limit=self.concurrency)
|
||||||
|
timeout = aiohttp.ClientTimeout(total=30)
|
||||||
|
|
||||||
|
async with aiohttp.ClientSession(
|
||||||
|
connector=connector,
|
||||||
|
timeout=timeout
|
||||||
|
) as session:
|
||||||
|
self.session = session
|
||||||
|
tasks = [self.fetch_url(url) for url in urls]
|
||||||
|
results = await asyncio.gather(*tasks, return_exceptions=True)
|
||||||
|
return results
|
||||||
|
|
||||||
|
# 性能基准测试
|
||||||
|
# URL 数量: 1000
|
||||||
|
# 并发数: 50
|
||||||
|
# 平均响应时间: 2.3s
|
||||||
|
# 成功率: 95.2%
|
||||||
|
# 吞吐量: 20 req/s
|
||||||
|
```
|
||||||
|
|
||||||
|
### 内存优化策略
|
||||||
|
```python
|
||||||
|
# 内存监控和优化
|
||||||
|
import psutil
|
||||||
|
import gc
|
||||||
|
|
||||||
|
class MemoryOptimizer:
|
||||||
|
@staticmethod
|
||||||
|
def monitor_memory():
|
||||||
|
process = psutil.Process()
|
||||||
|
memory_info = process.memory_info()
|
||||||
|
return {
|
||||||
|
'rss': memory_info.rss / 1024 / 1024, # MB
|
||||||
|
'vms': memory_info.vms / 1024 / 1024, # MB
|
||||||
|
'percent': process.memory_percent()
|
||||||
|
}
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def optimize_memory():
|
||||||
|
# 强制垃圾回收
|
||||||
|
gc.collect()
|
||||||
|
|
||||||
|
# 清理变量缓存
|
||||||
|
if hasattr(gc, 'collect'):
|
||||||
|
gc.collect()
|
||||||
|
|
||||||
|
return MemoryOptimizer.monitor_memory()
|
||||||
|
|
||||||
|
# 定期内存优化
|
||||||
|
async def schedule_memory_optimization():
|
||||||
|
while True:
|
||||||
|
await asyncio.sleep(300) # 每5分钟
|
||||||
|
memory_info = MemoryOptimizer.optimize_memory()
|
||||||
|
|
||||||
|
if memory_info['rss'] > 500: # 超过 500MB
|
||||||
|
logger.warning(f"High memory usage: {memory_info['rss']:.2f}MB")
|
||||||
|
```
|
||||||
|
|
||||||
|
## 📊 监控与日志
|
||||||
|
|
||||||
|
### 结构化日志
|
||||||
|
```python
|
||||||
|
import structlog
|
||||||
|
|
||||||
|
# 配置结构化日志
|
||||||
|
structlog.configure(
|
||||||
|
processors=[
|
||||||
|
structlog.stdlib.filter_by_level,
|
||||||
|
structlog.stdlib.add_logger_name,
|
||||||
|
structlog.stdlib.add_log_level,
|
||||||
|
structlog.stdlib.PositionalArgumentsFormatter(),
|
||||||
|
structlog.processors.TimeStamper(fmt="iso"),
|
||||||
|
structlog.processors.StackInfoRenderer(),
|
||||||
|
structlog.processors.format_exc_info,
|
||||||
|
structlog.processors.UnicodeDecoder(),
|
||||||
|
structlog.processors.JSONRenderer()
|
||||||
|
],
|
||||||
|
context_class=dict,
|
||||||
|
logger_factory=structlog.stdlib.LoggerFactory(),
|
||||||
|
wrapper_class=structlog.stdlib.BoundLogger,
|
||||||
|
cache_logger_on_first_use=True,
|
||||||
|
)
|
||||||
|
|
||||||
|
logger = structlog.get_logger()
|
||||||
|
|
||||||
|
# 日志使用示例
|
||||||
|
logger.info(
|
||||||
|
"Spider started",
|
||||||
|
spider_name="kami-spider",
|
||||||
|
url_count=1000,
|
||||||
|
concurrency=10
|
||||||
|
)
|
||||||
|
|
||||||
|
logger.error(
|
||||||
|
"Request failed",
|
||||||
|
url="https://example.com",
|
||||||
|
error_message="Connection timeout",
|
||||||
|
retry_count=3
|
||||||
|
)
|
||||||
|
```
|
||||||
|
|
||||||
|
### Prometheus 监控指标
|
||||||
|
```python
|
||||||
|
from prometheus_client import Counter, Histogram, Gauge, start_http_server
|
||||||
|
|
||||||
|
# 定义指标
|
||||||
|
REQUEST_COUNT = Counter(
|
||||||
|
'spider_requests_total',
|
||||||
|
'Total number of spider requests',
|
||||||
|
['method', 'endpoint', 'status']
|
||||||
|
)
|
||||||
|
|
||||||
|
REQUEST_DURATION = Histogram(
|
||||||
|
'spider_request_duration_seconds',
|
||||||
|
'Spider request duration',
|
||||||
|
['method', 'endpoint']
|
||||||
|
)
|
||||||
|
|
||||||
|
ACTIVE_CONNECTIONS = Gauge(
|
||||||
|
'spider_active_connections',
|
||||||
|
'Number of active connections'
|
||||||
|
)
|
||||||
|
|
||||||
|
DATA_PROCESSED = Counter(
|
||||||
|
'spider_data_processed_total',
|
||||||
|
'Total amount of data processed',
|
||||||
|
['data_type']
|
||||||
|
)
|
||||||
|
|
||||||
|
# 指标使用示例
|
||||||
|
REQUEST_COUNT.labels(
|
||||||
|
method='GET',
|
||||||
|
endpoint='https://example.com',
|
||||||
|
status='200'
|
||||||
|
).inc()
|
||||||
|
|
||||||
|
REQUEST_DURATION.labels(
|
||||||
|
method='GET',
|
||||||
|
endpoint='https://example.com'
|
||||||
|
).observe(2.5)
|
||||||
|
|
||||||
|
# 启动指标服务器
|
||||||
|
start_http_server(9090)
|
||||||
|
```
|
||||||
|
|
||||||
|
## 🐛 故障排除
|
||||||
|
|
||||||
|
### 常见问题及解决方案
|
||||||
|
|
||||||
|
#### 1. Playwright 浏览器启动失败
|
||||||
|
```bash
|
||||||
|
# 问题:浏览器无法启动
|
||||||
|
# 解决方案:
|
||||||
|
docker run -it --rm \
|
||||||
|
--cap-add=SYS_ADMIN \
|
||||||
|
--security-opt seccomp=unconfined \
|
||||||
|
kami-spider-base:latest \
|
||||||
|
playwright install chromium
|
||||||
|
|
||||||
|
# 或在 Dockerfile 中添加
|
||||||
|
USER root
|
||||||
|
RUN playwright install-deps chromium
|
||||||
|
USER appuser
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 2. 内存不足问题
|
||||||
|
```bash
|
||||||
|
# 问题:容器内存溢出
|
||||||
|
# 解决方案:增加内存限制和优化代码
|
||||||
|
docker run -d \
|
||||||
|
--memory=2g \
|
||||||
|
--memory-swap=2g \
|
||||||
|
--memory-reservation=1g \
|
||||||
|
kami-spider-base:latest
|
||||||
|
|
||||||
|
# Python 代码优化
|
||||||
|
def batch_process(items, batch_size=100):
|
||||||
|
for i in range(0, len(items), batch_size):
|
||||||
|
batch = items[i:i + batch_size]
|
||||||
|
yield process_batch(batch)
|
||||||
|
gc.collect() # 强制垃圾回收
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 3. 网络连接问题
|
||||||
|
```python
|
||||||
|
# 问题:网站反爬虫机制
|
||||||
|
# 解决方案:实现智能重试和代理轮换
|
||||||
|
import random
|
||||||
|
import time
|
||||||
|
|
||||||
|
class SmartRetryClient:
|
||||||
|
def __init__(self, max_retries=3, base_delay=1):
|
||||||
|
self.max_retries = max_retries
|
||||||
|
self.base_delay = base_delay
|
||||||
|
|
||||||
|
async def request_with_retry(self, url, headers=None):
|
||||||
|
for attempt in range(self.max_retries):
|
||||||
|
try:
|
||||||
|
# 随机延迟
|
||||||
|
delay = self.base_delay * (2 ** attempt) + random.random()
|
||||||
|
await asyncio.sleep(delay)
|
||||||
|
|
||||||
|
# 轮换 User-Agent
|
||||||
|
if headers is None:
|
||||||
|
headers = {}
|
||||||
|
headers['User-Agent'] = self.get_random_user_agent()
|
||||||
|
|
||||||
|
async with self.session.get(url, headers=headers) as response:
|
||||||
|
return await response.text()
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
if attempt == self.max_retries - 1:
|
||||||
|
raise e
|
||||||
|
logger.warning(f"Request failed (attempt {attempt + 1}): {e}")
|
||||||
|
|
||||||
|
def get_random_user_agent(self):
|
||||||
|
agents = [
|
||||||
|
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36',
|
||||||
|
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36',
|
||||||
|
'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36'
|
||||||
|
]
|
||||||
|
return random.choice(agents)
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 4. 依赖安装失败
|
||||||
|
```bash
|
||||||
|
# 问题:UV 或 Python 包安装失败
|
||||||
|
# 解决方案:清理缓存并重试
|
||||||
|
docker run --rm kami-spider-base:latest uv cache clean
|
||||||
|
|
||||||
|
# 手动安装特定包
|
||||||
|
docker run --rm kami-spider-base:latest \
|
||||||
|
uv pip install --force-reinstall requests
|
||||||
|
|
||||||
|
# 检查网络连接
|
||||||
|
docker run --rm kami-spider-base:latest \
|
||||||
|
ping -c 3 pypi.org
|
||||||
|
```
|
||||||
|
|
||||||
|
### 调试技巧
|
||||||
|
```bash
|
||||||
|
# 进入容器调试
|
||||||
|
docker exec -it kami-spider /bin/sh
|
||||||
|
|
||||||
|
# 查看 Python 环境
|
||||||
|
docker exec kami-spider python --version
|
||||||
|
docker exec kami-spider uv --version
|
||||||
|
|
||||||
|
# 检查 Playwright 安装
|
||||||
|
docker exec kami-spider playwright install --help
|
||||||
|
|
||||||
|
# 查看系统资源
|
||||||
|
docker stats kami-spider
|
||||||
|
|
||||||
|
# 查看日志
|
||||||
|
docker logs -f kami-spider
|
||||||
|
|
||||||
|
# 检查进程
|
||||||
|
docker exec kami-spider ps aux
|
||||||
|
```
|
||||||
|
|
||||||
|
## 🔒 安全最佳实践
|
||||||
|
|
||||||
|
### 容器安全配置
|
||||||
|
```dockerfile
|
||||||
|
# 安全配置示例
|
||||||
|
FROM git.oceanpay.cc/danial/alpine-base:latest
|
||||||
|
|
||||||
|
# 非 root 用户
|
||||||
|
USER appuser
|
||||||
|
|
||||||
|
# 只读文件系统
|
||||||
|
VOLUME ["/app/config", "/app/data", "/app/logs"]
|
||||||
|
|
||||||
|
# 限制 capabilities
|
||||||
|
RUN echo 'appuser ALL=(ALL) NOPASSWD: /usr/bin/playwright' >> /etc/sudoers
|
||||||
|
|
||||||
|
# 安全扫描
|
||||||
|
RUN apk add --no-cache \
|
||||||
|
&& rm -rf /var/cache/apk/* \
|
||||||
|
&& rm -rf /tmp/*
|
||||||
|
```
|
||||||
|
|
||||||
|
### 敏感信息保护
|
||||||
|
```python
|
||||||
|
# 环境变量加密
|
||||||
|
import os
|
||||||
|
from cryptography.fernet import Fernet
|
||||||
|
|
||||||
|
class SecureConfig:
|
||||||
|
def __init__(self, key: str = None):
|
||||||
|
self.key = key or os.getenv('ENCRYPTION_KEY')
|
||||||
|
self.cipher = Fernet(self.key.encode() if self.key else None)
|
||||||
|
|
||||||
|
def encrypt(self, data: str) -> str:
|
||||||
|
return self.cipher.encrypt(data.encode()).decode()
|
||||||
|
|
||||||
|
def decrypt(self, encrypted_data: str) -> str:
|
||||||
|
return self.cipher.decrypt(encrypted_data.encode()).decode()
|
||||||
|
|
||||||
|
# 敏感配置使用
|
||||||
|
config = SecureConfig()
|
||||||
|
db_password = config.decrypt(os.getenv('ENCRYPTED_DB_PASSWORD'))
|
||||||
|
```
|
||||||
|
|
||||||
|
## 📈 版本信息
|
||||||
|
|
||||||
|
- **当前版本**: v1.2.0
|
||||||
|
- **Python 版本**: 3.13
|
||||||
|
- **UV 版本**: latest
|
||||||
|
- **Playwright 版本**: latest
|
||||||
|
- **Alpine 版本**: 3.18
|
||||||
|
- **构建日期**: 2024-12-17
|
||||||
|
- **维护团队**: kami-spider development team
|
||||||
|
|
||||||
|
## 📞 支持与贡献
|
||||||
|
|
||||||
|
### 获取帮助
|
||||||
|
- **项目仓库**: [GitHub Repository](https://github.com/yourcompany/kami-spider)
|
||||||
|
- **问题反馈**: [Issues](https://github.com/yourcompany/kami-spider/issues)
|
||||||
|
- **讨论区**: [Discussions](https://github.com/yourcompany/kami-spider/discussions)
|
||||||
|
- **技术文档**: [Documentation](https://docs.yourcompany.com/kami-spider)
|
||||||
|
|
||||||
|
### 贡献指南
|
||||||
|
1. Fork 项目仓库
|
||||||
|
2. 创建功能分支 (`git checkout -b feature/amazing-feature`)
|
||||||
|
3. 提交更改 (`git commit -m 'Add amazing feature'`)
|
||||||
|
4. 推送到分支 (`git push origin feature/amazing-feature`)
|
||||||
|
5. 创建 Pull Request
|
||||||
|
|
||||||
|
### 联系方式
|
||||||
|
- **技术支持**: spider-support@yourcompany.com
|
||||||
|
- **安全报告**: security@yourcompany.com
|
||||||
|
- **商务合作**: business@yourcompany.com
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
构建完成后镜像将标记为 `kami-spider-base:latest`,建议在生产环境中使用具体版本标签以确保部署一致性。定期更新基础镜像和依赖包以保持安全性和性能。
|
||||||
Reference in New Issue
Block a user