在自动化运维时,经常需要用ssh登录一台远程主机,然后执行一条命令,获取结果后,再登录下一台。
每次ssh登录的开销都要0.5秒以上。
所以想到用gearman来替代ssh快速执行远程命令,每次远程执行的开销可以降到0.01秒。
当然是在安全的前提下,可以是集群的机器间互访,肯定不能对公网开放gearman服务端口。
下面是本地运行pwd,ssh远程运行pwd,gearman远程运行pwd的时间对比:
本地运行: 0毫秒
ssh运行: 129毫秒
gearman运行:10毫秒
ssh和gearman服务器都在本机,实际局域网多机间的ssh需要500毫秒,gearman还是10毫秒。
shen@debian:~$ time pwd
/home/shen
real 0m0.000s
user 0m0.000s
sys 0m0.000s
shen@debian:~$ time sshpass -p 123456 ssh debian 'pwd'
/home/shen
real 0m0.129s
user 0m0.012s
sys 0m0.000s
shen@debian:~$ time echo 'pwd' | gearman -h debian -f run
/home/shen/gearman-workers
real 0m0.010s
user 0m0.004s
sys 0m0.000s
shen@debian:~/gearman-workers$ cat run.sh
#!/bin/bash
read line
$line 2> _run_stderr
echo $? > _run_rc
shen@debian:~/gearman-workers$ cat stderr.sh
#!/bin/bash
cat _run_stderr
shen@debian:~/gearman-workers$ cat rc.sh
#!/bin/bash
cat _run_rc
启动gearman worker:
shen@debian:~/gearman-workers$ gearman -h debian -w -f run -- ./run.sh
shen@debian:~/gearman-workers$ gearman -h debian -w -f stderr -- ./stderr.sh
shen@debian:~/gearman-workers$ gearman -h debian -w -f rc -- ./rc.sh
gearman -h debian -w -f run -- ./run.sh
相当于开启了一个ssh server,可访问远程结点名称为debian。
shen@debian:~$ echo 'df -h' | gearman -h debian -f run
文件系统 容量 已用 可用 已用% 挂载点
rootfs 455G 6.0G 426G 2% /
udev 10M 0 10M 0% /dev
tmpfs 388M 660K 387M 1% /run
/dev/disk/by-uuid/f863a021-7966-4cad-a24a-2670bba22d10 455G 6.0G 426G 2% /
tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs 1.6G 19M 1.6G 2% /run/shm
/dev/sda1 487M 128K 486M 1% /boot/efi
shen@debian:~$ gearman -h debian -s -f stderr
shen@debian:~$ gearman -h debian -s -f rc
0
shen@debian:~$ echo 'ls ~/aabbcd' | gearman -h debian -f run
shen@debian:~$ gearman -h debian -s -f stderr
ls: 无法访问~/aabbcd: 没有那个文件或目录
shen@debian:~$ gearman -h debian -s -f rc
2
shen@debian:~$ echo 'ps -edf' | gearman -h debian -f run | grep gearmand
root 4807 1 0 07:32 ? 00:00:00 /usr/local/sbin/gearmand -d
shen 5504 4892 0 08:38 pts/3 00:00:00 grep gearmand
注意:
RedHat:
sudo gearmand -d
Debian:
sudo /usr/local/sbin/gearmand -d
不需要sudo执行
gearadmin --shutdown
远程也可以关闭
shen@debian:~/gearman-workers$ gearadmin -h debian --shutdown
shen@debian:~/gearman-workers$ gearadmin -h debian --workers
Error: Error connecting to debian.
gearman -h <gearman_server_ip> -w -i <pidfile> -f <worker> -- <command>
确保启动worker时,实用-i参数生成pid文件
shen@debian:~/gearman-workers$ gearman -h debian -i run.pid -w -f run -- ./run.sh
停止worker
shen@debian:~/gearman-workers$ kill -9 `cat run.pid`
3个worker: run, stderr, rc 都已启动情况
shen@debian:~$ gearadmin -h debian --workers
34 127.0.0.1 - : stderr
35 127.0.0.1 - : rc
36 127.0.0.1 - :
33 127.0.0.1 - : run
.
3个worker: run, stderr, rc 都未启动情况
shen@debian:~$ gearadmin -h debian --workers
33 127.0.0.1 - :
.
远程结点的gearman server未启动的情况
shen@debian:~$ gearadmin -h debian --workers
Error: Error connecting to debian.
这个时候需要ssh 帮忙了
远程启动gearman server
#准备工作:把远程结点的/usr/local/var/log/gearmand.log权限设为666
sudo chmod 777 /usr/local/var/log/
sudo chmod 666 /usr/local/var/log/gearmand.log
sshpass -p 123456 ssh debian '/usr/local/sbin/gearmand -d'
远程关闭gearman server
shen@debian:~/gearman-workers$ gearadmin -h debian --shutdown
远程启动gearman worker
shen@debian:~/gearman-workers$ cat start_worker_run.sh
#!/bin/bash
cd $(dirname $(readlink -f $0))
nohup gearman -h debian -i run.pid -w -f run -- ./run.sh &
#必须使用ssh的-f参数
shen@debian:~$ sshpass -p 123456 ssh -f debian '/home/shen/gearman-workers/start_worker_run.sh'
远程关闭gearman worker
shen@debian:~$ sshpass -p 123456 ssh -f debian 'kill -9 `cat /home/shen/gearman-workers/run.pid`'
Gearman是一个分发任务的程序框架,可以用在各种场合,与Hadoop相比,Gearman更偏向于任务分发功能。它的 任务分布非常 简单,简单得可以只需要用脚本即可完成。
Redhat/CentOS上二进制安装gearman
yum install gearmand
Debian/Ubuntu上编译安装gearman: http://my.oschina.net/fitnessefan/blog/349664
官网: http://gearman.org/
使用入门: http://gearman.org/getting-started/