环境要求

此次流程记录是 Ubuntu Server 20.04 系统下 Hadoop 集群的搭建。Linux 和 Windows 所需软件包括:(1)Java 1.5.x 及以上,必须安装,建议选择 Java 8 版本。(2)ssh 必须安装并且保证 sshd 一直运行,以便用 Hadoop 脚本管理远端 Hadoop 守护进程。

软件下载

Mobaxterm:一款多功能的远程连接工具,支持多种协议,自带 SFTP 功能,下载链接
20220316004252
Java:Java 8 是目前使用最为广泛的版本(可能是因为 Java 版本迭代所带来的学习成本问题),自此安装也是选择 Java 8 为运行环境,方便维护,下载链接

20220316004305
SSH:集群的工作环境需要远程连接进行管理,通过对 master 和 slaves 安装 SSH 服务,方便管理。Ubuntu 安装 SSH 服务命令 apt-get install openssh-server。

20220316004315
Hadoop:此次安装的重头戏,前往 Apache 官方网站下载即可,https://www.apache.org/dyn/closer.cgi/hadoop/common/hadoop-3.3.2/hadoop-3.3.2.tar.gz

20220316004323

软件的安装和配置

Mobaxterm 的安装

Mobaxterm 下载后,双击安装包进行安装,经典的 “下一步”,并进行安装位置的选择,完成安装。

20220316004331

SSH 的安装

Ubuntu 安装后默认就具有 SSH 服务,由于未进行配置,此时还是个单向的服务,“只出不进”—— 能通过 SSH 命令远程连接其他主机,其他主机不能通过 SSH 访问本机。安装 OpenSSH,在 Terminal 中输入命令:

1
2
apt-get update
apt-get install openssh-server

20220316004341

配置 SSH

新建 root 用户,键入命令 passwd root,并输入、确定密码。
首先,编辑系统或者重要服务配置文件时,做好备份。键入命令

1
cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak

再通过 Vim 进行 SSH 的配置,

1
vim /etc/ssh/sshd_config

添加一行 PermitRootLogin yes,按键盘 Esc,输入英文冒号、wq,推出并保存。再重启 SSH 服务:

1
service ssh restart

远程连接

在虚拟主机 Terminal 中输入 ip addr,查看默认分配的 IP 地址。

20220316004355
打开 Mobaxterm,进行远程连接配置,新建远程连接任务,选择协议类型,根据上一步获取的信息,在 Mobaxterm 中输入虚拟主机的相关信息。

20220316004403
输入之前配置的 root 账户的密码,完成远程连接。

20220316004410

Java 的安装和配置

使用的 Mobaxterm 的 SFTP 的功能,支持拖拽上传,直接将下载的 Java 8 打包压缩包拖至文件夹中。由于处在内网环境下,传输速率应该会很快。

20220316004420
进入到 Java 8 所在文件夹,进行解压

1
tar -zxvf jdk-8u321-linux-x64.tar.gz

配置环境变量,vim /etc/profile,在末尾添加语句,配置环境变量。

1
2
3
4
JAVA_HOME=/path/to/java/jdk1.8.0_321
PATH=$PATH:$HOME/bin:$JAVA_HOME/bin
export JAVA_HOME
export PATH

重载环境变量配置文件 source /etc/profile,并检查 Java 是否配置完成 java -version
20220316004434

Hadoop 的安装和配置

重复安装 Java 时的步骤,拖拽上传 Hadoop 压缩包,并进行解压,验证安装。
20220316004452

Hadoop 分布式配置

克隆虚拟主机

将配置好的已有主机(Master)克隆两台(Node1、Node2)。

在克隆选项时,需注意选择 “创建完整克隆”,否则后续步骤将失败。

20220316004534

配置网络和主机名

由于 VMware 克隆机制,会将源主机的所有信息原封不动得复制一份,包括软件和硬件信息。所以需要对克隆的 Nodes 的网络进行配置。

  • 生成 MAC 地址。

