Vue项目不能直接用npm run serve部署到K8s,因其是开发服务器,不支持gzip、缓存头、HTTP/2及history模式fallback,且监听localhost不可被外部访问;必须构建为静态文件并用Nginx容器托管。

Vue项目在Kubernetes上不是“直接部署”,而是打包为静态文件 + Nginx容器后,以无状态Pod方式运行;跳过构建阶段、用多阶段Dockerfile、漏配nginx.conf或targetPort是高频失败原因。
为什么不能直接用 npm run serve 部署到 K8s
Vue CLI 的 serve 是开发服务器(基于 webpack-dev-server),不适用于生产环境:它不支持 gzip、缓存头、HTTP/2,且默认不处理 Vue Router 的 history 模式回退——访问 /user/123 会 404。K8s 要求容器启动即稳定提供静态服务,所以必须用 Nginx 或 serve(仅限调试)等生产级 HTTP 服务器。
-
npm run serve启动的进程监听的是localhost:8080,容器内无 host 网络,外部无法访问 - 它没有对
index.html的 fallback 配置,Vue Router history 模式下刷新页面必 404 - 镜像体积大(含 node_modules + dev 依赖),启动慢,内存占用高
推荐 Dockerfile:多阶段构建 + 官方 nginx 镜像
用 Node 阶段构建,Nginx 阶段托管,镜像体积可压到 ~15MB,且完全剥离构建依赖。关键点是:复制 dist 到 /usr/share/nginx/html,并覆盖默认 nginx.conf 支持 history 模式。
# Dockerfile FROM node:18-alpine AS build-stage WORKDIR /app COPY package*.json ./ RUN npm ci --only=production COPY . . RUN npm run build <p>FROM nginx:1.25-alpine COPY --from=build-stage /app/dist /usr/share/nginx/html COPY nginx.conf /etc/nginx/nginx.conf EXPOSE 80
-
npm ci --only=production比npm install更快更确定,跳过 devDependencies -
nginx.conf必须包含try_files $uri $uri/ /index.html;,否则路由刷新 404 - 不要用
node:alpine+serve,它不支持优雅 shutdown,K8sterminationGracePeriodSeconds触发时可能丢请求
Deployment 和 Service 的最小可用配置
K8s 不需要为纯静态前端配 HPA 或复杂探针;但 livenessProbe 和 readinessProbe 建议保留,避免滚动更新卡住。注意 containerPort 与 targetPort 必须一致,且 Service 的 selector 标签要和 Pod template 完全匹配。
立即学习“前端免费学习笔记(深入)”;
# deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: vue-app
spec:
replicas: 2
selector:
matchLabels:
app: vue-app
template:
metadata:
labels:
app: vue-app
spec:
containers:
- name: nginx
image: your-registry/vue-app:v1.2
ports:
- containerPort: 80
livenessProbe:
httpGet:
path: /healthz
port: 80
initialDelaySeconds: 30
readinessProbe:
httpGet:
path: /
port: 80
initialDelaySeconds: 5
---
# service.yaml
apiVersion: v1
kind: Service
metadata:
name: vue-app-svc
spec:
selector:
app: vue-app
ports:
- port: 80
targetPort: 80
type: ClusterIP
-
livenessProbe用/healthz(需在nginx.conf中配 location 块返回 200),比探测/更轻量 -
type: ClusterIP是默认值,外网不可达;要暴露服务,后续必须接Ingress或设type: LoadBalancer - 别漏掉
replicas—— 设为 1 时,单点故障;设为 0 则 Pod 永远不起来
域名访问绕不开 Ingress,不是 Service 的 type: LoadBalancer
Service 的 type: LoadBalancer 只能给整个服务分配一个 IP,无法做路径路由(如 /api 转后端、/ 转前端)。真实场景中,你一定需要 Ingress 来实现:统一入口、TLS 终止、host 匹配、路径重写。
# ingress.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: vue-ingress
annotations:
nginx.ingress.kubernetes.io/ssl-redirect: "true"
spec:
tls:
- hosts:
- app.example.com
secretName: app-tls-secret
rules:
- host: app.example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: vue-app-svc
port:
number: 80
- 没配
tls或没创建Secret,Ingress controller 会拒绝加载该规则 -
pathType: Prefix是必须项(v1 Ingress 要求),path: /表示所有路径都走这个后端 - 别指望
Service自带域名——my-service.default.svc.cluster.local是集群内 DNS 名,对外无效
最常被跳过的一步:在 nginx.conf 里加 try_files fallback。哪怕 Deployment 和 Service 全绿,只要用户刷新二级路由就 404,问题就出在这儿。它不报错,也不阻断部署,但线上功能直接残缺。


















