You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Minikube Kubernetes集群中为Node.js应用添加NGINX服务器

实现指导:在Minikube集群中添加NGINX反向代理服务器

看起来你已经在Minikube里搭好了Node.js应用的集群,接下来要加NGINX做反向代理来统一入口,我给你一步步的实现方案:

前提确认

先确保你的Node.js应用已经有了一个ClusterIP类型的Service(用来暴露4个副本),假设这个Service名叫hello-world-service,端口对应Node.js应用的监听端口(比如3000)。如果还没创建,可先执行以下配置:

apiVersion: v1
kind: Service
metadata:
  name: hello-world-service
spec:
  selector:
    app: hello-world # 需与你的Node.js Deployment的selector匹配
  ports:
    - protocol: TCP
      port: 80
      targetPort: 3000 # Node.js应用的监听端口
  type: ClusterIP

运行命令:kubectl apply -f service.yaml完成创建。


步骤1:准备NGINX反向代理配置

创建nginx.conf文件,配置反向代理指向Node.js Service(K8s集群内可通过Service名称直接DNS解析):

server {
    listen 80;
    server_name localhost;

    location / {
        proxy_pass http://hello-world-service;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

步骤2:将NGINX配置转为Kubernetes ConfigMap

用ConfigMap托管配置,避免将配置打包进镜像,方便后续修改:

kubectl create configmap nginx-config --from-file=nginx.conf

步骤3:部署NGINX实例

创建nginx-deployment.yaml,使用轻量版NGINX镜像并挂载ConfigMap配置:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-proxy
spec:
  replicas: 2 # 可根据需求调整副本数
  selector:
    matchLabels:
      app: nginx-proxy
  template:
    metadata:
      labels:
        app: nginx-proxy
    spec:
      containers:
      - name: nginx
        image: nginx:alpine
        ports:
        - containerPort: 80
        volumeMounts:
        - name: nginx-config-volume
          mountPath: /etc/nginx/conf.d/default.conf # 替换NGINX默认配置
          subPath: nginx.conf
      volumes:
      - name: nginx-config-volume
        configMap:
          name: nginx-config

运行命令:kubectl apply -f nginx-deployment.yaml完成部署。

步骤4:暴露NGINX服务给外部访问

Minikube环境下推荐两种暴露方式:

方案A:NodePort

创建nginx-service-nodeport.yaml:

apiVersion: v1
kind: Service
metadata:
  name: nginx-proxy-service
spec:
  selector:
    app: nginx-proxy
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80
      nodePort: 30080 # 可选,端口范围30000-32767
  type: NodePort

部署后运行minikube service nginx-proxy-service,可直接打开浏览器访问。

方案B:LoadBalancer(推荐)

创建nginx-service-loadbalancer.yaml:

apiVersion: v1
kind: Service
metadata:
  name: nginx-proxy-service
spec:
  selector:
    app: nginx-proxy
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80
  type: LoadBalancer

部署后在新终端运行minikube tunnel,NGINX会获得一个本地IP,直接访问该IP即可看到Node.js的Hello-World页面。


额外优化建议

  • 优化Node.js Dockerfile:用官方Node.js轻量镜像代替Ubuntu,结合多阶段构建缩减镜像大小:
# 构建阶段
FROM node:18-alpine AS builder
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production

# 运行阶段
FROM node:18-alpine
WORKDIR /app
COPY --from=builder /app/node_modules ./node_modules
COPY . .
CMD ["node", "app.js"]
  • 添加健康检查:给NGINX和Node.js应用配置livenessProbe和readinessProbe,提升集群稳定性。

内容的提问来源于stack exchange,提问作者Chen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 09:44:36