高可用集群环境下,至少需要3台服务器,这里准备5台。
IP地址 | 主机名称 | 角色 |
---|---|---|
10.0.0.5 | node1 | JournalNode、NameNode、ResourceManager |
10.0.0.6 | node2 | JournalNode、NameNode、ResourceManager |
10.0.0.7 | node3 | JournalNode、DataNode、NodeManager |
10.0.0.8 | node4 | DataNode、NodeManager |
10.0.0.9 | node5 | DataNode、NodeManager |
需要保证每台服务器的配置都一致,以下步骤在5台服务器上都需要做一次。
本次安装采用的操作系统是Ubuntu 20.04。
更新一下软件包列表。
sudo apt-get update
使用命令安装Java 8。
sudo apt-get install -y openjdk-8-jdk
配置环境变量。
vi ~/.bashrc
export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64
让环境变量生效。
source ~/.bashrc
从Hadoop官网Apache Hadoop下载安装包软件。
或者直接通过命令下载。
wget https://dlcdn.apache.org/hadoop/common/hadoop-3.3.4/hadoop-3.3.4.tar.gz
可以选择以下任意一种方式安装Zookeeper,本文选择集群环境安装。
参考大数据必知必会:Zookeeper(1)单机环境安装
参考大数据必知必会:Zookeeper(2)伪分布式安装
参考大数据必知必会:Zookeeper(3)集群环境安装
高可用集群是在多个节点上运行进程来实现Hadoop集群,并在集群中提供两个NameNode、两个ResourceManager节点。
在后续使用过程中,都使用主机名称,所以需要配置域名解析。
配置 /etc/hosts
。
由于该配置文件的修改需要root权限,所以在每个节点上都手动配置。
10.0.0.5 node1
10.0.0.6 node2
10.0.0.7 node3
10.0.0.8 node4
10.0.0.8 node5
以下配置过程在node1上完成,并且配置完成后将配置文件复制到其他节点。
Hadoop分布式集群的运行,需要配置密钥对实现免密登录。
hadoop@node1:~$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/hadoop/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/hadoop/.ssh/id_rsa
Your public key has been saved in /home/hadoop/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:pp2AC1bQAQ5J6CJJCij1QA7bgKOsVxpoPVNi+cxhcyg hadoop@node1
The key's randomart image is:
+---[RSA 3072]----+
|O=*oo.. |
|OX E.* . |
|X+* @ + |
|B+.=.= |
|= o++ . S |
|..o. . = . |
| . . . o |
| |
| |
+----[SHA256]-----+
hadoop@node1:~$ cp ~/.ssh/id_rsa.pub ~/.ssh/authorized_keys
hadoop@node1:~$ scp -r .ssh node1:~/
id_rsa.pub 100% 566 1.7MB/s 00:00
authorized_keys 100% 566 2.0MB/s 00:00
known_hosts 100% 1332 4.5MB/s 00:00
id_rsa 100% 2602 10.1MB/s 00:00
hadoop@node1:~$ scp -r .ssh node2:~/
hadoop@node2's password:
id_rsa.pub 100% 566 934.6KB/s 00:00
authorized_keys 100% 566 107.3KB/s 00:00
known_hosts 100% 1332 2.5MB/s 00:00
id_rsa 100% 2602 4.8MB/s 00:00
hadoop@node1:~$ scp -r .ssh node3:~/
hadoop@node3's password:
id_rsa.pub 100% 566 1.0MB/s 00:00
authorized_keys 100% 566 1.3MB/s 00:00
known_hosts 100% 1332 2.8MB/s 00:00
id_rsa 100% 2602 5.2MB/s 00:00
hadoop@node1:~$ scp -r .ssh node4:~/
hadoop@node3's password:
id_rsa.pub 100% 566 1.0MB/s 00:00
authorized_keys 100% 566 1.3MB/s 00:00
known_hosts 100% 1332 2.8MB/s 00:00
id_rsa 100% 2602 5.2MB/s 00:00
hadoop@node1:~$ scp -r .ssh node5:~/
hadoop@node3's password:
id_rsa.pub 100% 566 1.0MB/s 00:00
authorized_keys 100% 566 1.3MB/s 00:00
known_hosts 100% 1332 2.8MB/s 00:00
id_rsa 100% 2602 5.2MB/s 00:00
确保执行ssh命令的时候不需要输入密码。
hadoop@node1:~$ ssh node1
hadoop@node1:~$ ssh node2
hadoop@node1:~$ ssh node3
hadoop@node1:~$ ssh node4
hadoop@node1:~$ ssh node5
将安装包解压到目标路径。
hadoop@node1:~$ mkdir -p apps
hadoop@node1:~$ tar -xzf hadoop-3.3.4.tar.gz -C apps
bin目录下存放的是Hadoop相关的常用命令,比如操作HDFS的hdfs命令,以及hadoop、yarn等命令。
etc目录下存放的是Hadoop的配置文件,对HDFS、MapReduce、YARN以及集群节点列表的配置都在这个里面。
sbin目录下存放的是管理集群相关的命令,比如启动集群、启动HDFS、启动YARN、停止集群等的命令。
share目录下存放了一些Hadoop的相关资源,比如文档以及各个模块的Jar包。
在集群的每个节点上都配置Hadoop的环境变量,Hadoop集群在启动的时候可以使用start-all.sh一次性启动集群中的HDFS和Yarn,为了能够正常使用该命令,需要将其路径配置到环境变量中。
hadoop@node1:~$ vi ~/.bashrc
export HADOOP_HOME=/home/hadoop/apps/hadoop-3.3.4
export HADOOP_CONF_DIR=/home/hadoop/apps/hadoop-3.3.4/etc/hadoop
export YARN_CONF_DIR=/home/hadoop/apps/hadoop-3.3.4/etc/hadoop
export PATH=$HADOOP_HOME/bin:$HADOOP_HOME/sbin:$PATH
使环境变量生效。
hadoop@node1:~$ source ~/.bashrc
Hadoop软件安装完成后,每个节点上的Hadoop都是独立的软件,需要进行配置才能组成Hadoop集群。Hadoop的配置文件在$HADOOP_HOME/etc/hadoop目录下,主要配置文件有6个:
这几个配置文件如果不存在,可以通过复制配置模板的方式创建,也可以通过创建新文件的方式创建。需要保证在集群的每个节点上这6个配置保持同步,可以在每个节点单独配置,也可以在一个节点上配置完成后同步到其他节点。
hadoop@node1:~$ vi $HADOOP_HOME/etc/hadoop/hadoop-env.sh
export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64
export HADOOP_HOME=/home/hadoop/apps/hadoop-3.3.4
export HADOOP_CONF_DIR=/home/hadoop/apps/hadoop-3.3.4/etc/hadoop
export HADOOP_LOG_DIR=/home/hadoop/logs/hadoop
hadoop@node1:~$ vi $HADOOP_HOME/etc/hadoop/core-site.xml
configuration>
<property>
<name>fs.defaultFS</name>
<value>hdfs://wuxlabs</value>
</property>
<property>
<name>hadoop.tmp.dir</name>
<value>/home/hadoop/data/hadoop/temp</value>
</property>
<property>
<name>ha.zookeeper.quorum</name>
<value>node1:2181,node2:2181,node3:2181</value>
<final>false</final>
</property>
</configuration>
hadoop@node1:~$ vi $HADOOP_HOME/etc/hadoop/hdfs-site.xml
<configuration>
<property>
<name>dfs.replication</name>
<value>3</value>
</property>
<property>
<name>dfs.namenode.name.dir</name>
<value>/home/hadoop/data/hadoop/hdfs/name</value>
</property>
<property>
<name>dfs.datanode.data.dir</name>
<value>/home/hadoop/data/hadoop/hdfs/data</value>
</property>
<property>
<name>dfs.nameservices</name>
<value>wuxlabs</value>
<final>false</final>
</property>
<property>
<name>dfs.ha.namenodes.wuxlabs</name>
<value>nn1,nn2</value>
<final>false</final>
</property>
<property>
<name>dfs.namenode.rpc-address.wuxlabs.nn1</name>
<value>node1:8020</value>
<final>false</final>
</property>
<property>
<name>dfs.namenode.rpc-address.wuxlabs.nn2</name>
<value>node2:8020</value>
<final>false</final>
</property>
<property>
<name>dfs.namenode.http-address.wuxlabs.nn1</name>
<value>node1:9870</value>
</property>
<property>
<name>dfs.namenode.http-address.wuxlabs.nn2</name>
<value>node2:9870</value>
</property>
<property>
<name>dfs.namenode.shared.edits.dir</name>
<value>qjournal://node1:8485;node2:8485;node3:8485/wuxlabs</value>
<final>false</final>
</property>
<property>
<name>dfs.ha.automatic-failover.enabled.wuxlabs</name>
<value>true</value>
<final>false</final>
</property>
<property>
<name>dfs.client.failover.proxy.provider.wuxlabs</name>
<value>org.apache.hadoop.hdfs.server.namenode.ha.ConfiguredFailoverProxyProvider</value>
<final>false</final>
</property>
<property>
<name>dfs.journalnode.edits.dir</name>
<value>/home/hadoop/data/hadoop/journal</value>
<final>false</final>
</property>
<property>
<name>dfs.ha.fencing.methods</name>
<value>sshfence</value>
<final>false</final>
</property>
<property>
<name>dfs.ha.fencing.ssh.private-key-files</name>
<value>/home/hadoop/.ssh/id_rsa</value>
</property>
<property>
<name>dfs.permissions</name>
<value>false</value>
</property>
</configuration>
hadoop@node1:~$ vi $HADOOP_HOME/etc/hadoop/mapred-site.xml
<configuration>
<property>
<name>mapreduce.framework.name</name>
<value>yarn</value>
</property>
<property>
<name>mapreduce.application.classpath</name>
<value>$HADOOP_HOME/share/hadoop/mapreduce/*:$HADOOP_HOME/share/hadoop/mapreduce/lib/*</value>
</property>
</configuration>
hadoop@node1:~$ vi $HADOOP_HOME/etc/hadoop/yarn-site.xml
<configuration>
<property>
<name>yarn.nodemanager.aux-services</name>
<value>mapreduce_shuffle</value>
</property>
<property>
<name>yarn.resourcemanager.ha.enabled</name>
<value>true</value>
</property>
<property>
<name>yarn.resourcemanager.cluster-id</name>
<value>wuxlabs</value>
</property>
<property>
<name>yarn.resourcemanager.ha.rm-ids</name>
<value>rm1,rm2</value>
</property>
<property>
<name>yarn.resourcemanager.hostname.rm1</name>
<value>node1</value>
</property>
<property>
<name>yarn.resourcemanager.hostname.rm2</name>
<value>node2</value>
</property>
<property>
<name>yarn.resourcemanager.webapp.address.rm1</name>
<value>node1:8088</value>
</property>
<property>
<name>yarn.resourcemanager.webapp.address.rm2</name>
<value>node2:8088</value>
</property>
<property>
<name>yarn.resourcemanager.zk-address</name>
<value>node1:2181,node2:2181,node3:2181</value>
</property>
</configuration>
hadoop@node1:~$ vi $HADOOP_HOME/etc/hadoop/workers
node3
node4
node5
在node1上配置好环境变量及配置文件,可以手动再在其他节点上完成同样的配置,或者直接将node1的文件复制到其他节点。
hadoop@node1:~$ scp -r .bashrc apps node2:~/
hadoop@node1:~$ scp -r .bashrc apps node3:~/
hadoop@node1:~$ scp -r .bashrc apps node4:~/
hadoop@node1:~$ scp -r .bashrc apps node5:~/
在node1、node2、node3上启动JournalNode。
hadoop@node1:~$ hdfs --daemon start journalnode
在启动集群前,需要对NameNode进行格式化,在node1上执行以下命令:
hadoop@node1:~$ hdfs namenode -format
先在node1上启动NameNode。
hadoop@node1:~$ hdfs --daemon start namenode
启动完成后,访问node1的9870端口,此时的node1是standby的。
在node1上执行Zookeeper格式化。
hadoop@node1:~$ hdfs zkfc -formatZK
在node2上拉取NameNode的镜像。
hadoop@node2:~$ hdfs namenode -bootstrapStandby
在node2上启动NameNode。
hadoop@node2:~$ hdfs --daemon start namenode
启动完成后,访问node2的9870端口,此时的node2是standby的。
此时,
先停止Hadoop相关的所有进程,保留Zookeeper的进程QuorumPeerMain。
hadoop@node1:~$ stop-all.sh
WARNING: Stopping all Apache Hadoop daemons as hadoop in 10 seconds.
WARNING: Use CTRL-C to abort.
Stopping namenodes on [node1 node2]
Stopping datanodes
Stopping journal nodes [node2 node3 node1]
WARNING: YARN_CONF_DIR has been replaced by HADOOP_CONF_DIR. Using value of YARN_CONF_DIR.
Stopping nodemanagers
WARNING: YARN_CONF_DIR has been replaced by HADOOP_CONF_DIR. Using value of YARN_CONF_DIR.
node4: WARNING: YARN_CONF_DIR has been replaced by HADOOP_CONF_DIR. Using value of YARN_CONF_DIR.
node3: WARNING: YARN_CONF_DIR has been replaced by HADOOP_CONF_DIR. Using value of YARN_CONF_DIR.
node5: WARNING: YARN_CONF_DIR has been replaced by HADOOP_CONF_DIR. Using value of YARN_CONF_DIR.
Stopping resourcemanagers on [ node1 node2]
WARNING: YARN_CONF_DIR has been replaced by HADOOP_CONF_DIR. Using value of YARN_CONF_DIR.
node1: WARNING: YARN_CONF_DIR has been replaced by HADOOP_CONF_DIR. Using value of YARN_CONF_DIR.
node2: WARNING: YARN_CONF_DIR has been replaced by HADOOP_CONF_DIR. Using value of YARN_CONF_DIR.
在node1上执行start-all.sh命令启动集群。
hadoop@node1:~$ jps
275701 Jps
214989 QuorumPeerMain
hadoop@node1:~$ start-all.sh
WARNING: Attempting to start all Apache Hadoop daemons as hadoop in 10 seconds.
WARNING: This is not a recommended production deployment configuration.
WARNING: Use CTRL-C to abort.
Starting namenodes on [node1 node2]
Starting datanodes
Starting journal nodes [node2 node3 node1]
WARNING: YARN_CONF_DIR has been replaced by HADOOP_CONF_DIR. Using value of YARN_CONF_DIR.
Starting resourcemanagers on [ node1 node2]
WARNING: YARN_CONF_DIR has been replaced by HADOOP_CONF_DIR. Using value of YARN_CONF_DIR.
node1: WARNING: YARN_CONF_DIR has been replaced by HADOOP_CONF_DIR. Using value of YARN_CONF_DIR.
node2: WARNING: YARN_CONF_DIR has been replaced by HADOOP_CONF_DIR. Using value of YARN_CONF_DIR.
Starting nodemanagers
WARNING: YARN_CONF_DIR has been replaced by HADOOP_CONF_DIR. Using value of YARN_CONF_DIR.
node4: WARNING: YARN_CONF_DIR has been replaced by HADOOP_CONF_DIR. Using value of YARN_CONF_DIR.
node5: WARNING: YARN_CONF_DIR has been replaced by HADOOP_CONF_DIR. Using value of YARN_CONF_DIR.
node3: WARNING: YARN_CONF_DIR has been replaced by HADOOP_CONF_DIR. Using value of YARN_CONF_DIR.
hadoop@node1:~$ jps
278677 NameNode
280787 Jps
279688 ResourceManager
214989 QuorumPeerMain
279115 JournalNode
此时,
hadoop@node1:~$ jps
278677 NameNode
280787 Jps
279688 ResourceManager
214989 QuorumPeerMain
279115 JournalNode
hadoop@node2:~$ jps
208369 QuorumPeerMain
264902 JournalNode
265264 ResourceManager
267858 Jps
264569 NameNode
hadoop@node3:~$ jps
215379 QuorumPeerMain
281221 Jps
278194 NodeManager
277487 DataNode
277754 JournalNode
hadoop@node4:~$ jps
183811 DataNode
187559 Jps
184343 NodeManager
hadoop@node5:~$ jps
186215 NodeManager
189848 Jps
185704 DataNode
在node1和node2上启动zkfc。
hadoop@node1:~$ hdfs --daemon start zkfc
hadoop@node2:~$ hdfs --daemon start zkfc
启动完成后,在node1和node2上就会启动DFSZKFailoverController进程。
此时,两个NameNode中的一个,这里是node1,就会变成active的。
上传一个文件到HDFS。
hadoop@node1:~$ hdfs dfs -put .bashrc /
打开node1的HDFS Web UI查看相关信息,默认端口9870。
打开node2的HDFS Web UI查看相关信息,默认端口9870,由于状态是standby的,所以不能操作。
打开node1的YARN Web UI查看相关信息,默认端口8088,状态是standby的。
打开node2的YARN Web UI查看相关信息,默认端口8088,状态是active的。
操作HDFS使用的命令是hdfs,命令格式为:
Usage: hdfs [OPTIONS] SUBCOMMAND [SUBCOMMAND OPTIONS]
支持的Client命令主要有:
Client Commands:
classpath prints the class path needed to get the hadoop jar and the required libraries
dfs run a filesystem command on the file system
envvars display computed Hadoop environment variables
fetchdt fetch a delegation token from the NameNode
getconf get config values from configuration
groups get the groups which users belong to
lsSnapshottableDir list all snapshottable dirs owned by the current user
snapshotDiff diff two snapshots of a directory or diff the current directory contents with a snapshot
version print the version
hdfs haadmin -transitionToActive --forcemanual nn1
hdfs haadmin -transitionToStandby --forcemanual nn2
操作HDFS使用的命令是yarn,命令格式为:
Usage: yarn [OPTIONS] SUBCOMMAND [SUBCOMMAND OPTIONS]
or yarn [OPTIONS] CLASSNAME [CLASSNAME OPTIONS]
where CLASSNAME is a user-provided Java class
支持的Client命令主要有:
Client Commands:
applicationattempt prints applicationattempt(s) report
app|application prints application(s) report/kill application/manage long running application
classpath prints the class path needed to get the hadoop jar and the required libraries
cluster prints cluster information
container prints container(s) report
envvars display computed Hadoop environment variables
fs2cs converts Fair Scheduler configuration to Capacity Scheduler (EXPERIMENTAL)
jar <jar> run a jar file
logs dump container logs
nodeattributes node attributes cli client
queue prints queue information
schedulerconf Updates scheduler configuration
timelinereader run the timeline reader server
top view cluster information
version print the version
yarn jar 可以执行一个jar文件。
创建一个input目录。
hadoop@node1:~$ hdfs dfs -mkdir /input
将Hadoop的配置文件复制到input目录下。
hadoop@node1:~$ hdfs dfs -put apps/hadoop-3.3.4/etc/hadoop/*.xml /input/
以下命令用于执行一个Hadoop自带的样例程序,统计input目录中含有dfs的字符串,结果输出到output目录。
hadoop@node1:~$ yarn jar $HADOOP_HOME/share/hadoop/mapreduce/hadoop-mapreduce-examples-3.3.4.jar grep /input /output 'dfs[a-z.]+'
在YARN上可以看到提交的Job。
执行结果为:
hadoop@node1:~$ hdfs dfs -cat /output/*
2 dfs.namenode.http
2 dfs.namenode.rpc
1 dfsadmin
1 dfs.server.namenode.ha.
1 dfs.replication
1 dfs.permissions
1 dfs.nameservices
1 dfs.namenode.shared.edits.dir
1 dfs.namenode.name.dir
1 dfs.journalnode.edits.dir
1 dfs.ha.namenodes.wuxlabs
1 dfs.ha.fencing.ssh.private
1 dfs.ha.fencing.methods
1 dfs.ha.automatic
1 dfs.datanode.data.dir
1 dfs.client.failover.proxy.provider.wuxlabs
同样执行Hadoop自带的案例,计算圆周率。
hadoop@node1:~$ yarn jar $HADOOP_HOME/share/hadoop/mapreduce/hadoop-mapreduce-examples-3.3.4.jar pi 10 10
执行结果为:
hadoop@node1:~$ yarn jar $HADOOP_HOME/share/hadoop/mapreduce/hadoop-mapreduce-examples-3.3.4.jar pi 10 10
WARNING: YARN_CONF_DIR has been replaced by HADOOP_CONF_DIR. Using value of YARN_CONF_DIR.
Number of Maps = 10
Samples per Map = 10
Wrote input for Map #0
Wrote input for Map #1
Wrote input for Map #2
Wrote input for Map #3
Wrote input for Map #4
Wrote input for Map #5
Wrote input for Map #6
Wrote input for Map #7
Wrote input for Map #8
Wrote input for Map #9
Starting Job
... ...
Job Finished in 35.017 seconds
Estimated value of Pi is 3.20000000000000000000
在YARN上可以看到提交的Job。
在node1上杀掉NameNode进程。
hadoop@node1:~$ kill -9 278677
node2将切换为active状态。