我们这次使用三台机器进行HA架构的搭建 hadoop1 hadoop2 hadoop3
机器及角色分配如下:
hadoop1 | hadoop2 | hadoop3 |
---|---|---|
zookeeper1 | zookeeper2 | zookeeper3 |
journalnode1 | journalnode2 | journalnode3 |
NameNode1 | NameNode2 | |
zkfc1 | zkfc2 | |
DataNode1 | DataNode2 | DataNode3 |
ResourceManager1 | ResourceManager2 | |
NodeMananger1 | NodeMananger2 | NodeMananger3 |
<configuration>
<property>
<name>fs.defaultFSname>
<value>hdfs://ns1/value>
property>
<property>
<name>hadoop.tmp.dirname>
<value>/data/hadoop-2.7.5/data/value>
property>
<property>
<name>ha.zookeeper.quorumname>
<value>hadoop1:2181,hadoop2:2181,hadoop3:2181value>
property>
configuration>
<configuration>
<property>
<name>dfs.nameservicesname>
<value>ns1value>
property>
<property>
<name>dfs.ha.namenodes.ns1name>
<value>nn1,nn2value>
property>
<property>
<name>dfs.namenode.rpc-address.ns1.nn1name>
<value>hadoop1:9000value>
property>
<property>
<name>dfs.namenode.http-address.ns1.nn1name>
<value>hadoop1:50070value>
property>
<property>
<name>dfs.namenode.rpc-address.ns1.nn2name>
<value>hadoop2:9000value>
property>
<property>
<name>dfs.namenode.http-address.ns1.nn2name>
<value>hadoop2:50070value>
property>
<property>
<name>dfs.namenode.shared.edits.dirname>
<value>qjournal://hadoop1:8485;hadoop2:8485;hadoop3:8485/ns1value>
property>
<property>
<name>dfs.journalnode.edits.dirname>
<value>/data/hadoop-2.7.5/journaldatavalue>
property>
<property>
<name>dfs.ha.automatic-failover.enabledname>
<value>truevalue>
property>
<property>
<name>dfs.client.failover.proxy.provider.ns1name>
<value>org.apache.hadoop.hdfs.server.namenode.ha.ConfiguredFailoverProxyProvidervalue>
property>
<property>
<name>dfs.ha.fencing.methodsname>
<value>
sshfence
shell(/bin/true)
value>
property>
<property>
<name>dfs.ha.fencing.ssh.private-key-filesname>
<value>/home/hadoop/.ssh/id_rsavalue>
property>
<property>
<name>dfs.ha.fencing.ssh.connect-timeoutname>
<value>30000value>
property>
configuration>
<configuration>
<property>
<name>mapreduce.framework.namename>
<value>yarnvalue>
property>
configuration>
<configuration>
<property>
<name>yarn.resourcemanager.ha.enabledname>
<value>truevalue>
property>
<property>
<name>yarn.resourcemanager.cluster-idname>
<value>yrcvalue>
property>
<property>
<name>yarn.resourcemanager.ha.rm-idsname>
<value>rm1,rm2value>
property>
<property>
<name>yarn.resourcemanager.hostname.rm1name>
<value>hadoop1value>
property>
<property>
<name>yarn.resourcemanager.hostname.rm2name>
<value>hadoop2value>
property>
<property>
<name>yarn.resourcemanager.zk-addressname>
<value>hadoop1:2181,hadoop2:2181,hadoop3:2181value>
property>
<property>
<name>yarn.nodemanager.aux-servicesname>
<value>mapreduce_shufflevalue>
property>
configuration>
只修改hadoop1上的/data/hadoop-2.7.5/etc/hadoop/slaves
hadoop1
hadoop2
hadoop3
ssh-keygen rsa
ssh-copy-id hadoop1
./zkServer.sh start 启动
./zkserver.sh status 查看状态
hadoop-daemon.sh start journalnode
在hadoop1上运行
hadoop namenode -format
为了保证两个NameNode初始化的fsimage一致,直接把 data文件 拷贝过去
scp -r /data/hadoop-2.7.5/data/ hadoop2:/data/hadoop-2.7.5/
或者在hadoop2上执行
hdfs namenode -bootstrapStandby
在hadoop1上执行
hdfs zkfc -formatZK
是为了在zookeeper上创建相应的文件,所以执行一次就好了
hadoop1上运行
[hadoop@hadoop1 hadoop]$ start-dfs.sh
Starting namenodes on [hadoop1 hadoop2]
hadoop1: namenode running as process 3826. Stop it first.
hadoop2: namenode running as process 2965. Stop it first.
hadoop1: datanode running as process 4024. Stop it first.
hadoop3: datanode running as process 2928. Stop it first.
hadoop2: datanode running as process 3036. Stop it first.
Starting journal nodes [hadoop1 hadoop2 hadoop3]
hadoop2: journalnode running as process 2657. Stop it first.
hadoop3: journalnode running as process 2661. Stop it first.
hadoop1: journalnode running as process 3077. Stop it first.
Starting ZK Failover Controllers on NN hosts [hadoop1 hadoop2]
hadoop2: zkfc running as process 3171. Stop it first.
hadoop1: zkfc running as process 4461. Stop it first.
hadoop1上运行
[hadoop@hadoop1 hadoop]$ start-yarn.sh
starting yarn daemons
starting resourcemanager, logging to /data/hadoop-2.7.5/logs/yarn-hadoop-resourcemanager-hadoop1.out
hadoop3: starting nodemanager, logging to /data/hadoop-2.7.5/logs/yarn-hadoop-nodemanager-hadoop3.out
hadoop2: starting nodemanager, logging to /data/hadoop-2.7.5/logs/yarn-hadoop-nodemanager-hadoop2.out
hadoop1: starting nodemanager, logging to /data/hadoop-2.7.5/logs/yarn-hadoop-nodemanager-hadoop1.out
在hadoop2上启动resourcemanager
[hadoop@hadoop2 hadoop]$ yarn-daemon.sh start resourcemanager
starting resourcemanager, logging to /data/hadoop-2.7.5/logs/yarn-hadoop-resourcemanager-hadoop2.out
全部启动完成
[hadoop@hadoop1 hadoop]$ jps
3826 NameNode
3077 JournalNode
2967 QuorumPeerMain
4024 DataNode
5692 ResourceManager
4461 DFSZKFailoverController
5821 NodeManager
6223 Jps
[hadoop@hadoop2 hadoop]$ jps
2657 JournalNode
3890 Jps
3171 DFSZKFailoverController
2965 NameNode
3782 ResourceManager
3640 NodeManager
2585 QuorumPeerMain
3036 DataNode
[hadoop@hadoop3 hadoop]$ jps
2928 DataNode
2661 JournalNode
2566 QuorumPeerMain
3270 NodeManager
3403 Jps
需要把配置文件拷贝到代码路径下
package cn.itcast.hadoop.hdfs;
import java.net.URI;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
public class HdfsUtailHA {
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://ns1/"), conf, "hadoop");
fs.copyFromLocalFile(new Path("/home/hadoop/jdk-8u161-linux-x64.tar.gz"), new Path("hdfs://ns1/"));
}
}