大数据系列(hadoop) Hadoop+Zookeeper 3节点高可用集群搭建
原文地址:https://www.cnblogs.com/YellowstonePark/p/7750213.html
一、集群规划
主机名 | ip | NameNode | DataNode | Yarn | ZooKeeper | JournalNode |
node01 | 192.168.1.201 | 是 | 是 | 否 | 是 | 是 |
node02 | 192.168.1.202 | 是 | 是 | 否 | 是 | 是 |
node03 | 192.168.1.203 | 否 | 是 | 否 | 是 | 是 |
注:所有操作都是在root用户下操作,如需其他用户,请自行创建。注意文件权限问题。
二、环境搭建
1、安装JDK
1.1 下载jdk
这里使用的是jdk-8u144-linux-x64.tar.gz,下载地址:链接:http://pan.baidu.com/s/1eSEjL8u 密码:mfdj
将下载的jdk 上传到node01服务器的/opt目录下
[[email protected] usr]# cd /opt/ [[email protected] opt]# ll total 727676 -rw-r--r--. 1 root root 149756462 Oct 23 12:24 apache-hive-2.1.1-bin.tar.gz -rw-r--r--. 1 root root 266688029 Oct 23 12:24 hadoop-2.7.4.tar.gz -rw-r--r--. 1 root root 104659474 Oct 23 12:24 hbase-1.2.6-bin.tar.gz -rw-r--r--. 1 root root 185515842 Oct 23 12:26 jdk-8u144-linux-x64.tar.gz -rw-r--r--. 1 root root 3465760 Oct 23 12:24 mysql-connector-java-5.1.44.tar.gz -rw-r--r-- 1 root root 35042811 Oct 23 13:37 zookeeper-3.4.10.tar.gz
然后切换到/usr 目录下
[[email protected] ~]# cd /usr/
新建app目录,用以安装所需软件
[[email protected] usr]# mkdir -p /usr/app
进入app目录
[[email protected] opt]# cd /usr/app/
解压jdk到当前路径下
[[email protected] app]# tar zxvf /opt/jdk-8u144-linux-x64.tar.gz
建立jdk软连接
[[email protected] app]# ln -s jdk1.8.0_144/ java
配置环境变量
[[email protected] app]# vi /etc/profile
在打开的文件末尾添加如下代码后保存
export JAVA_HOME=/usr/app/java export CLASS_PATH=$JAVA_HOME/lib:$JAVA_HOME/jre/lib export PATH=$PATH:$JAVA_HOME/bin
使配置生效
[[email protected] app]# source /etc/profile
查看Java版本,显示如下则jdk安装完成
[[email protected] app]# java -version java version "1.8.0_144" Java(TM) SE Runtime Environment (build 1.8.0_144-b01) Java HotSpot(TM) 64-Bit Server VM (build 25.144-b01, mixed mode)
根据以上流程,分别在node02、node03 节点上完成jdk 安装。
2、安装zookeeper
下载zookeeper
这里使用的是zookeeper-3.4.10.tar.gz,下载地址:链接:http://pan.baidu.com/s/1hrQGIHI 密码:37up
将下载的zookeeper上传到node01服务器的/opt目录下
[[email protected] usr]# cd /opt/ [[email protected] opt]# ll total 727676 -rw-r--r--. 1 root root 149756462 Oct 23 12:24 apache-hive-2.1.1-bin.tar.gz -rw-r--r--. 1 root root 266688029 Oct 23 12:24 hadoop-2.7.4.tar.gz -rw-r--r--. 1 root root 104659474 Oct 23 12:24 hbase-1.2.6-bin.tar.gz -rw-r--r--. 1 root root 185515842 Oct 23 12:26 jdk-8u144-linux-x64.tar.gz -rw-r--r--. 1 root root 3465760 Oct 23 12:24 mysql-connector-java-5.1.44.tar.gz -rw-r--r-- 1 root root 35042811 Oct 23 13:37 zookeeper-3.4.10.tar.gz
然后切换到/usr/app 目录下
[[email protected] ~]# cd /usr/app/
解压zookeeper到当前路径下
[[email protected] app]# tar zxvf /opt/zookeeper-3.4.10.tar.gz
建立zookeeper软连接
[[email protected] app]# ln -s zookeeper-3.4.10/ zookeeper
配置环境变量
[[email protected] app]# vi /etc/profile
在打开的文件末尾添加如下代码后保存
export JAVA_HOME=/usr/app/java export CLASS_PATH=$JAVA_HOME/lib:$JAVA_HOME/jre/lib export ZOOKEEPER_HOME=/usr/app/zookeeper
export PATH=$PATH:$JAVA_HOME/bin:$ZOOKEEPER_HOME/bin
使配置生效
[[email protected] app]# source /etc/profile
切换到conf目录,修改zookeepe配置文件
[[email protected] app]# cd /usr/app/zookeeper/conf/ [[email protected] conf]# cp zoo_sample.cfg zoo.cfg
[[email protected] conf]# vi zoo.cfg
按照如下内容修改zoo.cfg 然后保存
# The number of milliseconds of each tick tickTime=2000 # The number of ticks that the initial # synchronization phase can take initLimit=10 # The number of ticks that can pass between # sending a request and getting an acknowledgement syncLimit=5 # the directory where the snapshot is stored. # do not use /tmp for storage, /tmp here is just # example sakes. dataDir=/usr/app/zookeeper/data dataLogDir=/usr/app/zookeeper/datalog # the port at which the clients will connect clientPort=21810 # the maximum number of client connections. # increase this if you need to handle more clients #maxClientCnxns=60 # # Be sure to read the maintenance section of the # administrator guide before turning on autopurge. # # http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance # # The number of snapshots to retain in dataDir #autopurge.snapRetainCount=3 # Purge task interval in hours # Set to "0" to disable auto purge feature #autopurge.purgeInterval=1 server.1=node01:28880:38880 server.2=node02:28880:38880 server.3=node03:28880:38880
在zookeeper目录下创建data和datalog两个目录
[[email protected] conf]# mkdir -p /usr/app/zookeeper/data [[email protected] conf]# mkdir -p /usr/app/zookeeper/datalog
在data目录下创建myid文件并添加内容为1
[[email protected] conf]# cd /usr/app/zookeeper/data
[[email protected] data]# echo 1 > myid
根据以上步骤在node02、node03节点安装好zookeeper
注:node02节点上的myid内容为2,node03节点上的myid内容为3。
三个节点的zookeeper配置完成后可以测试安装是否成功
在三个节点上分别执行如下命令启动zookeeper
[[email protected] data]# zkServer.sh start ZooKeeper JMX enabled by default Using config: /usr/app/zookeeper/bin/../conf/zoo.cfg Starting zookeeper ... STARTED
分别查看三个节点的状态,可以看到一个leader和两个follower,说明zookeeper安装成功。
[[email protected] data]# zkServer.sh status ZooKeeper JMX enabled by default Using config: /usr/app/zookeeper/bin/../conf/zoo.cfg Mode: follower
[[email protected] data]# zkServer.sh status ZooKeeper JMX enabled by default Using config: /usr/app/zookeeper/bin/../conf/zoo.cfg Mode: leader
[[email protected] hadoop]# zkServer.sh status ZooKeeper JMX enabled by default Using config: /usr/app/zookeeper/bin/../conf/zoo.cfg Mode: follower
分别停止三个节点上的zookeeper
[[email protected] data]# zkServer.sh stop
3、安装hadoop
下载hadoop
这里使用的是 hadoop-2.7.4.tar.gz 下载地址:链接:http://pan.baidu.com/s/1bKNs30 密码:qxep
将下载的hadoop上传到node01服务器的/opt目录下
[[email protected] usr]# cd /opt/ [[email protected] opt]# ll total 727676 -rw-r--r--. 1 root root 149756462 Oct 23 12:24 apache-hive-2.1.1-bin.tar.gz -rw-r--r--. 1 root root 266688029 Oct 23 12:24 hadoop-2.7.4.tar.gz -rw-r--r--. 1 root root 104659474 Oct 23 12:24 hbase-1.2.6-bin.tar.gz -rw-r--r--. 1 root root 185515842 Oct 23 12:26 jdk-8u144-linux-x64.tar.gz -rw-r--r--. 1 root root 3465760 Oct 23 12:24 mysql-connector-java-5.1.44.tar.gz -rw-r--r-- 1 root root 35042811 Oct 23 13:37 zookeeper-3.4.10.tar.gz
然后切换到/usr/app 目录下
[[email protected] ~]# cd /usr/app/
解压hadoop到当前路径下
[[email protected] app]# tar zxvf /opt/hadoop-2.7.4.tar.gz
建立hadoop软连接
[[email protected] app]# ln -s hadoop-2.7.4/ hadoop
配置环境变量
[[email protected] app]# vi /etc/profile
在打开的文件末尾添加如下代码后保存
export JAVA_HOME=/usr/app/java
export CLASS_PATH=$JAVA_HOME/lib:$JAVA_HOME/jre/lib
export ZOOKEEPER_HOME=/usr/app/zookeeper
export HADOOP_HOME=/usr/app/hadoop
export PATH=$PATH:$JAVA_HOME/bin:$ZOOKEEPER_HOME/bin:$HADOOP_HOME/bin:$HADOOP_HOME/sbin
使配置生效
[[email protected] app]# source /etc/profile
切换到hadoop目录,分别创建journal、hdfs/data、hdfs/name 路径
[[email protected] app]# cd /usr/app/hadoop [[email protected] hadoop]# mkdir journal
[[email protected] hadoop]# mkdir -p hdfs/name
[[email protected] hadoop]# mkdir -p hdfs/data
切换到配置文件目录
[[email protected] hadoop]# cd /usr/app/hadoop/etc/hadoop/
将如下代码写入 core-site.xml
[[email protected] hadoop]# vi core-site.xml
<configuration>
<!-- 指定hdfs的nameservice为ns -->
<property>
<name>fs.defaultFS</name>
<value>hdfs://ns</value>
</property>
<!--指定hadoop数据临时存放目录-->
<property>
<name>hadoop.tmp.dir</name>
<value>/usr/app/hadoop/tmp</value>
</property>
<property>
<name>io.file.buffer.size</name>
<value>4096</value>
</property>
<!--指定zookeeper地址-->
<property>
<name>ha.zookeeper.quorum</name>
<value>node01:21810,node02:21810,node03:21810</value>
</property>
</configuration>
将如下代码写入到 hdfs-site.xml
[[email protected] hadoop]# vi hdfs-site.xml
<configuration> <!--指定hdfs的nameservice为ns,需要和core-site.xml中的保持一致 --> <property> <name>dfs.nameservices</name> <value>ns</value> </property> <!-- ns下面有两个NameNode,分别是nn1,nn2 --> <property> <name>dfs.ha.namenodes.ns</name> <value>nn1,nn2</value> </property> <!-- nn1的RPC通信地址 --> <property> <name>dfs.namenode.rpc-address.ns.nn1</name> <value>node01:9000</value> </property> <!-- nn1的http通信地址 --> <property> <name>dfs.namenode.http-address.ns.nn1</name> <value>node01:50070</value> </property> <!-- nn2的RPC通信地址 --> <property> <name>dfs.namenode.rpc-address.ns.nn2</name> <value>node02:9000</value> </property> <!-- nn2的http通信地址 --> <property> <name>dfs.namenode.http-address.ns.nn2</name> <value>node02:50070</value> </property> <!-- 指定NameNode的元数据在JournalNode上的存放位置 --> <property> <name>dfs.namenode.shared.edits.dir</name> <value>qjournal://node01:8485;node02:8485;node03:8485/ns</value> </property> <!-- 指定JournalNode在本地磁盘存放数据的位置 --> <property> <name>dfs.journalnode.edits.dir</name> <value>/usr/app/hadoop/journal</value> </property> <!-- 开启NameNode故障时自动切换 --> <property> <name>dfs.ha.automatic-failover.enabled</name> <value>true</value> </property> <!-- 配置失败自动切换实现方式 --> <property> <name>dfs.client.failover.proxy.provider.ns</name> <value>org.apache.hadoop.hdfs.server.namenode.ha.ConfiguredFailoverProxyProvider</value> </property> <!-- 配置隔离机制 --> <property> <name>dfs.ha.fencing.methods</name> <value>sshfence</value> </property> <!-- 使用隔离机制时需要ssh免登陆 --> <property> <name>dfs.ha.fencing.ssh.private-key-files</name> <value>/root/.ssh/id_rsa</value> </property> <property> <name>dfs.namenode.name.dir</name> <value>file:///usr/app/hadoop/hdfs/name</value> </property> <property> <name>dfs.datanode.data.dir</name> <value>file:///usr/app/hadoop/hdfs/data</value> </property> <property> <name>dfs.replication</name> <value>2</value> </property> <!-- 在NN和DN上开启WebHDFS (REST API)功能,不是必须 --> <property> <name>dfs.webhdfs.enabled</name> <value>true</value> </property> </configuration>
将如下代码写入到 mapred-site.xml
[[email protected] hadoop]# cp mapred-site.xml.template mapred-site.xml
[[email protected] hadoop]# vi mapred-site.xml
<configuration>
<property>
<name>mapreduce.framework.name</name>
<value>yarn</value>
</property>
</configuration>
将如下代码写入到 yarn-site.xml
[[email protected] hadoop]# vi yarn-site.xml
<configuration>
<!-- 指定nodemanager启动时加载server的方式为shuffle server -->
<property>
<name>yarn.nodemanager.aux-services</name>
<value>mapreduce_shuffle</value>
</property>
<!-- 指定resourcemanager地址 -->
<property>
<name>yarn.resourcemanager.hostname</name>
<value>node03</value>
</property>
</configuration>
修改 hadoop-env.sh
找到文件的export JAVA_HOME=${JAVA_HOME} 一行,将其修改为 export JAVA_HOME=/usr/app/java
修改 slaves 文件
node01
node02
node03
按照如上步骤将node02、node03节点的hadoop配置完成
至此,Hadoop HA 集群环境搭建完成,以下开始测试集群
4、集群启动
分别在node01、node02、node03 三个节点执行以下命令启动zookeeper
[[email protected] hadoop]# zkServer.sh start
验证zookeeper集群状态
[[email protected] data]# zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/app/zookeeper/bin/../conf/zoo.cfg
Mode: follower
[[email protected] data]# zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/app/zookeeper/bin/../conf/zoo.cfg
Mode: leader
[[email protected] hadoop]# zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/app/zookeeper/bin/../conf/zoo.cfg
Mode: follower启动
在node01上启动journalnode集群
[[email protected] hadoop]# hadoop-daemons.sh start journalnode
执行jps命令,可以查看到JournalNode的java进程pid
在node01上格式化zkfc
[[email protected] hadoop]# hdfs zkfc -formatZK
在node01上格式化hdfs
[[email protected] hadoop]# hadoop namenode -format
在node01上启动namenode
[[email protected] hadoop]# hadoop-daemon.sh start namenode
在node02上启动数据同步和standby的namenode
[[email protected] hadoop]# hdfs namenode -bootstrapStandby
[[email protected] hadoop]# hadoop-daemon.sh start namenode
在node01 上启动datanode
[[email protected] hadoop]# hadoop-daemons.sh start datanode
在node03上启动yarn
[[email protected] hadoop]# start-yarn.sh
在node01上启动zkfc
[[email protected] hadoop]# hadoop-daemons.sh start zkfc
全部启动后分别在node01、node02、node03上执行jps 可以看到下面这些进程
[[email protected] hadoop]# jps 7073 QuorumPeerMain 2406 DFSZKFailoverController 2150 DataNode 2248 NodeManager 2536 NameNode 1849 JournalNode 7882 Jps
[[email protected] data]# jps 1654 QuorumPeerMain 3430 Jps 1895 DataNode 1993 NodeManager 1707 JournalNode 1806 NameNode 2126 DFSZKFailoverController
[[email protected] hadoop]# jps 1604 QuorumPeerMain 2518 Jps 1961 NodeManager 1756 DataNode 1869 ResourceManager 1679 JournalNode
4、测试HA高可用性
http://192.168.1.202:50070/dfshealth.html#tab-overview
http://192.168.1.201:50070/dfshealth.html#tab-overview
在node02(active)节点关闭namenode
1 |
[[email protected] hadoop] # hadoop-daemon.sh stop namenode
|
在node02(active)节点开启namenode
1 |
[[email protected] hadoop] # hadoop-daemon.sh start namenode
|
---恢复内容结束---