HDFS扩容

hdfs的存储容量不足,需要放入新磁盘扩容
扩容有两种方式,一种是linux层面的,一种是hdfs层面的
hdfs的datanode存储的目录可以查看hdfs-site.xml的dfs.datanode.data.dir的值

linux层面

linux层面的就是将hdfs的datanode所挂载的分区或逻辑卷扩容

如果是直接挂在固定分区上,而磁盘还有余量可以进行分区扩容 参考:http://blog.51cto.com/wutou/1782931
这种直接挂载在分区上的磁盘空间管理方式十分不方便,扩容也很容易出错,而且扩容只有在磁盘还有余量的情况下,
无法将两块磁盘的空间结合使用,一块磁盘空间占满,即使加入一块新磁盘也无法对原来磁盘上的分区扩容。
所以才会有逻辑卷管理LVM的出现

如果是lvm对磁盘分区进行管理,那么可以方便的创建物理卷,扩容datanode所挂载的逻辑卷所属的卷组
接着扩容该逻辑卷,具体操作方式参考: https://linux.cn/article-3218-1.html

hdfs层面

扩容

这种不需要对原来的空间扩容,是修改hdfs-site.xml的dfs.datanode.data.dir的值添加新目录,如

1
2
3
4
5
6
7
<property>
<name>dfs.datanode.data.dir</name>
<value>
/hadoop/hdfs/data,
/data/hadoop/hdfs
</value>
</property

将多个目录用逗号隔开,所以只要将新磁盘分区挂载在新目录上(分区方式可以自己决定,最好永久挂载),将需要添加的新目录设置用户权限

1
chown -R hdfs:hadoop dirPath

修改dfs.datanode.data.dir,重启datanode即可

1
./bin/hdfs dfsadmin -report

可以查看各个datanode的存储容量

解决数据倾斜

但是这种方式只是增加新磁盘到hdfs中,原来的老磁盘空间依旧被占满。如果老磁盘的容量比新磁盘小,当之后hdfs不断添加新数据,老磁盘依旧占满比新磁盘快,为了防止hdfs的存储容量无限扩张占满磁盘,甚至导致系统盘占满,影响这个系统,通过设置dfs.datanode.du.reserved来预留磁盘空间。该参数在hdfs-site.xml中设置。

1
2
3
4
<property>  
<name>dfs.datanode.du.reserved</name>
<value>32212254720</value>
</property>

这里设置预留了30G的磁盘空间(参数单位为byte),也就是当磁盘的空间剩余30G及以下时,将不会存放数据副本到该磁盘。

但是这还是没有解决目前刚刚扩容时,老磁盘空间依旧被占满的问题。这里就时需要移动老磁盘的副本到新磁盘,但是目前hdfs并没有提供该功能,因此只能曲线救国,删除一些不必要的文件(一些文件的副本可能存在老磁盘中)。

如果不想动任何数据的情况下,可以采用升降副本的方法,如原来发副本数量为3,降低副本数量为2,这样会删除一些副本,会删除存在老磁盘上从一些副本,接着在升高副本数量到原来发副本数量,这样由于设置了磁盘预留空间,新副本会存放在新磁盘当中,升降副本采用hdfs的setrep -w命令,-R为递归调整整个文件夹下的所有文件。

1
2
hadoop dfs -setrep -w -R 2 path
hadoop dfs -setrep -w -R 3 path

最后使用Hadoop balancer工具来调整各个datanodes的数据平衡。

官方对balancer的描述:

The balancer is a tool that balances disk space usage on an HDFS cluster when some datanodes become full or when new empty nodes join the cluster.

The tool is deployed as an application program that can be run by the cluster administrator on a live HDFS cluster while applications adding and deleting files.

balancer命令详解:

1
2
3
4
5
6
7
8
9
hdfs balancer
[-policy <policy>]
[-threshold <threshold>]
[-exclude [-f <hosts-file> | <comma-separated list of hosts>]]
[-include [-f <hosts-file> | <comma-separated list of hosts>]]
[-source [-f <hosts-file> | <comma-separated list of hosts>]]
[-blockpools <comma-separated list of blockpool ids>]
[-idleiterations <idleiterations>]
[-runDuringUpgrade]
COMMAND_OPTION Description
-policy datanode (default): Cluster is balanced if each datanode is balanced. blockpool: Cluster is balanced if each block pool in each datanode is balanced.
-threshold Percentage of disk capacity. This overwrites the default threshold.
-exclude -f \ Excludes the specified datanodes from being balanced by the balancer.
-include -f \ Includes only the specified datanodes to be balanced by the balancer.
-source -f \ Pick only the specified datanodes as source nodes.
-blockpools The balancer will only run on blockpools included in this list.
-idleiterations Maximum number of idle iterations before exit. This overwrites the default idleiterations.
-runDuringUpgrade Whether to run the balancer during an ongoing HDFS upgrade. This is usually not desired since it will not affect used space on over-utilized machines.
-h --help

这里我只是调小datanode之间的百分比的差

1
hdfs balancer -threshold 5

这样可以解决hadoop数据出现不均衡情况。

0%