当我们的hadoop
集群运行了一段时间之后,原有的数据节点的容量已经不能满足我们的存储了,这个时候就需要往集群中增加新的数据节点。此时我们就需要动态的对hdfs集群进行扩容操作(节点服役
)。
在hdfs
集群中是存在黑名单
和白名单
的。
黑名单:
该文件包含不允许
连接到namenode
的主机列表。必须指定文件的完整路径名
。如果该值为空,则表示不排除任何主机。
白名单:
该文件包含允许
连接到namenode
的主机列表。必须指定文件的完整路径名
。如果该值为空,则允许所有主机
黑白名单配置
vim hdfs-site.xml
<property>
<name>dfs.hosts.excludename>
<value>/opt/bigdata/hadoop-3.3.4/etc/hadoop/blacklist.hostsvalue>
property>
<property>
<name>dfs.hostsname>
<value>/opt/bigdata/hadoop-3.3.4/etc/hadoop/whitelist.hostsvalue>
property>
注意:
第一次配置黑白名单时,需要重启集群才可以生效,之后修改了黑白名单文件,只需要执行 hdfs dfsadmin -refreshNodes
命令即可。
[root@appbasic ~]# vim /etc/hostname
[root@appbasic ~]# cat /etc/hostname
hadoop04
[root@appbasic ~]# vim /etc/hosts
[root@appbasic ~]# cat /etc/hosts
192.168.121.140 hadoop01
192.168.121.141 hadoop02
192.168.121.142 hadoop03
192.168.121.143 hadoop04
[root@appbasic ~]#
hadoop
集群中的各个机器之间的时间最好都保持一致
[root@hadoop04 ~]# ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
[root@hadoop04 ~]# yum install ntp
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
base | 3.6 kB 00:00:00
extras | 2.9 kB 00:00:00
updates | 2.9 kB 00:00:00
(1/2): extras/7/aarch64/primary_db | 252 kB 00:00:00
(2/2): updates/7/aarch64/primary_db | 3.5 MB 00:00:03
软件包 ntp-4.2.6p5-29.el7.centos.2.aarch64 已安装并且是最新版本
无须任何处理
[root@hadoop04 ~]# systemctl enable ntpd
[root@hadoop04 ~]# service ntpd restart
Redirecting to /bin/systemctl restart ntpd.service
[root@hadoop04 ~]# ntpdate asia.pool.ntp.org
29 Mar 21:42:52 ntpdate[1697]: the NTP socket is in use, exiting
[root@hadoop04 ~]# /sbin/hwclock --systohc
[root@hadoop04 ~]# timedatectl
Local time: 三 2023-03-29 21:43:03 CST
Universal time: 三 2023-03-29 13:43:03 UTC
RTC time: 三 2023-03-29 13:43:03
Time zone: Asia/Shanghai (CST, +0800)
NTP enabled: yes
NTP synchronized: no
RTC in local TZ: no
DST active: n/a
[root@hadoop04 ~]# timedatectl set-ntp true
[root@hadoop04 ~]#
[root@hadoop04 ~]# systemctl stop firewalld
systemctl stop firewalld
[root@hadoop04 ~]# systemctl disable firewalld.service
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@hadoop04 ~]#
[root@hadoop04 ~]# useradd hadoopdeploy
[root@hadoop04 ~]# passwd hadoopdeploy
更改用户 hadoopdeploy 的密码 。
新的 密码:
无效的密码: 密码包含用户名在某些地方
重新输入新的 密码:
passwd:所有的身份验证令牌已经成功更新。
[root@hadoop04 ~]# vim /etc/sudoers
[root@hadoop04 ~]# cat /etc/sudoers | grep hadoopdeploy -C 3
## Same thing without a password
# %wheel ALL=(ALL) NOPASSWD: ALL
hadoopdeploy ALL=(ALL) NOPASSWD: ALL
## Allows members of the users group to mount and unmount the
## cdrom as root
[root@hadoop04 ~]#
[root@hadoop04 ~]# scp /etc/hosts root@hadoop01:/etc/hosts
[root@hadoop04 ~]# scp /etc/hosts root@hadoop02:/etc/hosts
[root@hadoop04 ~]# scp /etc/hosts root@hadoop03:/etc/hosts
此处配置 namenode(hadoop01)
与hadoop04
之间的免密登录。
[hadoopdeploy@hadoop01 ~]$ ssh-copy-id hadoop04
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/hadoopdeploy/.ssh/id_rsa.pub"
The authenticity of host 'hadoop04 (192.168.121.143)' can't be established.
ECDSA key fingerprint is SHA256:4GL0zHVCdSl3czA0wqcuLT60lUljyEq3DqwPFxNwYsE.
ECDSA key fingerprint is MD5:3e:42:a6:50:0d:fb:f0:41:a8:0d:fb:cc:fd:20:2c:c8.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
hadoopdeploy@hadoop04's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'hadoop04'"
and check to make sure that only the key(s) you wanted were added.
[hadoopdeploy@hadoop01 ~]$
[root@hadoop04 ~]# sudo mkdir /opt/bigdata
mkdir: cannot create directory ‘/opt/bigdata’: No such file or directory
[root@hadoop04 ~]# sudo mkdir -p /opt/bigdata
[root@hadoop04 ~]# sudo chown -R hadoopdeploy:hadoopdeploy /opt/bigdata/
[root@hadoop04 ~]# su - hadoopdeploy
Last login: Wed Mar 29 22:19:54 CST 2023 on pts/0
[hadoopdeploy@hadoop04 ~]$ scp -r hadoopdeploy@hadoop01:/opt/bigdata/hadoop-3.3.4/ /opt/bigdata/
[hadoopdeploy@hadoop04 hadoop]$ rm -rvf /opt/bigdata/hadoop-3.3.4/data/* rm -rvf /opt/bigdata/hadoop-3.3.4/logs/*
注意
目录的创建用户、执行scp命令的用户
注意:
如果hadoop-3.3.4
目录下存在我们之前配置的数据目录,则需要删除,否则启动这个节点的时候会有问题。日志目录也相应的删除
配置这个文件是为了方便集群的一键启动。
[hadoopdeploy@hadoop04 hadoop]$ vim /opt/bigdata/hadoop-3.3.4/etc/hadoop/workers
[hadoopdeploy@hadoop04 hadoop]$ cat /opt/bigdata/hadoop-3.3.4/etc/hadoop/workers
hadoop01
hadoop02
hadoop03
hadoop04
[hadoopdeploy@hadoop04 hadoop]$
注意:
将这个workers
文件分发到集群的各个机器上。
[hadoopdeploy@hadoop04 hadoop]$ scp /opt/bigdata/hadoop-3.3.4/etc/hadoop/workers hadoopdeploy@hadoop01:/opt/bigdata/hadoop-3.3.4/etc/hadoop/workers
[hadoopdeploy@hadoop04 hadoop]$ scp /opt/bigdata/hadoop-3.3.4/etc/hadoop/workers hadoopdeploy@hadoop02:/opt/bigdata/hadoop-3.3.4/etc/hadoop/workers
[hadoopdeploy@hadoop04 hadoop]$ scp /opt/bigdata/hadoop-3.3.4/etc/hadoop/workers hadoopdeploy@hadoop03:/opt/bigdata/hadoop-3.3.4/etc/hadoop/workers
[hadoopdeploy@hadoop04 hadoop]$ source /etc/profile
[hadoopdeploy@hadoop04 logs]$ hdfs --daemon start datanode
[hadoopdeploy@hadoop04 logs]$ jps
2278 DataNode
2349 Jps
[hadoopdeploy@hadoop04 logs]$
注意:
如果我们不想让任意一台机器随便就加入到了我们的集群中,那么我们通过白名单
来控制。
新加入的节点磁盘空间比较大,这个时候我们就可以将别的节点的数据均衡到这个节点中来。
# 设置数据传输带宽
[hadoopdeploy@hadoop04 logs]$ hdfs dfsadmin -setBalancerBandwidth 10485760
Balancer bandwidth is set to 10485760
# 执行banalce
[hadoopdeploy@hadoop04 logs]$ hdfs balancer -policy datanode -threshold 5
注意:
只需要在NameNode
或者ResourceManager
上执行即可。
vim hdfs-site.xml
dfs.hosts.exclude
/opt/bigdata/hadoop-3.3.4/etc/hadoop/blacklist.hosts
注意:
这个配置文件如果之间没有配置过,则需要重启集群才生效,如果之前配置过,则在NameNode
或ResourceManager
节点上执行hdfs dfsadmin -refreshNodes
命令即可。
编辑blacklist.hosts
文件,加入需要下线的节点。(NameNode或ResourceManager上操作 )
[hadoopdeploy@hadoop01 ~]$ cd /opt/bigdata/hadoop-3.3.4/etc/hadoop/
[hadoopdeploy@hadoop01 hadoop]$ vim blacklist.hosts
[hadoopdeploy@hadoop01 hadoop]$ cat blacklist.hosts
hadoop04
[hadoopdeploy@hadoop01 hadoop]$ hdfs dfsadmin -refreshNodes
Refresh nodes successful
[hadoopdeploy@hadoop01 hadoop]$
节点移除后,可以考虑再次均衡集群中的数据。
注意:
此时可以看到我们的集群中有4台机器,假设我们集群的副本设置为4,那么此时是不可下线节点的,需要修改集群的副本<4。
[hadoopdeploy@hadoop04 logs]$ hdfs --daemon stop datanode
[hadoopdeploy@hadoop04 logs]$