通过Rex + module的形式来分发配置文件到不同服务节点 以及集中控制各节点的web服务...

1,所有业务的测试机不同的node跑不同的业务(通过hosts指定域名 因为只是简单的测试没有专门用dns)

[root@localhost rex]# cat /etc/hosts
127.0.0.1  mail.weike.com mx localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.0.128    node1    caisen
192.168.0.129    node2    mail
192.168.0.45     node3    guanwang
192.168.0.164    node4    shopwap
192.168.0.193    node5    gerenwap
192.168.0.196    node6    appwap
192.168.0.224    node7    geren
192.168.0.226    node8    chaoshi
192.168.0.228    node9    quanguo
192.168.0.215    node10    boss


2,分别将不同的web服务以模块的形式组织起来进行集中管理,,Rexfile配置信息如下:

use Rex -feature => ['1.3'];
use strict;
use warnings;
#use Data::Dumper;
#针对不同的服务类型通过模块化配置
require Servers::Quanguo;
require Servers::Chaoshi;
require Servers::Geren;
require Servers::Boss;
require Servers::Shopwap;
require Servers::Gerenwap;
require Servers::Appwap;
require Servers::Guanwang;
#远程连接登陆方式
private_key "/root/.ssh/id_rsa";
public_key "/root/.ssh/id_rsa.pub";
#设置线程数
parallelism 'max';
#所有web服务器节点
group all_servers       =>"node[1..2,4..10]";

desc "关闭所有节点上的tomcat和weblogic服务";
task "stop_web",group=>"all_servers",sub{
        #my @ps=ps();
        #print Dumper(@ps);
        #my @list=grep{$_->{"command"}==-5}ps("command","ni");
        my $server = Rex::get_current_connection()->{server};
        for my $proc(ps()){
                if($proc->{"command"}=~/java.*tomcat/is){
                        run "./shutdown.sh",
                                cwd     =>"/usr/local/tomcat/bin",
                                only_if =>"pgrep java";
                        say "[$server]: tomcat服务关闭完成";
                        last;
                }elsif($proc->{"command"}=~/java.*?node1.*?weblogic/is){
                        run "stop_node.sh",
                                only_if=>"pgrep java";
                        say "[$server]: weblogic服务关闭完成";
                        last;
                }else{
                        next;
                }
        }
        say "[$server]: 服务已经关闭";
};

desc "启动所有节点上的tomcat和weblogic服务";
task "start_web",group=>"all_servers",sub{
        my $server = Rex::get_current_connection()->{server};
        if(is_file("/usr/local/tomcat/bin/startup.sh")){
                run "./startup.sh",sub {
                        my($stdout,$stderr)=@_;
                        say "[$server]: $stdout";
                        },
                        cwd     =>"/usr/local/tomcat/bin",
                        unless  =>"pgrep  java";
        }else{
                run "start_node.sh",
                        unless  =>"pgrep java";
        }
        for my $proc(ps()){
                if($proc->{"command"}=~/java.*tomcat/i){
                        say "[$server]: tomcat已经启动,进程pid:".$proc->{"pid"};
                        last;
                }elsif($proc->{"command"}=~/java.*weblogic/isg){
                        say "[$server]: weblogic服务已经启动,进程pid:".$proc->{"pid"};
                        last;
                }else{
                        next;
                }
        }
};

desc "重启所有节点上的tomcat和weblogic服务";
task "restart_web",group=>"all_servers",sub {
        my $server = Rex::get_current_connection()->{server};
        needs 'stop_web';
        sleep 3;
        needs 'start_web';
        say "[$server]: 服务重启完成";
};

desc "分发数据源文件到不同的web服务节点上";
task "deploy_file",group=>"all_servers",sub {
        my $server = Rex::get_current_connection()->{server};
        Servers::Quanguo->deploy_quanguo if $server eq 'node9';
        Servers::Chaoshi->deploy_chaoshi if $server eq 'node8';
        Servers::Geren->deploy_geren   if $server eq 'node7';
        Servers::Boss->deploy_boss    if $server eq 'node10';
        Servers::Shopwap->deploy_shopwap if $server eq 'node4';
        Servers::Gerenwap->deploy_gerenwap if $server eq 'node5';
        Servers::Appwap->deploy_appwap  if $server eq 'node6';
        Servers::Guanwang->deploy_guanwang if $server eq 'node3';
};

