Ceph —— 均衡PG
日常工作中,我們常常會發現PG不均衡,而集群中只要有一個OSD先達到full的狀態,則整個集群都無法寫入數據,所以為了盡可能的提高集群存儲空間的利用率,我們都希望PG盡可能的均勻分布在OSD上。
出現PG不均衡最常見的背景情況:
-
剛剛安裝部署Ceph集群完畢
-
集群擴容或是其他情況,進行了加盤的操作,OSD數量發生變化
為了節省日常工作的時間,針對PG均衡問題,寫了個python腳本:
version : ceph_luminous (12.2.2)
1 #!/usr/bin/python 2 3 import os 4 5 6 def Ceph_balance(): 7 # Gets the PG number for each OSD 8 PGS = os.popen(""" ceph osd df | awk '{print $10}' | egrep -v "^$|PGS" """).readlines() 9 # Get the max difference of PG number 10 MAX_DIFFERENT_PG = max(map(eval, PGS)) - min(map(eval, PGS)) 11 # Get pool list 12 lspools = os.popen('ceph osd lspools').read().split(',')[:-1] 13 POOL_LIST = map(lambda x: x[2:], lspools) 14 # To allow use of the feature, you must tell the cluster that it only needs to support luminous (and newer) clients with: 15 os.system('ceph osd set-require-min-compat-client luminous') 16 if MAX_DIFFERENT_PG >= 1: 17 # Grab the latest copy of your osdmap 18 os.system('ceph osd getmap -o /tmp/osd.map') 19 for i in POOL_LIST: 20 # Run the optimizer 21 os.system('osdmaptool /tmp/osd.map --upmap /tmp/%sout.txt --upmap-pool %s' % (i, i)) 22 for i in POOL_LIST: 23 # Apply the changes to the cluster 24 os.system('source /tmp/%sout.txt' % i) 25 # clean up txt file 26 for i in POOL_LIST: 27 os.system('rm -f /tmp/%sout.txt' % i) 28 os.system('rm -f /tmp/osd.map') 29 print("Ceph balance has been successful !") 30 31 32 if __name__ == '__main__': 33 Ceph_balance()
此腳本只適用於luminous及以上的版本
ps:因本人awk比較菜,所以這個腳本獲取PGS部分無法達到准確應用。大家可以根據自己的情況酌情修改腳本。適合自己的才是最好的。
參考文檔:Ceph官方文檔地址
有何意見建議,可以留言,歡迎指正。
覺得寫的不錯,用着還可以的,可以點個推薦關注啥的。