Hadoop完全分布式的搭建
环境准备:
(1)VMware Workstation Pro17(其他也可)
(2)Centos7
(3)FinalShell
(一)模型机配置
0 ****)安装模板虚拟机,IP地址192.168.10.100 、主机名称node1 、内存4G、硬盘****40G
网络环境及基础环境准备:
1)修改VMware的网络虚拟器:将子网ip设置在统一的字段,我统一设置为10

图片1

图片2

图片3
点击确定
(2)修改电脑的VMent8:也统一在 10这个字段
在搜索栏搜索查看网络连接

图片4
找到VMnet8

图片5
右击,选择属性

图片6
单击Internet协议版本4(TCP/IPv4)

图片7
修改相应的IP地址,默认网关,和刚才虚拟机中的保持统一字段,在本文中统一为10.
添加DNS服务器如图所示

图片8
修改Centos 里的ip,将动态地址改为静态
进入到centos7,用root用户登录,修改ifcfg-ens33文件,将其中BOOTPROTO的参数从动态改为静态,并添加对应的IP,依然与刚才前两步保持统一字段

图片9
vim /etc/sysconfig/network-scripts/ifcfg-ens33

图片10
ping一下百度表示网络可用,若失败则需检查相应的问题。网络要通才可进行后续操作。

图片11
修改主机名称为Master01
vim /etc/hostname

图片12
将文件内容改成Master01
将接下来预计要进行连接的虚拟机ip都入加入到hosts文件,根据自己需要搭建的集群数量决定,本文的Master01用于伪分布式搭建,node1,node2及node3用于完全分布式的搭建,要记录对应的ip地址,以便进行后续操作。
vim /etc/hosts

图片13
(4)连接finallshell
打开准备好的远程连接软件,我们后续将都在此进行操作,因为可直接粘贴复制和传送文件(用xshell也可),此处可用root登录也可用普通用户,后续因为yarn的使用,需要用普通用户连接。根据自己的主机ip和密码连接。

图片14
连接成功

图片15
(5)更换阿里云镜像:执行以下命令
a.备份官方的原yum源的配置
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
b.下载Centos-7.repo文件
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
c.脚本配置本地源
找个目录,创建脚本文件:
本人以/root/目录为例
vi /root/auto_aliyun.sh
编写如下内容:
#!/bin/bash
# 备份官方的原yum源的配置
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
# 下载Centos-7.repo文件,或者curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo,取决于你是否有wget的命令
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
# 清除yum缓存
yum clean all
# 缓存本地yum源
yum makecache
按ESC。输入:wq保存,完成镜像阿里云源的脚本文件的编写
d.赋予脚本可执行的权限
chmod +x /root/auto_aliyun.sh
e.执行脚本文件,即可
/root/auto_aliyun.sh

图片16

图片17
f.安装epel-release
yum install -y epel-release

图片18
(6)关闭防火墙
systemctl stop firewalld
systemctl disable firewalld.service

图片19
(7)将hduser用户(你自己的普通用户)赋予root权限
创建hduser用户,并修改hduser用户的密码
useradd hduser(创建的用户名)
passwd 123456(创建的用户密码)
配置hduser用户具有root权限,方便后期加sudo执行root权限的命令
vim /etc/sudoers
找到图中位置添加:

图片20
保存并退出
在/opt目录下创建module、software文件夹
mkdir /opt/module
mkdir /opt/software
修改module、software文件夹的所有者和所属组均为hduser用户
chown hduser:hduser /opt/module
chown hduser:hduser /opt/software
查看module、software文件夹的所有者和所属组

图片21
测试hduser已拥有root权限

图片22
(8)卸载环境自带jdk
查询jdk
jdk rpm -qa | grep -i java

图片23
卸载jdk
rpm -qa | grep -i java | xargs -n1 rpm -e --nodeps
注意:如果你的虚拟机是最小化安装不需要执行这一步。
- rpm -qa:查询所安装的所有rpm软件包
- grep -i:忽略大小写
- xargs -n1:表示每次只传递一个参数
- rpm -e --nodeps:强制卸载软件
查询

图片24
重启虚拟机
[root@Master01?~]# reboot
模型机配置完成
(二)完全分布式搭建
(1)三台虚拟机node1,node2,node3的准备
克隆三台虚拟node1,node2,node3(名称要与前文hosts中一致,注意是完全克隆)

图片25
分别修改三台机器的ip和主机名并重启,检查是否修改完成,并检查网络是否可用
Node1

图片26

图片27
Node2

图片28

图片29
Node3

图片30

图片31
将三台主机均按前面方法连接finalshell

图片32

图片33

图片34
(2)给node1安装jdk
将下好的jdk和hadoop利用finallshell传到node1中,在finallshell的文件中找到之前建立的software文件夹,直接拖入。

图片35
进入到software文件夹,查看tar包已经存在。

图片36
解压缩:
tar -zxvf jdk-8u202-linux-x64.tar.gz -C /opt/module/

图片37
进入到module目录,已经存在jdk。

图片38
(3)配置JAVA_HOME
在/etc/profile.d/中创建my_env.sh,填写以下内容:
vim /etc/profile.d/my_env.sh

