大数据集群 Ansible 自动化部署教程#
基于 Ansible 的声明式自动化部署方案,与现有 Shell/Python 脚本并存,适用于 3-10 节点集群。
一、前置准备#
1.1 管理机要求#
| 需求 | 说明 |
|---|---|
| OS | Linux / macOS(Windows WSL 兼容) |
| Ansible | >= 2.15(安装方法见下) |
| sshpass | yum install sshpass(CentOS)或 apt install sshpass(Ubuntu) |
| 网络 | 可 SSH 到达所有集群节点 |
1.2 集群节点要求#
| 需求 | 说明 |
|---|---|
| OS | CentOS 7.9+ / Rocky Linux 8+ |
| SSH | 已启用、密码认证或密钥认证 |
| Sudo | hadoop 用户有 sudo 权限 |
| 磁盘 | /opt/module 至少 10GB 可用空间 |
1.3 安装 Ansible#
# CentOS / Rocky
sudo yum install -y epel-release
sudo yum install -y ansible
# Ubuntu / Debian
sudo apt update && sudo apt install -y ansible
# macOS
brew install ansible
# 验证安装
ansible --version # 应显示 >= 2.151.4 本地缓存安装包(推荐,加速部署)#
cd ansible/files/cache/
# 下载 Hadoop 各组件,已下载的跳过(共约 1.8GB)
wget -c https://mirrors.tuna.tsinghua.edu.cn/apache/hadoop/common/hadoop-3.3.6/hadoop-3.3.6.tar.gz
wget -c https://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/zookeeper-3.5.9/apache-zookeeper-3.5.9-bin.tar.gz
wget -c https://mirrors.tuna.tsinghua.edu.cn/apache/hive/hive-3.1.3/apache-hive-3.1.3-bin.tar.gz
wget -c https://mirrors.tuna.tsinghua.edu.cn/apache/hbase/2.6.4/hbase-2.6.4-hadoop3-bin.tar.gz
wget -c https://mirrors.tuna.tsinghua.edu.cn/apache/kafka/2.8.0/kafka_2.13-2.8.0.tgz
wget -c https://mirrors.tuna.tsinghua.edu.cn/apache/spark/spark-3.5.8/spark-3.5.8-bin-hadoop3-scala2.13.tgz
wget -c https://maven.aliyun.com/repository/public/mysql/mysql-connector-java/5.1.49/mysql-connector-java-5.1.49.jarJDK 需预先手动解压到所有节点的
/opt/module/jdk1.8.0_471(或通过 common role 自动下载,但 Adoptium 镜像速度较慢,建议手动安装)。
二、集群配置#
2.1 编辑 Inventory#
编辑 ansible/inventory/hosts.yml,修改节点信息为你的实际环境:
# 只需修改这里
cluster:
hosts:
node1:
ansible_host: 192.168.137.155 # 主节点 IP
hostname: hd2 # 主节点主机名
node2:
ansible_host: 192.168.137.156
hostname: hd3
node3:
ansible_host: 192.168.137.157
hostname: hd4
# 添加更多节点...
# node4:
# ansible_host: 192.168.137.158
# hostname: hd52.2 修改全局变量#
编辑 ansible/inventory/hosts.yml 中 all.vars 部分的网络和凭据配置:
# 网络模式: "hostname"(使用主机名.域名)或 "ip"(直接使用IP)
network_mode: "hostname" # 改为 "ip" 如果无 DNS
domain_suffix: "pblh123.cn" # 改为你的域名后缀
# SSH 凭据
ansible_user: hadoop
ansible_password: hadoop # 改为你的密码
# MySQL(Hive 元数据库)
mysql:
host: localhost
database: hive_metastore
user: hive
password: "Hive@12345" # 改为你的 MySQL 密码2.3 完整变量参考#
| 配置组 | 变量 | 默认值 | 说明 |
|---|---|---|---|
| 网络 | network_mode | hostname | 域名模式:hostname 或 ip |
| 网络 | domain_suffix | pblh123.cn | 域名后缀 |
| SSH | ansible_user | hadoop | SSH 登录用户 |
| SSH | ansible_password | hadoop | SSH 登录密码 |
| 路径 | base_dir | /opt/module | 组件安装根目录 |
| 路径 | data_dir | /opt/data | 数据存储根目录 |
| 版本 | versions.hadoop | hadoop-3.3.6 | Hadoop 版本目录名 |
| 版本 | versions.zookeeper | apache-zookeeper-3.5.9-bin | ZK 版本目录名 |
| 版本 | versions.hive | apache-hive-3.1.3-bin | Hive 版本目录名 |
| 版本 | versions.hbase | hbase-2.6.4-hadoop3 | HBase 版本目录名 |
| 版本 | versions.kafka | kafka_2.13-2.8.0 | Kafka 版本目录名 |
| 版本 | versions.spark | spark-3.5.8-bin-hadoop3-scala2.13 | Spark 版本目录名 |
| HDFS | hdfs.replication | 2 | HDFS 副本数 |
| Spark | spark.driver_memory | 512m | Spark Driver 内存 |
| Spark | spark.executor_memory | 512m | Spark Executor 内存 |
| JVM | java_heap.namenode | 512m | NameNode 堆大小 |
三、部署步骤#
3.1 预检查#
# 测试 SSH 连通性
ansible -i ansible/inventory/hosts.yml cluster -m ping
# 预期输出: 每个节点返回 SUCCESS / "pong"
# 如果失败,检查:
# 1. IP 是否正确
# 2. SSH 用户名/密码是否正确
# 3. 节点是否在线
# 查看节点详情
ansible -i ansible/inventory/hosts.yml cluster -m setup -a "filter=ansible_memtotal_mb"3.2 部署方式一:分步执行(推荐首次使用)#
cd ansible/
# 步骤1: 安装组件(下载 + 解压)
ansible-playbook -i inventory/hosts.yml playbooks/install.yml
# 步骤2: 配置集群(分发配置文件)
ansible-playbook -i inventory/hosts.yml playbooks/configure.yml
# 步骤3: 格式化 HDFS 并启动服务
# 先只格式化 + 启动
ansible-playbook -i inventory/hosts.yml playbooks/deploy.yml --tags format,hdfs,zookeeper,yarn3.3 部署方式二:一键部署#
cd ansible/
# 完整部署(含 HDFS 格式化)
ansible-playbook -i inventory/hosts.yml playbooks/deploy.yml
# 重新部署(跳过 HDFS 格式化,保留数据)
ansible-playbook -i inventory/hosts.yml playbooks/deploy.yml --skip-tags format
# 仅部署到特定节点
ansible-playbook -i inventory/hosts.yml playbooks/deploy.yml --limit node1
# 仅安装特定组件
ansible-playbook -i inventory/hosts.yml playbooks/deploy.yml --tags hadoop,hive3.4 按需执行#
# 仅重新分发配置(无需重启)
ansible-playbook -i inventory/hosts.yml playbooks/configure.yml
# 仅重新分发 Hadoop 配置
ansible-playbook -i inventory/hosts.yml playbooks/configure.yml --tags hadoop
# 添加新节点后,仅配置新节点
ansible-playbook -i inventory/hosts.yml playbooks/configure.yml --limit node4四、集群管理#
4.1 查看服务状态#
# JPS 进程 + 端口检查
ansible-playbook -i inventory/hosts.yml playbooks/status.yml4.2 启停服务#
# 启动全部服务
ansible-playbook -i inventory/hosts.yml playbooks/start.yml
# 停止全部服务
ansible-playbook -i inventory/hosts.yml playbooks/stop.yml
# 重启全部服务
ansible-playbook -i inventory/hosts.yml playbooks/restart.yml
# 启动单个服务
ansible-playbook -i inventory/hosts.yml playbooks/start.yml --tags kafka
# 停止单个服务
ansible-playbook -i inventory/hosts.yml playbooks/stop.yml --tags hbase4.3 功能验证#
# 13 项端到端检查(失败不中断,生成汇总报告)
ansible-playbook -i inventory/hosts.yml playbooks/verify.yml验证项目:
| # | 检查项 | 方法 |
|---|---|---|
| 1 | 节点连通性 | ping |
| 2 | HDFS 读写 | 写文件 → 读回 → 校验 → 删除 |
| 3 | HDFS 容量 | hdfs dfsadmin -report |
| 4 | YARN 节点 | yarn node -list |
| 5 | ZooKeeper 角色 | echo stat | nc localhost 2181 (逐个节点) |
| 6 | Hive Metastore 端口 | TCP 9083 |
| 7 | HiveServer2 端口 | TCP 10000 |
| 8 | HBase Shell | echo "status" | hbase shell |
| 9 | Kafka 端到端 | 创建主题 → 生产 → 消费 → 删除 |
| 10 | Spark Pi 作业 | spark-submit --class SparkPi 集群模式 |
| 11 | Spark History 端口 | TCP 18080 |
| 12 | Web UI(4 个) | HDFS/YARN/Spark/HBase HTTP 可达 |
五、Web 管理界面#
部署完成后,通过浏览器访问:
| 服务 | URL |
|---|---|
| HDFS NameNode | http://<master_ip>:9870 |
| YARN ResourceManager | http://<master_ip>:8088 |
| Spark History Server | http://<master_ip>:18080 |
| HBase Master | http://<master_ip>:16010 |
六、常见问题#
6.1 SSH 连接失败#
# 检查 SSH 连通性
ssh hadoop@192.168.137.155 "hostname"
# 确认密码认证已启用
sudo grep PasswordAuthentication /etc/ssh/sshd_config
# 应该是 yes,如果不是:
sudo sed -i 's/PasswordAuthentication no/PasswordAuthentication yes/' /etc/ssh/sshd_config
sudo systemctl restart sshd6.2 sudo 需要 TTY#
如果遇到 sudo: a terminal is required 错误:
# 在 Ansible 不启用 become 时通常不需要 PTY
# 如果仍然报错,在所有节点上执行:
sudo visudo
# 注释掉或删除: Defaults requiretty
# 或添加: Defaults:hadoop !requiretty6.3 端口被占用#
# 查看哪个进程占用了端口
sudo netstat -tlnp | grep <端口号>
# 或
sudo ss -tlnp | grep <端口号>
# 强制停止所有 Java 进程
jps -l | grep -v Jps | awk '{print $1}' | xargs -r kill -96.4 服务启动超时#
# 检查组件是否已安装
ls /opt/module/
# 查看日志
tail -100 /opt/module/hadoop-3.3.6/logs/*.log
# 手动启动调试
source /etc/profile.d/hadoop_env.sh
hdfs namenode # 前台启动 NameNode 查看错误6.5 添加新节点#
- 在
inventory/hosts.yml中添加新节点 - 分配
zk_myid和kafka_broker_id(递增) - 运行配置分发:
ansible-playbook -i inventory/hosts.yml playbooks/configure.yml --limit <new_node>- 重启受影响的服务(ZooKeeper、HBase regionservers、Kafka)
6.6 下载速度慢#
# 方案1: 预下载到本地缓存(推荐)
cd ansible/files/cache/
# 用国内镜像下载全部组件
# 方案2: 修改镜像源
# 编辑 inventory/hosts.yml 中的 mirrors.apache 为阿里云镜像
# mirrors.apache: "https://mirrors.aliyun.com/apache"七、与 Shell/Python 脚本的关系#
两种方案独立运行、互不影响:
| 场景 | 推荐方案 |
|---|---|
| 初次部署、配置一致性要求高 | Ansible |
| 快速启停、日常运维 | Shell 脚本(cluster_ctl.sh) |
| 远程管理 | Python 脚本(cluster_ctl.py) |
| 配置漂移修复 | Ansible configure.yml |
Ansible 和现有脚本共享相同的 config.ini 配置语义,但使用独立的 inventory/hosts.yml 变量文件。修改配置时需同步更新两处。