一、搭建
1、準備插件
yum install -y epel-release
yum install -y psmisc nc net-tools rsync vim lrzsz ntp libzstd openssl-static tree iotop git
2、關(guān)閉防火墻
systemctl stop firewalld
systemctl disable firewalld
3、創(chuàng)建用戶
useradd bigdata
passwd bigdata
4、配置用戶權(quán)限
vim /etc/sudoers
## Allow root to run any commands anywhere
root ALL=(ALL) ALL
bigdata ALL=(ALL) NOPASSWD:ALL
5、在/opt目錄下創(chuàng)建文件夾,并修改所屬主和所屬組
mkdir /opt/module
mkdir /opt/software
chown bigdata:bigdata /opt/module
chown bigdata:bigdata /opt/software
6、卸載虛擬機自帶的open JDK
rpm -qa | grep -i JAVA | xargs -n1 rpm -e --nodeps
7、修改克隆虛擬機的靜態(tài)IP
vim /etc/sysconfig/network-scripts/ifcfg-ens33
改成
DEVICE=ens33
TYPE=Ethernet
ONBOOT=yes
BOOTPROTO=static
NAME="ens33"
IPADDR=192.168.1.102
PREFIX=24
GATEWAY=192.168.1.2
DNS1=192.168.1.2
8、查看linux虛擬機的虛擬網(wǎng)絡(luò)編輯器,編輯->虛擬網(wǎng)絡(luò)編輯器->VMnet8
9、修改克隆機主機名
hostnamectl --static set-hostname bigdata
vim /etc/hostname
bigdata
10、配置linux克隆機主機名稱映射hosts文件,打開/etc/hosts
vim /etc/hosts
192.168.1.102 bigdata
11、如果操作系統(tǒng)是window10,先拷貝出來,修改保存以后,再覆蓋即可
(a)進入C:windowsSystem32driversetc路徑
(b)拷貝hosts文件到桌面
(c)打開桌面hosts文件并添加如下內(nèi)容
192.168.1.102 bigdata
12、安裝jdk
1、在Linux系統(tǒng)下的opt目錄中查看軟件包是否導(dǎo)入成功
ls /opt/software/
hadoop-3.1.3.tar.gz jdk-8u212-linux-x64.tar.gz
2、解壓
tar -zxvf jdk-8u212-linux-x64.tar.gz -C /opt/module/
3、配置JDK環(huán)境變量
vim /etc/profile.d/my_env.sh
#JAVA_HOME
export JAVA_HOME=/opt/module/jdk1.8.0_212
export PATH=$PATH:$JAVA_HOME/bin
4、加載環(huán)境變量
source /etc/profile
5、測試JDK是否安裝成功
java -version
13、安裝hadoop
1、解壓
tar -zxvf hadoop-3.1.3.tar.gz -C /opt/module/
ls /opt/module/
hadoop-3.1.3
2、配置環(huán)境
vim /etc/profile.d/my_env.sh
#HADOOP_HOME
export HADOOP_HOME=/opt/module/hadoop-3.1.3
export PATH=$PATH:$HADOOP_HOME/bin
export PATH=$PATH:$HADOOP_HOME/sbin
source /etc/profile
3、測試是否安裝成功
hadoop version
Hadoop 3.1.3
14、本地模式
1、創(chuàng)建在hadoop-3.1.3文件下面創(chuàng)建一個wcinput文件夾
mkdir wcinput
2、在wcinput文件下創(chuàng)建一個word.txt文件
cd wcinput
vim word.txt
hadoop yarn
hadoop mapreduce
3、回到Hadoop目錄/opt/module/hadoop-3.1.3
hadoop jar share/hadoop/mapreduce/hadoop-mapreduce-examples-3.1.3.jar wordcount wcinput wcoutput
4、驗證結(jié)果
cat wcoutput/part-r-00000
hadoop 2
mapreduce 1
yarn 1
15、完全分布式運行模式
1、分發(fā)jdk和hadoop到bigdata1和bigdata2
scp -r /opt/module/jdk1.8.0_212 bigdata@bigdata1:/opt/module
scp -r /opt/module/hadoop-3.1.3 bigdata@bigdata1:/opt/module
2、xsync集群分發(fā)腳本
cd /home/bigdata
mkdir bin
cd bin
vim xsync
#!/bin/bash
#1. 判斷參數(shù)個數(shù)
if [ $# -lt 1 ]
then
echo Not Enough Arguement!
exit;
fi
#2. 遍歷集群所有機器
for host in bigdata bigdata1 bigdata2
do
echo ==================== $host ====================
#3. 遍歷所有目錄,挨個發(fā)送
for file in $@
do
#4. 判斷文件是否存在
if [ -e $file ]
then
#5. 獲取父目錄
pdir=$(cd -P $(dirname $file); pwd)
#6. 獲取當前文件的名稱
fname=$(basename $file)
ssh $host "mkdir -p $pdir"
rsync -av $pdir/$fname $host:$pdir
else
echo $file does not exists!
fi
done
done
chmod +x xsync
cp xsync /bin/
xsync /home/bigdata/bin
3、無密登陸
ssh-keygen -t rsa
一直下一步
將公鑰拷貝到要免密登錄的目標機器上
ssh-copy-id bigdata
ssh-copy-id bigdata1
ssh-copy-id bigdata2
4、配置集群
1、配置core-site.xml
cd $HADOOP_HOME/etc/hadoop
vim core-site.xml
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<configuration>
<!-- 指定NameNode的地址 -->
<property>
<name>fs.defaultFS</name>
<value>hdfs://bigdata:9820</value>
</property>
<!-- 指定hadoop數(shù)據(jù)的存儲目錄 -->
<property>
<name>hadoop.tmp.dir</name>
<value>/opt/module/hadoop-3.1.3/data</value>
</property>
<!-- 配置HDFS網(wǎng)頁登錄使用的靜態(tài)用戶為atguigu -->
<property>
<name>hadoop.http.staticuser.user</name>
<value>bigdata</value>
</property>
<!-- 配置該bigdata(superUser)允許通過代理訪問的主機節(jié)點 -->
<property>
<name>hadoop.proxyuser.bigdata.hosts</name>
<value>*</value>
</property>
<!-- 配置該bigdata(superUser)允許通過代理用戶所屬組 -->
<property>
<name>hadoop.proxyuser.bigdata.groups</name>
<value>*</value>
</property>
<!-- 配置該atguigu(superUser)允許通過代理的用戶-->
<property>
<name>hadoop.proxyuser.bigdata.groups</name>
<value>*</value>
</property>
</configuration>
2、配置hdfs-site.xml
vim hdfs-site.xml
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<configuration>
<!-- nn web端訪問地址-->
<property>
<name>dfs.namenode.http-address</name>
<value>bigdata:9870</value>
</property>
<!-- 2nn web端訪問地址-->
<property>
<name>dfs.namenode.secondary.http-address</name>
<value>bigdata1:9868</value>
</property>
</configuration>
3、配置yarn-site.xml
vim yarn-site.xml
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<configuration>
<!-- 指定MR走shuffle -->
<property>
<name>yarn.nodemanager.aux-services</name>
<value>mapreduce_shuffle</value>
</property>
<!-- 指定ResourceManager的地址-->
<property>
<name>yarn.resourcemanager.hostname</name>
<value>bigdata2</value>
</property>
<!-- 環(huán)境變量的繼承 -->
<property>
<name>yarn.nodemanager.env-whitelist</name>
<value>JAVA_HOME,HADOOP_COMMON_HOME,HADOOP_HDFS_HOME,HADOOP_CONF_DIR,CLASSPATH_PREPEND_DISTCACHE,HADOOP_YARN_HOME,HADOOP_MAPRED_HOME</value>
</property>
<!-- yarn容器允許分配的最大最小內(nèi)存 -->
<property>
<name>yarn.scheduler.minimum-allocation-mb</name>
<value>512</value>
</property>
<property>
<name>yarn.scheduler.maximum-allocation-mb</name>
<value>4096</value>
</property>
<!-- yarn容器允許管理的物理內(nèi)存大小 -->
<property>
<name>yarn.nodemanager.resource.memory-mb</name>
<value>4096</value>
</property>
<!-- 關(guān)閉yarn對物理內(nèi)存和虛擬內(nèi)存的限制檢查 -->
<property>
<name>yarn.nodemanager.pmem-check-enabled</name>
<value>false</value>
</property>
<property>
<name>yarn.nodemanager.vmem-check-enabled</name>
<value>false</value>
</property>
</configuration>
4、配置mapred-site.xml
vim mapred-site.xml
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<configuration>
<!-- 指定MapReduce程序運行在Yarn上 -->
<property>
<name>mapreduce.framework.name</name>
<value>yarn</value>
</property>
</configuration>
5、在集群上分發(fā)配置好的Hadoop配置文件
xsync /opt/module/hadoop-3.1.3/etc/hadoop/
5、群起集群
1、配置workers
vim /opt/module/hadoop-3.1.3/etc/hadoop/workers
bigdata
bigdata1
bigdata2
2、格式化hadoop
hdfs namenode -format
3、啟動hdfs
sbin/start-dfs.sh
4、啟動yarn
sbin/start-yarn.sh
5、瀏覽器驗證
#hdfs
http://bigdata:9870
#yarn
http://bigdata1:8088
6、錯誤總結(jié)
1、hdfs啟動失敗
在/hadoop/sbin路徑下:
將start-dfs.sh,stop-dfs.sh兩個文件頂部添加以下參數(shù)
#!/usr/bin/env bash
HDFS_DATANODE_USER=root
HADOOP_SECURE_DN_USER=hdfs
HDFS_NAMENODE_USER=root
HDFS_SECONDARYNAMENODE_USER=root
2、yarn啟動失敗
在/hadoop/sbin路徑下:
還有,start-yarn.sh,stop-yarn.sh頂部也需添加以下:
#!/usr/bin/env bash
YARN_RESOURCEMANAGER_USER=root
HADOOP_SECURE_DN_USER=yarn
YARN_NODEMANAGER_USER=root
7、集群測試
1、上傳
#小文件
hadoop fs -mkdir /input
hadoop fs -put $HADOOP_HOME/wcinput/word.txt /input
#大文件
hadoop fs -put /opt/software/jdk-8u212-linux-x64.tar.gz /
#驗證
pwd
/opt/module/hadoop-3.1.3/data/dfs/data/current/BP-938951106-192.168.10.107-1495462844069/current/finalized/subdir0/subdir0
cat blk_1073741825
hadoop yarn
hadoop mapreduce
#拼接
cat blk_1073741836>>tmp.tar.gz
cat blk_1073741837>>tmp.tar.gz
tar -zxvf tmp.tar.gz
2、下載
hadoop fs -get /jdk-8u212-linux-x64.tar.gz ./
3、執(zhí)行wordcount程序
hadoop jar share/hadoop/mapreduce/hadoop-mapreduce-examples-3.1.3.jar wordcount /input /output
8、配置歷史服務(wù)器
為了查看程序的歷史運行情況,需要配置一下歷史服務(wù)器
1、配置mapred-site.xml
vim mapred-site.xml
<!-- 歷史服務(wù)器端地址 -->
<property>
<name>mapreduce.jobhistory.address</name>
<value>bigdata:10020</value>
</property>
<!-- 歷史服務(wù)器web端地址 -->
<property>
<name>mapreduce.jobhistory.webApp.address</name>
<value>bigdata:19888</value>
</property>
2、分發(fā)配置
xsync $HADOOP_HOME/etc/hadoop/mapred-site.xml
3、啟動歷史服務(wù)器
mapred --daemon start historyserver
4、驗證
http://bigdata:19888/jobhistory
9、配置日志聚集
日志聚集概念:應(yīng)用運行完成以后,將程序運行日志信息上傳到HDFS系統(tǒng)上。
日志聚集功能好處:可以方便的查看到程序運行詳情,方便開發(fā)調(diào)試。
注意:開啟日志聚集功能,需要重新啟動NodeManager 、ResourceManager和HistoryServer。
1、配置yarn-site.xml
vim yarn-site.xml
<!-- 開啟日志聚集功能 -->
<property>
<name>yarn.log-aggregation-enable</name>
<value>true</value>
</property>
<!-- 設(shè)置日志聚集服務(wù)器地址 -->
<property>
<name>yarn.log.server.url</name>
<value>http://bigdata:19888/jobhistory/logs</value>
</property>
<!-- 設(shè)置日志保留時間為7天 -->
<property>
<name>yarn.log-aggregation.retain-seconds</name>
<value>604800</value>
</property>
2、分發(fā)配置
xsync $HADOOP_HOME/etc/hadoop/yarn-site.xml
3、關(guān)閉NodeManager、ResourceManager和HistoryServer
stop-yarn.sh
mapred --daemon stop historyserver
4、啟動NodeManager、ResourceManage和HistoryServer
start-yarn.sh
mapred --daemon start historyserver
5、刪除HDFS上已經(jīng)存在的輸出文件
hadoop fs -rm -r /output
6、執(zhí)行WordCount程序
hadoop jar $HADOOP_HOME/share/hadoop/mapreduce/hadoop-mapreduce-examples-3.1.3.jar wordcount /input /output
7、查看日志
http://bigdata:19888/jobhistory
10、集群腳本編寫
1、查看java進程腳本
cd /home/bigdata/bin
vim jpsall
#!/bin/bash
for host in bigdata bigdata bigdata
do
echo =============== $host ===============
ssh $host jps $@ | grep -v Jps
done
chmod +x jpsall
2、hadoop進程腳本
vim myhadoop.sh
#!/bin/bash
if [ $# -lt 1 ]
then
echo "No Args Input..."
exit ;
fi
case $1 in
"start")
echo " =================== 啟動 hadoop集群 ==================="
echo " --------------- 啟動 hdfs ---------------"
ssh bigdata "/opt/module/hadoop-3.1.3/sbin/start-dfs.sh"
echo " --------------- 啟動 yarn ---------------"
ssh bigdata1 "/opt/module/hadoop-3.1.3/sbin/start-yarn.sh"
echo " --------------- 啟動 historyserver ---------------"
ssh bigdata "/opt/module/hadoop-3.1.3/bin/mapred --daemon start historyserver"
;;
"stop")
echo " =================== 關(guān)閉 hadoop集群 ==================="
echo " --------------- 關(guān)閉 historyserver ---------------"
ssh bigdata "/opt/module/hadoop-3.1.3/bin/mapred --daemon stop historyserver"
echo " --------------- 關(guān)閉 yarn ---------------"
ssh bigdata1 "/opt/module/hadoop-3.1.3/sbin/stop-yarn.sh"
echo " --------------- 關(guān)閉 hdfs ---------------"
ssh bigdata "/opt/module/hadoop-3.1.3/sbin/stop-dfs.sh"
;;
*)
echo "Input Args Error..."
;;
esac
chmod +x myhadoop.sh
xsync /home/atguigu/bin/