# HDFS Rack Awareness

> Rack awareness for Apache Hadoop HDFS on Kubernetes, covering how block replicas are spread across racks or zones for better fault tolerance.

[Rack awareness](https://hadoop.apache.org/docs/stable/hadoop-project-dist/hadoop-common/RackAwareness.html) is a feature in Apache Hadoop that allows users to define a cluster’s node topology. Hadoop uses that topology to distribute block replicas in a way that maximizes fault tolerance.

For example, when a new block is created, the default behavior is to place one replica on a different node within the same rack, and another on a node in a remote rack. To do this effectively, Hadoop must access information about the underlying infrastructure. In a Kubernetes environment, this involves retrieving data from Pods or Nodes in the cluster.

## Configuring rack awareness

To configure rack awareness, use the `rackAwareness` field in the cluster configuration:

```yaml
apiVersion: hdfs.stackable.tech/v1alpha1
kind: HdfsCluster
metadata:
  name: simple-hdfs
spec:
  clusterConfig:
    rackAwareness:
      - nodeLabel: topology.kubernetes.io/zone
      - podLabel: app.kubernetes.io/role-group
  nameNodes:
    ...
```

This creates an internal topology label by combining the values of the `topology.kubernetes.io/zone` Node label and the `app.kubernetes.io/role-group` Pod label (e.g. `/eu-central-1/rg1`).

## How it works

In order to enable gathering this information the Hadoop images contain the [hdfs-topology-provider](https://github.com/stackabletech/hdfs-utils/blob/v0.5.0/src/main/java/tech/stackable/hadoop/StackableTopologyProvider.java) on the classpath, which can be configured to read labels from Kubernetes objects.

The operator deploys ClusterRoles and ServicesAccounts with the relevant RBAC rules to allow the Hadoop Pod to access the necessary Kubernetes objects. Topologies and other metadata such as Node- and Pod-IPs and endpoints are held in separate caches so that they can be refreshed independently of one another. The [hdfs-topology-provider](https://github.com/stackabletech/hdfs-utils/blob/v0.5.0/src/main/java/tech/stackable/hadoop/StackableTopologyProvider.java) is namespace-scoped and pods in the active namespace are watched so that changes can be propagated to the internal cache to minimise cache misses.

> **Note:** Rack awareness may not work as expected on clusters such as `kind` or `k3s` that configure IP-masquerading differently to production-ready distributions.