desc "查看所有节点的当前时间";
task "current_time",group=>"all_servers",sub{
        say run "date";
};

3,各个模块的配置内容都一样除了分发的文件名。模块内容如下

package Servers::Appwap;
use Rex -feature => ['1.3'];

desc "分发数据源配置文件到个人中心wap节点服务器";
task "deploy_appwap","appwap",sub{
        my $server = Rex::get_current_connection()->{server};
        if(!is_dir("/usr/local/dbsrc")){
                file "/usr/local/dbsrc",
                        ensure  =>"directory",
                        owner   =>"root",
                        group   =>"root",
                        mode    =>755;
                upload "/rex/conf/jdbc_moser.properties","/usr/local/dbsrc";
        };
        file "/usr/local/dbsrc/jdbc_moser.properties",
                source=>"/rex/conf/jdbc_moser.properties",
                owner=>"root",
                group=>"root",
                mode=>644,
                on_change=> sub {
                        say "[$server]: 数据源配置更新成功";
                };
};

1;

4,通过rex -T可以查看自己定义的所有任务:

[root@localhost rex]# rex -T
Tasks
 deploy_file			   分发数据源文件到不同的web服务节点上
 restart_web			   重启所有节点上的tomcat和weblogic服务
 start_web			   启动所有节点上的tomcat和weblogic服务
 stop_web			   关闭所有节点上的tomcat和weblogic服务

 Servers:Appwap:deploy_appwap	   分发数据源配置文件到个人中心wap节点服务器
 Servers:Boss:deploy_boss	   分发数据源配置文件到Boss节点服务器
 Servers:Chaoshi:deploy_chaoshi    分发数据源配置文件到超市节点服务器
 Servers:Geren:deploy_geren	   分发数据源配置文件到个人中心节点服务器
 Servers:Gerenwap:deploy_gerenwap  分发数据源配置文件到个人中心wap节点服务器
 Servers:Guanwang:deploy_guanwang  分发数据源配置文件到官网节点服务器
 Servers:Quanguo:deploy_quanguo    分发数据源配置文件到全国节点服务器
 Servers:Shopwap:deploy_shopwap    分发数据源配置文件到商城wap节点服务器
Server Groups
 all_servers  node[1..2,4..10]

5,运行某个人物只需要用rex + task就可以比如:我要启动所有节点上的tomcat和weblogic服务 如下所示:

[root@localhost rex]# rex start_web
[2015-09-06 06:07:51] INFO - Running task start_web on node1
[2015-09-06 06:07:51] INFO - Connecting to node1:22 (root)
[2015-09-06 06:07:51] INFO - Connected and authenticated to node1.
[2015-09-06 06:07:51] INFO - Successfully authenticated on node1.
[node1]: tomcat已经启动,进程pid:5147
[2015-09-06 06:07:52] INFO - Running task start_web on node2
[2015-09-06 06:07:52] INFO - Connecting to node2:22 (root)
[2015-09-06 06:07:52] INFO - Connected and authenticated to node2.
[2015-09-06 06:07:52] INFO - Successfully authenticated on node2.
[node2]: tomcat已经启动,进程pid:5220
[2015-09-06 06:07:52] INFO - Running task start_web on node4
[2015-09-06 06:07:52] INFO - Connecting to node4:22 (root)
[2015-09-06 06:07:53] INFO - Connected and authenticated to node4.
[2015-09-06 06:07:53] INFO - Successfully authenticated on node4.
[node4]: tomcat已经启动,进程pid:13178
[2015-09-06 06:07:53] INFO - Running task start_web on node5
[2015-09-06 06:07:53] INFO - Connecting to node5:22 (root)
[2015-09-06 06:07:54] INFO - Connected and authenticated to node5.
[2015-09-06 06:07:54] INFO - Successfully authenticated on node5.
[node5]: tomcat已经启动,进程pid:23775
[2015-09-06 06:07:54] INFO - Running task start_web on node6
[2015-09-06 06:07:54] INFO - Connecting to node6:22 (root)
[2015-09-06 06:07:55] INFO - Connected and authenticated to node6.
[2015-09-06 06:07:55] INFO - Successfully authenticated on node6.
[node6]: tomcat已经启动,进程pid:19614
[2015-09-06 06:07:55] INFO - Running task start_web on node7
[2015-09-06 06:07:55] INFO - Connecting to node7:22 (root)
[2015-09-06 06:07:56] INFO - Connected and authenticated to node7.
[2015-09-06 06:07:56] INFO - Successfully authenticated on node7.
[node7]: weblogic服务已经启动,进程pid:26615
[2015-09-06 06:07:56] INFO - Running task start_web on node8
[2015-09-06 06:07:56] INFO - Connecting to node8:22 (root)
[2015-09-06 06:07:56] INFO - Connected and authenticated to node8.
[2015-09-06 06:07:57] INFO - Successfully authenticated on node8.
[node8]: weblogic服务已经启动,进程pid:24851
[2015-09-06 06:07:57] INFO - Running task start_web on node9
[2015-09-06 06:07:57] INFO - Connecting to node9:22 (root)
[2015-09-06 06:07:57] INFO - Connected and authenticated to node9.
[2015-09-06 06:07:57] INFO - Successfully authenticated on node9.
[node9]: weblogic服务已经启动,进程pid:670
[2015-09-06 06:07:58] INFO - Running task start_web on node10
[2015-09-06 06:07:58] INFO - Connecting to node10:22 (root)
[2015-09-06 06:07:58] INFO - Connected and authenticated to node10.
[2015-09-06 06:07:58] INFO - Successfully authenticated on node10.
[node10]: weblogic服务已经启动,进程pid:9809

