部署环境
- xxl-job-2.4.0
- kubernetes-1.26
这里以xxl-job官方的2.4.0的代码为例子,在官方编写的Dockerfile基础上使用docker+kubernetes进行部署,++xxl-job-admin和执行器的Dockerfile、application等配置文件并不是关键,所以这里示例安装以官方Demo文件示例++
编写Dockerfile、application文件
xxl-job-admin-Dockerfile文件
FROM openjdk:8-jre-slim
MAINTAINER xuxueli
ENV PARAMS=""
ENV TZ=PRC
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
ADD target/xxl-job-admin-*.jar /app.jar
ENTRYPOINT ["sh","-c","java -jar $JAVA_OPTS /app.jar $PARAMS"]
xxl-job-admin-application.properties文件
### web
server.port=8080
server.servlet.context-path=/xxl-job-admin
### actuator
management.server.servlet.context-path=/actuator
management.health.mail.enabled=false
### resources
spring.mvc.servlet.load-on-startup=0
spring.mvc.static-path-pattern=/static/**
spring.resources.static-locations=classpath:/static/
### freemarker
spring.freemarker.templateLoaderPath=classpath:/templates/
spring.freemarker.suffix=.ftl
spring.freemarker.charset=UTF-8
spring.freemarker.request-context-attribute=request
spring.freemarker.settings.number_format=0.##########
### mybatis
mybatis.mapper-locations=classpath:/mybatis-mapper/*Mapper.xml
#mybatis.type-aliases-package=com.xxl.job.admin.core.model
### xxl-job, datasource
spring.datasource.url=jdbc:mysql://localhost:3306/xxl_job?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&serverTimezone=Asia/Shanghai
spring.datasource.username=admin
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
### datasource-pool
spring.datasource.type=com.zaxxer.hikari.HikariDataSource
spring.datasource.hikari.minimum-idle=10
spring.datasource.hikari.maximum-pool-size=30
spring.datasource.hikari.auto-commit=true
spring.datasource.hikari.idle-timeout=30000
spring.datasource.hikari.pool-name=HikariCP
spring.datasource.hikari.max-lifetime=900000
spring.datasource.hikari.connection-timeout=10000
spring.datasource.hikari.connection-test-query=SELECT 1
spring.datasource.hikari.validation-timeout=1000
### xxl-job, email
spring.mail.host=smtp.qq.com
spring.mail.port=25
spring.mail.username=2207080688@qq.com
spring.mail.from=2207080688@qq.com
spring.mail.password=123456
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
spring.mail.properties.mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory
### xxl-job, access token
xxl.job.accessToken=
### xxl-job, i18n (default is zh_CN, and you can choose "zh_CN", "zh_TC" and "en")
xxl.job.i18n=zh_CN
## xxl-job, triggerpool max size
xxl.job.triggerpool.fast.max=200
xxl.job.triggerpool.slow.max=100
### xxl-job, log retention days
xxl.job.logretentiondays=30
xxl-job-executor-sample-springboot(执行器)-Dockerfile文件
FROM openjdk:8-jre-slim
MAINTAINER xuxueli
ENV PARAMS=""
ENV TZ=PRC
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
ADD target/xxl-job-executor-sample-springboot-*.jar /app.jar
ENTRYPOINT ["sh","-c","java -jar $JAVA_OPTS /app.jar $PARAMS"]
xxl-job-executor-sample-springboot(执行器)-application文件
# web port
server.port=8081
# no web
#spring.main.web-environment=false
# log config
logging.config=classpath:logback.xml
### xxl-job admin address list, such as "http://address" or "http://address01,http://address02"
xxl.job.admin.addresses=http://localhost:8080/xxl-job-admin/
### xxl-job, access token
xxl.job.accessToken=
### xxl-job executor appname
xxl.job.executor.appname=xxl-job-executor-sample
### xxl-job executor registry-address: default use address to registry , otherwise use ip:port if address is null
xxl.job.executor.address=
### xxl-job executor server-info
xxl.job.executor.ip=
xxl.job.executor.port=9999
### xxl-job executor log-path
xxl.job.executor.logpath=/data/applogs/xxl-job/jobhandler
### xxl-job executor log-retention-days
xxl.job.executor.logretentiondays=30
注意事项:这里以springboot服务作为执行器服务,一般建议执行器服务和具体的系统业务都是合并为同一个服务,这样有利于后期具体的系统业务使用xxl-job的GLUE模式,并不建议把执行器服务单独设为一个独立的服务。所以这里执行器的服务端口号为8081,xxl-job接收调度的端口号为9999,其中9999会接收admin的NettyHttp的请求。
以上的4个文件基本和官方保持一样即可,也可根据各自需要增加配置。
构建Docker镜像
在xxl-job-admin的Dockerfile同级目录下构建镜像:
docker build -t xxl-job-admin:v1 .
在xxl-job-executor-sample-springboot的Dockerfile同级目录下构建镜像:
docker build -t xxl-job-executor-sample-springboot .
编写Kubernete的deploy、svc等部署文件
mkdir /opt/k8s/xxl-job/admin
cd /opt/k8s/xxl-job/admin
创建命名空间
kubectl create ns xxl-job
创建xxl-job-admin的deploy和service文件
vi xxl-job-admin-ds.yml
apiVersion: apps/v1
kind: Deployment
metadata:
name: xxl-job-admin
namespace: xxl-job
labels:
name: xxl-job-admin
spec:
replicas: 1
selector:
matchLabels:
name: xxl-job-admin
template:
metadata:
labels:
name: xxl-job-admin
spec:
containers:
- name: xxl-job-admin
image: xxl-job-admin:v1
ports:
- containerPort: 8080
---
apiVersion: v1
kind: Service
metadata:
name: xxl-job-admin
namespace: xxl-job
labels:
name: xxl-job-admin
spec:
selector:
name: xxl-job-admin
type: NodePort
ports:
- name: http
nodePort: 28080
targetPort: 8080
port: 80
protocol: TCP
在xxl-job-admin-ds.yml的同级目录下部署运行
kubectl apply -f .
查看部署运行状态
kubectl get all -n xxl-job
当看到pod的运行状态为Running,并且svc的端口已经成功映射则在浏览器访问:http://localhost:28080/xxl-job-admin
++访问成功则代表部署xxl-job-admin成功++
接下来编写执行器服务的deploy和service文件
mkdir /opt/k8s/xxl-job/executor-simple
cd /opt/k8s/xxl-job/executor-simple
vi xxl-job-executor-simple-ds.yml
apiVersion: apps/v1
kind: Deployment
metadata:
name: xxl-job-executor-simple
namespace: xxl-job
labels:
name: xxl-job-executor-simple
spec:
replicas: 1
selector:
matchLabels:
name: xxl-job-executor-simple
template:
metadata:
labels:
name: xxl-job-executor-simple
spec:
containers:
- name: xxl-job-executor-simple
image: xxl-job-executor-sample-springboot:latest
ports:
- containerPort: 8081
protocol: TCP
- containerPort: 9999
protocol: TCP
---
apiVersion: v1
kind: Service
metadata:
name: xxl-job-executor-simple
namespace: xxl-job
labels:
name: xxl-job-executor-simple
spec:
type: ClusterIP
ports:
- name: http
targetPort: 8081
port: 80
protocol: TCP
- name: job
targetPort: 9999
port: 9999
protocol: TCP
selector:
name: xxl-job-executor-simple
注意:这里的执行器服务有需要两个端口号,一个是HttpMethod的8081端口,一个是xxl-job调度使用NettyHttp的9999端口
在xxl-job-executor-simple-ds.yml目录下部署执行器服务
kubectl apply -f .
运行命令观察执行器的pod、svc等运行状态
kubectl get all -n xxl-job
观察执行器的pod、svc状态,当pod为Running即部署执行器成功。
由于xxl-job的注册方式有两种:
- 自动注册:执行器会以心跳的方式,隔断时间就会向注册地址进行自动注册。
- 手动注册:用户在xxl-job-admin手动填写执行器的地址
由于我们是使用Kubernetes进行部署的,这里不推荐使用自动注册,因为在kubernetes环境下自动注册的地址是pod的虚拟ip会随着pod的重启而变化,所以不推荐自动注册。
额外踩坑:使用xxl-job官方执行器服务的话,关于xxl.job.admin的配置不能配置在nacos上,需要部署在bootstrap或者application等本地文件上,否则不会生效。
以上为例,使用 http://serviceName.namespace:端口号 进行注册,如下图
以上则使用Kubernetes部署xxl-job的全部教程,踩坑不易,支持原创。