欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

HadoopHA高可用

时间:2023-07-23

个人学习整理,所有资料来自尚硅谷

文章目录

Hadoop HA高可用

1、HA概述2、HDFS-HA集群搭建

2.1 HDFS-HA核心问题 3、HDFS-HA手动模式

3.1 环境准备3.2 配置HDFS-HA集群3.3 启动HDFS-HA集群 4、HDFS-HA自动模式

4.1 HDFS-HA自动故障转移工作机制4.2 HDFS-HA自动故障转移的集群规划4.3 配置HDFS-HA自动故障转移

4.3.1 具体配置4.3.2 启动 5、YARN-HA配置

5.1 YARN-HA工作机制5.2 配置YARN-HA集群 6、HADOOP HA 的最终规划 Hadoop HA高可用 1、HA概述 所谓HA(High Availability),即高可用(7*24小时不中断服务)实现高可用最关键的策略是消除单点故障。HA严格来说应该分成各个组件的HA机制:HDFS的HA和YARN的HA.NameNode主要在以下两个方面影响HDFS集群:

NameNode机器发生意外,如宕机,集群将无法使用,直到管理员重启。

NameNode机器需要升级,包括软件、硬件升级,此时集群也将无法使用。

HDFS HA功能通过配置多个NameNode(Active/Standby)实现在集群中对NameNode的热备来解决上述问题。如果出现故障,如机器崩溃或者机器需要升级维护,此时可通过此方式将NameNode很快的切换到另外一台机器。

2、HDFS-HA集群搭建

​ 当前HDFS集群的规划

hadoop102hadoop103hadoop104NameNodeSecondarynamenodeDataNodeDataNodeDataNode

​ HA的主要目的是消除namenode的单点故障,需要将hdfs集群规划成以下模样

hadoop102hadoop103hadoop104NameNodeNameNodeNameNodeDataNodeDataNodeDataNode2.1 HDFS-HA核心问题

怎么保证三台namenode的数据一致?

a、Fsimage:让一台nn生成数据,让其他机器nn同步

b、Edits:需要引进新的模块JournalNode来保证edits的文件的数据一致性

怎么让同时只有一台nn是active,其他所有是standby?

a、手动分配

b、自动分配

2nn在ha架构中并不存在,定期合并fsimage和edits谁来做?

由standby的nn来做

如果nn发生了什么问题,如何让其他的nn上位干活?

a、手动故障转移

b、自动故障转移

3、HDFS-HA手动模式 3.1 环境准备 修改IP修改主机名及主机名和IP地址的映射关闭防火墙ssh免密登录安装JDK,配置环境变量等 3.2 配置HDFS-HA集群 在opt目录下创建一个ha文件

[atguigu@hadoop102 ~]$ cd /opt[atguigu@hadoop102 opt]$ sudo mkdir ha[atguigu@hadoop102 opt]$ sudo chown atguigu:atguigu /opt/ha

将/opt/module下的hadoop-3.1.3拷贝到/opt/ha目录下(删除data和log目录)

[atguigu@hadoop102 opt]$ cp -r /opt/module/hadoop-3.1.3 /opt/ha/

配置core-site.xml

fs.defaultFS hdfs://myclusterhadoop.tmp.dir/opt/ha/hadoop-3.1.3/data

配置hdfs-site.xml

dfs.namenode.name.dirfile://${hadoop.tmp.dir}/namedfs.datanode.data.dirfile://${hadoop.tmp.dir}/datadfs.journalnode.edits.dir${hadoop.tmp.dir}/jndfs.nameservicesmyclusterdfs.ha.namenodes.myclusternn1,nn2,nn3dfs.namenode.rpc-address.mycluster.nn1hadoop102:8020dfs.namenode.rpc-address.mycluster.nn2hadoop103:8020 dfs.namenode.rpc-address.mycluster.nn3hadoop104:8020dfs.namenode.http-address.mycluster.nn1hadoop102:9870dfs.namenode.http-address.mycluster.nn2hadoop103:9870dfs.namenode.http-address.mycluster.nn3hadoop104:9870 dfs.namenode.shared.edits.dir qjournal://hadoop102:8485;hadoop103:8485;hadoop104:8485/myclusterdfs.client.failover.proxy.provider.myclusterorg.apache.hadoop.hdfs.server.namenode.ha.ConfiguredFailoverProxyProviderdfs.ha.fencing.methodssshfencedfs.ha.fencing.ssh.private-key-files/home/atguigu/.ssh/id_rsa