20220316004542

  • 配置 IP 和网关,分别对三个主机进行配置。使用 shell 命令
    1
    vim etc/netplan/00-installer-config.yaml
    对网络进行配置。使用 netplan –debug apply 使得配置生效。
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    # This is the network config written by 'subiquity'
    network:
    ethernets:
    ens33:
    addresses: [192.168.***.***/24]
    gateway4: 192.168.***.2
    nameservers:
    addresses: [8.8.8.8, 8.8.4.4]
    dhcp4: false
    version: 2
    20220316004549
  • 修改主机名
    1
    vim /etc/hostname
    分别为 master、node1、node2。

20220316004557

分布式集群

经过上述步骤,再通过 Mobaxterm 连接这三台主机(master、node1、node2)。

20220316004604

  • 防火墙:分别关闭这三台主机的防火墙,并不允许防火墙自启。在 Ubuntu 20.04 版本中,ufw status 防火墙默认时关闭(inactive)状态。

20220316004612

  • SSH 免密:在 master 主机执行
    1
    ssh-keygen -t rsa
    生成密钥文件,生成过程一路回车即可,并使用
    1
    2
    ssh-copy-id -i node1
    ssh-copy-id -i node2
    命令,该命令的作用其实就是将密钥复制到 node1 和 node2 的 /root/.ssh/authorized_keys 文件中,方法不限,也可手动将密钥复制到两个节点的 /root/.ssh/authorized_keys 文件中去,能达到同样的效果。实现 master 能够免密登录到 node1 和 node2。为后续的工作打好基础。

20220316004619

  • IP 映射:修改 master 主机 IP hosts 映射,vim /etc/hosts。在 node1、node2 中做出相同的配置,或者使用命令
    1
    scp /etc/hosts node<id>:/etc/hosts

20220316004627

  • Hadoop 配置文件:修改 master 主机的 Hadoop 配置文件 workers(/path/to/hadoop/etc/hadoop/workers),向其中添加主机名 node1、node2 或者二者对应的 IP。修改 hadoop-env.sh(/path/to/hadoop/etc/hadoop/hadoop-env.sh),添加 Java 安装的根目录。

20220316004634
修改 core-site.xml 文件,hdfs 监听端口、Hadoop 临时文件夹内容因人而异。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<?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>fs.defaultFS</name>
<value>hdfs://master:12369</value>
</property>
<property>
<name>hadoop.tmp.dir</name>
<value>/usr/local/hadoop/tmp</value>
</property>
<property>
<name>fs.trash.interval</name>
<value>1440</value>
</property>
</configuration>

20220316004640
修改 hdfs-site.xml 配置文件。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?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.replication</name>
<value>1</value>
</property>
<property>
<name>dfs.permissions</name>
<value>false</value>
</property>
<property>
<name>dfs.http.address</name>
<value>0.0.0.0:50070</value>
</property>
</configuration>

20220316004646
修改 yarn-site.xml 配置文件。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<?xml version="1.0"?>
<!--
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.
-->
<configuration>

<!-- Site specific YARN configuration properties -->

<property>
<name>yarn.resourcemanager.hostname</name>
<value>master</value>
</property>
<property>
<name>yarn.nodemanager.aux-services</name>
<value>mapreduce_shuffle</value>
</property>
<property>
<name>yarn.log-aggregation-enable</name>
<value>true</value>
</property>
<property>
<name>yarn.log-aggregation.retain-seconds</name>
<value>604800</value>
</property>
</configuration>

20220316004659
修改 mapred-site.xml 配置文件。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?xml version="1.0"?>
<?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>mapreduce.framework.name</name>
<value>yarn</value>
</property>
<property>
<name>mapreduce.jobhistory.address</name>
<value>master:10020</value>
</property>
<property>
<name>mapreduce.jobhistory.webapp.address</name>
<value>master:19888</value>
</property>
</configuration>

20220316004707

  • 拷贝 master 主机已经配置完毕的 hadoop 到 node1 和 node2 中
    1
    scp -rf /usr/local/hadoop/hadoop-3.3.2 node<id>:/usr/local/hadoop/
    此时应该是覆盖了。
  • 启动 Hadoop:输入
    1
    /usr/local/hadoop/hadoop-3.3.2/bin/hdfs namenode -format
    命令生成初始化文件。运行启动键本:
    1
    /usr/local/hadoop/hadoop-3.3.2/sbin/start-all.sh

20220316004716

  • 大功告成!。

20220316004724