1 Hadoop 简介
1.1 优势
1)高可靠性:Hadoop 底层维护多个数据副本,所以即使Hadoop某个计算元素或存储出现故障,也不会导致数据的丢失。
2)高扩展性:在集群间分配任务数据,可方便的扩展数以千计的节点。
3)高效性:在MapReduce 的思想下,Hadoop 是并行工作的,以加快任务处理速度。
4)高容错性:能够自动将失败的任务重新分配。
1.2 组成
在 Hadoop1.x 时 代,Hadoop 中 的 MapReduce 同时处理业务逻辑运算和资源的调度,耦合性较大。
在 Hadoop2.x 时代,增加了 Yarn。Yarn 只负责资 源 的 调 度,MapReduce 只负责运算。
Hadoop3.x 在组成上没有变化。
1.1.1 HDFS 架构概述
1)NameNode(NN):存储文件的元数据,如文件名,文件目录结构,文件属性(生成时间、副本数、文件权限),以及每个文件的块列表和块所在的 DataNode 等。
2)DataNode(DN):在本地文件系统存储文件块数据,以及块数据的校验和。
3)Secondary NameNode(2NN):每隔一段时间对 NameNode 元数据备份。
1.1.2 YARN 架构概述
Yet Another Resource Negotiator 简称 YARN,另一种资源协调者,是 Hadoop 的资源管理器。
1)ResourceManager(RM):整个集群资源(内存、CPU等)的老大。
3)ApplicationMaster(AM):单个任务运行的老大。
2)NodeManager(NM):单个节点服务器资源老大。
4)Container:容器,相当一台独立的服务器,里面封装了任务运行所需要的资源,如内存、CPU、磁盘、网络等。
说明:
- 客户端可以有多个
- 集群上可以运行多个ApplicationMaster
- 每个NodeManager上可以有多个Container
1.1.3 MapReduce 架构概述
MapReduce 将计算过程分为两个阶段:Map 和 Reduce。
1)Map 阶段并行处理输入数据。
2)Reduce 阶段对 Map 结果进行汇总。
1.1.4 HDFS,YARN,MapReduce 三者关系
2 Hadoop 运行环境搭建
2.1 模板虚拟机环境准备
2.1.1 安装模板虚拟机
IP 地址 192.168.10.100,主机名称 hadoop102,内存 4G,硬盘 50G。
1)新建一个虚拟机
2)自定义新的虚拟机
3)选择虚拟机将来要安装的系统
4)配置电脑的名称和存放位置
5)配置电脑的 CPU 个数
有个原则就是选满(跟物理机的CPU个数相同,但是不能超过)。
(1)查看物理机 CPU 个数
(2)设置虚拟机处理器个数
(3)内存大小有一定要求,建议4G,不能给太多,后期会有多台虚拟机同时启动
6)选择虚拟即上网方式
选择 NAT 的方式
7)选择对应的文件系统的 IO 方式
8)选择磁盘类型
9)选择磁盘种类和磁盘大小
10)虚拟机文件的存放位置
11)完成配置
12)安装系统
(1)选择要安装的系统镜像文件
(2)开始安装
(3)选择语言
(4)配置时间
(5)最小化安装系统
(6)配置磁盘分区
(7)手动添加分区
添加 1G 容量的 boot 区
添加 swap 4g 分区
根目录作为存储使用,将剩下的空间都给他(50 - 4 - 1)= 45
点击分区完成
(8)关闭 kdump,减少内存消耗。
(9)修改主机名
(10)开始安装
(11)安装时间比较长(设置root用户密码,一定要设置)
(12)安装完成后,重启虚拟机
13)配置网络
对安装好的 VMware 进行网络配置,方便虚拟机连接网络,本次设置建议选择 NAT 模式,需要宿主机的 Windows 和虚拟机的 Linux 能够进行网络连接,同时虚拟机的 Linux 可以通过宿主机的 Windows 进入互联网。
(1)编辑 VMware 的网络配置
然后全部点击确定,VMware的网络配置就好了。
(2)Windows 的网络配置
双击
修改 ipv4
按如上格式修改IP信息(地址,网关,DNS服务器),修改完毕后全部点击确定退出。
(3)修改虚拟机网络 IP 配置
(4)执行systemctl restart network命令,重启网络服务。
[root@hadoop100 ~]# systemctl restart network
(5)修改主机名
[root@hadoop100 ~]# vim /etc/hostname
hadoop100
(6)配置 hosts 文件
[root@hadoop100 ~]# vim /etc/hosts
192.168.10.100 hadoop100
192.168.10.101 hadoop101
192.168.10.102 hadoop102
192.168.10.103 hadoop103
192.168.10.104 hadoop104
192.168.10.105 hadoop105
192.168.10.106 hadoop106
192.168.10.107 hadoop107
192.168.10.108 hadoop108
(7)重启
[root@hadoop100 ~]# reboot
(8)修改 Windows 的 hosts 文件
进入 C:\Windows\System32\drivers\etc 路径;
打开hosts文件并添加如下内容,然后保存:
192.168.10.100 hadoop100
192.168.10.101 hadoop101
192.168.10.102 hadoop102
192.168.10.103 hadoop103
192.168.10.104 hadoop104
192.168.10.105 hadoop105
192.168.10.106 hadoop106
192.168.10.107 hadoop107
192.168.10.108 hadoop108
2.1.2 安装必要软件
1)安装 epel-release:Extra Packages for Enterprise Linux是为“红帽系”的操作系统提供额外的软件包,适用于RHEL、CentOS和Scientific Linux。相当于是一个软件仓库,大多数rpm包在官方 repository 中是找不到的)。
[root@hadoop100 ~]# yum install -y epel-release
2)如果Linux安装的是最小系统版,还需要安装如下工具;如果安装的是Linux桌面标准版,不需要执行如下操作
- net-tool:工具包集合,包含ifconfig等命令
[root@hadoop100 ~]# yum install -y net-tools
- vim:编辑器
[root@hadoop100 ~]# yum install -y vim
2.1.3 关闭防火墙
[root@hadoop100 ~]# systemctl stop firewalld
[root@hadoop100 ~]# systemctl disable firewalld.service
2.1.4 配置自定义用户具 mkdif 权限的命令
[root@hadoop100 ~]# vim /etc/sudoers
## Allow root to run any commands anywhere
root ALL=(ALL) ALL
## Allows members of the 'sys' group to run networking, software,
## service management apps and more.
# %sys ALL = NETWORKING, SOFTWARE, SERVICES, STORAGE, DELEGATING, PROCESSES, LOCATE, DRIVERS
## Allows people in group wheel to run all commands
%wheel ALL=(ALL) ALL
# 添加一行
liyibin ALL=(ALL) NOPASSWD:ALL
## Same thing without a password
# %wheel ALL=(ALL) NOPASSWD: ALL
2.1.5 在 /opt 目录下创建文件夹,并修改所属主和所属组
1)在/opt目录下创建module、software文件夹
[root@hadoop100 ~]# mkdir /opt/module
[root@hadoop100 ~]# mkdir /opt/software
2)修改module、software文件夹的所有者和所属组均为自定义用户
[root@hadoop100 ~]# chown liyibin:liyibin /opt/module
[root@hadoop100 ~]# chown liyibin:liyibin /opt/software
2.1.6 卸载虚拟机自带的 JDK 后重启虚拟机
[root@hadoop100 ~]# rpm -qa | grep -i java | xargs -n1 rpm -e --nodeps
[root@hadoop100 ~]# reboot
2.2 克隆虚拟机
1)利用模板机 hadoop100,克隆三台虚拟机:hadoop102 hadoop103 hadoop104。
2)修改克隆机IP,以下以 hadoop102 举例说明。
(1)修改克隆虚拟机的静态IP
[root@hadoop100 ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33
TYPE="Ethernet"
PROXY_METHOD="none"
BROWSER_ONLY="no"
BOOTPROTO="static"
DEFROUTE="yes"
IPV4_FAILURE_FATAL="no"
IPV6INIT="yes"
IPV6_AUTOCONF="yes"
IPV6_DEFROUTE="yes"
IPV6_FAILURE_FATAL="no"
IPV6_ADDR_GEN_MODE="stable-privacy"
NAME="ens33"
UUID="84251f4a-50b7-41df-9dc2-6682548df157"
DEVICE="ens33"
ONBOOT="yes"
IPADDR=192.168.10.102
GATEWAY=192.168.10.2
DNS1=192.168.10.2
(2)修改克隆机的主机名
[root@hadoop100 ~]# vim /etc/hostname
hadoop102
(3)重启克隆机
[root@hadoop100 ~]# reboot
2.3 在 hadoop102 安装JDK
1)卸载现有 JDK。参照步骤 2.1.6。
2)将 JDK 导入到 /opt/software 目录进行安装。
[liyibin@hadoop102 ~]# cd /opt/software/
[liyibin@hadoop102 software]$ tar -zxvf jdk-8u212-linux-x64.tar.gz -C /opt/module/
3)配置环境变量
(1)新建配置文件
[liyibin@hadoop102 ~]$ sudo vim /etc/profile.d/my_env.sh
# 添加如下内容
#JAVA_HOME
export JAVA_HOME=/opt/module/jdk1.8.0_212
export PATH=$PATH:$JAVA_HOME/bin
(2)保存退出
(3)让新的环境变量生效
[liyibin@hadoop102 ~]$ source /etc/profile
4)测试 JDK 是否安装成功
[liyibin@hadoop102 ~]$ java -version
java version "1.8.0_212"
Java(TM) SE Runtime Environment (build 1.8.0_212-b10)
Java HotSpot(TM) 64-Bit Server VM (build 25.212-b10, mixed mode)
2.4 在 hadoop102 安装 Hadoop
Hadoop下载地址:https://archive.apache.org/dist/hadoop/common/hadoop-3.1.3/
1)将压缩包 hadoop-3.1.3.tar.gz 导入到 /opt/software 下
2)解压缩并安装
[liyibin@hadoop102 ~]$ cd /opt/software/
[liyibin@hadoop102 module]$ tar -zxvf hadoop-3.1.3.tar.gz -C /opt/module/
3)将 hadoop 添加到环境变量
[liyibin@hadoop102 ~]$ sudo vim /etc/profile.d/my_env.sh
# 添加如下内容
#HADOOP_HOME
export HADOOP_HOME=/opt/module/hadoop-3.1.3
export PATH=$PATH:$HADOOP_HOME/sbin:$HADOOP_HOME/bin
(2)保存退出
(3)让新的环境变量生效
[liyibin@hadoop102 ~]$ source /etc/profile
4)测试是否安装成功
[liyibin@hadoop102 ~]$ hadoop version
Hadoop 3.1.3
Source code repository https://gitbox.apache.org/repos/asf/hadoop.git -r ba631c436b806728f8ec2f54ab1e289526c90579
Compiled by ztang on 2019-09-12T02:47Z
Compiled with protoc 2.5.0
From source with checksum ec785077c385118ac91aadde5ec9799
This command was run using /opt/module/hadoop-3.1.3/share/hadoop/common/hadoop-common-3.1.3.jar
5)重启(如果Hadoop命令不能用再重启虚拟机)
[liyibin@hadoop102 ~]$ reboot
2.5 Hadoop 目录结构
1)查看 Hadoop 目录结构
[liyibin@hadoop102 hadoop-3.1.3]$ ll
总用量 180
drwxr-xr-x. 2 liyibin liyibin 183 9月 12 2019 bin
drwxrwxr-x. 4 liyibin liyibin 37 10月 7 20:12 data
drwxr-xr-x. 3 liyibin liyibin 20 9月 12 2019 etc
drwxr-xr-x. 2 liyibin liyibin 106 9月 12 2019 include
drwxr-xr-x. 3 liyibin liyibin 20 9月 12 2019 lib
drwxr-xr-x. 4 liyibin liyibin 288 9月 12 2019 libexec
-rw-rw-r--. 1 liyibin liyibin 147145 9月 4 2019 LICENSE.txt
drwxrwxr-x. 3 liyibin liyibin 4096 10月 24 17:37 logs
-rw-rw-r--. 1 liyibin liyibin 21867 9月 4 2019 NOTICE.txt
-rw-rw-r--. 1 liyibin liyibin 1366 9月 4 2019 README.txt
drwxr-xr-x. 3 liyibin liyibin 4096 9月 12 2019 sbin
drwxr-xr-x. 4 liyibin liyibin 31 9月 12 2019 share
drwxrwxr-x. 2 liyibin liyibin 22 10月 5 16:43 wcinput
drwxr-xr-x. 2 liyibin liyibin 88 10月 5 16:44 wcoutput
2)重要目录
(1)bin 目录:存放对 Hadoop 相关服务(hdfs,yarn,mapred)进行操作的脚本。
(2)etc 目录:Hadoop 的配置文件目录,存放Hadoop的配置文件。
(3)lib 目录:存放 Hadoop 的本地库(对数据进行压缩解压缩功能)。
(4)sbin 目录:存放启动或停止 Hadoop 相关服务的脚本。
(5)share 目录:存放 Hadoop 的依赖jar包、文档、和官方案例。
3 Hadoop 运行模式
1)Hadoop官方网站:http://hadoop.apache.org/
2)Hadoop运行模式包括:本地模式、伪分布式模式以及完全分布式模式。
(1)本地模式:单机运行,只是用来演示一下官方案例。生产环境不用。
(2)伪分布式模式:也是单机运行,但是具备 Hadoop 集群的所有功能,一台服务器模拟一个分布式的环境。个别缺钱的公司用来测试,生产环境不用。
(3)完全分布式模式:多台服务器组成分布式环境。生产环境使用。
3.1 完全分布式运行模式搭建
3.1.1 运行环境搭建
虚拟机配置见上一章。
3.1.2 编写集群分发脚本
1)scp(secure copy) 安全拷贝
(1)scp 定义:scp可以实现服务器与服务器之间的数据拷贝。(from server1 to server2)。
(2)基本语法:scp -r
p
d
i
r
/
pdir/
pdir/fname
u
s
e
r
@
user@
user@host:
p
d
i
r
/
pdir/
pdir/fname
命令 递归 要拷贝的文件路径/名称 目的地用户@主机:目的地路径/名称
(3)从 hadoop102 拷贝 JDK,Hadoop 到 hadoop103 和 hadoop104.。
需要在 hadoop103 和 hadoop104 已经创建好的/opt/module、/opt/software两个目录,并且已经把这两个目录修改为liyibin:liyibin。
[liyibin@hadoop102 hadoop-3.1.3]$ scp -r /opt/module/* liyibin@hadoop103:/opt/module
[liyibin@hadoop102 hadoop-3.1.3]$ scp -r /opt/module/* liyibin@hadoop104:/opt/module
2)rsync 远程同步工具
rsync 主要用于备份和镜像。具有速度快、避免复制相同内容和支持符号链接的优点。
rsync 和scp 区别:用 rsync 做文件的复制要比 scp 的速度快,rsync 只对差异文件做更新。scp 是把所有文件都复制过去。
(1)基本语法:
rsync -av
p
d
i
r
/
pdir/
pdir/fname
u
s
e
r
@
user@
user@host:
p
d
i
r
/
pdir/
pdir/fname
命令 选项参数 要拷贝的文件路径/名称 目的地用户@主机:目的地路径/名称
参数说明:
-a 归档拷贝
-v 显示复制过程
3)xsync 集群分发脚本
(1)需求:循环复制文件到所有节点的相同目录下
(2)脚本实现
[liyibin@hadoop102 bin]$ cd /home/liyibin/bin/
[liyibin@hadoop102 bin]$ vim xsync
#!/bin/bash
#1. 判断参数个数
if [ $# -lt 1 ]
then
echo Not Enough Arguement!
exit;
fi
#2. 遍历集群所有机器
for host in hadoop102 hadoop103 hadoop104
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
(3)修改脚本 xsync 具有执行权限
liyibin@hadoop102 bin]$ chmod +x xsyncshell
(4)同步环境变量
[liyibin@hadoop102 ~]$ sudo ./bin/xsync /etc/profile.d/my_env.sh
(5)让环境变量生效
[liyibin@hadoop103 bin]$ source /etc/profile
[liyibin@hadoop104 opt]$ source /etc/profile
3.1.3 SSH 无密登录
1)配置 ssh
基本语法:ssh另一台电脑的IP地址
[liyibin@hadoop102 ~]$ ssh hadoop103
Are you sure you want to continue connecting (yes/no)?
输入yes,并回车。
2)无密钥配置
(1)生成公钥和私钥
[liyibin@hadoop102 ~]$ cd /home/liyibin/.ssh/
[liyibin@hadoop102 .ssh]$ ssh-keygen -t rsa
然后敲(三个回车),就会生成两个文件 id_rsa(私钥)、id_rsa.pub(公钥)
(2)公钥拷贝到要免密登录的目标机器上
[liyibin@hadoop102 .ssh]$ ssh-copy-id hadoop102
[liyibin@hadoop102 .ssh]$ ssh-copy-id hadoop103
[liyibin@hadoop102 .ssh]$ ssh-copy-id hadoop104
同理需要在 hadoop103 和 hadoop104 进行同样的配置。
(3).ssh文件夹下(~/.ssh)的文件功能解释
文件 | 功能 |
---|---|
known_hosts | 记录ssh访问过计算机的公钥(public key) |
id_rsa | 生成的私钥 |
id_rsa.pub | 生成的公钥 |
authorized_keys | 存放授权过的无密登录服务器公钥 |
3.1.4 集群配置
1)集群部署规划
(1)NameNode和SecondaryNameNode不要安装在同一台服务器
(2)ResourceManager也很消耗内存,不要和NameNode、SecondaryNameNode配置在同一台机器上。
hadoop102 | hadoop103 | hadoop104 | |
---|---|---|---|
HDFS | NameNode DataNode | DataNode | SecondaryNameNode DataNode |
YARN | NodeManager | ResourceManager NodeManager | NodeManager |
2)配置文件说明
Hadoop配置文件分两类:默认配置文件和自定义配置文件,只有用户想修改某一默认配置值时,才需要修改自定义配置文件,更改相应属性值。
(1)默认配置文件
默认文件 | 位置 |
---|---|
core-default.xml | hadoop-common-3.1.3.jar/core-default.xml |
hdfs-default.xml | hadoop-hdfs-3.1.3.jar/hdfs-default.xml |
yarn-default.xml | hadoop-yarn-common-3.1.3.jar/yarn-default.xml |
mapred-default.xml | hadoop-mapreduce-client-core-3.1.3.jar/mapred-default.xml |
(2)自定义配置文件
core-site.xml、hdfs-site.xml、yarn-site.xml、mapred-site.xml 四个配置文件存放在==$HADOOP_HOME/etc/hadoop== 这个路径上,用户可以根据项目需求重新进行修改配置。
3)配置集群
(1)核心配置文件 core-site.xml
[liyibin@hadoop102 ~]$ cd /opt/module/hadoop-3.1.3/etc/hadoop/
[liyibin@hadoop102 hadoop]$ vim core-site.xml
文件内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<!--
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. See accompanying LICENSE file.
-->
<!-- Put site-specific property overrides in this file. -->
<configuration>
<!-- 指定NameNode的地址 -->
<property>
<name>fs.defaultFS</name>
<value>hdfs://hadoop102:8020</value>
</property>
<!-- 指定hadoop数据的存储目录 -->
<property>
<name>hadoop.tmp.dir</name>
<value>/opt/module/hadoop-3.1.3/data</value>
</property>
<!-- 配置HDFS网页登录使用的静态用户为liyibin -->
<property>
<name>hadoop.http.staticuser.user</name>
<value>liyibin</value>
</property>
</configuration>
(2)HDFS 配置文件
[liyibin@hadoop102 hadoop]$ vim hdfs-site.xml
文件内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<!--
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. See accompanying LICENSE file.
-->
<!-- Put site-specific property overrides in this file. -->
<configuration>
<!-- nn web端访问地址 NameNode -->
<property>
<name>dfs.namenode.http-address</name>
<value>hadoop102:9870</value>
</property>
<!-- 2nn web端访问地址 SecondaryNameNode -->
<property>
<name>dfs.namenode.secondary.http-address</name>
<value>hadoop104:9868</value>
</property>
</configuration>
(3)YARN 配置文件
[liyibin@hadoop102 hadoop]$ vim yarn-site.xml
文件内容如下:
<?xml version="1.0"?>
<!--
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. See accompanying LICENSE file.
-->
<configuration>
<!-- Site specific YARN configuration properties -->
<!-- 指定MR走shuffle -->
<property>
<name>yarn.nodemanager.aux-services</name>
<value>mapreduce_shuffle</value>
</property>
<!-- 指定ResourceManager的地址-->
<property>
<name>yarn.resourcemanager.hostname</name>
<value>hadoop103</value>
</property>
<!-- 环境变量的继承 -->
<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>
<!-- 开启日志聚集功能 -->
<property>
<name>yarn.log-aggregation-enable</name>
<value>true</value>
</property>
<!-- 设置日志聚集服务器地址 -->
<property>
<name>yarn.log.server.url</name>
<value>http://hadoop102:19888/jobhistory/logs</value>
</property>
<!-- 设置日志保留时间为7天 -->
<property>
<name>yarn.log-aggregation.retain-seconds</name>
<value>604800</value>
</property>
</configuration>
(4)MapReduce 配置文件
[liyibin@hadoop102 hadoop]$ vim mapred-site.xml
文件内容如下:
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<!--
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. See accompanying LICENSE file.
-->
<!-- Put site-specific property overrides in this file. -->
<configuration>
<!-- 指定MapReduce程序运行在Yarn上 默认local -->
<property>
<name>mapreduce.framework.name</name>
<value>yarn</value>
</property>
<!-- 历史服务器端地址 -->
<property>
<name>mapreduce.jobhistory.address</name>
<value>hadoop102:10020</value>
</property>
<!-- 历史服务器web端地址 -->
<property>
<name>mapreduce.jobhistory.webapp.address</name>
<value>hadoop102:19888</value>
</property>
</configuration>
4)分发配置好的 Hadoop 配置文件
[liyibin@hadoop102 hadoop]$ xsync /opt/module/hadoop-3.1.3/etc/hadoop/
5)在 hadoop103 和 hadoop104 上查看分发的文件
[liyibin@hadoop103 ~]$ cat /opt/module/hadoop-3.1.3/etc/hadoop/core-site.xml
[liyibin@hadoop104 ~]$ cat /opt/module/hadoop-3.1.3/etc/hadoop/core-site.xml
6)配置 workers
[liyibin@hadoop102 hadoop]$ vim /opt/module/hadoop-3.1.3/etc/hadoop/workers
# 在该文件中增加如下内容
hadoop102
hadoop103
hadoop104
分发改文件
[liyibin@hadoop102 hadoop]$ xsync /opt/module/hadoop-3.1.3/etc/hadoop/workers
3.1.5 群起集群
1)如果集群是第一次启动,需要在 hadoop102 节点格式化 NameNode(注意:格式化 NameNode,会产生新的集群id,导致 NameNode 和 DataNode 的集群id不一致,集群找不到已往数据。如果集群在运行过程中报错,需要重新格式化 NameNode 的话,一定要先停止 namenode 和 datanode 进程,并且要删除所有机器的data和logs目录,然后再进行格式化)。
[liyibin@hadoop102 hadoop-3.1.3]$ hdfs namenode -format
2)启动 HDFS
[liyibin@hadoop102 hadoop-3.1.3]$ sbin/start-dfs.sh
3)在配置了ResourceManager的节点(hadoop103)启动YARN
[liyibin@hadoop103 hadoop-3.1.3]$ sbin/start-yarn.sh
- Web端查看HDFS的NameNode
浏览器中输入:http://hadoop102:9870
5)Web端查看YARN的ResourceManager
浏览器中输入:http://hadoop103:8088
3.1.6 集群启动/停止方式总结
1)各个模块分开启动/停止(配置ssh是前提)常用
(1)整体启动/停止 HDFS
start-dfs.sh/stop-dfs.sh
(2)整体启动/停止 YARN
start-yarn.sh/stop-yarn.sh
2)各个服务组件逐一启动/停止
(1)分别启动/停止HDFS组件
hdfs --daemon start/stop namenode/datanode/secondarynamenode
(2)启动/停止YARN
yarn --daemon start/stop resourcemanager/nodemanager
3.1.7 编写 Hadoop 寄存常用脚本
1)Hadoop集群启停脚本(包含HDFS,Yarn,Historyserver):myhadoop.sh
[liyibin@hadoop102 ~]$ cd /home/liyibin/bin
[liyibin@hadoop102 bin]$ vim myhadoop.sh
文件内容如下
#!/bin/bash
if [ $# -lt 1 ]
then
echo "No Args Input..."
exit ;
fi
case $1 in
"start")
echo " =================== 启动 hadoop集群 ==================="
echo " --------------- 启动 hdfs ---------------"
ssh hadoop102 "/opt/module/hadoop-3.1.3/sbin/start-dfs.sh"
echo " --------------- 启动 yarn ---------------"
ssh hadoop103 "/opt/module/hadoop-3.1.3/sbin/start-yarn.sh"
echo " --------------- 启动 historyserver ---------------"
ssh hadoop102 "/opt/module/hadoop-3.1.3/bin/mapred --daemon start historyserver"
;;
"stop")
echo " =================== 关闭 hadoop集群 ==================="
echo " --------------- 关闭 historyserver ---------------"
ssh hadoop102 "/opt/module/hadoop-3.1.3/bin/mapred --daemon stop historyserver"
echo " --------------- 关闭 yarn ---------------"
ssh hadoop103 "/opt/module/hadoop-3.1.3/sbin/stop-yarn.sh"
echo " --------------- 关闭 hdfs ---------------"
ssh hadoop102 "/opt/module/hadoop-3.1.3/sbin/stop-dfs.sh"
;;
*)
echo "Input Args Error..."
;;
esac
保存后退出,然后赋予脚本执行权限
[liyibin@hadoop102 bin]$ chmod +x myhadoop.sh
2)查看三台服务器Java进程脚本:jpsall
[liyibin@hadoop102 ~]$ cd /home/liyibin/bin
[liyibin@hadoop102 bin]$ vim jpsall
文件内容如下
#!/bin/bash
for host in hadoop102 hadoop103 hadoop104
do
echo =============== $host ===============
ssh $host jps
done
保存后退出,然后赋予脚本执行权限
[liyibin@hadoop102 bin]$ chmod +x jpsall
4 Hadoop 常用端口
端口名称 | Hadoop2.x | Hadoop3.x |
---|---|---|
NameNode内部通信端口 | 8020/9000 | 8020/9000/9820 |
NameNode Web 端口 | 50070 | 9870 |
SecondaryNameNode Web 端口 | 9868 | 9868 |
MapReduce查看执行任务端口 | 8088 | 8088 |
历史服务器通信端口 | 10020 | 10020 |
历史服务器 web 端口 | 19888 | 19888 |