k8s部署ruoyi也是一样的思路, 这里部署的是ruoyi-vue-plus版本

如果vue项目也需要使用容器化部署的, 可以参考另一篇 https://blog.csdn.net/weimeibuqieryu/article/details/141394622

1.挂载存储

可参考 之前文章设置 https://blog.csdn.net/weimeibuqieryu/article/details/140183843

2.部署yaml

先创建命名空间ruoyi, 有就不用创建了

kubectl create namespace ruoyi

我暂不需要使用xxjob和Monitor模块, 所以去除了. 有需要再自行添加

需要先启动后端服务ruoyi-service ,不然k8s会找不到后端服务报错

需要先启动后端服务ruoyi-service ,不然k8s会找不到后端服务报错

创建部署文件 nginx-deploy.yaml

apiVersion: v1
kind: PersistentVolume
metadata:
  name: nginx-pv
  namespace: ruoyi #使用ns ruoyi
spec:
  capacity:
    storage: 1Gi #存储容量为 1 GiB
  accessModes:
    - ReadWriteMany
  nfs:
    path: /nfs/nginx/html/ruoyi-vue-plus   #使用 NFS 服务器的路径和地址, 使用自己定义的
    server: 139.159.140.xxx
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: nginx-pvc
  namespace: ruoyi
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 1Gi
---

apiVersion: v1
kind: Service
metadata:
  labels:
    app: nginx-service
  name: nginx-service
  namespace: ruoyi
spec:
  ports:
    - nodePort: 30088   #节点端口 30088
      port: 80
      protocol: TCP
      targetPort: 80
  selector:
    app: nginx-pod
  type: NodePort
---
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: nginx-deploy
  name: nginx-deploy
  namespace: ruoyi
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx-pod
  strategy: {}
  template:
    metadata:
      labels:
        app: nginx-pod
      namespace: ruoyi
    spec:
      containers:
        - image: nginx:1.22.1   使用 Nginx 1.22.1 镜像
          name: nginx
          ports:
            - containerPort: 80
          resources: { }
          volumeMounts:
            - name: nginx-config
              mountPath: /etc/nginx/nginx.conf
              subPath: nginx.conf
            - name: html-files
              mountPath: "/usr/share/nginx/html/ruoyi-vue-plus"
          env:
            - name: TZ  # 配置环境变量,设置时区
              value: Asia/Shanghai
      volumes:
        - name: nginx-config
          configMap:
            name: nginx-configmap
            items:
              - key: nginx.conf
                path: nginx.conf
        - name: html-files
          persistentVolumeClaim:
            claimName: nginx-pvc
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: nginx-configmap
  namespace: ruoyi
data:
  nginx.conf: |   #直接从项目里面复制. 修改后端服务为ruoyi-service:8080, 记得修改
    worker_processes  1;

    error_log  /var/log/nginx/error.log warn;
    pid        /var/run/nginx.pid;

    events {
      worker_connections  1024;
    }

    http {
      include       mime.types;
      default_type  application/octet-stream;
      sendfile        on;
      keepalive_timeout  65;
      # 限制body大小
      client_max_body_size 100m;

      log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                        '$status $body_bytes_sent "$http_referer" '
                        '"$http_user_agent" "$http_x_forwarded_for"';

      access_log  /var/log/nginx/access.log  main;

      upstream server {
        ip_hash;
        server ruoyi-service:8080;   #就是这里记得修改
      }

      server {
        listen       80;
        server_name  localhost;

        #https配置参考 start
        # listen       444 ssl;

        # 证书直接存放 /docker/nginx/cert/ 目录下即可 更改证书名称即可 无需更改证书路径
        # ssl on;
        # ssl_certificate      /etc/nginx/cert/origin.pem; # /etc/nginx/cert/ 为docker映射路径 不允许更改
        # ssl_certificate_key  /etc/nginx/cert/originPrivate.pem; # /etc/nginx/cert/ 为docker映射路径 不允许更改
        # ssl_session_timeout 5m;
        # ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
        # ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        # ssl_prefer_server_ciphers on;
        # https配置参考 end

        # 演示环境配置 拦截除 GET POST 之外的所有请求
        # if ($request_method !~* GET|POST) {
        #     rewrite  ^/(.*)$  /403;
        # }

        # location = /403 {
        #     default_type application/json;
        #     return 200 '{"msg":"演示模式,不允许操作","code":500}';
        # }

        # 限制外网访问内网 actuator 相关路径
        location ~ ^(/[^/]*)?/actuator(/.*)?$ {
          return 403;
        }

        location / {
          root   /usr/share/nginx/html/ruoyi-vue-plus;
          try_files $uri $uri/ /index.html;
          index  index.html index.htm;
        }

        location /prod-api/ {
          proxy_set_header Host $http_host;
          proxy_set_header X-Real-IP $remote_addr;
          proxy_set_header REMOTE-HOST $remote_addr;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
          proxy_pass http://server/;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
          root   html;
        }
      }
    }

部署该服务

kubectl apply -f nginx-deploy.yaml

就可以使用节点的ip+30088来访问了. 公网记得防火墙的端口要打开

3.前端部署

执行打包命令

# 打包正式环境
npm run build:prod

打包后生成打包文件在 ruoyi-ui/dist 目录 将 dist 目录下文件(不包含 dist 目录) 上传到部署服务器 /nfs/nginx/html/ruoyi-vue-plus 目录下

Logo

快速构建 Web 应用程序

更多推荐