分发配置好的hadoop环境到其他节点 3.3 启动HDFS-HA集群 将HADOOP_HOME环境更改到HA目录(三台机器)

[atguigu@hadoop102 ~]$ sudo vim /etc/profile.d/my_env.sh#HADOOP_HOMEexport HADOOP_HOME=/opt/ha/hadoop-3.1.3export PATH=$PATH:$HADOOP_HOME/binexport PATH=$PATH:$HADOOP_HOME/sbin

在三台机器上source环境变量

[atguigu@hadoop102 ~]$source /etc/profile

在各个JournalNode节点上,输入以下命令启动journalnode服务

[atguigu@hadoop102 ~]$ hdfs --daemon start journalnode[atguigu@hadoop103 ~]$ hdfs --daemon start journalnode[atguigu@hadoop104 ~]$ hdfs --daemon start journalnode

在【nn1】上,对其进行格式化并启动

[atguigu@hadoop102 ~]$ hdfs namenode -format[atguigu@hadoop102 ~]$ hdfs --daemon start namenode

在【nn2】和【nn3】上同步【nn1】的元数据信息

[atguigu@hadoop103 ~]$ hdfs namenode -bootstrapStandby[atguigu@hadoop104 ~]$ hdfs namenode -bootstrapStandby

启动【nn2】和【nn3】

[atguigu@hadoop103 ~]$ hdfs --daemon start namenode[atguigu@hadoop104 ~]$ hdfs --daemon start namenode

查看web页面显示

三台机器目前都是standby,手动配置高可用集群,需将一台改成active。

在所有节点上,启动datanode

[atguigu@hadoop102 ~]$ hdfs --daemon start datanode[atguigu@hadoop103 ~]$ hdfs --daemon start datanode[atguigu@hadoop104 ~]$ hdfs --daemon start datanode

将【nn1】切换为Active

[atguigu@hadoop102 ~]$ hdfs haadmin -transitionToActive nn1

查看是否Active

[atguigu@hadoop102 ~]$ hdfs haadmin -getServiceState nn1

kill掉hadoop102上的NameNode进程

此时在hadoop103上将【nn2】切换为Active,出现如下情况:

再次启动【nn1】:

[atguigu@hadoop102 ~]$ hdfs --daemon start namenode

此时hadoop102状态重新变为standby,此时若再在hadoop103上将【nn2】切换为Active:

[atguigu@hadoop103 ~]$ hdfs haadmin -transitionToActive nn2[atguigu@hadoop103 ~]$ hdfs haadmin -getServiceState nn2active

分析:为什么手动配置高可用集群时需要所有namenode是启动状态,才能让其中一个节点转换为active?

原因:当前集群中设置了一个隔离机制,同一时间只能允许有一个active的namenode对外服务。现在配置了三个namenode,要让hadoop102的namenode切换为Active就要保证它能和hadoop103和hadoop104相互连接。如果hadoop102与hadoop104无法连接成功,那么只能代表hadoop102与hadoop104之间无法通信,但是hadoop104可能能与其他服务器进行通信。假如hadoop104的namenode是Active状态,然后现在再让hadoop102的namenode切换为Active,那么之后就会出现两个Acitve,出现脑裂情况,因此手动配置高可用集群时需要所有namenode是启动状态。

因此这种HA手动模式并不是真正意义上的高可用。

4、HDFS-HA自动模式 4.1 HDFS-HA自动故障转移工作机制

