原生K8S部署pig微服务项目
简介
项目地址:码云
Pig微服务项目
基于 Spring Cloud 2021 、Spring Boot 2.7、 OAuth2 的 RBAC 权限管理系统
基于数据驱动视图的理念封装 element-plus,即使没有 vue 的使用经验也能快速上手
提供对常见容器化支持 Docker、Kubernetes、Rancher2 支持
提供 lambda 、stream api 、webflux 的生产实践
pig-ui – https://gitee.com/log4j/pig-ui
pig
├── pig-auth – 授权服务提供[3000]
└── pig-common – 系统公共模块
├── pig-common-bom – 全局依赖管理控制
├── pig-common-core – 公共工具类核心包
├── pig-common-datasource – 动态数据源包
├── pig-common-job – xxl-job 封装
├── pig-common-log – 日志服务
├── pig-common-mybatis – mybatis 扩展封装
├── pig-common-seata – 分布式事务
├── pig-common-security – 安全工具类
├── pig-common-swagger – 接口文档
├── pig-common-feign – feign 扩展封装
└── pig-common-xss – xss 安全封装
├── pig-register – Nacos Server[8848]
├── pig-gateway – Spring Cloud Gateway网关[9999]
└── pig-upms – 通用用户权限管理模块
└── pig-upms-api – 通用用户权限管理系统公共api模块
└── pig-upms-biz – 通用用户权限管理系统业务处理模块[4000]
└── pig-visual
└── pig-monitor – 服务监控 [5001]
├── pig-codegen – 图形化代码生成 [5002]
├── pig-sentinel-dashboard – 流量高可用 [5003]
└── pig-xxl-job-admin – 分布式定时任务管理台 [5004]
一、前提条件
1.需要搭建好K8S集群环境
master:192.168.10.140
node:192.168.10.141
node2:192.168.10.142
2.搭建好Kubernetes Dashboard(本次部署基于它)
3.新建名称空间:PIG
4.打包规划
由于K8S的Services默认端口范围为30000-32767,为了使部署简单,规划以下服务访问端口
**
pig-ui:30000
pig-mysql:30001
pig-redis:30002
pig-gateway:30003
pig-register:30004
**
二、拉取Pig项目前后端代码到本地测试运行
1.pig-ui地址:https://gitee.com/log4j/pig-ui
2.pig地址:https://gitee.com/log4j/pig
三、部署MySQL与Redis
我这里是离线部署,docker中已经有mysql:8.0.18与redis:7.0.5镜像
如果没有,则修改imagePullPolicy配置
Always 总是拉取镜像
IfNotPresent 本地有则使用本地镜像,不拉取
Never 只使用本地镜像,从不拉取,即使本地没有
如果省略imagePullPolicy, 策略为always
1.部署MySQL
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: pig-mysql
name: pig-mysql
spec:
replicas: 1
selector:
matchLabels:
app: pig-mysql
template:
metadata:
labels:
app: pig-mysql
spec:
containers:
- image: mysql:8.0.18
name: pig-mysql
imagePullPolicy: IfNotPresent
ports:
- containerPort: 3306
name: pig-mysql
env:
- name: MYSQL_ROOT_PASSWORD
value: root
---
apiVersion: v1
kind: Service
metadata:
labels:
app: pig-mysql
name: pig-mysql
spec:
ports:
- port: 3306
protocol: TCP
targetPort: 3306
nodePort: 30001
selector:
app: pig-mysql
type: NodePort
复制Yaml到Kubernetes Dashboard点击上传
查看Deployments
查看Services
本地使用navcat连接测试
导入PIG项目数据库
2.部署Redis
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: pig-redis
name: pig-redis
spec:
replicas: 1
selector:
matchLabels:
app: pig-redis
template:
metadata:
labels:
app: pig-redis
spec:
containers:
- image: redis:7.0.5
name: pig-redis
imagePullPolicy: IfNotPresent
---
apiVersion: v1
kind: Service
metadata:
labels:
app: pig-redis
name: pig-redis
spec:
ports:
- port: 6379
protocol: TCP
targetPort: 6379
nodePort: 30002
selector:
app: pig-redis
type: NodePort
同mysql部署
四、打包后端镜像
1.IDEA安装docker插件
2.配置打包仓库地址
集群查看docker连接地址
新增docker配置
3.修改各服务yml配置
pig-register服务application.yml
pig-gateway服务application.yml
pig-auth服务application.yml
pig-upms-biz服务application.yml
其它服务同理注意修改数据库和pig-register配置即可
3.依次打包镜像到K8S
4.打包成功查看服务器镜像
五、打包前端镜像
1.修改DockerFile指定Nginx版本
2.指定pig-gateway访问路径
这里我们可以通过 服务名.名称空间.svc来部署,这样可移植性更高
3.执行npm run build:docker
执行后会生成dist文件夹
4.压缩docker文件夹,上传到服务器打包镜像
执行命令docker build -f Dockerfile -t pig-ui:1.0.0 .
5.查看docker镜像
六、部署服务
1.部署pig-register
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: pig-register
name: pig-register
spec:
replicas: 1
selector:
matchLabels:
app: pig-register
template:
metadata:
labels:
app: pig-register
spec:
containers:
- image: pig-register:1.0.0
name: pig-register
imagePullPolicy: IfNotPresent
---
apiVersion: v1
kind: Service
metadata:
labels:
app: pig-register
name: pig-register
spec:
ports:
- port: 8848
protocol: TCP
name: server
targetPort: 8848
nodePort: 30004
- port: 9848
protocol: TCP
name: client-rpc
targetPort: 9848
nodePort: 30005
- port: 9849
protocol: TCP
name: raft-rpc
targetPort: 9849
nodePort: 30006
selector:
app: pig-register
type: NodePort
注意,每个服务启动需要等60S
查看POD启动日志
访问pig-register修改各yaml配置内Mysql,Redis访问地址
application-dev.yml
# 加解密根密码
jasypt:
encryptor:
password: pig #根密码
# Spring 相关
spring:
cache:
type: redis
redis:
host: pig-redis.pig.svc
cloud:
sentinel:
eager: true
transport:
dashboard: pig-sentinel.pig.svc:5003
# 暴露监控端点
management:
endpoints:
web:
exposure:
include: "*"
endpoint:
health:
show-details: ALWAYS
# feign 配置
feign:
sentinel:
enabled: true
okhttp:
enabled: true
httpclient:
enabled: false
client:
config:
default:
connectTimeout: 10000
readTimeout: 10000
compression:
request:
enabled: true
response:
enabled: true
# mybaits-plus配置
mybatis-plus:
mapper-locations: classpath:/mapper/*Mapper.xml
global-config:
banner: false
db-config:
id-type: auto
table-underline: true
logic-delete-value: 1
logic-not-delete-value: 0
configuration:
map-underscore-to-camel-case: true
# spring security 配置
security:
oauth2:
# 通用放行URL,服务个性化,请在对应配置文件覆盖
ignore:
urls:
- /v3/api-docs
- /actuator/**
# swagger 配置
swagger:
enabled: true
title: Pig Swagger API
gateway: http://${GATEWAY_HOST:pig-gateway.pig.svc}:${GATEWAY-PORT:30003}
token-url: ${swagger.gateway}/auth/oauth2/token
scope: server
services:
pig-upms-biz: admin
pig-codegen: gen
pig-codegen-dev.yml
## spring security 配置
security:
oauth2:
client:
client-id: ENC(27v1agvAug87ANOVnbKdsw==)
client-secret: ENC(VbnkopxrwgbFVKp+UxJ2pg==)
scope: server
# 数据源配置
spring:
datasource:
type: com.zaxxer.hikari.HikariDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: root
url: jdbc:mysql://pig-mysql.pig.svc:3306/pig_codegen?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=Asia/Shanghai&nullCatalogMeansCurrent=true&allowPublicKeyRetrieval=true
resources:
static-locations: classpath:/static/,classpath:/views/
pig-gateway-dev.yml
spring:
cloud:
gateway:
locator:
enabled: true
routes:
# 认证中心
- id: pig-auth
uri: lb://pig-auth
predicates:
- Path=/auth/**
filters:
# 验证码处理
- ValidateCodeGatewayFilter
# 前端密码解密
- PasswordDecoderFilter
#UPMS 模块
- id: pig-upms-biz
uri: lb://pig-upms-biz
predicates:
- Path=/admin/**
filters:
# 限流配置
- name: RequestRateLimiter
args:
key-resolver: '#{@remoteAddrKeyResolver}'
redis-rate-limiter.replenishRate: 100
redis-rate-limiter.burstCapacity: 200
# 代码生成模块
- id: pig-codegen
uri: lb://pig-codegen
predicates:
- Path=/gen/**
# 固定路由转发配置 无修改
- id: openapi
uri: lb://pig-gateway
predicates:
- Path=/v3/api-docs/**
filters:
- RewritePath=/v3/api-docs/(?<path>.*), /$\{path}/$\{path}/v3/api-docs
gateway:
encode-key: 'thanks,pig4cloud'
ignore-clients:
- test
- client
pig-upms-biz-dev.yml文章来源:https://www.toymoban.com/news/detail-438456.html
security:
oauth2:
client:
client-id: ENC(imENTO7M8bLO38LFSIxnzw==)
client-secret: ENC(i3cDFhs26sa2Ucrfz2hnQw==)
scope: server
# 数据源
spring:
datasource:
type: com.zaxxer.hikari.HikariDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: root
url: jdbc:mysql://pig-mysql.pig.svc:3306/pig?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowMultiQueries=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=Asia/Shanghai&nullCatalogMeansCurrent=true&allowPublicKeyRetrieval=true
# 文件上传相关 支持阿里云、华为云、腾讯、minio
oss:
path-style-access: true #使用云OSS 需要关闭
endpoint: http://192.168.10.140:9000
accessKey: minioadmin
secretKey: minioadmin
bucket-name: test
2.部署pig-gateway
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: pig-gateway
name: pig-gateway
spec:
replicas: 1
selector:
matchLabels:
app: pig-gateway
template:
metadata:
labels:
app: pig-gateway
spec:
containers:
- image: pig-gateway:1.0.0
name: pig-gateway
imagePullPolicy: IfNotPresent
---
apiVersion: v1
kind: Service
metadata:
labels:
app: pig-gateway
name: pig-gateway
spec:
ports:
- port: 9999
protocol: TCP
targetPort: 9999
nodePort: 30003
selector:
app: pig-gateway
type: NodePort
3.部署pig-auth
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: pig-auth
name: pig-auth
spec:
replicas: 1
selector:
matchLabels:
app: pig-auth
template:
metadata:
labels:
app: pig-auth
spec:
containers:
- image: pig-auth:1.0.0
name: pig-auth
imagePullPolicy: IfNotPresent
---
apiVersion: v1
kind: Service
metadata:
labels:
app: pig-auth
name: pig-auth
spec:
ports:
- port: 3000
protocol: TCP
targetPort: 3000
selector:
app: pig-auth
type: ClusterIP
4.部署pig-upms-biz
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: pig-upms-biz
name: pig-upms-biz
spec:
replicas: 1
selector:
matchLabels:
app: pig-upms-biz
template:
metadata:
labels:
app: pig-upms-biz
spec:
containers:
- image: pig-upms-biz:1.0.0
name: pig-upms-biz
imagePullPolicy: IfNotPresent
---
apiVersion: v1
kind: Service
metadata:
labels:
app: pig-upms-biz
name: pig-upms-biz
spec:
ports:
- port: 4000
protocol: TCP
targetPort: 4000
selector:
app: pig-upms-biz
type: ClusterIP
5.部署pig-monitor
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: pig-monitor
name: pig-monitor
spec:
replicas: 1
selector:
matchLabels:
app: pig-monitor
template:
metadata:
labels:
app: pig-monitor
spec:
containers:
- image: pig-monitor:1.0.0
name: pig-monitor
imagePullPolicy: IfNotPresent
---
apiVersion: v1
kind: Service
metadata:
labels:
app: pig-monitor
name: pig-monitor
spec:
ports:
- port: 5001
protocol: TCP
targetPort: 5001
selector:
app: pig-monitor
type: ClusterIP
6.部署pig-sentinel-dashboard
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: pig-sentinel-dashboard
name: pig-sentinel-dashboard
spec:
replicas: 1
selector:
matchLabels:
app: pig-sentinel-dashboard
template:
metadata:
labels:
app: pig-sentinel-dashboard
spec:
containers:
- image: pig-sentinel-dashboard:1.0.0
name: pig-sentinel-dashboard
imagePullPolicy: IfNotPresent
---
apiVersion: v1
kind: Service
metadata:
labels:
app: pig-sentinel-dashboard
name: pig-sentinel-dashboard
spec:
ports:
- port: 5003
protocol: TCP
targetPort: 5003
selector:
app: pig-sentinel-dashboard
type: NodePort
7.部署pig-codegen
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: pig-codegen
name: pig-codegen
spec:
replicas: 1
selector:
matchLabels:
app: pig-codegen
template:
metadata:
labels:
app: pig-codegen
spec:
containers:
- image: pig-codegen:1.0.0
name: pig-codegen
imagePullPolicy: IfNotPresent
---
apiVersion: v1
kind: Service
metadata:
labels:
app: pig-codegen
name: pig-codegen
spec:
ports:
- port: 5002
protocol: TCP
targetPort: 5002
selector:
app: pig-codegen
type: NodePort
8.部署pig-xxl-job-admin
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: pig-xxl-job-admin
name: pig-xxl-job-admin
spec:
replicas: 1
selector:
matchLabels:
app: pig-xxl-job-admin
template:
metadata:
labels:
app: pig-xxl-job-admin
spec:
containers:
- image: pig-xxl-job-admin:1.0.0
name: pig-xxl-job-admin
imagePullPolicy: IfNotPresent
---
apiVersion: v1
kind: Service
metadata:
labels:
app: pig-xxl-job-admin
name: pig-xxl-job-admin
spec:
ports:
- port: 5004
protocol: TCP
targetPort: 5004
selector:
app: pig-xxl-job-admin
type: NodePort
查看服务列表
文章来源地址https://www.toymoban.com/news/detail-438456.html
9.部署minio
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: minio
name: minio
spec:
replicas: 1
selector:
matchLabels:
app: minio
template:
metadata:
labels:
app: minio
spec:
containers:
- image: minio:latest
name: minio
imagePullPolicy: IfNotPresent
command:
- /bin/sh
- -c
- minio server /data --console-address ":5000"
ports:
- name: data
containerPort: 9000
protocol: "TCP"
- name: console
containerPort: 5000
protocol: "TCP"
---
apiVersion: v1
kind: Service
metadata:
labels:
app: minio
name: minio
spec:
ports:
- port: 9000
name: data
protocol: TCP
targetPort: 80
nodePort: 30090
- port: 5000
name: console
protocol: TCP
targetPort: 5000
nodePort: 30050
selector:
app: minio
type: NodePort
到了这里,关于原生K8S部署pig微服务项目的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!