6,分发不同文件到不同的web服务节点上 可以通过同步 上传等方式都可以 如下所示:

[root@localhost rex]# rex deploy_file
[2015-09-06 06:13:36] INFO - Running task deploy_file on node1
[2015-09-06 06:13:36] INFO - Connecting to node1:22 (root)
[2015-09-06 06:13:36] INFO - Connected and authenticated to node1.
[2015-09-06 06:13:36] INFO - Successfully authenticated on node1.
[2015-09-06 06:13:37] INFO - Running task deploy_file on node2
[2015-09-06 06:13:37] INFO - Connecting to node2:22 (root)
[2015-09-06 06:13:37] INFO - Connected and authenticated to node2.
[2015-09-06 06:13:37] INFO - Successfully authenticated on node2.
[2015-09-06 06:13:37] INFO - Running task deploy_file on node4
[2015-09-06 06:13:37] INFO - Connecting to node4:22 (root)
[2015-09-06 06:13:37] INFO - Connected and authenticated to node4.
[2015-09-06 06:13:37] INFO - Successfully authenticated on node4.
[2015-09-06 06:13:38] INFO - Running task deploy_shopwap on current connection
[node4]: 数据源配置更新成功
[2015-09-06 06:13:38] INFO - Running task deploy_file on node5
[2015-09-06 06:13:38] INFO - Connecting to node5:22 (root)
[2015-09-06 06:13:38] INFO - Connected and authenticated to node5.
[2015-09-06 06:13:38] INFO - Successfully authenticated on node5.
[2015-09-06 06:13:39] INFO - Running task deploy_gerenwap on current connection
[node5]: 数据源配置更新成功
[2015-09-06 06:13:39] INFO - Running task deploy_file on node6
[2015-09-06 06:13:39] INFO - Connecting to node6:22 (root)
[2015-09-06 06:13:40] INFO - Connected and authenticated to node6.
[2015-09-06 06:13:40] INFO - Successfully authenticated on node6.
[2015-09-06 06:13:40] INFO - Running task deploy_appwap on current connection
[node6]: 数据源配置更新成功
[2015-09-06 06:13:40] INFO - Running task deploy_file on node7
[2015-09-06 06:13:40] INFO - Connecting to node7:22 (root)
[2015-09-06 06:13:41] INFO - Connected and authenticated to node7.
[2015-09-06 06:13:41] INFO - Successfully authenticated on node7.
[2015-09-06 06:13:41] INFO - Running task deploy_geren on current connection
[node7]: 数据源配置更新成功
[2015-09-06 06:13:42] INFO - Running task deploy_file on node8
[2015-09-06 06:13:42] INFO - Connecting to node8:22 (root)
[2015-09-06 06:13:42] INFO - Connected and authenticated to node8.
[2015-09-06 06:13:42] INFO - Successfully authenticated on node8.
[2015-09-06 06:13:42] INFO - Running task deploy_chaoshi on current connection
[node8]: 数据源配置更新成功
[2015-09-06 06:13:43] INFO - Running task deploy_file on node9
[2015-09-06 06:13:43] INFO - Connecting to node9:22 (root)
[2015-09-06 06:13:43] INFO - Connected and authenticated to node9.
[2015-09-06 06:13:43] INFO - Successfully authenticated on node9.
[2015-09-06 06:13:43] INFO - Running task deploy_quanguo on current connection
[node9]: 数据源配置更新成功
[2015-09-06 06:13:44] INFO - Running task deploy_file on node10
[2015-09-06 06:13:44] INFO - Connecting to node10:22 (root)
[2015-09-06 06:13:44] INFO - Connected and authenticated to node10.
[2015-09-06 06:13:44] INFO - Successfully authenticated on node10.
[2015-09-06 06:13:44] INFO - Running task deploy_boss on current connection
[node10]:数据源配置更新成功

