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