Skip to content

k8s部署ruoyi

踩坑指南

创建命名空间

bash
kubectl create ns ruoyi

部署mysql

  1. 在nfs共享目录下创建init、data和logs,分别挂载mysql的初始化脚本、数据和日志(配置文件没有复杂的配置,就直接用configmap挂载)

    shell
    mkdir -p /nfs/data/mysql/init 
    mkdir -p /nfs/data/mysql/data 
    mkdir -p /nfs/data/mysql/logs
  2. 将ruoyi项目中的ry_20240629.sql和quartz.sql放到init目录下

  3. 创建资源文件 mysql-svc-deploy-configmap.yaml

    yaml
    apiVersion: v1
    kind: Service
    metadata:
      namespace: ruoyi
      name: mysql
      labels:
        app: mysql-service
    spec:
      selector:
        app: mysql-pod
      type: NodePort
      ports:
        - nodePort: 30006
          port: 3306
          protocol: TCP
          targetPort: 3306
    
    ---
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      namespace: ruoyi
      name: mysql
      labels:
        app: mysql-deploy
    spec:
      selector:
        matchLabels:
          app: mysql-pod
      replicas: 1
      template:
        metadata:
          labels:
            app: mysql-pod
        spec:
          containers:
          - name: mysql
            image: registry.cn-shanghai.aliyuncs.com/cr-k8s-images/mysql:8.0.31
            ports:
              - containerPort: 3306
            args:
              - --character-set-server=utf8mb4
              - --collation-server=utf8mb4_general_ci
              - --default-authentication-plugin=mysql_native_password
            env:
              - name: MYSQL_DATABASE
                value: ry-vue
              - name: MYSQL_ROOT_PASSWORD
                value: '123123'
              - name: TZ
                value: Asia/Shanghai
            volumeMounts:
              - name: init
                mountPath: /docker-entrypoint-initdb.d
              - name: data
                mountPath: /var/lib/mysql
              - name: logs
                mountPath: /var/log/mysql/
              - name: config
                mountPath: /etc/mysql/conf.d
          volumes:
            - name: init
              nfs:
                server: 192.168.10.113
                path: /nfs/data/mysql/init
            - name: data
              nfs:
                server: 192.168.10.113
                path: /nfs/data/mysql/data
            - name: logs
              nfs:
                server: 192.168.10.113
                path: /nfs/data/mysql/logs
            - name: config
              configMap:
                name: mysql-configmap
    
    ---
    apiVersion: v1
    kind: ConfigMap
    metadata:
      namespace: ruoyi
      name: mysql-configmap
    data:
      my.cnf: |
        [mysqld]
        datadir=/var/lib/mysql
        socket=/var/lib/mysql/mysql.sock
        symbolic-links=0
        [mysqld_safe]
        log-error=/var/log/mysql/mysql.log
        pid-file=/var/run/mysql/mariadb.pid
  4. 应用资源

    shell
    kubectl apply -f mysql-svc-deploy-configmap.yaml
  5. 查看 mysql 容器 状态

    shell
    kubectl exec -it -n ruoyi mysql-859c4c69b-xfrj7 -- /bin/bash

部署redis

  1. 在nfs共享目录下创建dataconfig,分别挂载redis的数据和配置

    shell
    mkdir -p /nfs/data/redis/data
    mkdir -p /nfs/data/redis/config
  2. 将配置文件redis.conf放到config目录下,注意修改其中的dir属性配置为datamountPath

    # requirepass 123456
    dir /data
  3. 创建资源文件redis-svc-deploy.yaml

    yaml
    apiVersion: v1
    kind: Service
    metadata:
      namespace: ruoyi
      name: redis
      labels:
        app: redis-service
    spec:
      selector:
        app: redis-pod
      type: NodePort
      ports:
        - nodePort: 30379
          port: 6379
          protocol: TCP
          targetPort: 6379
    
    ---
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      namespace: ruoyi
      name: redis
      labels:
        app: redis-deploy
    spec:
      selector:
        matchLabels:
          app: redis-pod
      replicas: 1
      template:
        metadata:
          labels:
            app: redis-pod
        spec:
          containers:
          - name: redis
            image: registry.cn-shanghai.aliyuncs.com/cr-k8s-images/redis:7.0.7
            ports:
              - containerPort: 6379
            command:
              - redis-server
            args:
              - /etc/redis/redis.conf
            env:
              - name: TZ
                value: Asia/Shanghai
            volumeMounts:
              - name: data
                mountPath: /var/lib/redis
              - name: config
                mountPath: /etc/redis/
          volumes:
            - name: data
              nfs:
                server: 192.168.10.113
                path: /nfs/data/redis/data
            - name: config
              nfs:
                server: 192.168.10.113
                path: /nfs/data/redis/config
  4. 应用资源

    shell
    kubectl apply -f redis-svc-deploy.yaml

