Ceph —— 均衡PG

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部分无法达到准确应用。大家可以根据自己的情况酌情修改脚本。适合自己的才是最好的。


有何意见建议,可以留言,欢迎指正。

觉得写的不错,用着还可以的,可以点个推荐关注啥的。

猜你喜欢

转载自www.cnblogs.com/shu-sheng/p/12149807.html