一、写在前面💕
大家好!这篇文章是我在搭建Hdfs的HA(高可用)时写下的详细笔记与感想,希望能帮助到大家!本篇文章收录于 初心 的 大数据 专栏。
🏠 个人主页:初心%个人主页
🧑 个人简介:大家好,我是初心,和大家共同努力
💕 座右铭:理想主义的花,终究会盛开在浪漫主义的土壤里!🍺🍺🍺
💕欢迎大家:这里是CSDN,我记录知识的地方,喜欢的话请三连,有问题请私信😘
二、Zookeeper安装✨
- 1.将Zookeeper压缩包上传到 Hadoop102的/opt/software 目录下
这里我们还是使用Xshell+Xftp进行文件上传,将Zookeeper上传。
- 2.解压到 /opt/module/HA 目录下
tar -xzvf /opt/software/apache-zookeeper-3.5.7-bin.tar.gz -C /opt/module/HA/
- 3.将Zookeeper重命名
mv /opt/module/HA/apache-zookeeper-3.5.7-bin/ zookeeper
- 4.重命名Zookeeper的zoo_sample.cfg为zoo.cfg文件
mv zoo_sample.cfg zoo.cfg
- 5.修改zoo.cfg文件
vim /opt/module/HA/zookeeper/conf/zoo.cfg
按下G,按下o,直接插入到最后一行,插入以下内容,其中hadoop102,hadoop103,hadoop104分别是三个节点的主机名。
server.1=hadoop102:2888:3888 server.2=hadoop103:2888:3888 server.3=hadoop104:2888:3888
- 6.新建myid文件
进入到zookeeper目录下:
cd /opt/module/HA/zookeeper/
新建zkData目录:
mkdir zkData
进入到zkData目录,并新建myid文件:
cd zkData
vim myid
只需分别在Hadoop102,Hadoop103,Hadoop104的myid文件添加数字 1,2,3 即可,保存退出:
- 7.配置环境变量
vim /etc/profile.d/my_env.sh
插入以下内容:
# ZOOKEEPER_HOME export ZOOKEEPER_HOME=/opt/module/HA/zookeeper export PATH=$PATH:$ZOOKEEPER_HOME/bin
- 8.刷新环境变量
source /etc/profile.d/my_env.sh
- 9.分发
这里需要分发的有Zookeeper,my_env.sh文件,以及都要刷新环境变量。前面两个命令在hadoop102执行即可,第三个命令在hadoop103,hadoop104上都要执行一次。
xsync /opt/module/HA/zookeeper/
xsync /etc/profile.d/my_env.sh
source /etc/profile.d/my_env.sh
三、Hadoop配置✨
- 1.保留原来的Hadoop集群
为什么要保留原来的集群? 在搭建Hadoop高可用之前,我们的集群是有Hdfs,Yarn,JobHistory,这些我们日后还需要继续学习使用,因此我选择保留下来,换句话说,**即使我们高可用(HA)搭建失败了,我们还能回到开始的状态。**保留方式就是我们搭建的时候不要直接使用Hadoop目录,而是复制一份。
- 2.复制Hadoop目录
cp -r /opt/module/hadoop-3.1.3/ /opt/module/HA/
- 3.删除data和logs目录
cd /opt/module/HA/hadoop-3.1.3/
rm -rf data
rm -rf logs
- 4.新建Zookeeper相关目录
cd /opt/module/HA/
mkdir logs
mkdir tmp
- 5.修改两个配置文件
这里的两个配置文件是 core-site.xml
文件和 hdfs-site.xml
文件,这是Hadoop目录中仅需要修改的两个文件.这里以注释形式给出需要修改的地方,不用修改这两个文件,直接覆盖即可.
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> <!-- 集群名称,此值在接下来的配置中将多次出现务必注意同步修改 --> <property> <name>dfs.nameservices</name> <value>mycluster</value> </property> <!-- 所有的namenode列表,此处也只是逻辑名称,非namenode所在的主机名称 --> <property> <name>dfs.ha.namenodes.mycluster</name> <value>nn1,nn2</value> </property> <!-- namenode之间用于RPC通信的地址,value填写namenode所在的主机地址 --> <!-- 默认端口8020,注意mycluster1与nn1要和上文的配置一致 --> <property> <name>dfs.namenode.rpc-address.mycluster.nn1</name> <value>hadoop102:8020</value> </property> <property> <name>dfs.namenode.rpc-address.mycluster.nn2</name> <value>hadoop103:8020</value> </property> <!-- namenode的web访问地址,默认端口9870 --> <property> <name>dfs.namenode.http-address.mycluster.nn1</name> <value>hadoop102:9870</value> </property> <property> <name>dfs.namenode.http-address.mycluster.nn2</name> <value>hadoop103:9870</value> </property> <!-- journalnode主机地址,最少三台,默认端口8485 --> <!-- 格式为 qjournal://jn1:port;jn2:port;jn3:port/${nameservices} --> <!-- a shared edits dir must not be specified if HA is not enabled --> <!-- 伪分布式时,取消该配置 --> <property> <name>dfs.namenode.shared.edits.dir</name> <value>qjournal://hadoop102:8485;hadoop103:8485;hadoop104:8485/mycluster</value> </property> <!-- 故障时自动切换的实现类,照抄即可 --> <property> <name>dfs.client.failover.proxy.provider.mycluster</name> <value>org.apache.hadoop.hdfs.server.namenode.ha.ConfiguredFailoverProxyProvider</value> </property> <!-- 故障时相互操作方式(namenode要切换active和standby),这里我们选ssh方式 --> <property> <name>dfs.ha.fencing.methods</name> <value>sshfence</value> </property> <!-- 修改为自己用户的ssh key存放地址 --> <property> <name>dfs.ha.fencing.ssh.private-key-files</name> <value>/home/sky/.ssh/id_rsa</value> </property> <!-- namenode日志文件输出路径,即journalnode读取变更的位置 --> <property> <name>dfs.journalnode.edits.dir</name> <value>/opt/module/HA/logs/</value> </property> <!-- 启用自动故障转移 --> <property> <name>dfs.ha.automatic-failover.enabled</name> <value>true</value> </property> <property> <name>dfs.webhdfs.enabled</name> <value>true</value> </property> <!-- 解决 DataXceiver error processing WRITE_BLOCK operation src --> <property> <name>dfs.datanode.max.transfer.threads</name> <value>8192</value> <description> Specifies the maximum number of threads to use for transferring data in and out of the DN. </description> </property> </configuration>
<?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> <!-- HDFS主入口,mycluster仅是作为集群的逻辑名称,可随意更改但务必与hdfs-site.xml中dfs.nameservices值保持一致 --> <property> <name>fs.defaultFS</name> <value>hdfs://mycluster</value> </property> <!-- 默认的hadoop.tmp.dir指向的是/tmp目录,将导致namenode与datanode数据全都保存在易失目录中,此处进行修改 --> <property> <name>hadoop.tmp.dir</name> <value>/opt/module/HA/tmp/</value> <!-- <value>/opt/bigdata/hadoopha</value> --> </property> <!-- 用户角色配置,不配置此项会导致web页面报错(不能操作数据) --> <property> <name>hadoop.http.staticuser.user</name> <value>sky</value> </property> <!-- zookeeper集群地址,这里只配置了单台,如是集群以逗号进行分隔 --> <property> <name>ha.zookeeper.quorum</name> <value>hadoop102:2181,hadoop103:2181,hadoop104:2181</value> </property> <!-- 权限配置 hadoop.proxyuser.{填写自己的用户名}.hosts--> <property> <name>hadoop.proxyuser.sky.hosts</name> <value>*</value> </property> <property> <name>hadoop.proxyuser.sky.groups</name> <value>*</value> </property> <!-- 解决journalnode连接不上,导致namenode启动问题 --> <!-- 也有可能是网络的问题,参考该文章:https://blog.csdn.net/tototuzuoquan/article/details/89644127 --> <!-- 在dev环境中出现连不上journalnode问题,添加该配置,以增加重试次数和间隔 --> <property> <name>ipc.client.connect.max.retries</name> <value>100</value> <description>Indicates the number of retries a client will make to establish a server connection.</description> </property> <property> <name>ipc.client.connect.retry.interval</name> <value>10000</value> <description>Indicates the number of milliseconds a client will wait for before retrying to establish a server connection.</description> </property> </configuration>
- 6.修改环境变量
vim /etc/profile.d/my_env.sh
将HADOOP_HOME改为新的Hadoop目录所在路径:
# HADOOP_HOME export HADOOP_HOME=/opt/module/HA/hadoop-3.1.3
- 7.分发Hadoop目录和my_env.sh文件
xsync /opt/module/HA/hadoop-3.1.3/
xsync /etc/profile.d/my_env.sh
- 8.刷新环境变量
分别在三个节点上刷新环境变量:
source /etc/profile.d/my_env.sh
四、Hadoop HA自动模式✨
- 1.修改hadoop/etc/hadoop/hadoop-env.sh文件
vim /opt/module/HA/hadoop-3.1.3/etc/hadoop/hadoop-env.sh
在末尾插入以下内容,sky是用户名,因为我至始至终都没使用root账号.
export HDFS_ZKFC_USER=sky export HDFS_JOURNALNODE_USER=sky
- 2.分发
xsync /opt/module/HA/hadoop-3.1.3/
- 2.启动与初始化集群
启动Zookeeper:
zkServer.sh start
刷新环境变量:
source /etc/profile.d/my_env.sh
启动Hdfs:
myhadoop start
myhadoop是我另外一个Hadoop集群启停脚本,具体的脚本内容和使用请参考:Hadoop集群启停脚本
- 3.查看namenode的活跃状态
zkServer.sh status
五、HA脚本分享✨
脚本名字是myHA.sh,功能是实现一键启动和停止Zookeeper和Hdfs,查看zookeeper状态.
#! /bin/bash if [ $# -lt 1 ] then echo "No Args Input..." exit; fi case $1 in "start"){ echo "----------启动zookeeper----------" for i in hadoop102 hadoop103 hadoop104 do echo ---------- zookeeper $i 启动 ------------ ssh $i "/opt/module/HA/zookeeper/bin/zkServer.sh start" done echo "---------- 启动hdfs------------" ssh hadoop102 "/opt/module/HA/hadoop-3.1.3/sbin/start-dfs.sh" echo "---------- hadoop HA启动成功------------" };; "stop"){ echo "----------关闭hdfs----------" ssh hadoop102 "/opt/module/HA/hadoop-3.1.3/sbin/stop-dfs.sh" echo "----------关闭zookeeper----------" for i in hadoop102 hadoop103 hadoop104 do echo ---------- zookeeper $i 停止 ------------ ssh $i "/opt/module/HA/zookeeper/bin/zkServer.sh stop" done echo "---------- hadoop HA停止成功------------" };; "status"){ for i in hadoop102 hadoop103 hadoop104 do echo ---------- zookeeper $i 状态 ------------ ssh $i "/opt/module/HA/zookeeper/bin/zkServer.sh status" done };; *) echo "Input Args Error" ;; esac
七、结语💕
😎 本文主要讲解了如何搭建Hdfs的高可用(HA),后期还会出加上Yarn的高可用教程,大家可以期待一下哦!😊
✨ 这就是今天要分享给大家的全部内容了,我们下期再见!😊
🍻 世间所有的相遇,都是久别重逢~
😍
🏠 我在CSDN等你哦!😍