步骤
登录服务器hostname
进入目录,将集群全部机器列表写入文件,每行一个机器名1
2cd /home/q/fabric/hbase/
vim fab_hbase_hostfile.txt
fab_hbase_hostfile.txt是hostname列表
删除上次执行脚本遗留的文件1
rm /home/q/fabric/hbase/auth_test.txt
执行以下命令行1
fab -f fabfile_hbase_hostfile.py set_hosts del_user mk_user host_make_idrsa get_idrsapub put_authfile delete_authfile
脚本内容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
42from fabric.api import *
from fabric.contrib.console import confirm
from fabric.contrib.files import exists
env.user="qnrdba"
env.key_filename="/home/qnrdba/.ssh/id_rsa"
def set_hosts():
env.hosts=open('/home/q/fabric/hbase/fab_hbase_hostfile.txt','r').readlines()
def del_user():
if exists('/home/hadoop',use_sudo=True):
sudo('userdel hadoop')
sudo('rm -r /home/hadoop/')
def mk_user():
sudo('useradd hadoop')
def host_make_idrsa():
sudo('ssh-keygen -d',user='hadoop')
def get_idrsapub():
sudo('cp /home/hadoop/.ssh/id_dsa.pub /tmp/')
get('/tmp/id_dsa.pub','/home/q/fabric/hbase/')
local('mv /home/q/fabric/hbase/id_dsa.pub /home/q/fabric/hbase/id_dsa_test.pub')
local('cat /home/q/fabric/hbase/id_dsa_test.pub >> /home/q/fabric/hbase/auth_test.txt')
local('rm /home/q/fabric/hbase/id_dsa_test.pub')
def put_authfile():
put('/home/q/fabric/hbase/auth_test.txt','/tmp/')
sudo('cp /tmp/auth_test.txt /home/hadoop/.ssh/authorized_keys')
sudo('chmod 600 /home/hadoop/.ssh/authorized_keys')
sudo('chown hadoop.hadoop /home/hadoop/.ssh/authorized_keys')
def delete_authfile():
sudo('rm /tmp/auth_test.txt')
sudo('rm /tmp/id_dsa.pub')
使用说明1
2
3
4
5
6
7
8
9
10
11
12
13del_user 判断:若已存在hadoop账户,删除该账户、家目录
delete_authfile 删除集群各服务器临时文件
get_idrsapub 获取公钥到本地,追加生成auth汇总文件
host_make_idrsa 使用hadoop账户生成公钥文件
mk_user 创建hadoop账户
put_authfile 将本地anth汇总文件分发至集群各服务器,修改权限
set_hosts 配置集群服务器主机列表