Go Web 编程快速入门 13 - 部署与运维:Docker容器化、Kubernetes编排与CI/CD

服务写完只是第一步,稳定、自动化地部署与运维才是走向生产的关键。本章沿用第 04.1 的结构与"可运行模板",提供 Docker 多阶段构建、Compose 本地编排、Kubernetes 核心资源与 GitHub Actions CI/CD 的最小实践。

1 Docker容器化

1.1 多阶段 Dockerfile(最佳实践)

dockerfile 复制代码
# 构建阶段
FROM golang:1.22-alpine AS builder
WORKDIR /app
RUN apk add --no-cache tzdata ca-certificates
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN CGO_ENABLED=0 GOOS=linux go build -o server ./cmd/server

# 运行阶段
FROM alpine:3.20
RUN apk add --no-cache tzdata ca-certificates
WORKDIR /root
COPY --from=builder /app/server ./server
COPY --from=builder /app/configs ./configs
EXPOSE 8080
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
  CMD wget -qO- http://localhost:8080/health || exit 1
ENTRYPOINT ["./server"]

1.2 Docker Compose(本地多服务编排)

yaml 复制代码
version: "3.8"
services:
  gateway:
    build: { context: ., dockerfile: Dockerfile }
    ports: ["8080:8080"]
    environment:
      - ENV=production
      - USER_SERVICE_URL=http://user-service:8081
    depends_on: [user-service]

  user-service:
    build: { context: ., dockerfile: Dockerfile }
    ports: ["8081:8081"]
    environment:
      - ENV=production

2 Kubernetes编排

2.1 Deployment + Service(最小模板)

yaml 复制代码
apiVersion: apps/v1
kind: Deployment
metadata:
  name: gateway
spec:
  replicas: 2
  selector:
    matchLabels: { app: gateway }
  template:
    metadata:
      labels: { app: gateway }
    spec:
      containers:
      - name: gateway
        image: yourrepo/gateway:latest
        ports: [{ containerPort: 8080 }]
        env:
        - name: ENV
          value: production
        resources:
          requests: { cpu: "250m", memory: "256Mi" }
          limits: { cpu: "500m", memory: "512Mi" }
        livenessProbe:
          httpGet: { path: /health, port: 8080 }
          initialDelaySeconds: 30
        readinessProbe:
          httpGet: { path: /health, port: 8080 }
          initialDelaySeconds: 5
---
apiVersion: v1
kind: Service
metadata:
  name: gateway
spec:
  type: ClusterIP
  selector: { app: gateway }
  ports:
  - name: http
    port: 80
    targetPort: 8080

2.2 配置与机密(ConfigMap/Secret)

yaml 复制代码
apiVersion: v1
kind: ConfigMap
metadata:
  name: app-config
data:
  LOG_LEVEL: "info"
---
apiVersion: v1
kind: Secret
metadata:
  name: app-secret
type: Opaque
data:
  DB_PASSWORD: cGFzc3dvcmQ= # base64("password")

3 CI/CD(GitHub Actions 示例)

3.1 构建并推送镜像

yaml 复制代码
name: ci
on: { push: { branches: [ main ] } }
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-go@v5
        with: { go-version: "1.22" }
      - name: Build
        run: go build -v ./cmd/server
      - name: Login to Registry
        uses: docker/login-action@v3
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}
      - name: Build and Push
        uses: docker/build-push-action@v6
        with:
          context: .
          push: true
          tags: ghcr.io/yourorg/gateway:latest

3.2 部署到 Kubernetes(可选)

yaml 复制代码
name: cd
on: { workflow_run: { workflows: ["ci"], types: ["completed"], branches: ["main"] } }
jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Setup Kubectl
        uses: azure/setup-kubectl@v4
        with: { version: "v1.29.0" }
      - name: Kubeconfig
        run: echo "${{ secrets.KUBECONFIG }}" > kubeconfig && chmod 600 kubeconfig
      - name: Deploy
        run: KUBECONFIG=./kubeconfig kubectl apply -f k8s/

4 完整示例:一键启动到生产路径

  • 本地开发:docker compose up -d 启动多服务并联调。
  • 构建镜像:docker build -t yourrepo/gateway:latest .
  • 推送镜像:docker push yourrepo/gateway:latest
  • 部署集群:准备 k8s/ 资源清单并通过 CI/CD 自动 kubectl apply

通过本章的模板,你能快速建立"从代码到生产"的基础路径。建议逐步引入:灰度发布、滚动升级、观测(Prometheus/Grafana)、日志汇总与告警、金丝雀与回滚策略,以持续提升系统的可运维性与可靠性。

相关推荐
Yeats_Liao7 小时前
Go Web 编程快速入门 14 - 性能优化与最佳实践:Go应用性能分析、内存管理、并发编程最佳实践
前端·后端·性能优化·golang
蒜香拿铁8 小时前
Angular【http服务端交互】
前端·http·angular.js
游戏开发爱好者88 小时前
Fiddler抓包实战教程 从安装配置到代理设置,详解Fiddler使用方法与调试技巧(HTTPHTTPS全面指南)
前端·测试工具·小程序·https·fiddler·uni-app·webview
universe_018 小时前
前端八股之HTTP
前端·网络协议·http
BD_Marathon8 小时前
【Linux】awk命令
linux·运维·服务器
七夜zippoe8 小时前
仓颉语言核心特性深度解析——现代编程范式的集大成者
开发语言·后端·鸿蒙·鸿蒙系统·仓颉
软件架构师-叶秋9 小时前
spring boot入门篇之开发环境搭建
java·spring boot·后端
技术砖家--Felix9 小时前
Spring Boot Web开发篇:构建RESTful API
前端·spring boot·restful
林一百二十八9 小时前
Win11配置VMware-workstation以及Ubuntu环境
linux·运维·ubuntu