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)、日志汇总与告警、金丝雀与回滚策略,以持续提升系统的可运维性与可靠性。

相关推荐
FreeTinker5 小时前
HTML本地存储技术解析:localStorage与sessionStorage的原理、差异与应用场景
前端·html
qq_452396235 小时前
第十二篇:《全链路监控与可观测性:前端错误追踪与性能分析》
前端
2601_962218475 小时前
万象生鲜系统区块链溯源技术帮助生鲜企业搭建食品安全数字化体系
大数据·运维·微服务·云原生·架构
智塑未来5 小时前
中小公司在线文档选型:从协作入口到安全边界
运维·安全
新时代牛马5 小时前
Linux 内核入门地图:架构、源码目录与五大子系统
linux·运维·架构
智购科技无人售货机厂家5 小时前
2026自动售货机制冷系统维护指南:从散热器清洁到压缩机换油的工程实践~YH
运维·redis·物联网·缓存·架构
2601_962055975 小时前
跟据spring boot版本,查看对应的tomcat,并查看可支持的tomcat的版本范围
spring boot·后端·tomcat
剑客的茶馆5 小时前
开发者,运维怎样转行FDE?
运维·ai·开发·fde
Julien20046 小时前
控制 SELinux 文件上下文
linux·运维·服务器
wangruofeng6 小时前
Phosphor Icons 官网源码拆解:URL 即存储、水波动画与 7362 Star 图标库的架构实践
前端·架构·github