Why Rack Awareness?
HDFS tries to place replicas on different racks so a rack failure (power or switch) does not lose all copies, and YARN prefers local or same-rack data for tasks. Without a topology, every node is treated as being in /default-rack.
text
Default placement of 3 replicas:
replica 1 -> the writer's node (or random)
replica 2 -> a node on a different rack
replica 3 -> another node on the same rack as replica 2
Topology Script
bash
#!/bin/bash
# /etc/hadoop/conf/topology.sh : receives IPs or host names, prints one rack path per argument
declare -A RACKS
while read -r host rack; do RACKS[$host]=$rack; done < /etc/hadoop/conf/topology.data
for node in "$@"; do
echo "${RACKS[$node]:-/default-rack}"
done
text
# /etc/hadoop/conf/topology.data
10.0.1.21 /dc1/rack1
10.0.1.22 /dc1/rack1
10.0.2.21 /dc1/rack2
10.0.2.22 /dc1/rack2
worker1.hadoop.example.com /dc1/rack1
xml
<!-- core-site.xml -->
<property><name>net.topology.script.file.name</name><value>/etc/hadoop/conf/topology.sh</value></property>
<property><name>net.topology.script.number.args</name><value>100</value></property>
bash
chmod 755 /etc/hadoop/conf/topology.sh
/etc/hadoop/conf/topology.sh 10.0.1.21 10.0.2.22 8.8.8.8 # test the script
hdfs dfsadmin -printTopology
# Rack: /dc1/rack1
# 10.0.1.21:9866 (worker1)
# Rack: /dc1/rack2
# 10.0.2.21:9866 (worker3)
Restart needed: The NameNode reads topology when a DataNode registers and caches results. After changing the mapping, restart the NameNode (or the affected DataNodes) and then check printTopology. Existing blocks are not moved automatically.
Fix Existing Placement
bash
hdfs fsck / -racks | grep -i "mis-replicated" # blocks violating the rack policy
# The NameNode repairs mis-replicated blocks gradually. You can also force a re-replication of a path:
hdfs dfs -setrep -w 3 /data/important
Configuration Management with Ansible
Ansible connects over SSH and applies idempotent tasks, so you can run the same playbook many times safely. Keep everything in Git.
text
hadoop-ansible/
inventory/prod.ini
group_vars/all.yml
roles/hadoop_config/
tasks/main.yml
templates/core-site.xml.j2
templates/hdfs-site.xml.j2
handlers/main.yml
site.yml
ini
# inventory/prod.ini
[masters]
master1.hadoop.example.com
master2.hadoop.example.com
[workers]
worker[1:20].hadoop.example.com
[hadoop:children]
masters
workers
yaml
# group_vars/all.yml
hadoop_conf_dir: /etc/hadoop/conf
nameservice: prod
dfs_replication: 3
dn_dirs: "file:///data/disk1/dn,file:///data/disk2/dn"
nn_dirs: "file:///data/nn1,file:///data/nn2"
yaml
# roles/hadoop_config/tasks/main.yml
- name: Deploy site files
ansible.builtin.template:
src: "{{ item }}.j2"
dest: "{{ hadoop_conf_dir }}/{{ item }}"
owner: root
group: hadoop
mode: "0644"
validate: "xmllint --noout %s"
loop: [core-site.xml, hdfs-site.xml, yarn-site.xml]
notify: restart hadoop services
- name: Deploy topology script and data
ansible.builtin.copy:
src: "{{ item }}"
dest: "{{ hadoop_conf_dir }}/{{ item }}"
mode: "0755"
loop: [topology.sh, topology.data]
xml
<!-- roles/hadoop_config/templates/hdfs-site.xml.j2 -->
<configuration>
<property><name>dfs.replication</name><value>{{ dfs_replication }}</value></property>
<property><name>dfs.datanode.data.dir</name><value>{{ dn_dirs }}</value></property>
</configuration>
yaml
# roles/hadoop_config/handlers/main.yml
- name: restart hadoop services
ansible.builtin.systemd:
name: "{{ 'hadoop-datanode' if 'workers' in group_names else 'hadoop-namenode' }}"
state: restarted
bash
ansible-playbook -i inventory/prod.ini site.yml --check --diff # dry run, show changes
ansible-playbook -i inventory/prod.ini site.yml --limit worker1.hadoop.example.com # try on one node first
ansible-playbook -i inventory/prod.ini site.yml --forks 5 --serial 5 # rolling: 5 nodes at a time
Rolling changes: Use serial to restart a few DataNodes at a time and wait for the cluster to be healthy between batches. A bad config pushed to all nodes at once is a classic outage.
Conclusion
A topology script gives HDFS rack awareness for safer replica placement. Ansible with templates, validation and serial batches keeps configuration identical and changes safe and reviewable.
Quick Summary: net.topology.script.file.name + printTopology; Ansible template + validate + serial.