7,如果你部署Rex的系统性能比较高的话,管理的节点很多,想要通过多线程的方式并发连接操作各个节点可以设置parallelism参数 比如我设置为最大值  parallelism 'max'; 最大值是根据你要并发连接的节点数,默认值是单个也就是始终只有一个连接。比如在我的测试里要连接10个节点服务器 ,在不设置该参数的情况下如下所示:

[root@localhost rex]# rex current_time
[2015-09-06 06:22:30] INFO - Running task current_time on node1
[2015-09-06 06:22:30] INFO - Connecting to node1:22 (root)
[2015-09-06 06:22:30] INFO - Connected and authenticated to node1.
[2015-09-06 06:22:30] INFO - Successfully authenticated on node1.
Sun Sep  6 18:22:20 CST 2015
[2015-09-06 06:22:31] INFO - Running task current_time on node2
[2015-09-06 06:22:31] INFO - Connecting to node2:22 (root)
[2015-09-06 06:22:31] INFO - Connected and authenticated to node2.
[2015-09-06 06:22:31] INFO - Successfully authenticated on node2.
Sun Sep  6 18:22:30 CST 2015
[2015-09-06 06:22:31] INFO - Running task current_time on node4
[2015-09-06 06:22:31] INFO - Connecting to node4:22 (root)
[2015-09-06 06:22:31] INFO - Connected and authenticated to node4.
[2015-09-06 06:22:31] INFO - Successfully authenticated on node4.
Sun Sep  6 18:23:14 CST 2015
[2015-09-06 06:22:32] INFO - Running task current_time on node5
[2015-09-06 06:22:32] INFO - Connecting to node5:22 (root)
[2015-09-06 06:22:32] INFO - Connected and authenticated to node5.
[2015-09-06 06:22:32] INFO - Successfully authenticated on node5.
Sun Sep  6 18:23:14 CST 2015
[2015-09-06 06:22:33] INFO - Running task current_time on node6
[2015-09-06 06:22:33] INFO - Connecting to node6:22 (root)
[2015-09-06 06:22:33] INFO - Connected and authenticated to node6.
[2015-09-06 06:22:33] INFO - Successfully authenticated on node6.
Sun Sep  6 18:23:09 CST 2015
[2015-09-06 06:22:34] INFO - Running task current_time on node7
[2015-09-06 06:22:34] INFO - Connecting to node7:22 (root)
[2015-09-06 06:22:34] INFO - Connected and authenticated to node7.
[2015-09-06 06:22:34] INFO - Successfully authenticated on node7.
Sun Sep  6 18:23:46 CST 2015
[2015-09-06 06:22:34] INFO - Running task current_time on node8
[2015-09-06 06:22:34] INFO - Connecting to node8:22 (root)
[2015-09-06 06:22:35] INFO - Connected and authenticated to node8.
[2015-09-06 06:22:35] INFO - Successfully authenticated on node8.
Sun Sep  6 18:25:22 CST 2015
[2015-09-06 06:22:35] INFO - Running task current_time on node9
[2015-09-06 06:22:35] INFO - Connecting to node9:22 (root)
[2015-09-06 06:22:35] INFO - Connected and authenticated to node9.
[2015-09-06 06:22:35] INFO - Successfully authenticated on node9.
Sun Sep  6 18:23:48 CST 2015
[2015-09-06 06:22:36] INFO - Running task current_time on node10
[2015-09-06 06:22:36] INFO - Connecting to node10:22 (root)
[2015-09-06 06:22:36] INFO - Connected and authenticated to node10.
[2015-09-06 06:22:36] INFO - Successfully authenticated on node10.
Sun Sep  6 18:25:23 CST 2015

