搭建完全分布式hadoop集群

1. 创建master和3个slave

创建4个host,分别定义hostname为master、slave1、slave2、slave3,检查4个host的ip地址,确保所有主机均处于1个网关,在hosts中配置4个主机的ip地址和hostname,并确保能够相互ping通

127.0.0.1       localhost

192.168.1.100   master
192.168.1.101   slave1
192.168.1.102   slave2
192.168.1.103   slave3

2. 关闭防火墙

关闭防火墙

sudo systemctl stop firewalld.service 

禁用防火墙

sudo systemctl disable firewalld.service

查看防火墙状态

firewall-cmd --state

重启主机

3. 定义ssh免密登录

在root用户下修改 /etc/ssh/sshd_config,设置以下三项后执行 service sshd restart

RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile      .ssh/authorized_keys

确保每台主机均安装并启动了ssh服务,在master主机上生成ssh秘钥对,并将其的公钥导入到每台主机的authorized_keys中。并且在centos下,需要将authorized_keys文件的权限改为644。

4. 3台主机均创建haddoop用户,并给这个用户添加sudo权限

sudo su
useradd -m hadoop
passwd hadoop
#添加sudo权限
visudo
#在改行root ALL=(ALL)ALL下添加hadoop ALL=(ALL)ALL 保存并退出,并且换到hadoop用户
su hadoop

5. jdk及hadoop安装

安装步骤及环境变量配置同伪分布式,每台主机均需执行

6. 配置hadoop文件

先在master上配置,配置完成后再拷贝至其他主机

core-site.xml    不配置端口时默认为8020

<configuration>
  <property>
    <name>fs.default.name</name>
    <value>hdfs://master:9000</value>
  </property>
  <property>
    <name>hadoop.tmp.dir</name>
    <value>file:/home/hadoop/hadoop/tmp</value>
  </property>
</configuration>

hdfs-site.xml

注意配置的路径是否拥有读写权限,当路径配置在根目录下的文件夹时(如 /usr),需要更改该文件夹的读写权限

sudo chmod -R a+w /usr
<configuration>
  <property>
    <name>dfs.replication</name>
    <value>2</value>
  </property>
  <property>
    <name>dfs.namenode.name.dir</name>
    <value>file:/home/hadoop/hadoop/tmp/dfs/name</value>
  </property>
  <property>
    <name>dfs.datanode.data.dir</name>
    <value>file:/home/hadoop/hadoop/tmp/dfs/data</value>
  </property>
  <property>
    <name>dfs.namenode.secondary.http-address</name>
    <value>master:9001</value>
  </property>
</configuration>

mapred-site.xml

<configuration>
  <property>
    <name>mapreduce.framework.name</name>
    <value>yarn</value>
  </property>
</configuration>

yarn-site.xml

<configuration>
  <property>
    <name>yarn.resourcemanager.hostname</name>
    <value>master</value>
  </property>
  <property>
    <name>yarn.nodemanager.aux-services</name>
    <value>mapreduce_shuffle</value>
  </property>
  <property>
    <name>yarn.log-aggregation-enable</name>
    <value>true</value>
  </property>
  <property>
    <name>yarn.log-aggregation.retain-seconds</name>
    <value>604800</value>
  </property>
</configuration>

配置slaves

slave1
slave2

7. hadoop启动

同伪分布式操作

hadoop namenode -format
start-dfs.sh
start-yarn.sh

如果报permission denied,将hadoop目录权限改为 a+w

sudo chmod -R a+w /soft/hadoop

如果出现ssh连接慢或警告

Are you sure you want to continue connecting (yes/no)? The authenticity of host 's204 (192.168.242.131)' can't be established.

可以在master上对每台主机运行

ssh -o "StrictHostKeyChecking no" user@host

并修改配置文件 /etc/ssh/ssh_config

Host *
   StrictHostKeyChecking no
   UserKnownHostsFile=/dev/null

8. hadoop集群测试

运行wordcount测试hadoop集群

在当前目录下创建文件README.txt,在其中随意粘贴一段英文。

在hdfs中创建一个工作目录

hdfs dfs -mkdir -p /data/input

将当前目录下的README.txt推送到hdfs文件系统中

hdfs dfs -put README.txt /data/input

查看是否推送成功

hdfs dfs -ls /data/input

运行hadoop自带的例子

hadoop jar /soft/hadoop/share/hadoop/mapreduce/hadoop-mapreduce-examples-2.7.7.jar wordcount /data/input /data/output/result

查看运行结果

hdfs dfs -cat /data/output/result/part-r-00000

猜你喜欢

转载自blog.csdn.net/koibiki/article/details/83863566