部署ry-admin

  1. 在nfs共享目录下创建ry-admin,挂载ruoyi-admin的日志以及头像等文件

  2. 注意替换spring配置文件中有关ip地址的配置,替换为servicename,如下:

    yaml
    spring:
      datasource:
        druid:
          master:
            url: jdbc:mysql://mysql:3306/ry-vue
      redis:
        host: redis
  3. 镜像制作

    bash
    mvn clean package
    docker build -t ry-admin:3.8.5 .

    Dockerfile

    FROM openjdk:8-jre
    MAINTAINER lxw@qq.com
    VOLUME /tmp
    ADD ruoyi-admin.jar app.jar
    ENV TZ 'Asia/Shanghai'
    ENV LANG C.UTF-8
    ENV LANGUAGE C.UTF-8
    ENV LC_ALL C.UTF-8
    ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
  4. 创建资源文件ry-admin-svc-deploy.yaml

    yaml
    apiVersion: v1
    kind: Service
    metadata:
      namespace: ruoyi
      name: ry-admin
      labels:
        app: ry-admin-service
    spec:
      selector:
        app: ry-admin-pod
      type: NodePort
      ports:
        - nodePort: 30080
          port: 8080
          protocol: TCP
          targetPort: 8080
    
    ---
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      namespace: ruoyi
      name: ry-admin
      labels:
        app: ry-admin-deploy
    spec:
      selector:
        matchLabels:
          app: ry-admin-pod
      replicas: 1
      template:
        metadata:
          labels:
            app: ry-admin-pod
        spec:
          containers:
          - name: ry-admin
            image: registry.cn-shanghai.aliyuncs.com/cr-k8s-images/ry-admin:3.8.5
            ports:
              - containerPort: 8080
            env:
              - name: TZ
                value: Asia/Shanghai
            volumeMounts:
              - name: data
                mountPath: /home/ruoyi
          volumes:
            - name: data
              nfs:
                server: 192.168.10.113
                path: /nfs/data/ry-admin
  5. 应用资源

    shell
    kubectl apply -f ry-admin-svc-deploy.yaml

这里 打包 ry-admin 镜像 有大坑 (有墙,本地无法打docker镜像)

  • 利用 远程云服务器打包

  • 打包完 推送 阿里云私有镜像库

  • 在k8s机器上 手动 拉取 阿里云私有镜像

docker pull registry.cn-shanghai.aliyuncs.com/cr-k8s-images/ry-admin:3.8.5

image-20241001221141251

shell
sudo docker login --username=theWe1 registry.cn-shanghai.aliyuncs.com

docker tag 13ffe4493175 registry.cn-shanghai.aliyuncs.com/cr-k8s-images/ry-admin:3.8.5

docker push registry.cn-shanghai.aliyuncs.com/cr-k8s-images/ry-admin:3.8.5

部署ry-ui

  1. 在nfs共享目录下创建ry-ui以及子目录distconfig,挂载ruoyi-ui的静态文件以及nginx的配置

  2. 将配置文件nginx.conf放到config目录下(这里踩过坑 最后方案是挂载的 conf.d 目录)

    ry-ui-custom.conf

    server {
    	listen       80;
    	server_name  localhost;
    	charset utf-8;
    
    	location / {
    		root   /home/ruoyi/projects/ruoyi-ui;
    		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://ry-admin:8080/;
    	}
    
    	error_page   500 502 503 504  /50x.html;
    	location = /50x.html {
    		root   html;
    	}
    }
  3. 静态文件打包,将静态文件放到dist目录下

    npm run build:prod
  4. 创建资源文件ry-ui-svc-deploy.yaml

    yaml
    apiVersion: v1
    kind: Service
    metadata:
      namespace: ruoyi
      name: ry-ui
      labels:
        app: ry-ui-service
    spec:
      selector:
        app: ry-ui-pod
      type: NodePort
      ports:
        - nodePort: 30081
          port: 80
          protocol: TCP
          targetPort: 80
    
    ---
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      namespace: ruoyi
      name: ry-ui
      labels:
        app: ry-ui-deploy
    spec:
      selector:
        matchLabels:
          app: ry-ui-pod
      replicas: 1
      template:
        metadata:
          labels:
            app: ry-ui-pod
        spec:
          containers:
          - name: nginx
            image: registry.cn-shanghai.aliyuncs.com/cr-k8s-images/nginx:1.22.1
            ports:
              - containerPort: 80
            volumeMounts:
              - name: dist
                mountPath: /home/ruoyi/projects/ruoyi-ui
              - name: config
                mountPath: /etc/nginx/conf.d
          volumes:
            - name: dist
              nfs:
                server: 192.168.10.113
                path: /nfs/data/ry-ui/dist
            - name: config
              nfs:
                server: 192.168.10.113
                path: /nfs/data/ry-ui/config

系统测试

若是k8s集群部署 ruoyi成功,通过任意节点的30081端口 应该都能够访问到 ruoyi-admin

附录

参考文档

k8s部署ruoyi前后端分离版实战