HDFS均衡操作快速参考

Posted on Wed 01 May 2024 in 技术

快速判断是否需要均衡

# 计算当前均衡度(标准差)
hdfs dfsadmin -report | python3 -c "
import sys, re
used_percents = []
for line in sys.stdin:
    if 'DFS Used%:' in line:
        percent = float(re.search(r'(\d+\.?\d*)%', line).group(1))
        used_percents.append(percent)
if used_percents:
    avg = sum(used_percents) / len(used_percents)
    variance = sum((x - avg) ** 2 for x in used_percents) / len(used_percents)
    std_dev = variance ** 0.5
    print(f'标准差: {std_dev:.2f}%')
    if std_dev > 15:
        print('⚠️  需要立即均衡')
    elif std_dev > 10:
        print('⚠️  建议进行均衡')
    else:
        print('✅ 集群已均衡')
"

常用均衡命令

基本均衡

# 标准均衡(推荐)
nohup hdfs balancer -threshold 10 -policy datanode > /tmp/balancer.log 2>&1 &

# 严格均衡
nohup hdfs balancer -threshold 5 -policy datanode > /tmp/balancer.log 2>&1 &

# 宽松均衡
nohup hdfs balancer -threshold 15 -policy datanode > /tmp/balancer.log 2>&1 &

高级均衡

# 排除特定节点
nohup hdfs balancer -threshold 10 -exclude 192.168.1.100,192.168.1.101 > /tmp/balancer.log 2>&1 &

# 只均衡特定节点
nohup hdfs balancer -threshold 10 -include 192.168.1.102,192.168.1.103 > /tmp/balancer.log 2>&1 &

# 指定源节点
nohup hdfs balancer -threshold 10 -source 192.168.1.100,192.168.1.101 > /tmp/balancer.log 2>&1 &

参数说明

参数 用途 默认值 推荐值
-threshold 均衡阈值(%) 10 5-15
-policy 均衡策略 datanode datanode
-exclude 排除节点 - 维护节点
-include 包含节点 - 特定节点
-source 源节点 - 高负载节点
-idleiterations 空闲迭代次数 5 3-5

监控命令

检查均衡状态

# 检查均衡进程
ps aux | grep balancer

# 查看均衡日志
tail -f /tmp/balancer.log

# 实时监控均衡进度
python3 /tmp/monitor_hdfs_balancer.py

停止均衡

# 查找并停止均衡进程
pkill -f "hdfs.*balancer"

# 或者通过PID停止
kill $(cat /tmp/balancer.pid)

性能优化

调整均衡带宽

<!-- 在hdfs-site.xml中添加 -->
<property>
  <name>dfs.datanode.balance.bandwidthPerSec</name>
  <value>52428800</value>  <!-- 50MB/s -->
</property>

系统优化

# 网络优化
echo 'net.core.rmem_max = 134217728' >> /etc/sysctl.conf
echo 'net.core.wmem_max = 134217728' >> /etc/sysctl.conf
sysctl -p

# 磁盘优化
echo noop > /sys/block/sda/queue/scheduler

故障排除

常见问题

  1. 均衡进程无法启动
  2. 检查HDFS服务状态:hdfs dfsadmin -report
  3. 检查权限:whoami
  4. 查看日志:tail -f $HADOOP_LOG_DIR/hadoop-*-balancer-*.log

  5. 均衡速度过慢

  6. 检查网络:iperf3 -c <target_node>
  7. 检查磁盘I/O:iostat -x 1 5
  8. 调整均衡带宽

  9. 均衡进程异常退出

  10. 检查系统资源:free -h, df -h
  11. 查看系统日志:dmesg | tail -50
  12. 重新启动均衡

最佳实践

  1. 时间选择:在业务低峰期进行均衡
  2. 参数设置:生产环境使用5-10%阈值
  3. 监控告警:设置自动化监控和告警
  4. 分批进行:大型集群可以分批均衡
  5. 数据验证:均衡后检查数据完整性

自动化脚本

一键均衡脚本

#!/bin/bash
# 检查均衡度并自动启动均衡

STD_DEV=$(hdfs dfsadmin -report | python3 -c "
import sys, re
used_percents = []
for line in sys.stdin:
    if 'DFS Used%:' in line:
        percent = float(re.search(r'(\d+\.?\d*)%', line).group(1))
        used_percents.append(percent)
if used_percents:
    avg = sum(used_percents) / len(used_percents)
    variance = sum((x - avg) ** 2 for x in used_percents) / len(used_percents)
    std_dev = variance ** 0.5
    print(f'{std_dev:.2f}')
else:
    print('0')
")

echo "当前均衡度: ${STD_DEV}%"

if (( $(echo "$STD_DEV > 10" | bc -l) )); then
    echo "启动均衡..."
    nohup hdfs balancer -threshold 10 > /tmp/balancer.log 2>&1 &
    echo "均衡进程已启动,PID: $!"
else
    echo "集群已均衡,无需操作"
fi

监控脚本

简化监控脚本

#!/bin/bash
# 简化版均衡监控

while true; do
    echo "=== $(date) ==="

    # 检查均衡进程
    if pgrep -f "hdfs.*balancer" > /dev/null; then
        echo "✅ 均衡进程正在运行"
    else
        echo "❌ 均衡进程未运行"
    fi

    # 显示各节点使用率
    echo "各节点使用率:"
    hdfs dfsadmin -report | grep -E "(Name:|DFS Used%:)" | \
        awk 'NR%2==1{name=$0} NR%2==0{print name " " $0}'

    echo "----------------------------------------"
    sleep 60
done

注意:本快速参考适用于日常运维,详细操作请参考完整版文档。