k8s部署ruoyi
踩坑指南
- 提前安装nfs,共享目录为:/nfs/data
- ruoyi 前后端分离版 下载地址:https://gitee.com/y_project/RuoYi-Vue
创建命名空间
kubectl create ns ruoyi
部署mysql
在nfs共享目录下创建init、data和logs,分别挂载mysql的初始化脚本、数据和日志(配置文件没有复杂的配置,就直接用configmap挂载)
shellmkdir -p /nfs/data/mysql/init mkdir -p /nfs/data/mysql/data mkdir -p /nfs/data/mysql/logs
将ruoyi项目中的ry_20240629.sql和quartz.sql放到init目录下
创建资源文件 mysql-svc-deploy-configmap.yaml
yamlapiVersion: 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
应用资源
shellkubectl apply -f mysql-svc-deploy-configmap.yaml
查看 mysql 容器 状态
shellkubectl exec -it -n ruoyi mysql-859c4c69b-xfrj7 -- /bin/bash
部署redis
在nfs共享目录下创建data和config,分别挂载redis的数据和配置
shellmkdir -p /nfs/data/redis/data mkdir -p /nfs/data/redis/config
将配置文件redis.conf放到config目录下,注意修改其中的dir属性配置为data的mountPath
# requirepass 123456 dir /data
创建资源文件redis-svc-deploy.yaml
yamlapiVersion: 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
应用资源
shellkubectl apply -f redis-svc-deploy.yaml
部署ry-admin
在nfs共享目录下创建ry-admin,挂载ruoyi-admin的日志以及头像等文件
注意替换spring配置文件中有关ip地址的配置,替换为service的name,如下:
yamlspring: datasource: druid: master: url: jdbc:mysql://mysql:3306/ry-vue redis: host: redis
镜像制作
bashmvn 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"]
创建资源文件ry-admin-svc-deploy.yaml
yamlapiVersion: 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
应用资源
shellkubectl 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
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
在nfs共享目录下创建ry-ui以及子目录dist和config,挂载ruoyi-ui的静态文件以及nginx的配置
将配置文件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; } }
静态文件打包,将静态文件放到dist目录下
npm run build:prod
创建资源文件ry-ui-svc-deploy.yaml
yamlapiVersion: 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