HDFS數據均衡篇


              HDFS數據均衡篇

                                   作者:尹正傑

版權聲明:原創作品,謝絕轉載!否則將追究法律責任。

 

 

 

一.HDFS數據均衡概述

  隨着時間的推移,HDFS存儲中數據分布可能變得不平衡,某些DataNode上可能具有更多的數據塊。在極端的情況下,在具有更多的節點上讀取和寫入過於頻繁,而一些較少的節點則未被充分利用。

  當向集群添加新節點時,HDFS的數據分布也會失去平衡。Hadoop不會自動移動現有數據到新節點,以均衡集群Datanode中的數據分布。它只是開始使用新的DataNode來存儲新數據。

  Hadoop不尋求實現完全均衡的集群。在具有連續數據流的集群中,這種狀態很難實現。相反,當每個Datanode上的空間使用率與Hadoop集群總的空間使用率的差值小於特定百分比時,Hadoop認為集群是均衡的。此外,它還利用閾值為數據均衡提供靈活性。

  Hadoop提供了一個有用的工具,即均衡器,使用它能夠重新均衡集群的快分布,因此所有DataNode都存儲大致相等的數據量。

  溫馨提示:
    在集群中定期運行HDFS均衡器是一個很好的做法。

 

二.HDFS數據不均衡的原因

  HDFS不能保證在集群中的DataNode之間均勻分配數據。例如,當向集群添加新節點時,所有新塊都可以分配給該節點,從而使數據分布不均衡。

  當Namenode將數據塊分配給Datanode時,它執行以下標准來決定哪些DataNode獲得新的塊:
    (1)在集群的DataNode上統一分布數據;
    (2)正在寫該塊的節點保留數據塊的一個副本;
    (3)將其中一個副本放置在與寫入塊節點相同的機架上,以最小化跨機架網絡I/O;
    (4)將副本跨機架進行復制,以支持冗余並在整個機架丟失后繼續運行;

  當給定Datanode中的空間百分比越高於或低於該集群中Datanode所使用的平均空間百分比時,Hadoop會認為集群時均衡的。這個"略高於或者略低於"的標准有參數閾值定義。

 

三.運行均衡器以均衡HDFS數據

1>.HDFS均衡器原理

  HDFS均衡器是Hadoop提供的工具,使用該工具可以從過度使用的DataNodes移動數據塊到利用不足的Datanode,從而均衡集群的DataNode數據。

  HDFS均衡器的原理如下圖所示,最初Rack 1和Rack 2有數據塊。新的機架(Rack 3)沒有數據,而且只有新添加的數據才被放置在那里。

  這意味着添加節點導致集群數據不均衡。需要將現有DataNode的數據移動到新的沒有數據的DataNode(或者添加新數據時將數據直接寫入到新節點)。

  當運行均衡器時,Hadoop將數據塊從現有位置移動到具有更多自由空間的節點,最終所有節點具有大致相同的空間使用率。

 

2>.運行均衡器的方式

可以通過"start-balancer.sh"腳本調用均衡器,也可以通過執行命令"hdfs balancer"來運行均衡器。下面是balancer命令的用法:

[root@hadoop101.yinzhengjie.com
~]# hdfs balancer --help Usage: hdfs balancer [-policy <policy>] the balancing policy: datanode or blockpool [-threshold <threshold>] Percentage of disk capacity [-exclude [-f <hosts-file> | <comma-separated list of hosts>]] Excludes the specified datanodes. [-include [-f <hosts-file> | <comma-separated list of hosts>]] Includes only the specified datanodes. [-source [-f <hosts-file> | <comma-separated list of hosts>]] Pick only the specified datanodes as source nodes. [-blockpools <comma-separated list of blockpool ids>] The balancer will only run on blockpools included in this list. [-idleiterations <idleiterations>] Number of consecutive idle iterations (-1 for Infinite) before exit. [-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. Generic options supported are: -conf <configuration file> specify an application configuration file -D <property=value> define a value for a given property -fs <file:///|hdfs://namenode:port> specify default filesystem URL to use, overrides 'fs.defaultFS' property from configurations. -jt <local|resourcemanager:port> specify a ResourceManager -files <file1,...> specify a comma-separated list of files to be copied to the map reduce cluster -libjars <jar1,...> specify a comma-separated list of jar files to be included in the classpath -archives <archive1,...> specify a comma-separated list of archives to be unarchived on the compute machines The general command line syntax is: command [genericOptions] [commandOptions] [root@hadoop101.yinzhengjie.com ~]#

