gocron 高可用部署 linux 持续更新

  • Post author:
  • Post category:linux




架构图

在这里插入图片描述



Gocron 介绍


gocron

是使用

go

语言开发的轻量级定时任务集中调度和管理系统, 可以用于替代 linux-crontab。


项目地址



环境介绍
服务器 应用
172.18.138.10 gocron-web nginx
172.18.35.144 gocron-web
172.18.138.4 gocron-node


操作系统:centos 7.9

gocron version:v1.5.3

安装包如下


gocron-web



gocron



10 144 服务器安装gocron-web
  1. 下载gocron-web
wget http://8.210.62.122/gocron/gocron-v1.5.3-linux-amd64.tar.gz
tar xzvf gocron-v1.5.3-linux-amd64.tar.gz -C /usr/local/services/
cd /usr/local/services/gocron-linux-amd64
[yingjian@iZwz9b8x8l2oe511fss4qfZ gocron-linux-amd64]$ ./gocron -v
gocron version 
   Version: v1.5.3
Go Version: go1.13
Git Commit: 93c544f
     Built: 2020-02-20 21:23:25
   OS/ARCH: linux/amd64
  1. systemd管理gocron-web
[yingjian@iZwz9b8x8l2oe511fss4qfZ gocron-linux-amd64]$ cat /usr/lib/systemd/system/gocron-web.service 
[Unit]
Description=gocron web manage
Documentation=https://github.com/ouqiang/gocron
After=network.target

[Service]
Type=simple
User=root
ExecStart=/usr/local/services/gocron-linux-amd64/gocron web
Restart=on-failure
WorkingDirectory=/usr/local/services/gocron-linux-amd64

[Install]
WantedBy=multi-user.target

systemctl daemon-reload
systemctl restart gocron-web  #启动
systemctl enable gocron-web #开机自启
systemctl status gocron-web #查看状态


创建数据库和对应的用户账号权限
CREATE DATABASE `gocron` CHARACTER SET utf8mb4 COLLATE utf8mb4_bin; # 创建 程序连接数据库

grant all on `gocron`.* to gocron@'%' identified by '******'; # 创建程序使用用户,并将对应库权限授予此用户进行使用

flush privileges;

访问 10 服务器5920端口

如下图所示配置数据库连接地址

在这里插入图片描述

144 服务器conf 配置

/usr/local/services/gocron-linux-amd64  #10服务器
[root@iZwz9b8x8l2oe511fss4qfZ gocron-linux-amd64]# ls
conf  gocron  log
[root@iZwz9b8x8l2oe511fss4qfZ gocron-linux-amd64]# cd conf/
[root@iZwz9b8x8l2oe511fss4qfZ conf]# ls
app.ini  install.lock

[root@iZwz9b8x8l2oe511fss4qfZ conf]# sz app.ini  #下载此文件后上传到144 服务器下的conf目录下


10 app.ini文件


在这里插入图片描述


144 app.ini文件


在这里插入图片描述



配置nginx 负载均衡gocron-web
cat nginx.conf 

    upstream gocron_web {
        server 172.18.138.10:5920;
        server 172.18.35.144:5920 backup;  #当10服务器不能提供服务时,请求才到144服务器上
    }
    server {
        listen  80;
        listen       443 ssl;
        server_name  cron.jinzjy.com;

        ssl_certificate      cert/3502__jinzjy.com.pem;
        ssl_certificate_key  cert/3502__jinzjy.com.key;

        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  5m;

        ssl_ciphers  ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_prefer_server_ciphers  on;

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        location / {
            proxy_pass   http://gocron_web;
            proxy_set_header   Host    $host;
            proxy_set_header   X-Real-IP   $remote_addr;
            proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_cookie_path  / /;
            proxy_set_header Cookie $http_cookie;
        }

    }

如图所示

在这里插入图片描述



4 服务器部署gocron-node

1.下载gocron-node

wget http://8.210.62.122/gocron/gocron-v1.5.3-linux-amd64.tar.gz
tar xzvf gocron-node-v1.5.3-linux-amd64.tar.gz -C /usr/local/services/
[yingjian@iZwz9c0rnhno789fl3ibk6Z src]$ cd /usr/local/services/gocron-node-linux-amd64/
[yingjian@iZwz9c0rnhno789fl3ibk6Z gocron-node-linux-amd64]$ ls
gocron-node  home_list.txt  nohup.out
[yingjian@iZwz9c0rnhno789fl3ibk6Z gocron-node-linux-amd64]$ ./gocron-node -v

   Version: v1.5.3
Go Version: go1.13
Git Commit: 93c544f
     Built: 2020-02-20 21:24:23
   OS/ARCH: linux/amd64
  1. systemd管理gocron-node
cat /usr/lib/systemd/system/gocron-node.service 
[Unit]
Description=gocron agent/node server
Documentation=https://github.com/ouqiang/gocron
After=network.target

[Service]
Type=simple
User=root
ExecStart=/usr/local/services/gocron-node-linux-amd64/gocron-node -allow-root
WorkingDirectory=/usr/local/services/gocron-node-linux-amd64   #执行计划任务时当前目录
Restart=on-failure

[Install]
WantedBy=multi-user.target

systemctl daemon-reload
systemctl restart gocron-node  #启动
systemctl enable gocron-node #开机自启
systemctl status gocron-node #查看状态


gocron-web 添加任务节点

在这里插入图片描述



配置邮件通知

在这里插入图片描述

相关报错处理

  1. gocron-node 执行计划任务time-out

    在这里插入图片描述


    查看该任务 配置任务超时时间


    在这里插入图片描述

本文参考:https://www.treesir.pub/post/gocron-install/

本文参考:http://www.xckey.com/952.html



定时清理 计划任务日志


脚本地址

[root@vpn-wiki scripts]# cat gocron_clean.sh 
#/usr/bin/bash

#Tate:2022.6.6
#Author:Yingjian
#function: 定时清理mysql gocron 库表cron_task_log的日志数据
#env

workdir=`cd $(dirname $0);pwd`
seven_day_age=`date -d '-7 day' "+%F"`
mysql_bin=/usr/local/services/mysql/bin/mysql
user=gocron
password=******
host=lan.mysql.****.com
logfile=$workdir/gocron_clean.log

#echo $seven_day_age
echo "----`date` mysql gocron clean cron_task_log started sh------" >> $logfile

delete_sql="delete from cron_task_log where (start_time >= '${seven_day_age} 00:00:00' and start_time <= '${seven_day_age} 23:59:59' )"

$mysql_bin -h${host} -u${user} -p${password} -D${user} -e"${delete_sql}"

echo "----`date` mysql gocron clean cron_task_log end sh------" >> $logfile


计划任务

在这里插入图片描述



版权声明:本文为weixin_45717886原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。