在 GCP 上部署 Next.js

发布: (2026年2月22日 GMT+8 02:36)
4 分钟阅读
原文: Dev.to

Source: Dev.to

要为您提供准确的中文翻译,我需要您粘贴或提供要翻译的完整文本内容(除代码块、URL 之外的部分)。请把文章的正文粘贴在这里,我会按照要求保持原有的 Markdown 格式和技术术语进行翻译。

为什么从 Vercel 迁移到 GCP?

LLM Gateway 是一个全栈应用,拥有多个 API 和前端。通过 Vercel 部署所有内容会增加一个仪表板、一套凭证以及另一项需要管理的事务。直接在 Google Cloud Platform 上运行,我们可以将基础设施统一:API、数据库和前端都位于同一位置。

架构

所有服务都运行在 GCP 上的 Kubernetes 集群中。每个组件——API、Gateway、UI、Playground、Docs、Admin 和 Worker——都部署为独立的容器。Kubernetes 根据资源使用情况自动进行弹性伸缩,在流量高峰时扩容,流量低时缩容。

构建和部署流水线通过 GitHub Actions 完全自动化。每次推送到 main 时,都会为每个服务构建 Docker 镜像并推送到容器注册表。

在独立模式下构建 Next.js 应用

next.config.js 中添加以下内容:

module.exports = {
  output: "standalone",
};

这会将应用打包到一个包含所有依赖的独立文件夹中。

Dockerfile

# Builder stage
FROM node:20-slim AS builder
WORKDIR /app
COPY package.json pnpm-lock.yaml ./
RUN npm install -g pnpm && pnpm install --frozen-lockfile
COPY . .
RUN pnpm build

# Runner stage
FROM node:20-slim AS runner
WORKDIR /app
ENV NODE_ENV=production
ENV HOSTNAME="0.0.0.0"
ENV PORT=80

COPY --from=builder /app/.next/standalone ./
COPY --from=builder /app/.next/static ./.next/static
COPY --from=builder /app/public ./public

EXPOSE 80
CMD ["node", "server.js"]

构建并推送镜像

docker build -t gcr.io/your-project/your-app:latest .
docker push gcr.io/your-project/your-app:latest

部署到 Cloud Run

gcloud run deploy your-app \
  --image gcr.io/your-project/your-app:latest \
  --platform managed \
  --region us-central1 \
  --allow-unauthenticated

Kubernetes 部署

apiVersion: apps/v1
kind: Deployment
metadata:
  name: your-app
spec:
  replicas: 2
  selector:
    matchLabels:
      app: your-app
  template:
    metadata:
      labels:
        app: your-app
    spec:
      containers:
        - name: your-app
          image: gcr.io/your-project/your-app:latest
          ports:
            - containerPort: 80
          resources:
            requests:
              memory: "256Mi"
              cpu: "100m"
            limits:
              memory: "512Mi"
              cpu: "500m"

使用以下命令应用:

kubectl apply -f deployment.yaml

水平 Pod 自动伸缩器

apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: your-app
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: your-app
  minReplicas: 2
  maxReplicas: 10
  metrics:
    - type: Resource
      resource:
        name: cpu
        target:
          type: Utilization
          averageUtilization: 70

此配置会根据 CPU 使用率在 2 到 10 个副本之间自动伸缩部署。

GitHub Actions 工作流(构建与推送)

将以下内容保存为 .github/workflows/build.yml

name: Build and Push

on:
  push:
    branches: [main]

jobs:
  build:
    runs-on: ubuntu-latest
    permissions:
      contents: read
      packages: write
    steps:
      - uses: actions/checkout@v4

      - uses: docker/setup-buildx-action@v3

      - uses: docker/login-action@v3
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - uses: docker/build-push-action@v6
        with:
          context: .
          push: true
          tags: ghcr.io/${{ github.repository }}:latest
          cache-from: type=gha
          cache-to: type=gha,mode=max

要点

如果您已经在 GCP 上,添加 Vercel 可能没有必要。Kubernetes 和 Cloud Run 能很好地处理 Next.js,且将所有内容集中在一起可以简化运维,同时提供出色的性能和低延迟。

0 浏览
Back to Blog

相关文章

阅读更多 »

Subnetting 详解

什么是 Subnetting?可以把它想象成把一栋大型公寓楼拆分成不同的楼层。每层 subnet 拥有自己的编号主机(hosts),以及建筑……