图片39
保存后运行,java配置成功。
source /etc/profile
执行java语句出现以下内容

图片40
(4)安装hadoop:
解压hadoop至/etc/module
tar -zxvf hadoop-3.2.4.tar.gz -C /opt/module/

图片41
进入到hadoop中配置环境变量

图片42
在之前创建的my_env.sh中添加以下内容
vim /etc/profile.d/my_env.sh

图片43
source /etc/profile?
运行
命令行输入hadoop,出现以下内容配置完成

图片44
(5)将node1中的jdk和hadoop复制到node2和node3上

图片45

图片46
集群分配脚本的编写:
创建一个脚本xsync,并将其配置在环境变量中,内容如下
注意是在hduser用户的bin目录下:

图片47
建立一个xsync文件,输入以下内容保存
#!/bin/bash
#1. 判断参数个数
if?[?$#?-lt?1?]
then
????echo?Not?Enough?Arguement!
????exit;
fi
#2. 遍历集群所有机器
for?host?in?node1?node2?node3
do
????echo?====================??$host??====================
????#3. 遍历所有目录,挨个发送
????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
注意以下内容修改成自己对应的主机名字

图片48
进入到相应的目录,找到相应的bin目录,查询刚才建立的xsync文件,注意是hduser下的bin目录。

图片49
vim xsync

图片50
赋予xsync相应权限
chomd 777 xsync
发现文件变绿

图片51
利用xsync脚本将环境变量分发到node2和node3中
sudo ./bin/xsync /etc/profile.d/my_env.sh

图片52
让环境变量生效
[hduser@node2?bin]$ source /etc/profile
[hduser@node2?opt]$ source /etc/profile
-
ssh免密登录设置
cd .ssh/
cat known_hosts

图片53
[hduser@node1?.ssh]$ssh-keygen -t rsa?
然后敲(三个回车),就会生成两个文件 id_rsa(私钥)、id_rsa.pub(公钥)

图片54
将公钥拷贝到要免密登录的目标机器上

图片55
ssh node2后免密登录成功

图片56
第三台机器同理

图片57
(三)配置集群
(1)核心配置文件
cd $HADOOP_HOME/etc/hadoop
vim core-site.xml

图片58
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://node1:8020</value>
</property>
<!-- 指定 hadoop 数据的存储目录 -->
<property>
<name>hadoop.tmp.dir</name>
<value>/opt/module/hadoop-3.2.4/data</value>
</property>
<!-- 配置 HDFS 网页登录使用的静态用户为 atguigu -->
<property>
<name>hadoop.http.staticuser.user</name>
<value>hduser</value>
</property>
</configuration>

图片59
(2)HDFS 配置文件
配置 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>node1:9870</value>
</property>
<!-- 2nn web 端访问地址-->
<property>
<name>dfs.namenode.secondary.http-address</name>
<value>node3:9868</value>
</property>
</configuration>

图片60
(3)YARN 配置文件
配置 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>node2</value>
</property>
<!-- 环境变量的继承 -->
<property>
<name>yarn.nodemanager.env-whitelist</name>
<value>JAVA_HOME,HADOOP_COMMON_HOME,HADOOP_HDFS_HOME,HADOOP_CO
NF_DIR,CLASSPATH_PREPEND_DISTCACHE,HADOOP_YARN_HOME,HADOOP_MAP
RED_HOME</value>
</property>
</configuration>

图片61
(4)MapReduce 配置文件
配置 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>

图片62
****5)****在集群上分发配置好的 Hadoop 配置文件
xsync /opt/module/hadoop- 3.2.4/etc/hadoop/

图片63
6)去 node2和 node3上查看文件分发情况
cat /opt/module/hadoop-3.2.4/etc/hadoop/core-site.xml
编辑workers文件
vim?/opt/module/hadoop-3.2.4/etc/hadoop/workers
在该文件中增加如下内容:

图片64
注意:该文件中添加的内容结尾不允许有空格,文件中不允许有空行。
同步所有节点配置文件
xsync /opt/module/hadoop-3.2.4/etc
启动集群
(1)如果集群是第一次启动,需要在node1节点格式化NameNode(注意:格式化NameNode,会产生新的集群id,导致NameNode和DataNode的集群id不一致,集群找不到已往数据。如果集群在运行过程中报错,需要重新格式化NameNode的话,一定要先停止namenode和datanode进程,并且要删除所有机器的data和logs目录,然后再进行格式化。)
[hduser@node1?hadoop-3.2.4]$ hdfs namenode -format

图片65
(2)启动HDFS
[hduser@node1?hadoop-3.2.4]$ sbin/start-dfs.sh

图片66
(3)****在配置了ResourceManager的节点(node2)****启动YARN
[hduser@node1?hadoop-3.2.4]$ sbin/start-yarn.sh
Web端查看HDFS的NameNode
浏览器中输入:http://node1:9870
(4)查看HDFS上存储的数据信息
Web端查看YARN的ResourceManager
浏览器中输入:http://node2:8088
(b)查看YARN上运行的Job信息
验证HDFS是否成功启动,可以使用jps命令查看Java进程,确认NameNode和DataNode等进程是否运行。

图片67

图片68

图片69
完全分布式搭建完成。