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

  • 提前安装nfs,共享目录为:/home/nfs/
  • 可以使用kuboard部署,但最好在掌握了kubectl的基础上

创建命名空间

kubectl create ns ruoyi

部署mysql

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

  2. 将ruoyi项目中的ry_20230223.sqlquartz.sql放到init目录下

  3. 创建资源文件mysql-svc-deploy-configmap.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: mysql:8.0.32
            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: 172.16.13.111
                path: /home/nfs/mysql/init
            - name: data
              nfs:
                server: 172.16.13.111
                path: /home/nfs/mysql/data
            - name: logs
              nfs:
                server: 172.16.13.111
                path: /home/nfs/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. 应用资源

    kubectl apply -f mysql-svc-deploy-configmap.yaml
    

部署redis

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

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

  3. 创建资源文件redis-svc-deploy.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: redis:7.0.9
            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: 172.16.13.111
                path: /home/nfs/redis/data
            - name: config
              nfs:
                server: 172.16.13.111
                path: /home/nfs/redis/config
    
  4. 应用资源

    kubectl apply -f redis-svc-deploy.yaml
    

部署ry-admin

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

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

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

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

    Dockerfile参考

    FROM openjdk:8
    MAINTAINER xiguapengpengwork@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

    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: 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: 172.16.13.111
                path: /home/nfs/ry-admin
    
  5. 应用资源

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

部署ry-ui

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

  2. 将配置文件nginx.conf放到config目录下,同样注意有关ip地址的配置,替换为servicename,配置参考

    worker_processes  1;
    events {
    	worker_connections  1024;
    }
    http {
    	include       mime.types;
    	default_type  application/octet-stream;
    	sendfile        on;
    	keepalive_timeout  65;
    
    	gzip on;
    	gzip_min_length 1k;
    	gzip_buffers 16 64K;
    	gzip_http_version 1.1;
    	gzip_comp_level 5;
    	gzip_types text/plain application/x-javascript text/css application/xml application/javascript;
    	gzip_vary on;
    	gzip_disable "MSIE [1-6]\.";
    
    	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

    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: nginx:1.23.3
            ports:
              - containerPort: 80
            volumeMounts:
              - name: dist
                mountPath: /home/ruoyi/projects/ruoyi-ui
              - name: config
                mountPath: /etc/nginx
          volumes:
            - name: dist
              nfs:
                server: 172.16.13.111
                path: /home/nfs/ry-ui/dist
            - name: config
              nfs:
                server: 172.16.13.111
                path: /home/nfs/ry-ui/config
    
  5. 应用资源

    kubectl apply -f ry-ui-svc-deploy.yaml
    

部署成功

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

Logo

快速构建 Web 应用程序

更多推荐