这样的情况下就会一个一个的进行操作 如果管理的节点多的话 非常浪费时间。在设置最大值的情况下,因为我的服务器是10台(该参数也可以指定数字)我直接设置成max 运行后如下所示:

[root@localhost rex]# rex current_time
[2015-09-06 06:25:25] INFO - Running task current_time on node2
[2015-09-06 06:25:25] INFO - Connecting to node2:22 (root)
[2015-09-06 06:25:25] INFO - Running task current_time on node1
[2015-09-06 06:25:25] INFO - Connecting to node1:22 (root)
[2015-09-06 06:25:25] INFO - Running task current_time on node4
[2015-09-06 06:25:25] INFO - Connecting to node4:22 (root)
[2015-09-06 06:25:25] INFO - Running task current_time on node5
[2015-09-06 06:25:25] INFO - Connecting to node5:22 (root)
[2015-09-06 06:25:25] INFO - Running task current_time on node7
[2015-09-06 06:25:25] INFO - Connecting to node7:22 (root)
[2015-09-06 06:25:25] INFO - Running task current_time on node8
[2015-09-06 06:25:25] INFO - Connecting to node8:22 (root)
[2015-09-06 06:25:25] INFO - Running task current_time on node6
[2015-09-06 06:25:25] INFO - Connecting to node6:22 (root)
[2015-09-06 06:25:25] INFO - Running task current_time on node9
[2015-09-06 06:25:25] INFO - Connecting to node9:22 (root)
[2015-09-06 06:25:25] INFO - Running task current_time on node10
[2015-09-06 06:25:25] INFO - Connecting to node10:22 (root)
[2015-09-06 06:25:25] INFO - Connected and authenticated to node2.
[2015-09-06 06:25:25] INFO - Connected and authenticated to node1.
[2015-09-06 06:25:25] INFO - Successfully authenticated on node2.
[2015-09-06 06:25:25] INFO - Successfully authenticated on node1.
[2015-09-06 06:25:25] INFO - Connected and authenticated to node4.
[2015-09-06 06:25:25] INFO - Connected and authenticated to node7.
[2015-09-06 06:25:25] INFO - Connected and authenticated to node8.
[2015-09-06 06:25:25] INFO - Connected and authenticated to node10.
[2015-09-06 06:25:25] INFO - Connected and authenticated to node9.
[2015-09-06 06:25:25] INFO - Connected and authenticated to node5.
[2015-09-06 06:25:25] INFO - Connected and authenticated to node6.
[2015-09-06 06:25:25] INFO - Successfully authenticated on node4.
[2015-09-06 06:25:25] INFO - Successfully authenticated on node10.
[2015-09-06 06:25:25] INFO - Successfully authenticated on node8.
[2015-09-06 06:25:25] INFO - Successfully authenticated on node6.
[2015-09-06 06:25:25] INFO - Successfully authenticated on node5.
[2015-09-06 06:25:25] INFO - Successfully authenticated on node7.
[2015-09-06 06:25:26] INFO - Successfully authenticated on node9.
Sun Sep  6 18:25:24 CST 2015
Sun Sep  6 18:25:15 CST 2015
Sun Sep  6 18:28:12 CST 2015
Sun Sep  6 18:28:12 CST 2015
Sun Sep  6 18:26:08 CST 2015
Sun Sep  6 18:26:08 CST 2015
Sun Sep  6 18:26:01 CST 2015
Sun Sep  6 18:26:38 CST 2015
Sun Sep  6 18:26:38 CST 2015

速度非常快 ,当然实际生产中,可以根据系统的负载能力 网络状况 管理服务节点数量 等来设置适合的值


至于连接超时时间 线程数 认证登陆方式 服务器信息配置,文件操作,系统服务管理,文件管理等等都可以在Rexfile中定义也可以以模块的形式来进行定义。rex还提供模板功能,具体还在研究。后续会继续更新。。。

建议学习rex的时候要认真的看perldoc文档可能有些函数在新版本中已经被干掉了,rex的管理很简单就是通过ssh连接然后通过一堆命令去管理对端的服务器。各种命令以及模块可以通过perldoc Rex::Commands查看,基本上介绍的也很细,然后通过实验多实践很快就能掌握!


如果任何一个连接操作失败的话 都会打印出相关的信息,根据信息可以排查错误的原因!

转载于:https://my.oschina.net/u/2420214/blog/501520

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值