3>.為均衡器設置適當的閾值

  threshold參數表示每個DataNode的HDFS使用率於集群的平均DFS利用率的偏差百分比。以任意一方式(更高或更低)超過該閾值將意味着該節點會被重新均衡。

  如下面的案例所示,可以運行不帶任何參數的balancer命令,則此均衡器明朗了使用10%的默認閾值,這意味着均衡器通過將塊從過度使用的節點移動到未充分使用的節點來均衡數據,直到每個Datanode的磁盤使用率不超過集群中平均磁盤使用率的正負10%。   有時,可能希望將閾值設置為不同的級別,例如,當集群中的可用空間變小,並且你希望將單個DataNode上使用的存儲量保持在比默認的10%閾值更小的范圍內時,可以這樣指定閾值"hdfs balancer -threshold 5"
  當運行均衡器時,它會查看集群中的兩個關鍵HDFS使用情況值:
    (1)平均DFS使用百分比:
        可以通過計算得到集群中使用的平均DFS百分比:"Average DFS Used = (DFS Used * 100) / Present Capacity"
    (2)節點使用的DFS百分比:
        此度量顯示每個節點使用的DFS百分比。
[root@hadoop101.yinzhengjie.com ~]# hdfs balancer           #運行均衡器若不帶任何參數的balancer命令,則使用默認閾值(10%)。
20/08/20 18:59:50 INFO balancer.Balancer: namenodes  = [hdfs://hadoop101.yinzhengjie.com:9000]
20/08/20 18:59:50 INFO balancer.Balancer: parameters = Balancer.BalancerParameters [BalancingPolicy.Node, threshold = 10.0, max idle iteration = 5, #excluded nodes = 0, #included nodes = 0,
 #source nodes = 0, #blockpools = 0, run during upgrade = false]20/08/20 18:59:50 INFO balancer.Balancer: included nodes = []
20/08/20 18:59:50 INFO balancer.Balancer: excluded nodes = []
20/08/20 18:59:50 INFO balancer.Balancer: source nodes = []
Time Stamp               Iteration#  Bytes Already Moved  Bytes Left To Move  Bytes Being Moved
20/08/20 18:59:51 INFO balancer.Balancer: dfs.balancer.movedWinWidth = 5400000 (default=5400000)
20/08/20 18:59:51 INFO balancer.Balancer: dfs.balancer.moverThreads = 1000 (default=1000)
20/08/20 18:59:51 INFO balancer.Balancer: dfs.balancer.dispatcherThreads = 200 (default=200)
20/08/20 18:59:51 INFO balancer.Balancer: dfs.datanode.balance.max.concurrent.moves = 50 (default=50)
20/08/20 18:59:51 INFO balancer.Balancer: dfs.balancer.getBlocks.size = 2147483648 (default=2147483648)
20/08/20 18:59:51 INFO balancer.Balancer: dfs.balancer.getBlocks.min-block-size = 10485760 (default=10485760)
20/08/20 18:59:51 INFO balancer.Balancer: dfs.balancer.max-size-to-move = 10737418240 (default=10737418240)
20/08/20 18:59:51 INFO balancer.Balancer: dfs.blocksize = 536870912 (default=134217728)
20/08/20 18:59:51 INFO net.NetworkTopology: Adding a new node: /rack001/172.200.6.102:50010
20/08/20 18:59:51 INFO net.NetworkTopology: Adding a new node: /rack002/172.200.6.104:50010
20/08/20 18:59:51 INFO net.NetworkTopology: Adding a new node: /rack002/172.200.6.103:50010
20/08/20 18:59:51 INFO balancer.Balancer: 0 over-utilized: []
20/08/20 18:59:51 INFO balancer.Balancer: 0 underutilized: []
The cluster is balanced. Exiting...
Aug 20, 2020 6:59:51 PM           0                  0 B                 0 B                0 B
Aug 20, 2020 6:59:51 PM  Balancing took 764.0 milliseconds
[root@hadoop101.yinzhengjie.com ~]# 
[root@hadoop101.yinzhengjie.com ~]# hdfs balancer           #運行均衡器若不帶任何參數的balancer命令,則使用默認閾值(10%)。
[root@hadoop101.yinzhengjie.com ~]# hdfs balancer -threshold 5            #我的集群相對較小,而且並沒有大量使用,因此盡管我設置的百分比很小,依舊沒有觸發數據均衡。
20/08/20 19:20:57 INFO balancer.Balancer: Using a threshold of 5.0
20/08/20 19:20:57 INFO balancer.Balancer: namenodes  = [hdfs://hadoop101.yinzhengjie.com:9000]
20/08/20 19:20:57 INFO balancer.Balancer: parameters = Balancer.BalancerParameters [BalancingPolicy.Node, threshold = 5.0, max idle iteration = 5, #excluded nodes = 0, #included nodes = 0, 
#source nodes = 0, #blockpools = 0, run during upgrade = false]20/08/20 19:20:57 INFO balancer.Balancer: included nodes = []
20/08/20 19:20:57 INFO balancer.Balancer: excluded nodes = []
20/08/20 19:20:57 INFO balancer.Balancer: source nodes = []
Time Stamp               Iteration#  Bytes Already Moved  Bytes Left To Move  Bytes Being Moved
20/08/20 19:20:58 INFO balancer.Balancer: dfs.balancer.movedWinWidth = 5400000 (default=5400000)
20/08/20 19:20:58 INFO balancer.Balancer: dfs.balancer.moverThreads = 1000 (default=1000)
20/08/20 19:20:58 INFO balancer.Balancer: dfs.balancer.dispatcherThreads = 200 (default=200)
20/08/20 19:20:58 INFO balancer.Balancer: dfs.datanode.balance.max.concurrent.moves = 50 (default=50)
20/08/20 19:20:58 INFO balancer.Balancer: dfs.balancer.getBlocks.size = 2147483648 (default=2147483648)
20/08/20 19:20:58 INFO balancer.Balancer: dfs.balancer.getBlocks.min-block-size = 10485760 (default=10485760)
20/08/20 19:20:58 INFO balancer.Balancer: dfs.balancer.max-size-to-move = 10737418240 (default=10737418240)
20/08/20 19:20:58 INFO balancer.Balancer: dfs.blocksize = 536870912 (default=134217728)
20/08/20 19:20:58 INFO net.NetworkTopology: Adding a new node: /rack001/172.200.6.102:50010
20/08/20 19:20:58 INFO net.NetworkTopology: Adding a new node: /rack002/172.200.6.103:50010
20/08/20 19:20:58 INFO net.NetworkTopology: Adding a new node: /rack002/172.200.6.104:50010
20/08/20 19:20:58 INFO balancer.Balancer: 0 over-utilized: []
20/08/20 19:20:58 INFO balancer.Balancer: 0 underutilized: []
The cluster is balanced. Exiting...
Aug 20, 2020 7:20:58 PM           0                  0 B                 0 B                0 B
Aug 20, 2020 7:20:58 PM  Balancing took 750.0 milliseconds
[root@hadoop101.yinzhengjie.com ~]# 
[root@hadoop101.yinzhengjie.com ~]# hdfs balancer -threshold 5     #我的集群相對較小,而且並沒有大量使用,因此盡管我設置的百分比很小,依舊沒有觸發數據均衡。
[root@hadoop101.yinzhengjie.com ~]# ll -h
total 374M
-rw-r--r-- 1 root root 374M Aug 10 15:42 hadoop-2.10.0.tar.gz
[root@hadoop101.yinzhengjie.com ~]# 
[root@hadoop101.yinzhengjie.com ~]# nohup hdfs balancer -threshold 5  > ~/ballancer-stdout.log 2> ~/ballancer-stderr.log &      #生產環境建議搭建讓均衡器后台執行
[1] 9066
[root@hadoop101.yinzhengjie.com ~]# 
[root@hadoop101.yinzhengjie.com ~]# jobs                    #通過jobs命令可以查看到后台任務,我使用的是測試集群,因此很快就執行完畢啦~(目的是為大家展示使用方式)                                 
[1]+  Done                    nohup hdfs balancer -threshold 5 > ~/ballancer-stdout.log 2> ~/ballancer-stderr.log
[root@hadoop101.yinzhengjie.com ~]# 
[root@hadoop101.yinzhengjie.com ~]# 
[root@hadoop101.yinzhengjie.com ~]# ll                    #可以通過查看日志來判斷當前集群是否處於均衡狀態,亦或者均衡器停止運行說明集群已處於均衡狀態啦~
total 382936
-rw-r--r-- 1 root root      1813 Aug 20 19:26 ballancer-stderr.log
-rw-r--r-- 1 root root       287 Aug 20 19:26 ballancer-stdout.log
-rw-r--r-- 1 root root 392115733 Aug 10 15:42 hadoop-2.10.0.tar.gz
[root@hadoop101.yinzhengjie.com ~]# 
[root@hadoop101.yinzhengjie.com ~]# nohup hdfs balancer -threshold 5 > ~/ballancer-stdout.log 2> ~/ballancer-stderr.log &      #生產環境建議搭建讓均衡器后台執行

4>.調整均衡器的帶寬

  在理想的情況下,必須在集群較控線的時段運行均衡器,這樣開銷通常不高。可以調整均衡器的帶寬,以確定集群中每個DataNode可用於重新均衡的每秒字節數。

  在增加帶寬之前,請確保有足夠的帶寬。可以通過"ethtool"等工具查看NIC卡的速度,如下所示。
[root@hadoop101.yinzhengjie.com ~]# ethtool bond0          #我的筆記本網卡的速度是1000Mb/s,因此可以將均衡器代碼設置為它的10%,即100MB。服務器的網卡速度會更快(基本上都是萬兆口),推薦設置1G以上。
Settings for bond0:
    Supported ports: [ ]
    Supported link modes:   Not reported
    Supported pause frame use: No
    Supports auto-negotiation: No
    Supported FEC modes: Not reported
    Advertised link modes:  Not reported
    Advertised pause frame use: No
    Advertised auto-negotiation: No
    Advertised FEC modes: Not reported
    Speed: 1000Mb/s
    Duplex: Full
    Port: Other
    PHYAD: 0
    Transceiver: internal
    Auto-negotiation: off
    Link detected: yes
[root@hadoop101.yinzhengjie.com ~]# 
[root@hadoop101.yinzhengjie.com ~]# ethtool bond0          #我的筆記本網卡的速度是1000Mb/s,因此可以將均衡器代碼設置為它的10%,即100MB。服務器的網卡速度會更快(基本上都是萬兆口),推薦設置1G以上。
  帶寬的默認值為10MB/s,可以提高該值以使均衡器更快地完成工作。可以將帶寬提高到網絡速度的大約10%,這不會對集群的工作負載造成任何明顯的影響。

  可以修改"hdfs-site.xml"中的"dfs.datanode.balance.bandwidthPerSec"屬性(默認10MB),也可以使用hdfs dfsadmin命令設置均衡器使用的網絡帶寬,如下所示。

  溫馨提示:
    如果均衡器要運行很長時間,則可以安排它在峰值和非峰值時段以不同的帶寬運行。可以在峰值時段以低帶寬運行,並且在集群不太忙的時段以較高的帶寬運行。一次只能運行一個均衡器作業,當非高峰作業啟動時,它停止高峰均衡器作業。
[root@hadoop101.yinzhengjie.com ~]# hdfs dfsadmin -help setBalancerBandwidth 
-setBalancerBandwidth <bandwidth>:
    Changes the network bandwidth used by each datanode during
    HDFS block balancing.

        <bandwidth> is the maximum number of bytes per second
        that will be used by each datanode. This value overrides
        the dfs.balance.bandwidthPerSec parameter.

        --- NOTE: The new value is not persistent on the DataNode.---

[root@hadoop101.yinzhengjie.com ~]# 
[root@hadoop101.yinzhengjie.com ~]# hdfs dfsadmin -help setBalancerBandwidth
[root@hadoop101.yinzhengjie.com ~]# hdfs dfsadmin -setBalancerBandwidth 104857600   #設置100MB帶寬,該值將覆蓋"dfs.datanode.balance.bandwidthPerSec"屬性(不推薦使用"dfs.balance.bandwidthPerSec"參數,該參數在Hadoop 3.x版本已廢棄)
Balancer bandwidth is set to 104857600
[root@hadoop101.yinzhengjie.com ~]# 

5>.使用均衡器注意事項

  (1)默認的DataNode策略是在DataNode級別均衡存儲,但均衡器不會在DataNode的各個存儲卷之間均衡數據。
  (2)僅當DataNode使用的DFS百分比和(由集群使用的)平均DFS之間的差大於(或小於)規定閾值時,均衡器才會均衡DataNode。否則,它不會重新均衡集群。
  (3)均衡器運行多長時間取決於集群的大小和數據的不平衡程度。第一次運行均衡器,或者不經常調度均衡器,以及在添加一組DataNode之后運行均衡器,它將運行很長時間(通常是幾天,如果數據量達到PB或者接近EB級別,可能需要一個多月的時間來均衡喲~)
  (4)如果有一個數據寫入和刪除頻繁的集群,集群可能永遠不會達到完全均衡的狀態,均衡器僅僅將數據從一個節點移動到另一個節點。
  (5)向集群添加新節點后最好立即運行均衡器。如果一次添加大量節點,則運行均衡器需要一段時間才能完成其工作。
  (6)如果確定閾值?這很容易,秩序選擇整個集群中節點最低DFS使用百分比即可。不必花費大量的時間了解每個節點使用的DFS百分比,使用"hdfs dfsadmin -report"命令即可找出正確的閾值。閾值越小,均衡器需要執行的工作越多,集群就越均衡。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM