一 介绍
在构建数据库自动化运维系统的时候,数据库服务器上必须要有一个agent来执行web服务器端发起的命令,我们研究了好几种技术Celery,Redis Queue 或者基于socket实现,当然还有自己写,因为之前有同事已经完成了一个agent---servant,在和同事沟通之后,我们决定复用servant,不用重复造轮子。servant是一款基于go语言编写的,通过http协议调用,提供权限认证和远程调用,支持异步执行命令的agent ,满足我们目前数据库备份任务,定时收集数据库元数据信息,定时校验备份的有效性的任务需求。本文是一篇how to 文档,相对比较详细的介绍如何安装和使用servant,希望对读者朋友有所帮助。
二安装
2.1 软件准备
因为该agent是基于go语言编写的,所以要安装 go语言包
- yum install -y go
- cd /opt/
- git clone https://github.com/xiezhenye/servant.git
- cd /opt/servant
- 方式一 make rpm
- 方式二 make
2.2 目录结构
编译之后查看主要的目录结构
bin # 编译的二进制文件
conf # 配置文件目录
example #配置文件的样例文件夹
README.md
scripts #servantctl 执行文件 用于启停 查看状态等
src #源代码文件
维护servant的操作命令
/opt/servant/scripts/servantctl (start|stop|restart|status|help)
2.3 配置文件详解
默认在/opt/servant/conf里面有配置文件 servant.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <config>
- <server>
- <listen>:2465</listen> #监听的端口
- <auth enabled="true"> #调用的时候是否启用 权限 验证,生产环境建议开启
- <maxTimeDelta>30</maxTimeDelta> # 启动权限验证的时候 超时时间,超过30s 则认为该调用无效
- </auth>
- <log>/data/logs/servant/servant.log</log> # 日志目录log ,这是有赞标准的日志目录,其他朋友在自己环境需要适当调整
- </server>
- <!-- ...... -->
- </config>
example 的配置文件,使用的时候需要根据实际情况进行调整
- <?xml version="1.0" encoding="utf-8" ?>
- <config>
- <server> ##server和/opt/servant/conf/servant.xml 配置是一样的。
- <listen>:2465</listen>
- <auth enabled="0">
- <maxTimeDelta>300</maxTimeDelta>
- </auth>
- <log>servant.log</log>
- </server>
#commands 定义了一个可执行的命令组,其中包含了多个command,其中lang可以是exec或者bash ,id 是每一组command的标示,runas标示以什么样的用户执行。background="true" 标示以后台方式执行,并且servant 立即返回。线上环境使用该功能最多,服务端远程调用,然后脚本执行某些业务逻辑并返回或者直接通过api和server做交互。
<commands id="db1">
- <command id="foo" runas="mysql" lang="bash">
- <code>echo "hello world $(whoami)"</code>
- </command>
- <command id="grep" lang="exec">
- <code>grep hello</code>
- </command>
- <command id="sleep" timeout="5" lang="exec">
- <code> sleep ${t}</code>
</command>
</commands>
# daemon 指定的命令后台运行,比较少用
<daemon id="daemon1" retries="10" lang="bash">
<code>sleep 10000</code>
</daemon>
# 定时器 ,定期执行某一个命令 tick执行命令的间隔。deadline 命令执行的最长时间,如果为5s 则命令最长执行5s ,超过5s会被kill掉。
<timer id="xx" tick="5" deadline="5" lang="bash">
- <code>
- <![CDATA[ date >>/tmp/timer.log ]]>
</code>
</timer>
#文件操作类,和commands类似,可以配置多个操作文件的命令,主要包含 获取文件内容,创建文件,删除文件,读取指定字节范围,root 表示有权限访问指定的目录,例子中是访问 /tmp/ 目录下的文件。
<files id="db1">
<dir id="binlog1">
- <root>/tmp/</root>
- <allow>get</allow>
- <allow>head</allow>
- <allow>post</allow>
- <allow>delete</allow>
- <allow>put</allow>
- <pattern>log-bin\.\d+</pattern> #正则表达式
</dir>
</files>
#这个比较少用 访问数据库
<database id="mysql" driver="mysql" dsn="root:@tcp(127.0.0.1:3306)/test">
<query id="select_1">select 1;</query>
</database>
# 配合auth=true的时候一起使用,访问的时候 必须使用和配置文件中指定的user ,否则不能调用servant
<user id="user1">
<key>someKey</key>
<host>192.168.1.0/24</host> #指定允许访问servant 的ip源地址。通常建议使用本地调用,更加安全。
<files id="db1" /> # 指定权限认证的可操作的命令范围
<commands id="db1" />
</user>
以上针对常用的配置做了解释,更加详细的解释可以参考 servant的readme.md
2.4 具体的测试用例
为了测试方便,先去掉权限认证。
comand 支持get 和post 两种方式调用
- [root@rac4 22:38:05 /opt/servant/conf/extra]
- # curl http://127.0.0.1:2465/commands/db1/foo
- hello world mysql
- [root@rac4 22:40:07 /opt/servant/conf/extra]
- # echo "hello world" | curl -XPOST http://127.0.0.1:2465/commands/db1/grep -d @-
- hello world
- [root@rac4 22:40:08 /opt/servant/conf/extra]
- # echo "hxxello world" | curl -XPOST http://127.0.0.1:2465/commands/db1/grep -d @-
文件类型操作
获取文件内容
- [root@rac4 22:38:00 /opt/servant/conf/extra]
- # curl http://127.0.0.1:2465/files/db1/test/yz.log
- youzan ,nihao ,yangyi dba
创建文件
- [root@rac4 22:41:56 /opt/servant/conf/extra]
- # curl -XPOST http://127.0.0.1:2465/files/db1/test/54.txt -d "hello world "
验证上面的写入情况
- [root@rac4 22:42:03 /opt/servant/conf/extra]
- # curl http://127.0.0.1:2465/files/db1/test/54.txt
- hello world
更新文件内容
- [root@rac4 22:45:13 /opt/servant/conf/extra]
- # curl -XPUT http://127.0.0.1:2465/files/db1/test/54.txt -d "yangyi dba"
- [root@rac4 22:45:26 /opt/servant/conf/extra]
- # curl http://127.0.0.1:2465/files/db1/test/54.txt
- yangyi dba
开启权限验证,生产环境下从安全的角度考虑建议开启权限验证
修改配置文件 启用auth 为true 和设置user 配置
- [root@rac4 22:16:50 /opt/servant/conf]
- # uri='/commands/db1/foo'
- # ts=$(date +%s)
- # key=someKey
- # curl -H "Authorization: ${user} ${ts} $(echo -n "${user}${key}${ts}GET${uri}"|sha1sum|cut -f1 -d' ')" "http://127.0.0.1:2465${uri}"
- [root@rac4 22:30:30 /opt/servant/conf]
log报错 执行失败,因为ts 的实际时间是22:16:50,执行的实际时间是22:30:30 超时时间是30s,故调用失败
- 2017/05/05 22:30:29 INFO (6) [commands] + 127.0.0.1:42798 GET /commands/db1/foo
- 2017/05/05 22:30:30 WARN (6) [commands] - auth failed: timestamp delta too large
重新设置时间 ts 再次执行 成功。
- [root@rac4 22:30:58 /opt/servant/conf]
- # ts=$(date +%s)
- [root@rac4 22:31:02 /opt/servant/conf]
- # curl -H "Authorization: ${user} ${ts} $(echo -n "${user}${key}${ts}GET${uri}"|sha1sum|cut -f1 -d' ')" "http://127.0.0.1:2465${uri}"
- hello world mysql
- 日志输出
- 2017/05/05 22:31:05 INFO (7) [commands] + 127.0.0.1:42808 GET /commands/db1/foo
- 2017/05/05 22:31:05 INFO (7) [commands] command: [bash -c echo "hello world $(whoami)"]
- 2017/05/05 22:31:05 INFO (7) [commands] process started. pid: 27706
- 2017/05/05 22:31:05 INFO (7) [commands] - execution done
2.5 安装过程中遇到的问题
1 安装的时候 需要创建
mkdir -p /opt/servant/conf/extra
2 认证权限问题
因为默认的/opt/servant/conf/servant.xml 的auth =true ,需要改为false。不然使用curl 执行命令
curl http://127.0.0.1:2465/commands/db1/foo
日志里面报错
2017/05/05 21:52:30 INFO (3) [commands] + 127.0.0.1:41988 GET /commands/db1/foo
2017/05/05 21:52:31 WARN (3) [commands] - auth failed: bad auth header
三 总结
总体上而言 ,servant能够满足大部分的agent需求,欢迎大家使用,如果有任何问题可以在后台留言向我反馈。原文链接是该工具的github地址,欢迎使用,添加star。