- 接 Hadoop运行环境搭建
- Hadoop运行模式包括:本地模式、伪分布式模式以及完全分布式模式。
1 本地运行模式
Hadoop安装完成不修改配置文件,就是本地运行模式,也就是只能在本地运行,并不能达到分布式的结果,常用于学习
2 伪分布式运行模式
2.1 配置HDFS
启动之前要修改Hadoop的配置文件
#进入Hadoop配置文件夹
[hadoop@hadoop102 hadoop-2.7.2]$ cd etc/hadoop/
#hadoop配置文件所在路径如下:
[hadoop@hadoop102 hadoop]$ pwd
/opt/module/hadoop-2.7.2/etc/hadoop
#配置文件列表:
[hadoop@hadoop102 hadoop]$ ls
capacity-scheduler.xml hadoop-policy.xml kms-log4j.properties ssl-client.xml.example
configuration.xsl hdfs-site.xml kms-site.xml ssl-server.xml.example
container-executor.cfg httpfs-env.sh log4j.properties yarn-env.cmd
core-site.xml httpfs-log4j.properties mapred-env.cmd yarn-env.sh
hadoop-env.cmd httpfs-signature.secret mapred-env.sh yarn-site.xml
hadoop-env.sh httpfs-site.xml mapred-queues.xml.template
hadoop-metrics2.properties kms-acls.xml mapred-site.xml.template
hadoop-metrics.properties kms-env.sh slaves
2.1.1 配置:hadoop-env.sh
#获取JDK的安装路径:
[hadoop@hadoop102 hadoop]$ echo $JAVA_HOME
/opt/module/jdk1.8.0_144
#编辑 hadoop-env.sh 找到并修改 JAVA_HOME 路径:如下:
# The java implementation to use.
export JAVA_HOME=/opt/module/jdk1.8.0_144
2.1.2 配置:core-site.xml
<!-- 指定HDFS中NameNode的地址 -->
<property>
<name>fs.defaultFS</name>
<value>hdfs://localhost:9000</value>
</property>
<!-- 指定Hadoop运行时产生文件的存储目录 -->
<property>
<name>hadoop.tmp.dir</name>
<value>/opt/module/hadoop-2.7.2/data/tmp</value>
</property>
2.1.3 配置:hdfs-site.xml
<!-- 指定HDFS副本的数量 -->
<property>
<name>dfs.replication</name>
<value>1</value>
</property>
2.2 启动HDFS
2.2.1 格式化NameNode(第一次启动时格式化,以后就不要总格式化,就算要格式化时也需要删掉 data/ logs/这两个目录)
[hadoop@hadoop102 hadoop-2.7.2]$ bin/hdfs namenode -format
2.2.2 判断格式化是否有错误:
util.ExitUtil: Exiting with status 0 :表示无错误
如果有显示1,2,3等情况,请上翻查看报错信息,解决问题后再重新格式化
2.2.1 启动hdfs
[hadoop@hadoop102 hadoop-2.7.2]$ sbin/start-dfs.sh
接下来按照提示操作:
需要填yes的地方填yes,需要输入密码的地方输入相应的密码
[hadoop@hadoop102 hadoop-2.7.2]$ sbin/start-dfs.sh
Starting namenodes on [localhost]
The authenticity of host 'localhost (::1)' can't be established.
ECDSA key fingerprint is SHA256:uDf+TxDo2T4+NIk1ixr85o07RcD+kSAQuyiwbiEQJ9k.
ECDSA key fingerprint is MD5:a9:aa:4e:d8:57:8e:0d:39:90:2f:4f:69:82:df:e9:4d.
Are you sure you want to continue connecting (yes/no)? yes
localhost: Warning: Permanently added 'localhost' (ECDSA) to the list of known hosts.
hadoop@localhost's password:
localhost: starting namenode, logging to /opt/module/hadoop-2.7.2/logs/hadoop-hadoop-namenode-hadoop102.out
hadoop@localhost's password:
localhost: starting datanode, logging to /opt/module/hadoop-2.7.2/logs/hadoop-hadoop-datanode-hadoop102.out
Starting secondary namenodes [0.0.0.0]
The authenticity of host '0.0.0.0 (0.0.0.0)' can't be established.
ECDSA key fingerprint is SHA256:uDf+TxDo2T4+NIk1ixr85o07RcD+kSAQuyiwbiEQJ9k.
ECDSA key fingerprint is MD5:a9:aa:4e:d8:57:8e:0d:39:90:2f:4f:69:82:df:e9:4d.
Are you sure you want to continue connecting (yes/no)? yes
0.0.0.0: Warning: Permanently added '0.0.0.0' (ECDSA) to the list of known hosts.
hadoop@0.0.0.0's password:
0.0.0.0: starting secondarynamenode, logging to /opt/module/hadoop-2.7.2/logs/hadoop-hadoop-secondarynamenode-hadoop102.out
[hadoop@hadoop102 hadoop-2.7.2]$
2.2.3 通过 jps 查看进程是否开启
[hadoop@hadoop102 hadoop-2.7.2]$ jps
4049 DataNode
3906 NameNode
4372 Jps
4236 SecondaryNameNode
[hadoop@hadoop102 hadoop-2.7.2]$
2.2.4 通过web端查看HDFS文件系统
http://hadoop102:50070/dfshealth.html#tab-overview
2.3 关闭HDFS
在关机之前需要手动关闭HDFS,否则可能将发生错误,导致集群下次不可启动
[hadoop@hadoop102 hadoop-2.7.2]$ sbin/stop-dfs.sh
Stopping namenodes on [localhost]
hadoop@localhost's password:
localhost: stopping namenode
hadoop@localhost's password:
localhost: stopping datanode
Stopping secondary namenodes [0.0.0.0]
hadoop@0.0.0.0's password:
0.0.0.0: stopping secondarynamenode
[hadoop@hadoop102 hadoop-2.7.2]$
[hadoop@hadoop102 hadoop-2.7.2]$ jps
4978 Jps
[hadoop@hadoop102 hadoop-2.7.2]$
2.4 配置YARN
2.4.1 配置:yarn-env.sh
#获取JDK的安装路径:
[hadoop@hadoop102 hadoop]$ echo $JAVA_HOME
/opt/module/jdk1.8.0_144
#编辑 yarn-env.sh 找到并修改 JAVA_HOME 路径:如下:
# some Java parameters
export JAVA_HOME=/opt/module/jdk1.8.0_144
2.4.2 配置:yarn-site.xml
<!-- Reducer获取数据的方式 -->
<property>
<name>yarn.nodemanager.aux-services</name>
<value>mapreduce_shuffle</value>
</property>
<!-- 指定YARN的ResourceManager的地址 -->
<property>
<name>yarn.resourcemanager.hostname</name>
<value>hadoop102</value>
</property>
2.4.3 配置:mapred-env.sh
#获取JDK的安装路径:
[hadoop@hadoop102 hadoop]$ echo $JAVA_HOME
/opt/module/jdk1.8.0_144
#配置 JAVA_HOME
export JAVA_HOME=/opt/module/jdk1.8.0_144
2.4.4 配置:mapred-site.xml
- mapred-site.xml.template 复制一份改名为 mapred-site.xml ```bash [hadoop@hadoop102 hadoop]$ cp mapred-site.xml.template mapred-site.xml
- 配置mapred-site.xml
```xml
<!-- 指定MR运行在YARN上 -->
<property>
<name>mapreduce.framework.name</name>
<value>yarn</value>
</property>
2.5 启动YARN
- 启动前必须保证NameNode和DataNode已经启动
[hadoop@hadoop102 hadoop-2.7.2]$ sbin/start-yarn.sh starting yarn daemons starting resourcemanager, logging to /opt/module/hadoop-2.7.2/logs/yarn-hadoop-resourcemanager-hadoop102.out hadoop@localhost's password: localhost: starting nodemanager, logging to /opt/module/hadoop-2.7.2/logs/yarn-hadoop-nodemanager-hadoop102.out [hadoop@hadoop102 hadoop-2.7.2]$
2.5.1 查看进程
[hadoop@hadoop102 hadoop-2.7.2]$ jps 3152 Jps 3042 NodeManager 2756 ResourceManager 2395 DataNode 2255 NameNode 2575 SecondaryNameNode [hadoop@hadoop102 hadoop-2.7.2]$
2.5.3 web端查看
YARN的浏览器页面查看
http://hadoop102:8088/cluster
2.6 关闭YARN
[hadoop@hadoop102 hadoop-2.7.2]$ sbin/stop-yarn.sh
3 完全分布式运行模式(重点)
3.1 编写集群分发脚本xsync
配置文件分发,一台一台的分发太繁琐,创建一个集群分发脚本用于分发配置文件,这样会大大提高效率
3.1.1 scp(secure copy)安全拷贝
(1)scp定义:
scp可以实现服务器与服务器之间的数据拷贝。(from server1 to server2)
(2)基本语法
scp -r $pdir/$fname $user@hadoop$host:$pdir/$fname
命令 递归 要拷贝的文件路径/名称 目的用户@主机:目的路径/名称
3.1.2 rsync 远程同步工具
rsync主要用于备份和镜像。具有速度快、避免复制相同内容和支持符号链接的优点。
rsync和scp区别:用rsync做文件的复制要比scp的速度快,rsync只对差异文件做更新。scp是把所有文件都复制过去。
(1)基本语法
rsync -rvl $pdir/$fname $user@hadoop$host:$pdir/$fname
命令 选项参数 要拷贝的文件路径/名称 目的用户@主机:目的路径/名称
(2)选项参数说明:
选项参数 | 功能 |
---|---|
-r | 递归 |
-v | 显示复制过程 |
-l | 拷贝符号连接 |
3.1.3 xsync集群分发脚本
(1)需求:循环复制文件到所有节点的相同目录下
(2)需求分析:
(a)rsync命令原始拷贝:
rsync -rvl /opt/module root@hadoop103:/opt/
(b)期望脚本:xsync 要同步的文件名称
(c)说明:在/home/hadoop/bin这个目录下存放的脚本,hadoop用户可以在系统任何地方直接执行。
(3)脚本实现:
(a)在/home/hadoop目录下创建bin目录,并在bin目录下xsync创建文件,文件内容如下:
[hadoop@hadoop102 ~]$ mkdir bin/
[hadoop@hadoop102 ~]$ cd bin/
[hadoop@hadoop102 bin]$ touch xsync
[hadoop@hadoop102 bin]$ ls
xsync
[hadoop@hadoop102 bin]$ vim xsync
在xsync编写如下代码:
#!/bin/bash
#1 获取输入参数个数,如果没有参数,直接退出
pcount=$#
if((pcount==0)); then
echo no args;
exit;
fi
#2 获取文件名称
p1=$1
fname=`basename $p1`
echo fname=$fname
#3 获取上级目录到绝对路径
pdir=`cd -P $(dirname $p1); pwd`
echo pdir=$pdir
#4 获取当前用户名称
user=`whoami`
#5 循环
for((host=103; host<105; host++)); do
echo ------------------- hadoop$host --------------
rsync -rvl $pdir/$fname $user@hadoop$host:$pdir
done
(b)修改脚本 xsync 具有执行权限
[hadoop@hadoop102 bin]$ chmod 777 xsync
(c)调用脚本形式:xsync 文件名称
[hadoop@hadoop102 bin]$ xsync /home/hadoop/bin/
注意:如果将xsync放到/home/hadoop/bin目录下仍然不能实现全局使用,可以将xsync移动到/usr/local/bin目录下。
3.2 集群配置
3.2.1 集群部署规划
hadoop102 | hadoop103 | hadoop104 | |
---|---|---|---|
HDFS |
NameNode DataNode |
DataNode |
SecondaryNameNode DataNode |
YARN | NodeManager |
ResourceManager NodeManager |
NodeManager |
3.2.2 配置集群
3.2.2.1 核心配置文件
配置core-site.xml
<!-- 指定HDFS中NameNode的地址 -->
<property>
<name>fs.defaultFS</name>
<value>hdfs://hadoop102:9000</value>
</property>
<!-- 指定Hadoop运行时产生文件的存储目录 -->
<property>
<name>hadoop.tmp.dir</name>
<value>/opt/module/hadoop-2.7.2/data/tmp</value>
</property>
3.2.2.2 HDFS配置文件
配置hadoop-env.sh
[hadoop@hadoop102 hadoop]$ vim hadoop-env.sh
export JAVA_HOME=/opt/module/jdk1.8.0_144
配置hdfs-site.xml
[hadoop@hadoop102 hadoop]$ vim hdfs-site.xml
<property>
<name>dfs.replication</name>
<value>3</value>
</property>
<!-- 指定Hadoop辅助名称节点主机配置 -->
<property>
<name>dfs.namenode.secondary.http-address</name>
<value>hadoop104:50090</value>
</property>
3.2.2.3 YARN配置文件
配置yarn-env.sh
[hadoop@hadoop102 hadoop]$ vim yarn-env.sh
export JAVA_HOME=/opt/module/jdk1.8.0_144
配置yarn-site.xml
[hadoop@hadoop102 hadoop]$ vim yarn-site.xml
<!-- Reducer获取数据的方式 -->
<property>
<name>yarn.nodemanager.aux-services</name>
<value>mapreduce_shuffle</value>
</property>
<!-- 指定YARN的ResourceManager的地址 -->
<property>
<name>yarn.resourcemanager.hostname</name>
<value>hadoop103</value>
</property>
3.2.2.4 MapReduce配置文件
配置mapred-env.sh
[hadoop@hadoop102 hadoop]$ vim mapred-env.sh
export JAVA_HOME=/opt/module/jdk1.8.0_144
配置mapred-site.xml
[hadoop@hadoop102 hadoop]$ cp mapred-site.xml.template mapred-site.xml
[hadoop@hadoop102 hadoop]$ vim mapred-site.xml
<!-- 指定MR运行在Yarn上 -->
<property>
<name>mapreduce.framework.name</name>
<value>yarn</value>
</property>
3.2.2.5 在集群上分发配置好的Hadoop配置文件
[hadoop@hadoop102 hadoop]$ xsync /opt/module/hadoop-2.7.2/etc/hadoop
3.2.2.5 查看文件分发情况
[hadoop@hadoop103 hadoop-2.7.2]$ cat /opt/module/hadoop-2.7.2/etc/hadoop/core-site.xml
3.3 集群单点启动
3.3.1 格式化NameNode
注意:
适用于第一次启动集群,如果之前配置过伪分布式运行就不要再格式化NameNode
格式化的操作在hdfs集群的主角色(namenode)所在机器上操作。(其他节点是不能格式化的)
[hadoop@hadoop102 hadoop-2.7.2]$ hadoop namenode -format
3.3.2 在hadoop102上启动NameNode
[hadoop@hadoop102 hadoop-2.7.2]$ hadoop-daemon.sh start namenode
starting namenode, logging to /opt/module/hadoop-2.7.2/logs/hadoop-hadoop-namenode-hadoop102.out
[hadoop@hadoop102 hadoop-2.7.2]$ jps
56403 Jps
56334 NameNode
3.3.3 在hadoop102、hadoop103以及hadoop104上分别启动DataNode
[hadoop@hadoop102 hadoop-2.7.2]$ hadoop-daemon.sh start datanode
starting datanode, logging to /opt/module/hadoop-2.7.2/logs/hadoop-hadoop-datanode-hadoop102.out
[hadoop@hadoop102 hadoop-2.7.2]$ jps
56450 DataNode
56525 Jps
56334 NameNode
[hadoop@hadoop103 hadoop-2.7.2]$ hadoop-daemon.sh start datanode
starting datanode, logging to /opt/module/hadoop-2.7.2/logs/hadoop-hadoop-datanode-hadoop103.out
[hadoop@hadoop103 hadoop-2.7.2]$ jps
56513 Jps
56436 DataNode
[hadoop@hadoop104 hadoop-2.7.2]$ hadoop-daemon.sh start datanode
starting datanode, logging to /opt/module/hadoop-2.7.2/logs/hadoop-hadoop-datanode-hadoop104.out
[hadoop@hadoop104 hadoop-2.7.2]$ jps
56386 DataNode
56471 Jps
3.3.2 思考?
这样一个一个节点启动效率太低,有什么方法可以快速启动所有节点呢?
解决办法:配置ssh免密登录,修改Hadoop配置文件下的slaves
3.4 SSH免密登录配置
3.4.1 免密登录原理
3.4.2 生成公钥和私钥
[hadoop@hadoop102 hadoop-2.7.2]$ ssh-keygen -t rsa
输入三个回车,就会生成两个文件id_rsa(私钥)、id_rsa.pub(公钥)
3.4.3 将公钥拷贝到要免密登录的目标机器上
[hadoop@hadoop102 hadoop-2.7.2]$ ssh-copy-id hadoop102
[hadoop@hadoop102 hadoop-2.7.2]$ ssh-copy-id hadoop104
[hadoop@hadoop102 hadoop-2.7.2]$ ssh-copy-id hadoop103
注意:
还需要在hadoop102上采用root账号,配置一下无密登录到hadoop102、hadoop103、hadoop104;
还需要在hadoop103上采用hadoop账号配置一下无密登录到hadoop102、hadoop103、hadoop104服务器上。
3.4.5 .ssh文件夹下(~/.ssh)的文件功能解释
known_hosts | 记录ssh访问过计算机的公钥(public key) |
---|---|
id_rsa | 生成的私钥 |
id_rsa.pub | 生成的公钥 |
authorized_keys | 存放授权过得无密登录服务器公钥 |
3.5 群起集群
3.5.1 配置slaves
[hadoop@hadoop103 hadoop]$ pwd
/opt/module/hadoop-2.7.2/etc/hadoop
[hadoop@hadoop103 hadoop]$ vim slaves
#把localhost修改为如下内容
hadoop102
hadoop103
hadoop104
注意:该文件添加的内容结尾不允许有空格,文件中不允许有空行。
同步所有节点配置文件
[hadoop@hadoop103 hadoop]$ xsync slaves
3.5.2 启动集群
(1)如果集群是第一次启动,需要格式化NameNode(注意格式化之前,一定要先停止上次启动的所有namenode和datanode进程,然后再删除data和log数据)
[hadoop@hadoop102 hadoop-2.7.2]$ hadoop namenode -format
(2)启动HDFS
启动之前记得先检查进程,没停止先停止再启动
[hadoop@hadoop102 hadoop-2.7.2]$ sbin/start-dfs.sh
[hadoop@hadoop102 hadoop-2.7.2]$ jps
57792 NameNode
58162 Jps
57933 DataNode
[hadoop@hadoop103 hadoop]$ jps
57897 Jps
57789 DataNode
[hadoop@hadoop104 hadoop]$ jps
57841 Jps
57658 DataNode
57775 SecondaryNameNode
(3)启动YARN
[hadoop@hadoop103 hadoop-2.7.2]$ sbin/start-yarn.sh
[hadoop@hadoop103 hadoop-2.7.2]$ jps
58292 Jps
57963 ResourceManager
58075 NodeManager
57789 DataNode
[hadoop@hadoop102 hadoop-2.7.2]$ jps
59376 Jps
59240 NodeManager
58795 NameNode
58941 DataNode
[hadoop@hadoop104 hadoop]$ jps
58035 Jps
57658 DataNode
57916 NodeManager
57775 SecondaryNameNode
注意:NameNode和ResourceManger如果不是同一台机器,不能在NameNode上启动 YARN,应该在ResouceManager所在的机器上启动YARN。
(4)Web端查看SecondaryNameNode
浏览器中输入:http://hadoop104:50090/status.html
3.6 集群启动/停止方式总结
3.6.1 各个服务组件逐一启动/停止
分别启动/停止HDFS组件
hadoop-daemon.sh start / stop namenode / datanode / secondarynamenode
启动/停止YARN
yarn-daemon.sh start / stop resourcemanager / nodemanager
3.6.2 各个模块分开启动/停止(配置ssh是前提)常用
整体启动/停止HDFS
start-dfs.sh / stop-dfs.sh
整体启动/停止YARN
start-yarn.sh / stop-yarn.sh