​ 自动故障转移为HDFS部署增加了两个新组件:Zookeeper和ZKFailoverController(ZKFC)进程。Zookeeper是维护少量协调数据,通知客户端这些数据的改变和监视客户端故障的高可用服务。

4.2 HDFS-HA自动故障转移的集群规划 hadoop102hhadoop103hadoop104NameNodeNameNodeNameNodeJournalNodeJournalNodeJournalNodeDataNodeDataNodeDataNodeZookeeperZookeeperZookeeperZKFCZKFCZKFC4.3 配置HDFS-HA自动故障转移 4.3.1 具体配置 在hdfs-site.xml中增加

dfs.ha.automatic-failover.enabledtrue

在core-site.xml文件中增加

ha.zookeeper.quorumhadoop102:2181,hadoop103:2181,hadoop104:2181

进行分发

[atguigu@hadoop102 hadoop]$ xsync hdfs-site.xml core-site.xml

4.3.2 启动 关闭所有HDFS服务

[atguigu@hadoop102 hadoop]$ stop-dfs.sh

启动Zookeeper集群

[atguigu@hadoop102 hadoop]$ zk.sh start

zk.sh脚本:

[atguigu@hadoop102 bin]$ cat zk.sh #!/bin/bashcase $1 in"start"){for i in hadoop102 hadoop103 hadoop104doecho ----------zookeeper $i 启动----------ssh $i "/opt/module/zookeeper-3.5.7/bin/zkServer.sh start" done};;"stop"){for i in hadoop102 hadoop103 hadoop104 do echo ----------zookeeper $i 停止---------- ssh $i "/opt/module/zookeeper-3.5.7/bin/zkServer.sh stop" done};;"status"){for i in hadoop102 hadoop103 hadoop104 do echo ----------zookeeper $i 状态 ---------- ssh $i "/opt/module/zookeeper-3.5.7/bin/zkServer.sh status" done};;esac

启动Zookeeper以后,然后再初始HA在Zookeeper中状态

[atguigu@hadoop102 hadoop-3.1.3]$ hdfs zkfc -formatZK

启动HDFS服务:

[atguigu@hadoop102 hadoop-3.1.3]$ start-dfs.sh

查看进程:

可以去zkCli.sh客户端查看Namenode选举锁节点内容:

[atguigu@hadoop102 zookeeper-3.5.7]$ bin/zkCli.sh[zk: localhost:2181(CONNECTED) 10] get -s /hadoop-ha/mycluster/ActiveStandbyElectorLock

发现hadoop103的namenode为active,观察web页面:

hadoop102

hadoop103

hadoop104
Kill掉Active的namenode的进程(注意:是在hadoop103上kill):

[atguigu@hadoop103 ~]$ jps41681 DFSZKFailoverController42789 Jps41334 NameNode41560 JournalNode41145 QuorumPeerMain41420 DataNode[atguigu@hadoop103 ~]$ kill -9 41334

再次去zkCli.sh客户端查看:

