部署ruoyi-vue实例前,需要准备看带有k8s环境的服务器,不懂部署环境请看我的上一篇文章centos 部署k8s集群
# mysql 数据持久化
mkdir -d /root/docker/ruoyi/mysql/data
# mysql 初始化sql文件
mkdir -d /root/docker/ruoyi/mysql/init
# mysql 日志
mkdir -d /root/docker/ruoyi/mysql/logs
# nginx
mkdir -d /root/docker/ruoyi/nginx/conf
mkdir -d /root/docker/ruoyi/nginx/logs
# ruoyi 后台打包jar文件
mkdir -d /root/docker/ruoyi/ry-admin
# ruoyi 前端j打包文件
mkdir -d /root/docker/ruoyi/ry-ui
将ruoyi-admin.jar放入/root/docker/ruoyi/ry-admin目录下
将打包文件dist放入/root/docker/ruoyi/ry-ui目录下
将sql文件[quartz.sql,ry_20230223.sql]放入/root/docker/ruoyi/mysql/init目录下
注:这里sql文件需要在文件首行加入以下代码,不然mysql自动导入时会出现乱码
SET character_set_client = utf8;
SET character_set_results = utf8;
SET character_set_connection = utf8;
由于k8s不能单独映射文件,对于前端项目、nginx、后台jar包需要自己构建docker镜像文件。
ry-admin dockerfile文件
FROM openjdk:8
MAINTAINER 1126680959@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 nohup java -jar app.jar --server.port=8090 --spring.profiles.active=prod
nginx dockerfile文件
# 基础镜像
FROM nginx
# author
MAINTAINER zhouhx
# 挂载目录
VOLUME /root/docker/ruoyi/ry-ui
# 创建目录
RUN mkdir -p /root/docker/ruoyi/ry-ui
# 指定路径
WORKDIR /root/docker/ruoyi/ry-ui
# 复制conf文件到路径
COPY ./conf/nginx.conf /etc/nginx/nginx.conf
#设置时区
RUN /bin/cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo 'Asia/Shanghai' >/etc/timezone
nginx conf文件
user root;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#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 logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
upstream tomcat-server{
# 后端服务地址
server 192.168.31.101:30809;
}
server {
listen 80;
server_name localhost;
charset utf-8;
location / {
# 前端文件路径
root /root/docker/ruoyi/ry-ui/dist;
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://tomcat-server/;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
# 后端镜像
cd /root/docker/ruoyi/ry-admin
docker build -f Dockerfile -t ry-nginx:3.8.5 .
# nginx镜像
cd /root/docker/ruoyi/nginx
docker build -f Dockerfile -t ry-nginx:3.8.5 .
注意:有几台服务器的话,就要分别在服务器上构建这些镜像,嫌麻烦的话可以自己装一个私有镜像仓库Harbor
apiVersion: v1
kind: Namespace
metadata:
name: ruoyi
---
apiVersion: v1
kind: Service
metadata:
namespace: ruoyi
name: ruoyi-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: ruoyi-mysql
labels:
app: mysql-deploy
spec:
selector:
matchLabels:
app: mysql-pod
replicas: 1
template:
metadata:
labels:
app: mysql-pod
spec:
nodeName: k8s-master
containers:
- name: mysql
image: mysql:5.7
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: '123456'
- 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: time-zone # 数据卷名称,需要与容器内挂载点名称一致
hostPath:
path: /etc/localtime # 挂载到容器里的路径,将localtime文件挂载到容器里,可让容器使用本地的时区
- name: data
hostPath:
path: /root/docker/ruoyi/mysql/data # 本地存放mysql数据的目录
- name: logs
hostPath:
path: /root/docker/ruoyi/mysql/logs # 本地存入mysql日志的目录
- name: init
hostPath:
path: /root/docker/ruoyi/mysql/init
- name: config
hostPath:
path: /root/docker/ruoyi/mysql/config
---
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
---
apiVersion: v1
kind: Service
metadata:
namespace: ruoyi
name: ruoyi-redis
labels:
app: redis-service
spec:
selector:
app: redis-pod
type: NodePort
ports:
- nodePort: 30007
port: 6379
protocol: TCP
targetPort: 6379
---
apiVersion: apps/v1
kind: Deployment
metadata:
namespace: ruoyi
name: ruoyi-redis
labels:
app: redis-deploy
spec:
selector:
matchLabels:
app: redis-pod
replicas: 1
template:
metadata:
labels:
app: redis-pod
spec:
nodeName: k8s-master
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
hostPath:
path: /root/docker/ruoyi/redis/data
#- name: config
# hostPath:
# path: /root/docker/ruoyi/redis/config
---
apiVersion: v1
kind: Service
metadata:
namespace: ruoyi
name: ruoyi-admin
labels:
app: ry-admin-service
spec:
selector:
app: ry-admin-pod
type: NodePort
ports:
- nodePort: 30809
port: 8090
protocol: TCP
targetPort: 8090
---
apiVersion: apps/v1
kind: Deployment
metadata:
namespace: ruoyi
name: ruoyi-admin
labels:
app: ry-admin-deploy
spec:
selector:
matchLabels:
app: ry-admin-pod
replicas: 1
template:
metadata:
labels:
app: ry-admin-pod
spec:
nodeName: k8s-master
containers:
- name: ry-admin
image: ry-admin:3.8.5
imagePullPolicy: Never # 使用本地镜像
ports:
- containerPort: 8090
env:
- name: TZ
value: Asia/Shanghai
volumeMounts:
- name: data
mountPath: /home/ruoyi
volumes:
- name: data
hostPath:
path: /root/docker/ruoyi/ry-admin/data
---
apiVersion: v1
kind: Service
metadata:
namespace: ruoyi
name: ruoyi-nginx
labels:
app: ry-nginx-service
spec:
selector:
app: ry-nginx-pod
type: NodePort
ports:
- nodePort: 30010
port: 80
protocol: TCP
targetPort: 80
---
apiVersion: apps/v1
kind: Deployment
metadata:
namespace: ruoyi
name: ruoyi-nginx
labels:
app: ry-nginx-deploy
spec:
selector:
matchLabels:
app: ry-nginx-pod
replicas: 1
template:
metadata:
labels:
app: ry-nginx-pod
spec:
nodeName: k8s-master
containers:
- name: ry-nginx
image: ry-nginx:3.8.5
imagePullPolicy: Never # 使用本地镜像
ports:
- containerPort: 80
env:
- name: TZ
value: Asia/Shanghai
volumeMounts:
- name: logs
mountPath: /var/log/nginx
- name: data
mountPath: /root/docker/ruoyi/ry-ui
volumes:
- name: logs
hostPath:
path: /root/docker/ruoyi/nginx/logs
- name: data
hostPath:
path: /root/docker/ruoyi/ry-ui
将文件放入/root/docker/ruoyi下
这里定义了ruoyi-admin、ruoyi-mysql、ruoyi-redis、ruoyi-nginx服务,主要是用nginx来分发后台服务实现负载均衡
cd /root/docker/ruoyi
# 创建服务
kubectl apply -f k8s-ruoyi.yaml
#执行完后查看服务
kubectl get pod -A -o wide
# 若要删除服务
kubectl delete -f k8s-ruoyi.yaml
状态都running后,访问ip:30010进入若依登录页
k8s有一个弹性伸缩的概念,即可以添加或删除副本数,以满足服务(Deployment)的调配
# 查看服务规模
kubectl get deployment -n ruoyi
# 调整deployment,指定副本数量
kubectl scale deployment/ruoyi-admin --replicas=2 -n ruoyi
通过kubectl describe service/ruoyi-admin -n ruoyi命令,可以看到Service的状态里已经侦测到多个EndPoint
上面是讲的手动调整服务规模,当前k8s还提供了自动伸缩的能力即AutoScaler
注意:k8s 默认master节点是不参与pod的调配的,如果需要请执行以下命令
#查看污点
[root@master1 ~]# kubectl describe node k8s-master | grep Taints
Taints: node-role.kubernetes.io/master:NoSchedule
#去除污点,允许 master 部署 pod ,这里报错不用管
[root@master1 ~]# kubectl taint nodes --all node-role.kubernetes.io/master-
node/master1 untainted
error: taint "node-role.kubernetes.io/master" not found
#再次查看,无显示,说明污点去除成功
[root@master1 ~]# kubectl describe node master1 | grep Taints
Taints:
# 查看所有pod信息
kubectl get pod -A -o wide
# 查看k8s命名空间
kubectl get ns
# 快速重启pod
kubectl -n 命名空间名 delete pod pod名字
#滚动重启命令
kubectl -n 命名空间名 rollout restart deployment deployment名字
#通常当一个pod处于Error状态或者是CrashLoopBackOff状态时我们需要查看pod的日志。
#查看pod日志的命令
kubectl -n 命名空间名 logs -f pod名
#当pod不属于running,Error,CrashLoopBackOff的其他异常状态时通常时k8s内部集群调度引发的异常。通过describe命令查看具体原因。
kubectl describe pod pod名 -n 命名空间名
# 查看服务规模
kubectl get deployment -n 命名空间名
# 查看服务
kubectl describe service/服务名 -n 命名空间名
# 调整deployment,指定副本数量
kubectl scale deployment/服务名 --replicas=副本数量 -n 命名空间名