k8s集群部署xxl-job-admin

Xxl-job admin和xxl-job-excecutor的安装和配置

xxl-job-admin安装配置

您好!这是关于在k8s集群部署xxl-job-admin的文章。

1.首先,导入xxl-job-admin的数据库库表 地址:https://github.com/xuxueli/xxl-job.git sql文件: sql如下:

  1. 准备deployment和service文件,将这个yaml存为xxl-job.yaml
# 部署应用
apiVersion: apps/v1
kind: Deployment
metadata:
  namespace: 51hone
  name: xxl-job-admin
spec:
  replicas: 2
  selector:
    matchLabels:
      app: xxl-job-admin
  template:
    metadata:
      labels:
        app: xxl-job-admin
    spec:
      containers:
      - name: xxl-job-admin
        image: xuxueli/xxl-job-admin:2.3.1
        imagePullPolicy: Always     # 优先使用本地镜像
        ports:
        - containerPort: 8080
        env:
        - name: PARAMS   # 定义变量,用来接收sql的用户/密码 mysql为k8s集群内的service名称,在k8s集群内部可以直接使用service名称,因为集群默认做了coredns解析
          value: "--spring.datasource.url=jdbc:mysql://10.200.180.213:3306/xxljob?Unicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true --spring.datasource.username=xxljob --spring.datasource.password=yim3mHWNluBaOFo4 --spring.mail.username=fdd39969@163.com --spring.mail.password=yim3mHWNluBaOFo4"
---
apiVersion: v1
kind: Service
metadata:
  namespace: 51hone
  name: xxl-job-admin
  labels:
    app: xxl-job-admin
spec:
  ports:
  type: NodePort
  ports:
  - port: 8080
    targetPort: 8080
    nodePort: 30081
    #protocol: TCP
    #name: http
  selector:
    app: xxl-job-admin

将数据库地址和用户名密码改为自己的。

3.部署

kubectl apply -f xxl-job.yaml

查看已部署的资源(这里的内网ip,没有连接我们的内网vpn也没法访问):

4.连接xxl-job

访问xxl-job-admin,地址:http://NodeIP:30081/xxl-job-admin 默认用户名密码:admin/123456 至此在k8s集群部署xxl-job-admin即已经完成。

xxl-job-executor部署

以上部署了xxl-job-admin,接下来在ubuntu上部署一个xxl-job-executor,以便测试xxl-job-admin可用性。 1.拉取最新代码,使用mvn编译得到job-executor-sample-springboot-2.4.0-SNAPSHOT.jar

$ git clone https://github.com/xuxueli/xxl-job.git
$ cd xxl-job && mvn package
$ ll ./xxl-job-executor-samples/xxl-job-executor-sample-springboot/target/xxl-job-executor-sample-springboot-2.4.0-SNAPSHOT.jar

2.创建service文件,启动服务

admin # cat /etc/systemd/system/xxl-job-client.service 
[Unit]
Description=xxljobexe-3.23
After=network.target
[Service]
Type=simple
Environment=JAVA_HOME=/usr/local/jdk1.8.0_201
#Environment=GPHOME_LOADERS=/usr/local/greenplum-db-clients
ExecStart=/usr/local/jdk1.8.0_201/bin/java -Dxxl.job.admin.addresses=http://10.200.180.148:30081/xxl-job-admin -Dxxl.job.executor.appname=xxl-job-executor-sample -Dxxl.job.executor.ip=localhost -jar /home/admin/xxl-job/xxl-job-executor-samples/xxl-job-executor-sample-springboot/target/xxl-job-executor-sample-springboot-2.4.0-SNAPSHOT.jar
ExecReload=/bin/kill -s HUP $MAINPID
PrivateTmp=False
[Install]
WantedBy=multi-user.target

$ systemctl daemon-reload 
$ systemctl enable xxl-job-client 
$ systemctl start xxl-job-client

xxl-job-executor的默认端口是9999

  1. 配置xxl-job-admin执行shell脚本任务 登陆xxl-job-admin的web,点击执行器管理,新增执行器:http://executorIP:9999 再新建任务,点击任务管理,新建: 填入执行器,运行模式等关键信息,保存,再点开下拉选项,点击GLUE IDE,编辑脚本 编辑一个测试shell脚本: 在服务器/home/admin目录下新建一个脚本,脚本内容只是输出时间戳

注意:在shell里的路径都要使用绝对路径,因为xxl-job-executor中后台执行脚本是在它自己的工作目录。 准备好脚本,在任务中点执行一次: 查看执行日志,显示执行成功,但是仍然要去服务器看是否有输出: 服务器查看,有脚本输出,任务执行成功。

经验分享 程序员 微信小程序 职场和发展