[zk: localhost:2181(CONNECTED) 0] get -s /hadoop-ha/mycluster/ActiveActiveBreadCrumb ActiveStandbyElectorLock [zk: localhost:2181(CONNECTED) 0] get -s /hadoop-ha/mycluster/ActiveStandbyElectorLockmyclusternn3hadoop104 �>(�>cZxid = 0x1100000016ctime = Tue Feb 01 16:42:21 CST 2022mZxid = 0x1100000016mtime = Tue Feb 01 16:42:21 CST 2022pZxid = 0x1100000016cversion = 0dataVersion = 0aclVersion = 0ephemeralOwner = 0x300137a86600001dataLength = 33numChildren = 0

发现hadoop104的namenode转移为active,观察web页面:

hadoop104

hadoop102

再次Kill掉Active的namenode的进程(注意:此时是在hadoop104上kill):

[atguigu@hadoop104 ~]$ jps40529 JournalNode40115 QuorumPeerMain40391 DataNode42136 Jps40652 DFSZKFailoverController40303 NameNode[atguigu@hadoop104 ~]$ kill -9 40303[atguigu@hadoop104 ~]$ jps40529 JournalNode40115 QuorumPeerMain40391 DataNode40652 DFSZKFailoverController42175 Jps

再次去zkCli.sh客户端查看:

[zk: localhost:2181(CONNECTED) 0] get -s /hadoop-ha/mycluster/ActiveStandbyElectorLockmyclusternn1hadoop102 �>(�>cZxid = 0x110000001bctime = Tue Feb 01 16:48:07 CST 2022mZxid = 0x110000001bmtime = Tue Feb 01 16:48:07 CST 2022pZxid = 0x110000001bcversion = 0dataVersion = 0aclVersion = 0ephemeralOwner = 0x400137b09dd0000dataLength = 33numChildren = 0

发现hadoop102的namenode转移为active,观察web页面:

hadoop102

最后重启hadoop103、hadoop104上的namenode进程

[atguigu@hadoop103 zookeeper-3.5.7]$ hdfs --daemon start namenode[atguigu@hadoop104 zookeeper-3.5.7]$ hdfs --daemon start namenode

观察web页面:

hadoop103

hadoop104 5、YARN-HA配置 5.1 YARN-HA工作机制

当前可以启动多个ResourceManager,谁先启动就会现在Zookeeper中注册一个临时节点,并成为Active ResourceManager,后启动的也会尝试注册,但会发现该临时节点已存在,成为Standby ResourceManager。所有Standby ResourceManager会维护一个长轮询查看该节点信息是否存在,若该临时节点不存在了(即Active ResourceManager挂了,该临时节点自动删除了),那么Standby ResourceManager将自动切换成Active ResourceManager。

5.2 配置YARN-HA集群 规划集群 hadoop102hadoop103hadoop104ResourceManagerResourceManagerResourceManagerNodeManagerNodeManagerNodeManagerZookeeperZookeeperZookeeper核心问题:

如果 如果当前 active rm 挂了,其他 rm 怎么将其他 standby rm 上位?

核心原理跟 hdfs 一样,利用了 zk 的临时节点。

当前 rm 上有很多的计算程序在等待运行, 其他的 rm 怎么将这些程序接手过来接着跑?

rm 会将当前的所有计算程序的状态存储在 zk 中,其他 rm 上位后会去读取,然后接着跑。

具体配置

yarn-site.xml

yarn.nodemanager.aux-servicesmapreduce_shuffleyarn.resourcemanager.ha.enabledtrueyarn.resourcemanager.cluster-idcluster-yarn1yarn.resourcemanager.ha.rm-idsrm1,rm2,rm3yarn.resourcemanager.hostname.rm1hadoop102yarn.resourcemanager.webapp.address.rm1hadoop102:8088yarn.resourcemanager.address.rm1hadoop102:8032 yarn.resourcemanager.scheduler.address.rm1hadoop102:8030yarn.resourcemanager.resource-tracker.address.rm1hadoop102:8031yarn.resourcemanager.hostname.rm2hadoop103yarn.resourcemanager.webapp.address.rm2hadoop103:8088yarn.resourcemanager.address.rm2hadoop103:8032yarn.resourcemanager.scheduler.address.rm2hadoop103:8030yarn.resourcemanager.resource-tracker.address.rm2hadoop103:8031yarn.resourcemanager.hostname.rm3hadoop104yarn.resourcemanager.webapp.address.rm3hadoop104:8088yarn.resourcemanager.address.rm3 hadoop104:8032yarn.resourcemanager.scheduler.address.rm3hadoop104:8030yarn.resourcemanager.resource-tracker.address.rm3hadoop104:8031yarn.resourcemanager.zk-addresshadoop102:2181,hadoop103:2181,hadoop104:2181yarn.resourcemanager.recovery.enabledtrueyarn.resourcemanager.store.classorg.apache.hadoop.yarn.server.resourcemanager.recovery.ZKRMStateStore yarn.nodemanager.env-whitelist JAVA_HOME,HADOOP_COMMON_HOME,HADOOP_HDFS_HOME,HADOOP_CONF_DIR,CLASSPATH_PREPEND_DISTCACHE,HADOOP_YARN_HOME,HADOOP_MAPRED_HOME

​ 分发yarn-site.xml。

启动YARN

(1)启动yarn

[atguigu@hadoop102 hadoop]$ start-yarn.sh Starting resourcemanagers on [ hadoop102 hadoop103 hadoop104]Starting nodemanagers[atguigu@hadoop102 hadoop]$ jpsall =============== hadoop102 ===============46101 DataNode46566 DFSZKFailoverController48871 ResourceManager46360 JournalNode49194 Jps48989 NodeManager45646 QuorumPeerMain47631 NameNode=============== hadoop103 ===============41681 DFSZKFailoverController44545 NodeManager44897 Jps44466 ResourceManager43221 NameNode41560 JournalNode41145 QuorumPeerMain41420 DataNode=============== hadoop104 ===============43744 NodeManager40529 JournalNode42434 NameNode40115 QuorumPeerMain43923 Jps40391 DataNode40652 DFSZKFailoverController43663 ResourceManager

​ (2)查看服务状态:

[atguigu@hadoop102 hadoop]$ yarn rmadmin -getServiceState rm1standby[atguigu@hadoop102 hadoop]$ yarn rmadmin -getServiceState rm2active[atguigu@hadoop102 hadoop]$ yarn rmadmin -getServiceState rm3standby

​ (3)可以去zkCli.sh客户端查看ResourceManager选举锁节点内容:

[zk: localhost:2181(CONNECTED) 0] get -s /yarn-leader-election/cluster-yarn1/ActiveStandbyElectorLockcluster-yarn1rm2cZxid = 0x1100000030ctime = Tue Feb 01 17:19:03 CST 2022mZxid = 0x1100000030mtime = Tue Feb 01 17:19:03 CST 2022pZxid = 0x1100000030cversion = 0dataVersion = 0aclVersion = 0ephemeralOwner = 0x300137a86600005dataLength = 20numChildren = 0

​ (4)web查看hadoop102:8088的yarn状态:

自动跳转至hadoop103:8088/cluster

​ (5)若kill掉hadoop103上的ResourceManager进程

[atguigu@hadoop103 zookeeper-3.5.7]$ jps41681 DFSZKFailoverController44545 NodeManager44466 ResourceManager43221 NameNode41560 JournalNode41145 QuorumPeerMain45195 Jps41420 DataNode[atguigu@hadoop103 zookeeper-3.5.7]$ kill -9 44466

查看服务状态:

[atguigu@hadoop103 zookeeper-3.5.7]$ yarn rmadmin -getServiceState rm1active[atguigu@hadoop103 zookeeper-3.5.7]$ yarn rmadmin -getServiceState rm22022-02-01 17:46:50,053 INFO ipc.Client: Retrying connect to server: hadoop103/192.168.10.103:8033、Already tried 0 time(s); retry policy is RetryUpToMaximumCountWithFixedSleep(maxRetries=1, sleepTime=1000 MILLISECONDS)Operation failed: Call From hadoop103/192.168.10.103 to hadoop103:8033 failed on connection exception: java.net.ConnectException: 拒绝连接; For more details see: http://wiki.apache.org/hadoop/ConnectionRefused[atguigu@hadoop103 zookeeper-3.5.7]$ yarn rmadmin -getServiceState rm3standby

web查看hadoop102:8088和hadoop104:8088的yarn状态:

自动跳转至hadoop102:8088/cluster

6、HADOOP HA 的最终规划

将整个 ha 搭建完成后,集群的最终规划:

hadoop102hadoop103hadoop104NameNodeNameNodeNameNodeJournalNodeJournalNodeJournalNodeDataNodeDataNodeDataNodeZookeeperZookeeperZookeeperZKFCZKFCZKFCResourceManagerResourceManagerResourceManagerNodeManagerNodeManagerNodeManager

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。