在 K8S 中部署 xxl-job 分布式任务调度平台
准备条件
- Kubernetes 1.23+
- 外部 MySQL 8.0+
- Nginx Ingress Controller 流量网关类
ingressClass
初始化数据库
GitHub Release 选择一个版本,下载项目源码。
调度数据库初始化SQL脚本” 位置为:
xxl-job/doc/db/tables_xxl_job.sql
导入 MySQL 实例。
提示:
调度中心支持集群部署,集群情况下各节点务必连接同一个mysql实例。
xxl-job 在 Kuberneres 中可以直接通过调节 Pod 副本数来实现应用横线扩展。
在 Kubernetes 中部署
deployment.yaml
- 需要重点环境变量
PARM
的部分,请将以下参数替换成实际的值:--spring.datasource.url=jdbc:mysql://${MYSQL_HOST}:${MYSQL_PORT}/${MYSQL_DATABASE}?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&serverTimezone=Asia/Shanghai --spring.datasource.username=${MYSQL_USERNAME} --spring.datasource.password=${MYSQL_PASSWORD}
- 请根据实际情况,调整应用的资源限制, YAML 为参考值
apiVersion: apps/v1
kind: Deployment
metadata:
name: xxl-job
namespace: middleware
spec:
replicas: 1
selector:
matchLabels:
app: xxl-job
template:
metadata:
labels:
app: xxl-job
spec:
containers:
- name: xxl-job
image: xuxueli/xxl-job-admin:2.4.1
ports:
- containerPort: 8080
env:
- name: TZ
value: Asia/Shanghai
- name: PARAMS
value: "--spring.datasource.url=jdbc:mysql://${MYSQL_HOST}:${MYSQL_PORT}/${MYSQL_DATABASE}?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&serverTimezone=Asia/Shanghai --spring.datasource.username=${MYSQL_USERNAME} --spring.datasource.password=${MYSQL_PASSWORD}"
readinessProbe:
tcpSocket:
port: 8080
initialDelaySeconds: 0
periodSeconds: 10
timeoutSeconds: 1
successThreshold: 1
failureThreshold: 3
livenessProbe:
tcpSocket:
port: 8080
initialDelaySeconds: 0
periodSeconds: 30
timeoutSeconds: 1
successThreshold: 1
failureThreshold: 3
startupProbe:
tcpSocket:
port: 8080
initialDelaySeconds: 0
periodSeconds: 10
timeoutSeconds: 1
successThreshold: 1
failureThreshold: 3
resources:
limits:
cpu: 0.5
memory: 1Gi
requests:
cpu: 0.5
memory: 1Gi
service.yaml
xxl-job 运行在 8080 端口号,使用 NodePort
暴露服务:
apiVersion: v1
kind: Service
metadata:
name: xxl-job
namespace: middleware
spec:
type: NodePort
selector:
app: xxl-job
ports:
- protocol: TCP
port: 80
targetPort: 8080
ingress.yaml
- 将域名绑定服务
- 可选配置:
- 开启 Prometheus 自动发现监控
- SSL 加密
- 在公网环境下建议绑定白名单
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: xxl-job
namespace: middleware
annotations:
prometheus.io/http-probe: "true"
prometheus.io/http-probe-path: /
nginx.ingress.kubernetes.io/whitelist-source-range: "10.0.0.0/8,172.16.0.0/12,192.168.0.0/16"
spec:
ingressClassName: clb
rules:
- host: xxl-job.example.cn
http:
paths:
- backend:
service:
name: xxl-job
port:
number: 80
path: /
pathType: Prefix
tls:
- hosts:
- xxl-job.example.cn
secretName: ssl-star-test-example-cn
访问项目
调度中心访问地址:http://localhost:8080/xxl-job-admin (该地址执行器将会使用到,作为回调地址)
默认登录账号 “admin/123456”, 登录后运行界面如下图所示。