赞
踩
主机配置环境如下表
主机名 | IP | 系统 | 软件版本 | 安装目录 | 数据目录 |
---|---|---|---|---|---|
hadoop-01 | 192.168.10.51 | Centos 7.6 | hadoop-2.7.7、jdk1.8、zookeeper-3.4.6 | /usr/local/hadoop | /data/hadoop |
hadoop-02 | 192.168.10.52 | Centos 7.6 | hadoop-2.7.7、jdk1.8、zookeeper-3.4.6 | /usr/local/hadoop | /data/hadoop |
hadoop-03 | 192.168.10.53 | Centos 7.6 | hadoop-2.7.7、jdk1.8、zookeeper-3.4.6 | /usr/local/hadoop | /data/hadoop |
安装后启动的进程如下表
hadoop-01 | hadoop-02 | hadoop-03 |
---|---|---|
NodeManager | NodeManager | NodeManager |
NameNode | NameNode | |
DataNode | DataNode | DataNode |
DFSZKFailoverController | DFSZKFailoverController | |
JournalNode | JournalNode | JournalNode |
ResourceManager | ResourceManager | |
QuorumPeerMain | QuorumPeerMain | QuorumPeerMain |
详细搭建过程记录如下
高可用模式下,hadoop的故障切换需要通过ssh登陆到其它机器,进行主备切换,因此需要配置主机间的免密码登陆
以hadoop-01为例,剩下两台服务器做相同配置
#第一步,ssh-keygen -t rsa生成密钥对 [root@hadoop-01 ~]$ ssh-keygen -t rsa # 连续三次回车,即在本地生成了公钥和私钥,不设置密码,默认存储在 ~/.ssh目录下 [root@hadoop-01 ~]# ll .ssh/ total 16 -rw------- 1 root root 1989 Jul 24 14:18 authorized_keys -rw------- 1 root root 1679 Jul 15 11:43 id_rsa -rw-r--r-- 1 root root 396 Jul 15 11:43 id_rsa.pub -rw-r--r-- 1 root root 1311 Jul 24 16:41 known_hosts #第二步,用ssh-copy-id拷贝将公钥复制到远程机器中 [root@hadoop-01 ~]# ssh-copy-id -i .ssh/id_rsa.pub root@192.168.10.51 [root@hadoop-01 ~]# ssh-copy-id -i .ssh/id_rsa.pub root@192.168.10.52 [root@hadoop-01 ~]# ssh-copy-id -i .ssh/id_rsa.pub root@192.168.10.53 #第三步,测试免密码登陆 [root@hadoop-01 ~]# ssh -p 9431 root@192.168.10.52 Last login: Mon Aug 19 15:30:09 2019 from 192.168.10.45 Welcome to Alibaba Cloud Elastic Compute Service ! [root@hadoop-02 ~]#
(1)将hadoop安装包解压,重命名为hadoop,拷贝到/usr/local下
(2)修改各配置文件,所有的配置文件在/usr/local/hadoop/etc/hadoop目录下
修改core-site.xml,内容如下:
<configuration>
<!--指定hdfs连接地址,集群模式(高可用)-->
<property>
<name>fs.defaultFS</name>
<value>hdfs://cluster</value>
</property>
<!-- 指定ZooKeeper集群的地址和端口。注意,数量一定是奇数,且不少于三个节点-->
<property>
<name>ha.zookeeper.quorum</name>
<value>hadoop-01:2181,hadoop-02:2181,hadoop-03:2181</value>
</property>
</configuration>
修改hdfs-site.xml,内容如下:
<configuration> <!--指定HDFS副本的数量,不能超过机器节点数--> <property> <name>dfs.replication</name> <value>3</value> </property> <!-- 为namenode集群定义一个services name --> <property> <name>dfs.nameservices</name> <value>cluster</value> </property> <!-- nameservice 包含哪些namenode,为各个namenode起名 --> <property> <name>dfs.ha.namenodes.cluster</name> <value>nn01,nn02</value> </property> <!-- 名为nn01的namenode的rpc地址和端口号,rpc用来和datanode通讯 --> <property> <name>dfs.namenode.rpc-address.cluster.nn01</name> <value>hadoop-01:9000</value> </property> <!--名为nn01的namenode的http地址和端口号,用来和web客户端通讯 --> <property> <name>dfs.namenode.http-address.cluster.nn01</name> <value>hadoop-01:50070</value> </property> <!-- 名为nn02的namenode的rpc地址和端口号,rpc用来和datanode通讯 --> <property> <name>dfs.namenode.rpc-address.cluster.nn02</name> <value>hadoop-02:9000</value> </property> <!--名为nn02的namenode的http地址和端口号,用来和web客户端通讯 --> <property> <name>dfs.namenode.http-address.cluster.nn02</name> <value>hadoop-02:50070</value> </property> <!-- namenode间用于共享编辑日志的journal节点列表 --> <property> <name>dfs.namenode.shared.edits.dir</name> <value>qjournal://hadoop-01:8485;hadoop-02:8485;hadoop-03:8485/cluster</value> </property> <!-- journalnode 上用于存放edits日志的目录 --> <property> <name>dfs.journalnode.edits.dir</name> <value>/data/hadoop/journaldata</value> </property> <!-- 指定该集群出现故障时,是否自动切换到另一台namenode --> <property> <name>dfs.ha.automatic-failover.enabled.cluster</name> <value>true</value> </property> <!-- 配置失败自动切换实现方式 --> <property> <name>dfs.client.failover.proxy.provider.cluster</name> <value>org.apache.hadoop.hdfs.server.namenode.ha.ConfiguredFailoverProxyProvider</value> </property> <!-- 一旦需要NameNode切换,使用ssh方式进行操作 --> <property> <name>dfs.ha.fencing.methods</name> <value>sshfence(root:9431)</value> </property> <!-- 如果使用ssh进行故障切换,使用ssh通信时用的密钥存储的位置 --> <property> <name>dfs.ha.fencing.ssh.private-key-files</name> <value>/root/.ssh/id_rsa</value> </property> <!-- connect-timeout超时时间 --> <property> <name>dfs.ha.fencing.ssh.connect-timeout</name> <value>30000</value> </property> <property> <name>dfs.name.dir</name> <value>/data/hadoop/tmp/dfs/name</value> </property> <property> <name>dfs.data.dir</name> <value>/data/hadoop/tmp/dfs/data</value> </property> </configuration>
修改yarn-site.xml,内容如下:
<configuration> <!-- 启用Resource Manager HA高可用性 --> <property> <name>yarn.resourcemanager.ha.enabled</name> <value>true</value> </property> <!-- 指定resourcemanager的名字 --> <property> <name>yarn.resourcemanager.cluster-id</name> <value>yrc</value> </property> <!-- 使用了2个resourcemanager,分别指定Resourcemanager的地址 --> <property> <name>yarn.resourcemanager.ha.rm-ids</name> <value>rm1,rm2</value> </property> <!-- 指定rm1的地址 --> <property> <name>yarn.resourcemanager.hostname.rm1</name> <value>hadoop-01</value> </property> <!-- 指定rm2的地址 --> <property> <name>yarn.resourcemanager.hostname.rm2</name> <value>hadoop-02</value> </property> <!-- 指定zookeeper集群机器 --> <property> <name>yarn.resourcemanager.zk-address</name> <value>hadoop-01:2181,hadoop-02:2181,hadoop-03:2181</value> </property> <!-- NodeManager上运行的附属服务,默认是mapreduce_shuffle --> <property> <name>yarn.nodemanager.aux-services</name> <value>mapreduce_shuffle</value> </property> </configuration>
修改mapred-site.xml(该文件不存在,需要手动创建),cp mapred-site.xml.template mapred-site.xml,内容如下:
<configuration>
<!-- 采用yarn作为mapreduce的资源调度框架 -->
<property>
<name>mapreduce.framework.name</name>
<value>yarn</value>
</property>
</configuration>
修改slaves文件,内容如下:
hadoop-01
hadoop-02
hadoop-03
修改hadoop-env.sh文件,指定jdk的地址
# The java implementation to use.
export JAVA_HOME=/usr/java/jdk1.8.0_131
# 如果ssh端口不是默认22,需要添加此配置
export HADOOP_SSH_OPTS="-p 9431"
# 指定hadoop相关的pid存放位置
export HADOOP_PID_DIR=/usr/local/hadoop/pids
配置hadoop环境变量,vim /etc/profile.d/hadoop.sh,内容如下
export HADOOP_HOME=/usr/local/hadoop
export LD_LIBRARY_PATH=$HADOOP_HOME/lib/native
export HADOOP_COMMON_LIB_NATIVE_DIR=/usr/local/hadoop/lib/native
export HADOOP_OPTS="-Djava.library.path=/usr/local/hadoop/lib"
export PATH=$HADOOP_HOME/bin:$HADOOP_HOME/sbin:$PATH
source /etc/profile.d/hadoop.sh
scp -r /usr/local/hadoop root@hadoop-02:/usr/local/
scp -r /usr/local/hadoop root@hadoop-03:/usr/local/
启动hadoop前,需要执行几步格式化操作:
(1)启动journalnode,三台机器都要这一步操作(仅第一次启动hadoop时,需要这一步操作,之后不再需要手动启动journalnode)
cd /usr/local/hadoop/sbin
sh hadoop-daemon.sh start journalnode
(2) 在hadoop-01上执行格式化操作,格式化namenode和zkfc
hdfs namenode -format
hdfs zkfc -formatZK
(3) namenode主从信息同步,在hadoop-02节点上执行同步命令
bin/hdfs namenode -bootstrapStandby
上述步骤完成后,接下来我们就可以启动hadoop了
在hadoop-01机器上执行下面的命令
# 启动hdfs
cd /usr/local/hadoop/sbin
sh start-dfs.sh
# 启动yarn
sh start-yarn.sh
#启动ZookeeperFailoverController
sh hadoop-daemon.sh start zkfc
在hadoop-02机器上执行下边命令
/usr/local/hadoop/sbin/yarn-daemon.sh start resourcemanager
/usr/local/hadoop/sbin/hadoop-daemon.sh start zkfc
也可以简化启动
启动命令
#hadoop-01
/usr/local/hadoop/sbin/start-all.sh
/usr/local/hadoop/sbin/hadoop-daemon.sh start zkfc
#hadoop-02
/usr/local/hadoop/sbin/yarn-daemon.sh start resourcemanager
/usr/local/hadoop/sbin/hadoop-daemon.sh start zkfc
停止命令
#hadoop-01
/usr/local/hadoop/sbin/stop-all.sh
/usr/local/hadoop/sbin/hadoop-daemon.sh stop zkfc
#hadoop-02
/usr/local/hadoop/sbin/yarn-daemon.sh stop resourcemanager
/usr/local/hadoop/sbin/hadoop-daemon.sh stop zkfc
hadoop-01
[root@hadoop-01 ~]# jps | grep -v Jps
26849 NodeManager
2770 QuorumPeerMain
27331 DFSZKFailoverController
26308 DataNode
26537 JournalNode
26154 NameNode
26733 ResourceManager
hadoop-02
[root@hadoop-02 ~]# jps | grep -v Jps
7489 JournalNode
7281 NameNode
627 QuorumPeerMain
8233 DFSZKFailoverController
8123 ResourceManager
7389 DataNode
7631 NodeManager
hadoop-03
[root@hadoop-03 ~]# jps | grep -v Jps
21762 QuorumPeerMain
29476 NodeManager
29271 DataNode
29370 JournalNode
(1) 浏览器访问http://192.168.10.51:50070
由图可知,当前hadoop-01是active节点,即namenode主节点
(2)浏览器访问http://192.168.10.52:50070
由图可知,当前hadoop-01是standby节点,即namenode备节点
(3) 查看yarn的web控制台,浏览器访问http://192.168.10.51:8088
(1)在hadoop-01上kill掉namenode进程,然后通过浏览器查看hadoop-02的状态,发现状态变为active,说明高可用测试成功
(2)重新启动hadoop-01的namenode进程,sh start-dfs.sh,浏览器访问hadoop-01,此时hadoop-01的状态为standby
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。