一、nginx介绍

        Nginx 是一款高性能、轻量级的 HTTP 服务器和反向代理服务器,核心优势是高并发、低资源消耗,还能实现负载均衡、静态资源托管等核心功能。

核心特性

  • 反向代理与负载均衡:转发客户端请求到后端服务,支持轮询、加权等多种算法,分发流量提升系统可用性。
  • 静态资源服务:高效处理 HTML、CSS、图片等静态文件,配合缓存机制大幅降低后端服务器压力。
  • 高并发与稳定性:基于事件驱动架构,单台服务器可支撑数万并发连接,内存占用低且运行稳定。

二、nginx持久卷和声明创建

        nginx转发静态资源时,需要将服务器的静态资源路径挂载并声明。需要注意的是,在节点上挂载的前端包文件可以实时更新无需重启pod。

#nginx-pv.yaml文件
apiVersion: v1
kind: PersistentVolume
metadata:
  name: nginx-frontend-pv
  namespace: zhongchuan141
spec:
  capacity:
    storage: 1Gi
  accessModes:
  - ReadWriteMany
  hostPath:
    path: /home/data/nginx/www
    type: DirectoryOrCreate
  storageClassName: nginx-storage 

storageClassName持久卷和声明的类名,不写类名时,k8s集群会根据声明所需资源自动匹配一个合适的持久卷,但不一定是自己想要的,因此需要写storageClassName类名,集群通过类名将持久卷和对应的声明进行匹配。

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: nginx-frontend-pvc
  namespace: zhongchuan141
spec:
  accessModes:
  - ReadWriteMany
  resources:
    requests:
      storage: 1Gi
  storageClassName: nginx-storage

PV/PVC 方式:直接更新节点上 /data/frontend 目录的文件(实时生效,无需重启 Pod):

sudo cp /新前端文件/* /data/frontend/  # 替换文件

清理资源

# 清理PV/PVC方式的资源(若需保留数据可跳过删除PV)
kubectl delete deploy nginx-frontend
kubectl delete svc nginx-frontend-svc
kubectl delete pvc nginx-frontend-pvc
kubectl delete pv nginx-frontend-pv

三、ngxin配置挂载

        nginx挂载配置的方式有两种,一种是直接使用现有的nginx.conf文件,另一种是编写nginx-conf.yaml文件。这里介绍直接挂载nginx.conf文件。

        通过kubectl create configmap命令,将本地nginx.conf文件直接导入为 ConfigMap:

# 语法:kubectl create configmap <configmap名称> --from-file=<目标文件名>=<本地文件路径>
kubectl create configmap nginx-main-config --from-file=nginx.conf=./nginx.conf
  • 说明:

    • nginx-main-config是 ConfigMap 的名称(可自定义);

    • --from-file=nginx.conf=./nginx.conf表示将本地./nginx.conf文件以 “键(key)为nginx.conf” 的形式存入 ConfigMap。

更新configmap方式

方式 1:直接编辑 ConfigMap(适合简单修改)
通过kubectl edit命令直接编辑现有 ConfigMap:
# 语法:kubectl edit configmap <configmap名称> -n <命名空间>
kubectl edit configmap nginx-main-config -n zhongchuan141

方式 2:通过文件重建 ConfigMap(适合大量修改)
# 1. 删除旧的ConfigMap
kubectl delete configmap nginx-main-config -n zhongchuan141
# 2. 用新的本地文件重新创建(确保本地文件已更新)
kubectl create configmap nginx-main-config -n zhongchuan141 --from-file=nginx.conf=./nginx.conf

四、创建nginx对应的deployment.yaml文件和service.yaml

 deployment.yaml文件,用于创建nginx的控制器和对应的pod容器,在文件中指明了控制器deployment的资源大小、标签、创建的容器镜像、启动命令等,后续对容器水平扩容时可对deployment.yaml文件进行修改。

         service.yaml文件,用于负责负载均衡和服务发现,暴露nginx的端口号和访问模式,其中,端口号可分pod容器对service暴露的端口,service对k8s集群暴露的端口,以及访问模式。

apiVersion: apps/v1
kind: Deployment
metadata:
  name: zhongchuan141-nginx
  namespace: zhongchuan141
spec:
  replicas: 1
  selector:
    matchLabels:
      app: zhongchuan141-nginx
  template:
    metadata:
      labels:
        app: zhongchuan141-nginx
    spec:
      containers:
      - name: zhongchuan141-nginx
        image: dockerhub.kubekey.local/tiangang/nginx:1.20.1
        ports:
        - containerPort: 80
        volumeMounts:  #对容器内挂载的目录
        - name: nginx-config-volume
          mountPath: /etc/nginx/nginx.conf
          subPath: nginx.conf
          readOnly: true
        - name: frontend-volume
          mountPath: /home/data/share/
          readOnly: true
      volumes:  #对容器外挂载的配置、目录
      - name: frontend-volume
        persistentVolumeClaim:
          claimName: nginx-frontend-pvc
      - name: nginx-config-volume
        configMap:
          name: nginx-main-config

---
apiVersion: v1
kind: Service
metadata:
  name: zhongchuan141-nginx
  namespace: zhongchuan141
spec:
  selector:
    app: zhongchuan141-nginx
  type: NodePort
  ports:
  - port: 80
    targetPort: 80
    nodePort: 30080

五、注意事项

        nginx在部署jar后端和vue前端实现转发时,upstream 中 server的地址可写成其他service的名字由k8s集群的DNS自动解析获取对应IP,对应端口好为其他service对内暴露的端口号。或者使用service对外暴露的端口号,IP则使用节点IP。这里更推荐写成service对内信息,但对应缺点为需要先将对应的service创建,否则nginx会启动失败,提示配置文件中的xxxservice不存在。

更多推荐