# Cronsun 任务管理器部署

#### 博客作者：联系请[点击](https://hezhiqiang.gitbook.io/about-the-author/lian-xi-zuo-zhe)，搬运不易，希望请作者喝咖啡，可以点击[联系博客作者](https://hezhiqiang.gitbook.io/about-the-author/lian-xi-zuo-zhe)

## 引言

`cronsun` 是一个分布式任务系统，单个节点和 `*nix` 机器上的 `crontab` 近似。支持界面管理机器上的任务，支持任务失败邮件提醒，安装简单，使用方便，是替换 `crontab` 一个不错的选择。

`cronsun` 是为了解决多台 `*nix` 机器上`crontab` 任务管理不方便的问题，同时提供任务高可用的支持（当某个节点死机的时候可以自动调度到正常的节点执行）。`cronsun` 和 [Azkaban](https://azkaban.github.io/)、[Chronos](https://mesos.github.io/chronos/)、[Airflow](https://airflow.incubator.apache.org/) 这些不是同一类型的。

## 项目状态

&#x20;`cronsun`已经在线上几百台规模的服务器上面稳定运行了一年多了，虽然目前版本不是正式版，但是我们认为是完全可以用于生产环境的。强烈建议你试用下，因为它非常简单易用，同时感受下他的强大，相信你会喜欢上这个工具的。

## cronsun特性：

* 方便对多台服务器上面的定时任务进行集中式管理
* 任务调度时间粒度支持到秒级别
* 任务失败自动重试
* 任务可靠性保障（从N个节点里面挑一个可用节点来执行任务）
* 简洁易用的管理后台，支持多语言
* 任务日志查看
* 任务失败邮件告警（也支持自定义http告警接口）
* 用户验证与授权

## 部署对象说明

### 程序名称

Cronsun任务管理器 MongoDB非关系性数据库 Etcd3键值存储仓库

### 程序架构

Cronsun的重要组件：Cronsun 主要有 3 个主件，都是通过 etcd 通讯的。cronnode 负责节点的分组及节点的状态，croeweb 是管理任务的、执行结果都可以在上面看

### 部署位置

| **服务器别名** | **用户名** | **部署目录**                                                                                              |
| --------- | ------- | ----------------------------------------------------------------------------------------------------- |
| VMware测试机 | root    | <p>/usr/local/cronsun-v0.3.5/</p><p>/usr/lib/systemd/system/etcd.service</p><p>/usr/local/mongodb</p> |

### 项目开源地址

{% embed url="<https://github.com/shunfei/cronsun>" %}

## 部署操作

### 环境准备

```
sed -i 's/ONBOOT=on/ONBOOT=yes/g' /etc/sysconfig/network-scripts/ifcfg-ens33 #配置网络
service network restart #重启网络
yum -y install vim unzip net-tools wget zip bash-completion java*
```

### 单节点安装etcd服务

下载安装包，下载地址：<https://github.com/etcd-io/etcd/releases>

```
wget https://github.com/etcd-io/etcd/releases/download/v3.3.13/etcd-v3.3.13-linux-amd64.tar.gz
```

解压文件

```
tar -zxvf etcd-v3.3.13-linux-amd64.tar.gz
cd etcd-v3.3.13-linux-amd64
```

目录下有两个可执行文件etcd 和 etcdctl

```
cp etcd /usr/bin/
cp etcdctl /usr/bin/
```

配置服务

```
vim /usr/lib/systemd/system/etcd.service (添加以下内容)
[Unit]
Description=Etcd Server
After=network.target
[Service]
Type=simple
WorkingDirectory=/var/lib/etcd/
EnvironmentFile=-/etc/etcd/etcd.conf
ExecStart=/usr/bin/etcd
[Install]
WantedBy=multi-user.target
```

创建工作文件夹

```
mkdir -p /var/lib/etcd && mkdir -p /etc/etcd
vim /etc/etcd/etcd.conf (添加以下内容)
#[member]
ETCD_NAME=default
ETCD_DATA_DIR="/var/lib/etcd/default.etcd"
ETCD_LISTEN_CLIENT_URLS="http://0.0.0.0:2379"
ETCD_ADVERTISE_CLIENT_URLS=
http://0.0.0.0:2379
```

{% hint style="danger" %}
注意：默认监听地址是127.0.0.1，允许远程连接需要改为0.0.0.0
{% endhint %}

启动etcd服务

```
systemctl daemon-reload && systemctl enable etcd.service && systemctl start etcd.service
```

测试etcd服务状态：

```
etcdctl cluster-health
```

![](/files/-MRz2iVHCFFf64FfkOWJ)

```
cd /root
```

### 单节点mongodb的安装

单节点mongodb的安装

```
wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-4.0.11.tgz
mkdir /usr/local/mongodb
tar -xf mongodb-linux-x86_64-4.0.11.tgz
cp -r mongodb-linux-x86_64-4.0.11/bin /usr/local/mongodb
cd /usr/local/mongodb
```

![](/files/-MRz2iVI4kkwHOHip7hk)

```
mkdir -p etc log data/db   #配置文件目录 日志目录 存放数据
```

配置文件：

```
vim /usr/local/mongodb/etc/mongodb.conf
logpath=/usr/local/mongodb/log/mongodb.log   #指定日志文件名
dbpath=/usr/local/mongodb/data/db            #指定数据库目录
logappend=true                               #日志为 追加方式记录
fork=true                                    #服务以守护进程的方式进行
bind_ip=0.0.0.0        #修改运行的ip，默认是127.0.0.1， 多节点需要允许其他的ip连接， 所以配置为0.0.0.0，允许其他ip连接
port=27017             #修改运行的端口号
/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/etc/mongodb.conf #指向配置文件执行bin目录下的快捷方式
```

![](/files/-MRz2iVJWGX96_Jbsvwh)

```
netstat -ntuap | grep mongod #查询服务是否启动
```

![](/files/-MRz2iVKULt2Uh6oVDx6)

MongoDB 的可执行文件位于 bin 目录下，所以可以将其添加到 PATH 路径中：

```
export PATH=<mongodb-install-directory>/bin:$PATH
```

![](/files/-MRz2iVLE_bpnK3X9iWM)

{% hint style="success" %}
**\<mongodb-install-directory>** 为MongoDB 的安装路径，如本文档的 /usr/local/mongodb
{% endhint %}

### 部署 cronsun 任务管理器

Cronsun下载地址： <https://github.com/shunfei/cronsun/releases>

```
wget https://github.com/shunfei/cronsun/releases/download/v0.3.5/cronsun-v0.3.5-linux-amd64.zip
unzip cronsun-v0.3.5 && mv cronsun-v0.3.5 /usr/local/
```

解压后修改 conf 目录下的配置文件：db.json 和 etcd.json，分别修改 MongoDB 和 etcd 的实际地址。

```
vim /usr/local/cronsun-v0.3.5/conf/etcd.json
{
 "Endpoints":[
 "http://10.88.216.18:2379"    #这里修改的是etcd服务的地址和端口
 ],
 "Username":"",
 "Password":"",
 "#DialTimeout":"单位秒",
 "DialTimeout": 2
}
```

```
vim /usr/local/cronsun-v0.3.5/conf/db.json
{
 "Hosts": [
 "10.88.216.18:27017"          #这里修改的是MongoDB服务的地址和端口
 ],
 "Database": "cronsun",
 "#AuthSource": "AuthSource Specify the database name associated with the user’s credentials.",
 "#AuthSource": "AuthSource defaults to the cronsun's Database.",
 "#AuthSource": "If connect mongodb like './bin/mongo mytest -u test -p 123 --authenticationDatabase admin' ",
 "#AuthSource": "the AuthSource is 'admin'. ",
 "AuthSource": "",
 "UserName": "",
 "Password": "",
 "#Timeout": "connect timeout duration/second",
 "Timeout": 15
}
```

### 添加节点

cronsun基于etcd实现了自动发现和注册的功能，所以添加节点非常简单，直接将cronnode和conf拷贝到客户端服务器启动之后，就能在前台->节点页面看到该服务器了，当然节点和Etcd以及MongoDB之间的网络必须畅通。

```
scp -r /usr/local/cronsun-v0.3.5 10.88.216.21:/usr/local/ #拷贝cronnode配置文件到节点主机
```

![](/files/-MRz2iVMiQazN_ibkS4F)

## 程序启动

### 启动web

启动web：./cronweb -conf conf/base.json

(若要后台运行则使用 nohup)

```
nohup /usr/local/cronsun-v0.3.5/cronweb -conf /usr/local/cronsun-v0.3.5/conf/base.json &
```

### 启动node

启动 node：./cronnode -conf conf/base.json

(若要后台运行则使用 nohup)

```
nohup /usr/local/cronsun-v0.3.5/cronnode -conf /usr/local/cronsun-v0.3.5/conf/base.json &
```

### 访问web页面

访问之前关闭防火墙

systemctl stop firewalld.service

访问web：<http://x.x.x.x:7079/ui/> 默认用户名： <admin@admin.com> 密码：admin

![](/files/-MRz2iVNRdU8W-I5ViLt)

### 节点管理

![](/files/-MRz2iVOCSVl7Y0qB8Gn)

添加了所需的节点服务器之后，我们可以将节点进行分组，从而方便定时任务的添加：

### 添加任务

节点和分组都搞定之后，就可以开始添加定时任务了。

![](/files/-MRz2iVPS8nDX3OaLO6C)

### 告警配置

首先要清楚 cronsun 的告警是由 cronweb 发出的，而不是 cronnode（但是 cronnode 的 mail.json 也必须 Enable：true，否则还是无法发出告警）。

编辑 cronweb 和 cronnode 的配置文件：mail.json，如下内容

```
vim /usr/local/cronsun-v0.3.5/conf/mail.json
{
 "Enable": true, # cronweb 的 mail.json 配置中必须将 Enable 填为 true 才可以看到上图的告警开关按钮，否则不显示。
 "To": ["这里填写缺省默认的告警邮件接收地址"],
 "#HttpAPI": "如有此字段，则按 http api 方式发送",
 "#Keepalive": "如果此时间段内没有邮件发送，则关闭 SMTP 连接，单位/秒",
 "Keepalive": 30,
 "#doc": "https://godoc.org/github.com/go-gomail/gomail#Dialer",
 "Host": "填写SMTP服务器地址，比如：stmp.qq.com",
 "Port": 25,
 "Username": "这里填写发送人邮箱地址（用于登陆SMTP+from地址）",
 "Password": "登陆密码",
 "SSL": false,
 "LocalName": ""
}
```

![](/files/-MRz2iVQr7X_AfKiRKgb)

{% hint style="info" %}
Ps：LocalName 建议留空，HttpAPI 模式未使用到，这里省略之。
{% endhint %}

重新启动 cronweb，然后在 web 上的单向任务界面开启告警，即可实现邮件告警，如下图所示：

![](/files/-MRz2iVR1tjdRwP7aN_G)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://hezhiqiang.gitbook.io/about-the-author/yun-wei-huan-jing-da-jian/cronsun-ren-wu-guan-li-qi-bu-shu.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
