hadoop@ubuntu:~$
sudo apt-get install nfs-kernel-server
二、配置/etc/exports文件设置共享目录
一个NFS服务器可以共享多个目录,
该文件中一行表示一个共享目录,没行的设置格式如下:
共享目录 客户机(参数1,参数2,参数3,...,参数n)
说明:
共享目录:可以是主机上任何一个目录,为方便使用,最后将其权限设置为777
客户机:客户机表示可以访问该共享目录的ip或主机名,可以设置通配符,若为*,则表示任何机器都可访问。
参数:典型和常用的参数说明如下:
ro 只读权限
rw 读写权限
sync 数据同步写入内存和磁盘
no_root_squash 客户机享有nfs服务器的root权限
我的/etc/exports文件内容如下:
/home/hadoop/share *(rw,sync,no_subtree_check,no_root_squash)
说明:首先我们必须先创建share目录。
三、客户机安装nfs客户端,命令如下:
hadoop@ubuntu:~$
sudo apt-get install nfs-common portmap
四、启动服务器nfs服务进程
hadoop@ubuntu:~$
sudo service nfs-kernel-server start
五、客户机挂载共享目录
hadoop@ubuntu:~$
sudo mount -t nfs 192.168.3.150:/home/hadoop/share /home/hadoop/nfs
说明:在客户机的/home/hadoop目录先需要先创建nfs目录
hadoop@ubuntu:~$
sudo umount /home/hadoop/nfs //卸载
至此,nfs服务器配置基本完成,现在我们利用nfs来实现hadoop集群的ssh无密钥登录。环境如下:
namenode 192.168.3.150
slave1 192.168.3.151
slave2 192.168.3.152
现在我们nfs服务器也放在namenode机器上。
1.在集群上所有机器生成公私密钥
[hadoop@localhost ~]$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/hadoop/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in
/home/hadoop/.ssh/id_rsa.
Your public key has been saved in
/home/hadoop/.ssh/id_rsa.pub.
The key fingerprint is:
64:d8:84:a6:a6:ba:ff:39:f8:f1:79:92:74:3f:c5:95 hadoop@localhost.localdomain
The key's randomart image is:
+--[ RSA 2048]----+
| .. |
| o+ |
| o. + . |
| o o E |
| o S . . |
| . . . o |
| . ... o . . |
|. . .+o.. o |
|.o.o+.oo . |
+-----------------+
以上命令执行时,出现
Enter file in which to save the key (/home/hadoop/.ssh/id_rsa): //私钥存放的位置
Enter passphrase (empty for no passphrase): //密码
Enter same passphrase again:
我们直接输入回车即可。由以上的命令,我们知道私钥存在/home/hadoop/.ssh/id_rsa文件中,而公钥存在
/home/hadoop/.ssh/id_rsa.pub中。
2.将nfs目录挂载到集群中所有机器中。
通常我们会在共享目录创建一个ssh目录,该目录下建一个
authorized_keys文件来存所有机器的公钥。
hadoop@ubuntu:~$
sudo mount -t nfs 192.168.3.150:/home/hadoop/share /home/hadoop/nfs
3.将集群中所有机器的公钥id_rsa.pub内容追加到nfs共享目下的authorized_keys中。
hadoop@ubuntu:~/.ssh$
cat id_rsa.pub >> /home/hadoop/nfs/ssh/authorized_keys
4.
将集群中所有机器.ssh目录下
authorized_keys文件软链接到各自的
/home/hadoop/nfs/ssh/authorized_keys文件。
hadoop@ubuntu:~/.ssh$
rm authorized_keys
hadoop@ubuntu:~/.ssh$
ln -s /home/hadoop/nfs/ssh/authorized_keys authorized_keys
5.重启ssh服务
hadoop@ubuntu:~$
sudo service ssh restart
hadoop@ubuntu:~$
ssh hadoop@slave2.hadoop
The authenticity of host 'slave2.hadoop (192.168.3.152)' can't be established.
ECDSA key fingerprint is eb:a6:06:b6:d8:ed:28:95:05:e9:f8:ad:a9:48:84:12.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'slave2.hadoop,192.168.3.152' (ECDSA) to the list of known hosts.
Welcome to Ubuntu 14.04.1 LTS (GNU/Linux 3.13.0-32-generic i686)
* Documentation: https://help.ubuntu.com/
Last login: Sun Apr 5 22:10:44 2015 from 192.168.3.110
注意1:第一次登录的时候,会停在
Are you sure you want to continue connecting (yes/no)?
,此时我们必须输入yes,不能直接光输入一个enter键。
命令ssh后面也可直接跟ip地址,如下四条命令都是登入到192.168.3.152的机器
hadoop@ubuntu:~$
ssh 192.168.3.152
hadoop@ubuntu:~$
ssh slave2.hadoop
hadoop@ubuntu:~$
ssh hadoop@slave2.hadoop
hadoop@ubuntu:~$
ssh hadoop@192.168.3.152
现在我们192.168.3.151的公钥没有加入到共享文件中。
hadoop@ubuntu:~$ ssh 192.168.3.151
hadoop@192.168.3.151's password:
Connection closed by 192.168.3.151
可以发现此时需要密码。
使用nfs来管理我们集群中所有机器的公钥后,当集群中需要增加一台机器时,我们只要如下几步就可以实现集群中所有机器之间无密钥登录。
① 安装nfs客户端
② 将共享nfs目录挂载到本机
③ 生成公私密钥对
④ 将公钥追加到nfs共享目录
authorized_keys文件中
⑤ 将本机中共享目录下的
authorized_keys文件软链接到本机.ssh目录下的
authorized_keys文件。