-
Notifications
You must be signed in to change notification settings - Fork 0
/
install_hadoop.pl
1288 lines (1172 loc) · 53.8 KB
/
install_hadoop.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#! /usr/bin/perl
#安装依赖
#yum -y install mysql mysql-server mysql-devel
#cpan install DBI
#cpan install DBD::mysql
#cpan install Expect;
#yum install perl-DBD-mysql
use 5.010;
use Time::HiRes qw(gettimeofday);
use Expect;
#use UUID::Random;
use File::Basename;
use ConfigUtil;
use DBUtil;
use LogUtil;
#http://www.th7.cn/system/lin/201402/50472.shtml
my $log = new LogUtil(FH,'> install.log');#获取日志句柄
my $config = new ConfigUtil(DB,'< db.properties',$log);#读取配置文件
my $db = new DBUtil($config,$log);#获取数据库连接句柄
#my $perl_install_dir = $config->get('perl_install_dir');#perl安装bin目录
#my $hadoop_install_dir = $config->get('hadoop_install_dir');#hadoop默认安装目录
#my $global_hadoop_user = $config->get('global_hadoop_user');#hadoop用户
#my $os_hadoop_pass = $config->get('os_hadoop_pass');#hadoop用户密码
#my $global_hive_port = $config->get('global_hive_port');#hive端口号
#my $install_template_dir = $config->get('install_template_dir');#模版文件所在目录
#my $os_root_pass = $config->get('os_root_pass');#系统root用户密码
#my $app_install_dir = $config->get('app_install_dir');#安装程序所在目录
#my $remote_upload_dir = $config->get('remote_upload_dir');#远程上传临时目录
#my $java_install_dir = $config->get('java_install_dir');#java安装目录
#my $global_hostname_prefix = $config->get('global_hostname_prefix');#节点hostname的前缀
my $perl_install_dir = '/usr/local/perl';#perl安装bin目录
my $hadoop_install_dir = '/usr/local/bigdata';#hadoop默认安装目录
my $global_hadoop_user = 'hadoop';#hadoop用户
my $os_hadoop_pass = 'ikang';#hadoop用户密码
my $global_hive_port = '55550';#hive端口号
my $os_root_pass = 'ikang';#系统root用户密码
my $app_install_dir = '/root/software';#安装程序所在目录
my $install_template_dir = "$app_install_dir/conf";#模版文件所在目录
my $remote_upload_dir = '/tmp';#远程上传临时目录
my $java_install_dir = '/usr/local/jvm';#java安装目录
my $global_hostname_prefix = 'bigdata-';#节点hostname的前缀
my $hadoop_dir_name = 'hadoop-2.6.0';#hadoop文件目录名
my $hadoop_install_name = 'bigdata_install_hadoop.tar.gz';#使用的Hadoop文件名
my $jdk_install_name = 'bigdata_install_jdk.tar.gz';#使用的JDK文件名
my $zk_install_name = 'bigdata_install_zookeeper.tar.gz';#使用的zk文件名
my $bigdata_tmpfile = '/root/bigdata/bigdata_tmp';
#***********************全局变量定义开始**************************************
my $sequence = 1000;#主机hostname序列
my $cluster = 1;#group组自增序列(Hadoop HA 特性)
my $alices = 1;#别名自增序列(Hadoop HA 特性,NameNode别名)
my $zk_server = 0;#zookeeper自增序列
#active与standby的划分实际上是由zk决定的
my @active_nn = ();#保存所有的activeNameNode的hostname
my @active_nn_ip = ();#保存所有的activeNameNode的ip 启动时用
my @standby_nn = ();#保存所有的standbyNameNode的hostname
my @standby_nn_ip = ();#保存所有的standbyNameNode的ip 启动时用
my @start_param = ();#hadoop和yarn启动参数
my @result_array = ();
my @name_services = ();#保存所有的dfs.nameservices
my $jn_ip = undef;
my $jn_ip_array = [];
my $job_history_ip = undef;#启动hadoop集群用
my $job_history_hostname = undef;
my $resource_manager_ip = undef;#启动hadoop集群用
my $resource_manager_hostname = undef;
my $hive_ip = undef; # hive所在机器ip
my $hive_hostname = undef;#hive所在机器域名
#***********************全局变量定义结束**************************************
#***********************数据结构定义开始**************************************
#存放所有应用的数组
my @service = [];
my $hash_ip={
ips => {
#192.168.10.13[6-9] => undef,
#192.168.10.13[6-8] => undef,
#192.168.10.13[6-8] => undef,
},
ip => {#集群所有去重后的ip
#192.168.10.136 => undef,
#192.168.10.137 => undef,
#192.168.10.137 => undef,
},
localhost => undef,#当前机器ip
};
my $data = {
#服务信息
service => {
#Hadoop => [NameNode,DataNode,JournalNode],
#HBase => [...],
},
#节点信息
nodeinfo => {
#Hadoop_NameNode => {
# ip => '192.168.10.13.[6-9] 192.168.22.140 192.168.11.140',#支持多个空格分割的ip
# user => root,
# passwd => limengyu,
# port => 22,
# nodetype => Hadoop,
# nodename => NameNode,
#}
},
app_ips => {
#Hadoop => {
#192.168.10.130 => host001, #去重后的ip、主机名映射
#192.168.10.131 => host002,
#},
}
};
my %all_ip_hostname = ();
#***********************数据结构定义结束*****************************************
#初始化数据结构
sub init{
#查询出所有的父节点
my $table = $db->execute("select id,name from bigdata_node_type where pid = 0 order by id asc;");
init_data_service($table,0,undef);
#my @keys = keys $data->{service};
#foreach $item(@keys){
foreach my $item(@$service){
$table = $db->execute("select ip,port,user,passwd,node_type,node_name".
" from bigdata_ssh_info".
" where node_type = '$item' and install_status = 0;");
init_data_nodeinfo($table);
}
#获取当前机器ip
# my $ip = `ifconfig eth0|grep -oE '([0-9]{1,3}\.?){4}'|head -n 1`;
# if ($ip) {
# $hash_ip->{localhost}=$ip;
# }
}
#递归获取server服务
#$table结果集
#$type=0代表父节点
#$p_name=父类名称
sub init_data_service{
my ($table,$type,$p_name) = @_;
foreach my $row (@$table) {
my $id = $row->[0];
my $name = $row->[1];
$log->info("--------------$id------$name------$p_name--------");
if (!$type) {
#父节点
$data->{service}->{$name} = [];
push(@$service,$name);
my $son_table = $db->execute("select id,name from bigdata_node_type where pid = ".$id." order by id asc");
init_data_service($son_table,1,$name);
}else{
#p_name:Hadoop name:NameNode
push($data->{service}->{$p_name},$name);
}
}
}
#获取nodeinfo服务
sub init_data_nodeinfo{
my $table = shift;
foreach $row (@$table) {
my $ip = $row->[0];
my $port = $row->[1];
my $user = $row->[2];
my $passwd = $row->[3];
my $node_type = $row->[4];
my $node_name = $row->[5];
$hash_ip->{ips}->{$ip} = undef;#?
$data->{nodeinfo}->{$node_type."_".$node_name}{ip} = $ip;
$data->{nodeinfo}->{$node_type."_".$node_name}{port} = $port;
$data->{nodeinfo}->{$node_type."_".$node_name}{user} = $user;
$data->{nodeinfo}->{$node_type."_".$node_name}{passwd} = $passwd;
$data->{nodeinfo}->{$node_type."_".$node_name}{node_type} = $node_type;
$data->{nodeinfo}->{$node_type."_".$node_name}{node_name} = $node_name;
if (ref($data->{app_ips}->{$node_type}) ne "HASH") {
$data->{app_ips}->{$node_type} = {};
}
if ($ip) {
my $ip_array = parse_ips($ip);#解析ip字符串成数组
foreach $item (@$ip_array) {
unless (exists($data->{app_ips}->{$node_type}{$item})) {
#配置主机ip和主机域名的映射
if (exists $all_ip_hostname{$item}) {
$data->{app_ips}->{$node_type}->{$item} = $all_ip_hostname{$item};
}else{
my $currhostname = $global_hostname_prefix.++$sequence;
$all_ip_hostname{$item} = $currhostname;
$data->{app_ips}->{$node_type}->{$item} = $all_ip_hostname{$item};
}
}
}
}
}
}
#执行命令(应用名称、执行的命令、命令描述)
sub exec_command{
my ($appname,$command,$desc) = @_;
$log->info("开始执行应用<$appname>的命令<$desc>:".$command);
my $obj = Expect->spawn($command) or $log->info("Expect Couldn't exec command:$command.");
#$obj->exp_internal(1);
my ($pos,$err,$match,$before,$after) = $obj->expect(10,
[ qr/Password:/i,
sub{ my $self = shift; $self->send("$os_root_pass\r"); exp_continue;}
]
);
$obj->soft_close();
$log->info("完成执行应用<$appname>的命令<$desc>:");
}
#执行命令(应用名称、执行的命令、命令描述)
sub exec_commands{
my ($appname,$command,$desc) = @_;
$log->info("开始执行应用<$appname>的命令<$desc>:".$command);
my $command_exec = `$command`;
$log->info("完成执行应用<$appname>的命令<$desc>:".$command_exec);
}
#打印哈希
sub print_hash{
my $hash = shift;
while (($k, $v) = each %$hash) {
say "$k =>";
if (ref($v) eq 'HASH') {
while (($x, $y) = each %$v) {
if (ref($y) eq 'HASH') {
say "\t$x => ";
while (($z, $x) = each %$y) {
if (ref($x) eq 'HASH') {
say "\t\t$z => ";
}else{
say "\t\t$z => $x";
}
}
}else{
foreach $item (@$y) {
say "\t$x => $item";
}
}
}
}else{
say "\t$k => $v";
}
}
}
#打印数组service
sub print_service{
my ($name,$array) = @_;
say "数组$name:----->";
foreach my $item (@$array) {
if (ref($item) eq "ARRAY") {
say "子数组:----->";
print_service("",$item);
}else{
say "\t$item";
}
}
}
#修改某个用户的配置文件(.bashrc_profile)
#参数如:hadoop,'192.168.10.140 192.168.10.14[2-3] 192.168.10.144',
#$app_hash = {
# 'HIVE_HOME' => $hadoop_install_dir/hive,
# 'HADOOP_HOME' => $hadoop_install_dir/hadoop,
#}
#/usr/local/perl/bin/atnodes -u hadoop 'cat ~/.bash_profile' 192.168.98.57 192.168.99.92 192.168.98.201
#/usr/local/perl/bin/tonodes -u hadoop -L .bash_profile 192.168.98.57 192.168.99.92 192.168.98.201:/home/hadoop/.bash_profile
sub update_bashrc_profile{
my ($username,$ipstr,$app_hash) = @_;
my $targetfile = "/home/$username/.bash_profile";
my @path_array = ();
if (ref($app_hash) eq 'HASH') {
while (my ($app_home,$app_path) = each %$app_hash) {
#1.清理$APP_HOME、添加$APP_HOME
#my $update_home_command = "/bin/sed -i -e '/$app_home=/d' -e '/PATH=/i export $app_home=$app_path' $targetfile;";
my $update_home_command = $perl_install_dir."/bin/atnodes -L -w -u $username \"/bin/sed -i -e '/$app_home=/d' -e '/PATH=/i export $app_home=$app_path' $targetfile;\" $ipstr";
exec_command("未知",$update_home_command,"修改配置文件$targetfile");
if ($app_home =~ m/[A-Z]+_HOME$/g) {
push(@path_array,":\\\${$app_home}\\\/bin");
}
}
#在每个节点上执行sed替换并添加:${APP_HOME}/bin
foreach my $app_home(@path_array) {
my $config_bashrc_command = $perl_install_dir."/bin/atnodes -w -L -u $username '/bin/sed -i -e \"s/$app_home//g\" -e \"/PATH=/s/\\\$/$app_home/\" $targetfile;source $targetfile;' $ipstr";
exec_command("未知",$config_bashrc_command,"修改配置文件(添加$app_home)$targetfile");
}
}
}
#清理jdk安装目录和/etc/profile文件
sub clean_jdk{
my ($appname,$ips) = @_;
my $clean_command = $perl_install_dir."/bin/atnodes -w -L -u root \"/bin/rm -rf ".$java_install_dir.";/bin/sed -i \"/JAVA_HOME=/d\" /etc/profile;/bin/sed -i \"/JRE_HOME=/d\" /etc/profile;/bin/sed -i \"/CLASSPATH=/d\" /etc/profile;/bin/sed -i \"/JAVA_HOME/d\" /etc/profile;/bin/sed -i \"/^\$/d\" /etc/profile;source /etc/profile;\" $ips";
exec_command($appname,$clean_command,"清理JDK");
}
#清理hadoop用户和hadoop安装目录和/host/hadoop/.bash_profile文件
sub clean_hadoop_user{
my $ips = shift;
my $clear_command = $perl_install_dir."/bin/atnodes -w -L -u root \" /usr/sbin/userdel -r $global_hadoop_user;/bin/rm -rf $hadoop_install_dir/$hadoop_dir_name;\" ".$ips;
exec_command("Hadoop",$clear_command,"清理Hadoop用户");
}
#将数组$array转换成以$flag分割字符串
#默认以空格分割
sub array2str{
my ($array,$flag) = @_;
if (ref($array) eq 'ARRAY') {
if (!$flag) {
$flag = " ";
}
return join($flag,@$array);
}
}
#创建安装目录,并且将安装文件解压到安装目录
sub init_install_dir{
my $filename = shift;
unless (-e $hadoop_install_dir) {
my $create_init_dir_command = "/bin/mkdir $hadoop_install_dir;";
my $create_init_dir_exec = `$create_init_dir_command`;
}
my $unpack_command = "/bin/tar -xzvf $app_install_dir/$filename -C $hadoop_install_dir/";
my $unpack_exec = `$unpack_command`;
}
#写k-v到某个文件
sub generate_key_value{
my ($filename,$hash) = @_;
my $command = undef;
my $command_exec = undef;
while (my ($key,$value) = each %$hash) {
$command = "/bin/echo \"$key=$value\" >> $filename";
$command_exec = `$command`;
}
}
#向本地某个文件写入数据
sub echo_native_file{
my ($file,$array) = @_;
my $command = undef;
my $command_exec = undef;
foreach my $str (@$array) {
$command = "/bin/echo \"$str\" >> $file";
$command_exec = `$command`;
}
}
#打包本地文件
#把$dir打包成$package
#pack_file('hive.tar.gz','hive');
sub pack_file{
my ($package,$dir) = @_;
my $new_pack = "cd $hadoop_install_dir;/bin/tar -zcvf $package $dir";
my $new_pack_exec = `$new_pack`;
}
#在hdfs上创建目录
sub mkdir_hdfs_dir{
my $dir_name = shift;
my $mkdir_command = $perl_install_dir."/bin/atnodes -w -L -u $global_hadoop_user '$hadoop_install_dir/bin/hadoop fs -mkdir -R $dir_name ' ".$active_nn[0];
exec_command("Hadoop",$mkdir_command,"在HDFS上创建目录");
}
#配置root用户ssh无密码登录
#$hash={ip=>hostname}
sub ssh_login_root{
my ($app_name,$ipstr) = @_;
my $ssh_command = $perl_install_dir."/bin/key2nodes -u root $ipstr";
#$log->info("--------------应用: $app_name 开始批量配置 root 用户的ssh无密码登录!--------------$ssh_command");
#my $ssh_command_exec = `$ssh_command`;
exec_command($app_name,$ssh_command,"批量配置 root 用户的ssh无密码登录");
#$log->info("--------------应用: $app_name 完成批量配置 root 用户的ssh无密码登录!--------------");
}
#sub ssh_login_root{
#my ($app_name,$ipstr) = @_;
#my $ssh_command = "su - root -c \"$perl_install_dir/bin/key2nodes -u root $ipstr\"";
#$log->info("--------------应用: $app_name 开始批量配置 root 用户的ssh无密码登录!--------------$ssh_command");
#my $obj = Expect->spawn($ssh_command) or $log->info("Couldn't exec command:$ssh_command.");
##$obj->exp_internal(1);
#my ( $pos, $err, $match, $before, $after ) = $obj->expect(10,
#[
#'connecting (yes/no)',
#sub {
#my $self = shift;
#$self->send("yes\n");
#}
#],
#[
#'Enter file in which to save the key',
#sub{
#my $self = shift;
#$self->clear_accum();
#exp_continue;
#}
#],
#[
#'Enter passphrase \\(empty for no passphrase\\): ',
#sub{
#my $self = shift;
#$self->send("\r");
#exp_continue;
#}
#],
#[
#'Enter same passphrase again: ',
#sub{
#my $self = shift;
#$self->send("\r");
#exp_continue;
#}
#],
#[ qr/[Pp]{1}assword:/i,
#sub{ my $self = shift; $self->send("$os_root_pass\r"); exp_continue;}
#]
#);
#$obj->soft_close( );
#$log->info("--------------应用: $app_name 完成批量配置 root 用户的ssh无密码登录!--------------");
#}
#配置ssh无密码登录
#$hash={ip=>hostname}
sub ssh_login{
my ($app_name,$user,$passwd,$ipstr) = @_;
my $ssh_command = "su - $user -c \"$perl_install_dir/bin/key2nodes -u $user $ipstr\"";
$log->info("--------------应用: $app_name 开始批量配置 $user 用户的ssh无密码登录!--------------$ssh_command");
my $obj = Expect->spawn($ssh_command) or $log->info("Couldn't exec command:$ssh_command.");
#$obj->exp_internal(1);
my ( $pos, $err, $match, $before, $after ) = $obj->expect(10,
[ qr/Password:/i,
sub{ my $self = shift; $self->send("$passwd\r"); exp_continue;}
]
);
$obj->soft_close( );
$log->info("--------------应用: $app_name 完成批量配置 $user 用户的ssh无密码登录!--------------");
}
#批量创建用户
#$username 创建的用户名
#$passwd 创建的用户名的密码
#$ipstr操作的节点ip
sub useradd{
my ($appname,$username,$passwd,$ipstr) = @_;
if ($username) {
$log->info("--------------应用: $app_name 开始批量创建用户!--------------");
#创建用户设置密码
my $useradd_command = $perl_install_dir."/bin/atnodes -w -L -u root \"/usr/sbin/useradd ".$username.";echo \"$passwd\" | passwd --stdin $username ;\" ".$ipstr;
exec_command($appname,$useradd_command,"批量创建\"$username\"用户");
}
}
#安装jdk #/usr/bin/java -> /etc/alternatives/java
#上传tar包,解压包,修改配置文件,测试是否安装成功
sub install_jdk{
my ($app_name,$ipstr) = @_;
$log->info("--------------应用: $app_name 开始安装JDK!--------------$ipstr");
#my $bit = `/usr/bin/getconf LONG_BIT`;
#if (!$bit) {
#$bit = "64";#默认64位
#}
#chomp($bit);
my $app_dir = $app_install_dir."/$jdk_install_name";#安装包所在目录
my $upload_dir = $remote_upload_dir."/$jdk_install_name";#远程上传目录
#my $java_bin = `which java`;
#if ($java_bin !~ m#/usr/bin/java#) {#如果不存在
my $java_bin = "/usr/bin/java";
#}
#chomp($java_bin);
#上传
my $upload_command = $perl_install_dir."/bin/tonodes -w -L $app_dir -u root \"$ipstr\":$remote_upload_dir/";
exec_commands($app_name,$upload_command,"分发上传JDK文件");
#解压、配置环境变量
my $tar_command = $perl_install_dir."/bin/atnodes -w -L -u root \"/bin/mkdir ".$java_install_dir.";/bin/tar -xzvf ".$upload_dir." -C ".$java_install_dir.";/bin/mv -f ".$java_bin." ".$java_bin.".old;/bin/ln -s ".$java_install_dir."/java/bin/java ".$java_bin.";chcon -t texrel_shlib_t ".$java_install_dir."/java/jre/lib/i386/client/libjvm.so;\" ".$ipstr;
exec_commands($app_name,$tar_command,"解压JDK");
my $config_command = $perl_install_dir."/bin/atnodes -w -L -u root '/bin/echo \"export JAVA_HOME=".$java_install_dir."/java\" >> /etc/profile;/bin/echo \"export JRE_HOME=\\\${JAVA_HOME}/jre\" >> /etc/profile;/bin/echo \"export CLASSPATH=.:\\\${JAVA_HOME}/lib:\\\${JRE_HOME}/lib\" >> /etc/profile;/bin/echo \"export PATH=\\\${JAVA_HOME}/bin:\\\$PATH \" >> /etc/profile;source /etc/profile' ".$ipstr;
exec_command($app_name,$config_command,"配置JDK环境变量");
#测试安装是否成功
my $test_command = $perl_install_dir."/bin/atnodes -w -L -u root \"java -version\" ".$ipstr;
exec_command($app_name,$test_command,"测试JDK是否安装成功");
$log->info("--------------应用: $app_name 完成安装JDK!--------------$ipstr");
}
#配置hostname和hosts
sub clean_config_hosts{
my ($appname,$ip_hostname_hash) = @_;
$log->info("--------------应用: $app_name 开始配置hosts和hostname!--------------");
my $ips = $data->{app_ips}->{$appname};#哈希,存放ip和hostname映射
my $keys = [keys %$ip_hostname_hash];#存放所有ip的数组
while (($ip, $hostname) = each %$ip_hostname_hash) {
#1、清理配置文件/etc/hosts和/etc/sysconfig/network
my $clean_command = $perl_install_dir."/bin/atnodes -w -L -u root \"/bin/sed -i \"/$global_hostname_prefix/d\" /etc/hosts;sed -i \"/HOSTNAME=/d\" /etc/sysconfig/network;\" $ip";
exec_command($appname,$clean_command,"清理\"$ip\"的hosts和hostname");
#2、修改hostname配置文件
my $hostname_command = $perl_install_dir."/bin/atnodes -w -L -u root \"/bin/echo \"HOSTNAME=$hostname\" >> /etc/sysconfig/network;/bin/hostname $hostname\" $ip";
exec_command($appname,$hostname_command,"配置\"$ip\"的hostname");
}
foreach my $item (@$keys) {
$log->info("开始配置《 $item 》hosts.");
while (($k, $v) = each %$ips) {
my $hosts_command = $perl_install_dir."/bin/atnodes -w -L -u root \"/bin/echo $k $v >> /etc/hosts \" $item";
exec_command($appname,$hosts_command,"配置hosts");
}
$log->info("完成配置《 $item 》hosts.");
}
#3、刷新配置文件
my $flush_command = $perl_install_dir."/bin/atnodes -w -L -u root \"service network restart;\" $ip";
my $flush_exec = `$flush_command`;
$log->info("--------------应用: $app_name 完成配置hosts和hostname!--------------");
}
#上传文件
#上传的文件名$full_name
#目标目录$targetdir /usr/local
#是否解压$ispack TRUE or FALSE
#ip地址
sub uploadFile{
my($full_name,$targetdir,$ispack,$ipstr) = @_;
my $filename = basename($full_name);
create_remote_dir($targetdir,$ipstr);
my $upload_command = $perl_install_dir."/bin/tonodes -w -L ".$full_name." -u root ".$ipstr.":".$targetdir."/";
exec_commands("未知",$upload_command,"上传文件$full_name");
if ($ispack eq TRUE) {
my $pack_command = $perl_install_dir."/bin/atnodes -w -L -u root \"/bin/tar -xzvf ".$targetdir."/".$filename." -C $targetdir/;\" $ipstr";
exec_commands("未知",$pack_command,"解压文件$full_name");
}
}
#配置
#$filename 目录+文件名
#ip地址
#$type操作类型,1为清理,2为更新
sub updateConfig{
my($ip,$type) = @_;
my $update_command = "";
if ($type eq '1') {
$update_command = $perl_install_dir."/bin/atnodes -w -L -u root \"/bin/rm -rf $perl_install_dir ;/bin/mv -f /usr/bin/perl.bigdata /usr/bin/perl; \" $ip";
}elsif($type eq '2'){
$update_command = $perl_install_dir."/bin/atnodes -w -L -u root \"/bin/mv -f /usr/bin/perl /usr/bin/perl.bigdata;ln -s ".$perl_install_dir."/bin/perl /usr/bin/perl; \" $ip";
}
exec_command("未知",$update_command,"更新配置文件$filename");
}
#解析ip集合,返回数组
#ips=192.18.10.14.[0-9] 192.18.10.130 192.18.10.12.[0-3]
sub parse_ips{
my $ips = shift;
#$ips =~ s/,/ /g;
my $parse_command = "$perl_install_dir/bin/fornodes $ips";
my $parse_result = `$parse_command`;
my @parse_array = split(/\s+/,$parse_result);
return \@parse_array;
}
#生成property标签
sub generateXML{
my ($full_name,$hash) = @_;
$log->info("开始修改配置文件:".$full_name);
my $str = undef;
while (($key, $value) = each %$hash) {
$str = $str."\t<property>\n\t\t<name>$key</name>\n\t\t<value>$value</value>\n\t</property>\n";
}
if ($str) {
open TMP, ">$bigdata_tmpfile"; #写入文件名
say TMP $str;
close(TMP);
my $insert_command = "/bin/sed -i '/<configuration>/r $bigdata_tmpfile' $full_name";
exec_command("未知",$insert_command,"插入数据$full_name");
}
$log->info("完成修改配置文件:".$full_name);
}
#生成generateHDFSSite标签
#hdfs-site.xml文件中只能配置一个dfs.namenode.shared.edits.dir属性
#<name>dfs.namenode.shared.edits.dir</name>
#<value>qjournal://hadoop002:8485;hadoop003:8485;hadoop004:8485/hadoop-cluster2</value>
#$array数组引用
sub generateHDFSSite{
my ($array,$journal_array,$ip_hostname_hash,$flag) = @_;
my $full_name = $hadoop_install_dir."/hdfs-site.xml";
my $str = "\t<property>\n\t\t<name>dfs.replication</name>\n\t\t<value>1</value>\n\t</property>\n";
my $nameservice = "";
my $nameservices = "";
my $first_nn_alice = "";
my $first_nn_hostname = "";
my $second_nn_alice = "";
my $second_nn_hostname = "";
my $first_nn_ip = undef;
my $second_nn_ip = undef;
my $journal_str = "";
foreach my $item (@$journal_array) {
$journal_str = $journal_str.$ip_hostname_hash->{$item}.":8485;";
}
chop($journal_str);#去掉最后一个分号
foreach my $item (@$array) {
if (ref($item) eq "ARRAY") {
#hadoop-cluster1,nn1,nn2,host1,host2,nn1_ip,nn2_ip
#hadoop-cluster2,nn3,nn4,host3,host4,nn3_ip,nn4_ip
$nameservice = @$item[0];
$nameservices = $nameservices.$nameservice.",";
$first_nn_alice = @$item[1];
$second_nn_alice = @$item[2];
$first_nn_hostname = @$item[3];
$second_nn_hostname = @$item[4];
$first_nn_ip = @$item[5];
$second_nn_ip = @$item[6];
$str = $str."\t<property>\n\t\t<name>dfs.ha.namenodes.$nameservice</name>\n\t\t<value>$first_nn_alice,$second_nn_alice</value>\n\t</property>\n";
$str = $str."\t<property>\n\t\t<name>dfs.namenode.rpc-address.$nameservice.$first_nn_alice</name>\n\t\t<value>$first_nn_hostname:8020</value>\n\t</property>\n";
$str = $str."\t<property>\n\t\t<name>dfs.namenode.rpc-address.$nameservice.$second_nn_alice</name>\n\t\t<value>$second_nn_hostname:8020</value>\n\t</property>\n";
$str = $str."\t<property>\n\t\t<name>dfs.namenode.http-address.$nameservice.$first_nn_alice</name>\n\t\t<value>$first_nn_hostname:50070</value>\n\t</property>\n";
$str = $str."\t<property>\n\t\t<name>dfs.namenode.http-address.$nameservice.$second_nn_alice</name>\n\t\t<value>$second_nn_hostname:50070</value>\n\t</property>\n";
$str = $str."\t<property>\n\t\t<name>dfs.ha.automatic-failover.enabled.$nameservice</name>\n\t\t<value>true</value>\n\t</property>\n";
$str = $str."\t<property>\n\t\t<name>dfs.client.failover.proxy.provider.$nameservice</name>\n\t\t<value>org.apache.hadoop.hdfs.server.namenode.ha.ConfiguredFailoverProxyProvider</value>\n\t</property>\n";
#特殊的属性值
if ($flag eq $nameservice) {
$str = $str."\t<property>\n\t\t<name>dfs.namenode.shared.edits.dir</name>\n\t\t<value>qjournal://$journal_str/$nameservice</value>\n\t</property>\n";
}
}
}
if ($nameservices) {
chop($nameservices);
}
$str = "\t<property>\n\t\t<name>dfs.nameservices</name>\n\t\t<value>$nameservices</value>\n\t</property>\n".$str;
$str = $str."\t<property>\n\t\t<name>dfs.namenode.name.dir</name>\n\t\t<value>file://$hadoop_install_dir/$hadoop_dir_name/tmp/namenode</value>\n\t</property>\n\t<property>\n\t\t<name>dfs.datanode.data.dir</name>\n\t\t<value>file://$hadoop_install_dir/$hadoop_dir_name/tmp/datanode</value>\n\t</property>\n\t<property>\n\t\t<name>dfs.ha.automatic-failover.enabled</name>\n\t\t<value>true</value>\n\t</property>\n\t<property>\n\t\t<name>dfs.journalnode.edits.dir</name>\n\t\t<value>$hadoop_install_dir/$hadoop_dir_name/tmp/journal/</value>\n\t</property>\n\t<property>\n\t\t<name>dfs.ha.fencing.methods</name>\n\t\t<value>sshfence</value>\n\t</property>\n\t<property>\n\t\t<name>dfs.ha.fencing.ssh.private-key-files</name>\n\t\t<value>/home/$global_hadoop_user/.ssh/id_rsa</value>\n\t</property>\n\t";
open TMP, ">$bigdata_tmpfile"; #写入文件名
say TMP $str;
close(TMP);
my $insert_command = "/bin/sed -i '/<configuration>/r $bigdata_tmpfile' $full_name";
exec_command("未知",$insert_command,"插入数据$full_name");
}
#同步文件到相应节点
sub sync_node_file{
my ($sourcefile,$node,$targetfile) = @_;
my $upload_command = $perl_install_dir."/bin/tonodes -w -L -u root $sourcefile \"$node\":$targetfile";
exec_command("未知",$upload_command,"同步文件到相应节点$targetfile");
}
#配置slaves文件
#所有的DataNode节点的hostname
sub generateSlaves{
my $full_name = $hadoop_install_dir."/$hadoop_dir_name/etc/hadoop/slaves";
my $command = undef;
my $exec = undef;
my $clear_command = "echo \"\" > $full_name";
exec_command("Hadoop",$clear_command,"清理slaves文件");
$log->info("开始配置slaves文件。");
my $ipstr = $data->{nodeinfo}->{Hadoop_DataNode}->{ip};#获取所有的DataNode节点
my $ip_hostname_hash = $data->{app_ips}->{Hadoop};#获取hadoop集群中的所有节点
my $ip_array = parse_ips($ipstr);
my $hostname = undef;
foreach my $ip (@$ip_array) {
$hostname = $ip_hostname_hash->{$ip};
$command = "echo \"$hostname\" >> $full_name";
$exec = `$command`;
}
$log->info("完成配置slaves文件。");
}
#创建远程目录
#ipstr ip字符串
#dir 需要创建的远程目录
sub create_remote_dir{
my($dir,$ipstr) = @_;
my $update_command = $perl_install_dir."/bin/atnodes -w -L -u root \" mkdir $dir;\" $ipstr";
exec_command("未知",$update_command,"创建远程目录$dir");
}
#修改远程文件的所属用户和所属用户组
sub update_remote_dir_auth{
my ($file,$user,$group,$ipstr) = @_;
my $update_command = $perl_install_dir."/bin/atnodes -w -L -u root \" /bin/chown -R $user:$group $file \" $ipstr";
exec_command("未知",$update_command,"修改文件所属用户和所属组$file");
}
#启动hadoop集群和yarn
#$start_param数组保存节点信息
#$jn_ip,$resource_manager_ip,$job_history_ip
sub start_hadoop_cluster{
my $flag = shift;#是否需要初始化数据
if ($flag eq TRUE) {
init_hadoop_data();
}
$log->info("开始启动Hadoop集群....");
my $nameservice = undef;
my $first_nn_alice = undef;
my $first_nn_ip = undef;
my $first_nn_hostname = undef;
my $second_nn_alice = undef;
my $second_nn_ip = undef;
my $second_nn_hostname = undef;
my $hadoop_home = $hadoop_install_dir."/$hadoop_dir_name";#$hadoop_home
#my $clusterId = "hadoop-cluster";#clusterId
my $master_nn_ip = $start_param[0][2];#master_nn_ip,first_namenode_ip
#step_0 启动zookeeper集群
start_zk_cluster();
#step_0_1格式化ZooKeeper集群,目的是在ZooKeeper集群上建立HA的相应节点。(在nn1和nn3节点上)
my $formatzk_ip = array2str(\@active_nn_ip);
my $formatzk_command = $perl_install_dir."/bin/atnodes -w -L -u $global_hadoop_user \"$hadoop_home/bin/hdfs zkfc –formatZK\" $formatzk_ip";
my $formatzk_command_exec = `$formatzk_command`;
#step_1 启动JournalNode集群
my $start_journalnode_command = $perl_install_dir."/bin/atnodes -w -L -u $global_hadoop_user \"$hadoop_home/sbin/hadoop-daemon.sh start journalnode\" $jn_ip";
exec_command("Hadoop",$start_journalnode_command,"开始启动journalnode服务");
foreach my $item (@start_param) {
if (ref($item) eq 'ARRAY') {
$nameservice = @$item[0];
$first_nn_alice = @$item[1];
$first_nn_ip = @$item[2];
$first_nn_hostname = @$item[3];
$second_nn_alice = @$item[4];
$second_nn_ip = @$item[5];
$second_nn_hostname = @$item[6];
#step_2 格式化nn1,并启动
my $first_nn_format_start_command = $perl_install_dir."/bin/atnodes -w -L -u $global_hadoop_user \" $hadoop_home/bin/hdfs namenode -format -clusterId $nameservice;$hadoop_home/sbin/hadoop-daemon.sh start namenode; \" $first_nn_ip";
my $first_nn_format_start_exec = `$first_nn_format_start_command`;
$log->info("NameNode_$first_nn_alice 地址:http://$first_nn_ip:50070/");
#step_3 在nn2上同步nn1的元数据信息 and step_4 启动nn2
my $second_nn_sync_start_command = $perl_install_dir."/bin/atnodes -w -L -u $global_hadoop_user \" $hadoop_home/bin/hdfs namenode -bootstrapStandby;$hadoop_home/sbin/hadoop-daemon.sh start namenode; \" $second_nn_ip";
my $second_nn_sync_start_exec = `$second_nn_sync_start_command`;
$log->info("NameNode_$second_nn_alice 地址:http://$second_nn_ip:50070/");
#step_5 将nn1设置为ActiveNameNode(手动启动),改为由zookeeper启动
#my $first_nn_active_command = $perl_install_dir."/bin/atnodes -w -L -u $global_hadoop_user \" $hadoop_home/bin/hdfs haadmin -ns $nameservice -transitionToActive $first_nn_alice;\" $first_nn_ip";
#my $first_nn_active_exec = `$first_nn_active_command`;
}
}
#step_6 在nn1上启动所有的datanode
if ($master_nn_ip) {
my $master_nn_start_datanode = $perl_install_dir."/bin/atnodes -w -L -u $global_hadoop_user \" $hadoop_home/sbin/hadoop-daemons.sh start datanode; \" $master_nn_ip";
my $master_nn_start_exec = `$master_nn_start_datanode`;
}
#step_7 启动Yarn
if ($resource_manager_ip) {
my $yarn_start_command = $perl_install_dir."/bin/atnodes -w -L -u $global_hadoop_user \" $hadoop_home/sbin/start-yarn.sh; \" $resource_manager_ip";
exec_command("Hadoop",$yarn_start_command,"开始启动Yarn");
$log->info("ResourceManager地址:http://$resource_manager_ip:8088/cluster");
$log->info("NodeManager地址:http://\${NodeManagerip}:8042/node");
}else{
$log->info("没有配置resource_manager,跳过启动ResourceManager.");
}
#step_8 启动JobHistory Server
if ($job_history_ip) {
my $jobhistory_start_command = $perl_install_dir."/bin/atnodes -w -L -u $global_hadoop_user \" $hadoop_home/sbin/mr-jobhistory-daemon.sh start historyserver; \" $job_history_ip";
exec_command("Hadoop",$jobhistory_start_command,"开始启动JobHistory Server");
}else{
$log->info("没有配置JobHistory Server.,跳过启动JobHistory Server.");
}
#step_9在所有NameNode节点上启动ZooKeeperFailoverController,产生java进程DFSZKFailoverController
my $all_nn_ip_array = parse_ips($data->{nodeinfo}->{Hadoop_NameNode}->{ip});
my $all_nn_ip_str = array2str($all_nn_ip_array);
my $start_controller_command = $perl_install_dir."/bin/atnodes -w -L -u $global_hadoop_user \"$hadoop_home/sbin/hadoop-daemon.sh start zkfc;\" $all_nn_ip_str";
exec_command("Hadoop",$start_controller_command,"开始启动ZooKeeperFailoverController");
#step_10 执行jsp命令测试是否启动
$log->info("完成启动Hadoop集群");
}
#初始化hadoop的一些数据
sub init_hadoop_data{
$log->info("开始初始化Hadoop数据....");
#2.获取所有的journalnode节点并存放到数组中
$jn_ip = $data->{nodeinfo}->{Hadoop_JournalNode}->{ip};#可能是多个空格分割的ip地址
$jn_ip_array = parse_ips($jn_ip);
my $jn_ip_array_leg = scalar(@$jn_ip_array);
if (!(($jn_ip_array_leg & 1) != 0 && $jn_ip_array_leg > 1)) {
$log->info("JournalNode至少需要三台节点,并且需要使用奇数。");
}
#获取JobHistory节点的ip
$job_history_ip = $data->{nodeinfo}->{Hadoop_HistoryServer}->{ip};#可能是多个空格分割的ip地址 应该只有一个ip
if ($job_history_ip) {
$job_history_hostname = $data->{app_ips}->{Hadoop}->{$job_history_ip};
}
#获取ResourceManager节点的ip
$resource_manager_ip = $data->{nodeinfo}->{Hadoop_ResourceManager}->{ip};#可能是多个空格分割的ip地址 应该只有一个ip
if ($resource_manager_ip) {
$resource_manager_hostname = $data->{app_ips}->{Hadoop}->{$resource_manager_ip};
}
#3.获取所有的namenode节点并存放到数组中
my $nn_ip = $data->{nodeinfo}->{Hadoop_NameNode}->{ip};#可能是多个空格分割的ip地址
my $nn_ip_array = parse_ips($nn_ip);
my $nn_ip_array_leg = scalar(@$nn_ip_array);
my $ips = $data->{app_ips}->{Hadoop};#哈希,存放ip和hostname映射
if ($nn_ip_array_leg > 0) {
#说明有多个namenode,需要检查journalnode
my $nn_group = 0;#分组数
if ($nn_ip_array_leg % 2 == 0) {
$nn_group = $nn_ip_array_leg / 2;
}#else{
# $nn_group = ($nn_ip_array_leg / 2) + 1;
#}
my $first_nn_ip = undef;
my $second_nn_ip = undef;
my $first_nn_hostname = undef;
my $second_nn_hostname = undef;
my $nameservices = undef;
my $first_nn_alice = undef;
my $second_nn_alice = undef;
for (my $k = 0; $k < $nn_group;$k++) {
$first_nn_ip = shift @$nn_ip_array;
$second_nn_ip = shift @$nn_ip_array;
if ($first_nn_ip && $second_nn_ip) {
$first_nn_hostname = $ips->{$first_nn_ip};
$second_nn_hostname = $ips->{$second_nn_ip};
$nameservices = "hadoop-cluster".$cluster++;
$first_nn_alice = "nn".$alices++;
$second_nn_alice = "nn".$alices++;
push(@name_services,$nameservices);#生成hdfs-site.xml时用
push(@active_nn,$first_nn_hostname);
push(@active_nn_ip,$first_nn_ip);#启动时用
push(@standby_nn,$second_nn_hostname);
push(@active_nn_ip,$second_nn_ip);#启动时用
push(@result_array,[$nameservices,$first_nn_alice,$second_nn_alice,$first_nn_hostname,$second_nn_hostname,$first_nn_ip,$second_nn_ip]);
push(@start_param,[$nameservices,$first_nn_alice,$first_nn_ip,$first_nn_hostname,$second_nn_alice,$second_nn_ip,$second_nn_hostname]);
}
}
}else{
$log->info("没有配置NameNode,请配置。");
}
print_service("\@name_services",\@name_services);
print_service("\@active_nn",\@active_nn);
print_service("\@active_nn_ip",\@active_nn_ip);
print_service("\@standby_nn",\@standby_nn);
print_service("\@result_array",\@result_array);
print_service("\@start_param",\@start_param);
$log->info("完成初始化Hadoop数据....");
}
#安装hadoop
sub install_hadoop{
my ($app_name,$ip_hostname_hash) = @_;
my $ip_str = array2str([keys %$ip_hostname_hash]);#获取以空格分割ip字符串
print_hash($ip_hostname_hash);
#ssh_login_root($app_name,$ip_str);#配置root用户ssh登录
clean_config_hosts($app_name,$ip_hostname_hash);#配置hosts和hostname
clean_jdk($app_name,$ip_str);#安装前清理jdk
install_jdk($app_name,$ip_str);#上传并安装jdk
clean_hadoop_user($ip_str);#清理hadoop用户
useradd($app_name,$global_hadoop_user,$os_hadoop_pass,$ip_str);#批量创建hadoop用户
#ssh -l hadoop 192.168.10.140
ssh_login($app_name,$global_hadoop_user,$os_hadoop_pass,$ip_str);#配置hadoop用户ssh登录
#1.解压Hadoop安装包到Hadoop安装目录(目录不存在则创建)
init_install_dir("$hadoop_install_name");
#2.修改配置文件hadoop-env.sh
# my $hadoop_env_sh_command = "/bin/sed -i -e \"/JAVA_HOME=/d\" -e \"/HADOOP_COMMON_LIB_NATIVE_DIR=/d\" -e \"/-Djava.library.path=/d\" -e \"/^\$/d\" ".$hadoop_install_dir."/$hadoop_dir_name/etc/hadoop/hadoop-env.sh;/bin/echo 'export JAVA_HOME=".$java_install_dir."/java \n export HADOOP_COMMON_LIB_NATIVE_DIR=\${HADOOP_HOME}/lib/native \n export HADOOP_OPTS=\"-Djava.library.path=\$HADOOP_HOME/lib\"' >> ".$hadoop_install_dir."/$hadoop_dir_name/etc/hadoop/hadoop-env.sh";
my $hadoop_env_sh_command = "/bin/sed -i -e \"/JAVA_HOME=/d\" -e \"/^\$/d\" $hadoop_install_dir/$hadoop_dir_name/etc/hadoop/hadoop-env.sh;/bin/echo 'export JAVA_HOME=".$java_install_dir."/java' >> $hadoop_install_dir/$hadoop_dir_name/etc/hadoop/hadoop-env.sh;";
my $hadoop_env_sh_exec = `$hadoop_env_sh_command`;
#初始化hadoop数据
#2.获取所有的journalnode节点并存放到数组中
#获取JobHistory节点的ip
#获取ResourceManager节点的ip
#3.获取所有的namenode节点并存放到数组中
init_hadoop_data();
$log->info('@active_nn_ip: '.scalar(@active_nn_ip));
if (scalar(@active_nn_ip) > 0) {
my @zk_server_host = values $data->{app_ips}->{Zookeeper};#获取zk的主机名和端口
my $zk_server_host_str = undef;
foreach my $zk_host (@zk_server_host) {
$zk_server_host_str .= $zk_host.":2888,";
}
chop($zk_server_host_str);#去掉最后一个逗号
#配置core-site.xml
my $core_site_hash = {
#'fs.default.name' => "hdfs://".$active_nn[0].":8020",
'fs.defaultFS' => $name_services[0],
'hadoop.tmp.dir' => "$hadoop_install_dir/$hadoop_dir_name/tmp",
'ha.zookeeper.quorum' => $zk_server_host_str,
};
generateXML("$hadoop_install_dir/$hadoop_dir_name/etc/hadoop/core-site.xml",$core_site_hash);
#配置mapred-site.xml
my $mapred_site_hash = {
'mapreduce.framework.name' => "yarn",
'mapreduce.jobhistory.address' => $job_history_hostname.":10020",
'mapreduce.jobhistory.webapp.address' => $job_history_hostname.":19888",
};
generateXML("$hadoop_install_dir/$hadoop_dir_name/etc/hadoop/mapred-site.xml",$mapred_site_hash);
#配置yarn-site.xml
my $yarn_site_hash = {
'yarn.resourcemanager.hostname' => "$resource_manager_hostname",
'yarn.resourcemanager.address' => '${yarn.resourcemanager.hostname}:8032',
'yarn.resourcemanager.scheduler.address' => '${yarn.resourcemanager.hostname}:8030',
'yarn.resourcemanager.webapp.address' => '${yarn.resourcemanager.hostname}:8088',
'yarn.resourcemanager.webapp.https.address' => '${yarn.resourcemanager.hostname}:8090',
'yarn.resourcemanager.resource-tracker.address' => '${yarn.resourcemanager.hostname}:8031',
'yarn.resourcemanager.admin.address' => '${yarn.resourcemanager.hostname}:8033',
#'yarn.resourcemanager.scheduler.class' => 'org.apache.hadoop.yarn.server.resourcemanager.scheduler.fair.FairScheduler',
#'yarn.scheduler.fair.allocation.file' => '${yarn.home.dir}/etc/hadoop/fairscheduler.xml',
'yarn.nodemanager.local-dirs' => $hadoop_install_dir."/$hadoop_dir_name/tmp/yarn/local",
'yarn.log-aggregation-enable' => 'true',
'yarn.nodemanager.remote-app-log-dir' => $hadoop_install_dir."/$hadoop_dir_name/tmp/nodemanager",
'yarn.nodemanager.resource.memory-mb' => 30720,
'yarn.nodemanager.resource.cpu-vcores' => 12,
'yarn.nodemanager.aux-services' => 'mapreduce_shuffle',
};
generateXML("$hadoop_install_dir/$hadoop_dir_name/etc/hadoop/yarn-site.xml",$yarn_site_hash);
#配置slaves(所有的DataNode节点hostname)
generateSlaves();
#打包配置好的Hadoop文件
pack_file($hadoop_install_name,$hadoop_dir_name);
#上传Hadoop文件到各个节点上、解压
uploadFile("$hadoop_install_dir/$hadoop_install_name",$hadoop_install_dir,TRUE,$ip_str);
#配置hdfs-site.xml
#1.copy一个hdfs-site.xml模板到安装目录
#2.根据nameservice生成对应节点的hdfs-site.xml文件
#3.拷贝相应文件到相应节点上hadoop-cluster1:hadoop001:hadoop002
foreach my $item (@result_array) {
my $copy_command = "/bin/cp -f $install_template_dir/hdfs-site.xml $hadoop_install_dir";
exec_commands($app_name,$copy_command,"拷贝hdfs-site.xml模板");
my $hadoop_cluster_flag = @$item[0];
#$hadoop_cluster_flag:hadoop-cluster1
$log->info("\$hadoop_cluster_flag: $hadoop_cluster_flag");
#$node_hostname: bigdata-[1001,1002]
#my $node = $global_hostname_prefix."[".substr(@$item[3],length($global_hostname_prefix),length(@$item[3])-length($global_hostname_prefix)).",".substr(@$item[4],length($global_hostname_prefix),length(@$item[4])-length($global_hostname_prefix))."]";
my $node = @$item[5]." ".@$item[6];
$log->info("\$node_ip: $node");
generateHDFSSite(\@result_array,$jn_ip_array,$ip_hostname_hash,$hadoop_cluster_flag);
sync_node_file($hadoop_install_dir."/hdfs-site.xml",$node,"$hadoop_install_dir/$hadoop_dir_name/etc/hadoop/");
}
#更改hadoop目录的用户和用户组
update_remote_dir_auth($hadoop_install_dir."/$hadoop_dir_name",$global_hadoop_user,$global_hadoop_user,$ip_str);
#创建远程目录
#create_remote_dir("/tmp/logs",$ip_str);
#update_remote_dir_auth("/tmp/logs",$global_hadoop_user,$global_hadoop_user,$ip_str);
#配置/home/hadoop/.bash_profile文件
my $bashrc_profile_hash = {
'HADOOP_HOME' => $hadoop_install_dir."/$hadoop_dir_name",
'HADOOP_COMMON_LIB_NATIVE_DIR' => '${HADOOP_HOME}/lib/native',
'HADOOP_OPTS' => '\"-Djava.library.path=$HADOOP_HOME/lib\"',
};
update_bashrc_profile($global_hadoop_user,$ip_str,$bashrc_profile_hash);
#启动Hadoop集群
#start_hadoop_cluster(FALSE);
}elsif($nn_ip_array_leg == 1){
#说明只有一个namenode
}else{
#说明没有设置namenode
}
}
#安装Zookeeper
#1.判断节点数是否是奇数
#2.解压安装包到安装目录,修改配置文件
#3.打包修改好的配置文件
#4.上传安装包到安装节点
#5.解压安装包,启动Zookeeper
sub install_zookeeper{
my ($app_name,$ip_hostname_hash) = @_;
my $all_ip = [keys %$ip_hostname_hash];