Thursday, June 05, 2014

Multi-Node Hadoop Cluster On Ubuntu 14.04

In this tutorial I will describe the steps for setting up a multi-node i.e. five nodes Hadoop cluster running on Ubuntu. I have created this setup inside a VMware ESXi box. There are five virtual machines all having the same common configurations like 20GB HDD, 512MB RAM, etc. For this I will be using Ubuntu 14.04 LTS as operating system and Hadoop 1.2.1. All the machines will be distinguished by their names which correspond to namenode, secondary namenode, datanode1, datanode2 and datanode3. So it is a five node hadoop cluster.

Node Name
IP Address
Secondary Namenode

Install Ubuntu 14.04 LTS in all the nodes and update/upgrade it.
To pull the newest sources, execute below command.
user@nn:~$ sudo apt-get update

To pull upgrades for the os and packages execute below command.
user@nn:~$ sudo apt-get upgrade

Reboot all the machines.

NOTE: There are a few configurations that are needed to be done in all nodes. I will show for single node as the commands and steps will be same for all other nodes.

Create a hadoop group
user@nn:~$ sudo groupadd hadoop

Create a hadoop user “huser” and place the user in hadoop group
user@nn:~$ sudo useradd -m -d /home/huser -g hadoop huser

Set the password for the user “huser”
user@nn:~$ sudo passwd huser

By default, ubuntu does not create users with sudo permissions. Hence we need to assign “huser” user sudo permissions.
user@nn:~$ sudo chmod 740 /etc/sudoers
user@nn:~$ sudo vi /etc/sudoers

NOTE: User and group configuration has to be done in all nodes.

Hadoop requires openssh-server for creating passwordless ssh environment to manage hadoop services from namenode to datanodes. The namenode seldom communicates with the datanodes. Instead datanodes communicate with namenode to send heartbeat and build block locations with the help of block reports sent by datanodes to namenodes. The datanode to namenode communication is based on simple socket communications. We will use ssh sessions that will be run as Hadoop user, “huser” that we created earlier.

user@nn:~$ su – huser

NOTE: Login as “huser” user on all nodes as we will be performing all the tasks as huser from now onwards on all nodes.

huser@nn:~$ sudo apt-get install openssh-server

huser@nn:~$ ssh-keygen -t rsa
huser@nn:~$ ssh-copy-id -i ~/.ssh/ huser@nn2
huser@nn:~$ ssh-copy-id -i ~/.ssh/ huser@dn1
huser@nn:~$ ssh-copy-id -i ~/.ssh/ huser@dn2
huser@nn:~$ ssh-copy-id -i ~/.ssh/ huser@dn3

NOTE: Verify the passwordless ssh environment from namenode to all datanodes as “huser” user.

I will be using openjdk-7-jdk package which installs easily.
huser@nn:~$ sudo apt-get install openjdk-7-jdk

NOTE: Openjdk installation has to be done on all nodes. After the installation verify the java installation by running the below command.
huser@nn:~$ java -version

Hadoop package can be downloaded from these mirror locations. I had copied the hadoop-1.2.1.tar.gz file into all nodes' “huser” home folder.
Unzip the downloaded file in your “huser” home directory itself.
huser@nn:~$ tar -xzvf hadoop-1.2.1.tar.gz

Now move it to /usr/local location.
huser@nn:~$ sudo mv hadoop-1.2.1 /usr/local/hadoop

Assign the permissions in favor of user “huser”.
 huser@nn:~$ sudo chown -R huser:hadoop /usr/local/hadoop/

NOTE: Installation of hadoop has to be done on all nodes.

Now we will configure the environment for user “huser”. As we are already logged into the huser home directory, we need to append the .bashrc file with the below mentioned variables to set the user environment.

huser@nn:~$ vi .bashrc
export JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64/
export PATH=$PATH:$JAVA_HOME/bin
export HADOOP_PREFIX=/usr/local/hadoop/

Execute the .bashrc file.
huser@nn:~$ exec bash

To verify the defined hadoop variables execute the below command to check the hadoop version that is installed.

huser@nn:~$ hadoop version

NOTE: User environment has to be configured on all nodes.

It is very important to define the hosts in /etc/hosts file on all nodes. Hence, I have copied the content of my namenode /etc/hosts file to all the datanodes and secondary namenode.

Finally we have arrived at this point where we are going to begin with hadoop configuration. All the configuration files are present in /usr/local/hadoop/conf directory.

Hadoop Environment -
First of all we will go ahead and edit the file which plays a major role in defining the overall runtime parameters for hadoop. Mainly we require to set the JAVA_HOME parameter to specify the location of java installation. If anyone has IPv6 enabled at your location, he has to define IPv4 Stack to true as hadoop does not work on IPv6. Also I have defined hadoop log directory inside /var/log/ directory for my own convenience.

huser@nn:~$ vi /usr/local/hadoop/conf/
export JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64
export HADOOP_LOG_DIR=/var/log/hadoop-log

After defining the variables we need to create the hadoop-log directory in /var/log location on all nodes with the required “huser” user ownership and permissions.

huser@nn:~$ sudo mkdir /var/log/hadoop-log
huser@nn:~$ sudo chown -R huser:hadoop /var/log/hadoop-log

NOTE: It is necessary to modify with same parameters across all nodes in the cluster and /var/log/hadoop-log directory has to be created on all nodes with required “huser” user ownership and permissions

Default Filesystem - core-site.xml
Next we move to core-site.xml file which helps in pointing the datanodes to namenodes and which port they should listen to. It mainly contains configuration properties like Apache Hadoop Core (such as I/O) that hadoop uses when starting up. These settings are common to HDFS and MapReduce.

Here we will configure a parameter called This parameter is used by clients to access the hdfs filesystem or the metadata for the requested file precisely. parameter is set to an HDFS URI like hdfs://<authority>:<port>, the URI identifies the filesystem and path specifies the location of file/directory in the filesystem. As the name suggests it points to the default URI for all filesystem requests in hadoop. Parameter: is used by client to get URI of the default filesystem and namenode to read the block adresses. It is basically specifies the hostname and the binded port number of the HDFS filesystem. Hence this file has to be defined in all nodes.

The next parameter that we will configure here is hadoop.tmp.dir. This parameter I have used to specify the base for temporary directories locally and also in HDFS.

Create the tmp dir as specified in the core-site.xml file.

huser@nn:~$ mkdir /usr/local/hadoop/tmp

NOTE: Copy this file to all nodes. We can use IP address or hostname and whichever port number of the namenode. Commonly used port number is 9000, but I have chosen 10001. “/tmp” directory has to be created on all nodes.

MapReduce Framework – mapred-site.xml
This file used to point all the task-trackers to the job-tracker. The parameter: mapred.job.tracker sets the hostname or IP and port of job-tracker.

NOTE: Copy this file to all nodes. Port number 10002 is my choice, it is not mandatory to use the same port number.

HDFS Configuration – hdfs-site.xml
File hdfs-site.xml contains various parmeters that configures HDFS.
The parameter “” and “” define the configuration settings for HDFS daemons: the namenode and the datanodes. “” defines the directory where namenode stores it's metadata and “” defines the directory where datanodes will store their HDFS data blocks. “dfs.replication” parameter defines the number of replicas to be made for each block of data that is created.
Since our cluster is a multi-node cluster it is important to node that the hdfs-site.xml file would be different on both namenode and datanodes. The namenode will only carry “” parameter, as we have separate machines for datanodes which will only carry “” parameter. The “dfs.replication” parameter will be common and same for all nodes.

hdfs-site.xml configuration for namenode
huser@nn:~$ vi /usr/local/hadoop/conf/hdfs-site.xml

As specified in the hdfs-site.xml file, we need to create a directories hdfs and name in namenode with appropriate “huser” user ownership and permissions.
huser@nn:~$ sudo mkdir -p /hdfs/name
huser@nn:~$ sudo chown -R huser:hadoop /hdfs

hdfs-site.xml configuration for all datanodes
huser@dn1:~$ vi /usr/local/hadoop/conf/hdfs-site.xml

Similary in all datanodes we need to create hdfs and data directories.
huser@dn1:~$ sudo mkdir -p /hdfs/data
huser@dn1:~$ sudo chown -R huser:hadoop /hdfs

NOTE: The hdfs-site.xml file is different in both namenode and datanode because I am not running datanode daemon on the namenode and we are trying to create a fully distributed mode cluster.

The masters file contains the location where the secondary namenode daemon would start.
huser@nn:~$ vi /usr/local/hadoop/conf/masters

We have configured separate machine for the secondary namenode hence we are defining secondary namenode explicitly here. There is no need to duplicate this file to all nodes.

The slaves file contains the list of datanodes where the datanode and task-tracker daemons will run.
huser@nn:~$ vi /usr/local/hadoop/conf/slaves

NOTE: The file should contain one entry per line. We can also mention “namenode” and “secondary namenode” hostname in this file if we want to run datanode and task-tracker daemons on “namenode” and “secondary namenode” too.

Formatting HDFS via namenode
Before we start our cluster we will format the HDFS via namenode. Formatting the namenode means to initialize the directory specified in “” and “” parameter in hdfs-site.xml file. After formatting the namenode, “current”, “image” and “previous.checkpoint” directories will be created on namenode and the data directory in datanodes will simply be formatted.

huser@nn1:~$ hadoop namenode -format

NOTE: We need to format the namenode only the first time we setup hadoop cluster. Formatting a running cluster will destroy all the existing data. After executing the format command, it will prompt for confirmation where we need to type “Y” as it is case-sensitive.

Starting the Multi-Node Hadoop Cluster
Starting a hadoop cluster can be done by a single command mentioned below.

This command will first start the HDFS daemons. The namenode daemon is started on namenode and datanode daemon is started on all datanodes. The secondary namenode daemon is also started on secondary namenode. In the second phase it will start theMapReduce daemons, job-tracker on namenode and task-trackers on datanodes.

JPS (Java Virtual Machine Process Status Tool) is used to verify that all the daemons are running on their respective nodes, we will execute “jps” command to see the java processes running.

Namenode java processes

Datanode java processes

Secondary namenode java processes

NOTE: Check for errors in /var/log/hadoop-log/ directory in case there is any missing java process.

Checking Ports Status
To check whether hadoop is listening to the configured ports execute the below command.

huser@nn:~$ sudo netstat -ntlp | grep java

We can see in the above screenshot that our namenode is running hdfs on port 50070 and job-tracker is running on port 50030.

Checking HDFS Status
huser@nn:~$ hadoop dfsadmin -report
Configured Capacity: 14713540608 (13.7 GB)
Present Capacity: 13865287680 (12.91 GB)
DFS Remaining: 13865164800 (12.91 GB)
DFS Used: 122880 (120 KB)
DFS Used%: 0%
Under replicated blocks: 0
Blocks with corrupt replicas: 0
Missing blocks: 0

Datanodes available: 3 (3 total, 0 dead)

Decommission Status : Normal
Configured Capacity: 4904513536 (4.57 GB)
DFS Used: 40960 (40 KB)
Non DFS Used: 282750976 (269.65 MB)
DFS Remaining: 4621721600(4.3 GB)
DFS Used%: 0%
DFS Remaining%: 94.23%
Last contact: Sun May 18 12:11:49 IST 2014

Decommission Status : Normal
Configured Capacity: 4904513536 (4.57 GB)
DFS Used: 40960 (40 KB)
Non DFS Used: 282750976 (269.65 MB)
DFS Remaining: 4621721600(4.3 GB)
DFS Used%: 0%
DFS Remaining%: 94.23%
Last contact: Sun May 18 12:11:49 IST 2014

Decommission Status : Normal
Configured Capacity: 4904513536 (4.57 GB)
DFS Used: 40960 (40 KB)
Non DFS Used: 282750976 (269.65 MB)
DFS Remaining: 4621721600(4.3 GB)
DFS Used%: 0%
DFS Remaining%: 94.23%
Last contact: Sun May 18 12:11:48 IST 2014

Checking HDFS content
Hadoop API provides the facility to run linux like commands to check and manage HDFS content.
Listing files in /
huser@nn:~$ hadoop fs -ls /

Creating directory
hadoop fs -mkdir /user

Copying Files to HDFS

Before we run a mapreduce job we will copy some text files from our local filesystem into the HDFS.
huser@nn:~$ hadoop dfs -copyFromLocal ~/Books/ /user/huser/Books/

Both HDFS and MapReduce provide Web-UI Management websites to browse the HDFS, monitor the logs and jobs.
HDFS: http://nn:50070
The DFSHealth site allows you to browse the HDFS, monitor namenode logs, view nodes, check the space used, etc.

Job-Tracker: http://nn:50030
The job-tracker site provides the information regarding running map and reduce tasks, running and completed jobs and much more.

If we want to bring a data node down for whatever reason, we have to decommission that datanode. To decommission a datanode we just have to create a file named excludes in the hadoop conf directory (/usr/local/hadoop/conf/) and mention the hostname of the datanode we want to decommission.
huser@nn:~$ vi /usr/local/hadoop/conf/excludes

Next we need to edit the core-site.xml file provide the excludes file location into a property setting.

Similarly we can have includes files too. Now we can restart the namenode or just refresh the configuration using below command.
huser@nn:~$ hadoop dfsadmin -refreshNodes

We can see the decommissioned nodes under “Decommissioning Nodes” section on namenode webUI (http://nn:50070).

So here we discussed about creating a fully distributed multi-node Hadoop cluster on Ubuntu 14.04. Hope that was easy to understand and implement. In the coming posts we will discuss about running a mapreduce job on our Hadoop cluster. Happy Hadooping!!!

Single-Node Hadoop Cluster on Ubuntu 14.04

In this tutorial I will demonstrate how to install and run Single-Node Hadoop Cluster in Ubuntu 14.04.

As a requirement java needs to be installed.
user@hadoop-lab:~$ sudo apt-get install openjdk-7-jdk

Create a dedicated user account and group for hadoop.
user@hadoop-lab:~$ sudo groupadd hadoop
user@hadoop-lab:~$ sudo useradd -m -d /home/huser/ -g hadoop huser
user@hadoop-lab:~$ sudo passwd huser

Login as “huser” user to do further configurations.
user@hadoop-lab:~$ su – huser

The master node manages slave nodes (starting and stopping services) using SSH.
huser@hadoop-lab:~$ sudo apt-get install openssh-server
huser@hadoop-lab:~$ ssh-keygen -t rsa
huser@hadoop-lab:~$ cp /home/huser/.ssh/ authorized_keys

Testing SSH Setup
huser@hadoop-lab:~$ ssh localhost

Download Apache Hadoop Release 1.2.1 from Apache Download Mirrors site into huser home directory, extract it and assign hadoop user & group ownership.
huser@hadoop-lab:~$ tar -xzvf hadoop-1.2.1.tar.gz
huser@hadoop-lab:~$ sudo mv /home/huser/hadoop-1.2.1 /usr/local/hadoop/
huser@hadoop-lab:~$ sudo chown -R huser:hadoop /usr/local/hadoop/

Set user environment variables for java and hadoop home directories.
huser@hadoop-lab:~$ vi .bashrc

Hadoop Environment Setup
Setup Java Home for Hadoop and disable IPv6.
huser@hadoop-lab$ vi /usr/local/hadoop/conf/

The main hadoop configurations are stored in 3 files listed below.

Contains default values for core Hadoop properties.
huser@hadoop-lab$ vi /usr/local/hadoop/conf/core-site.xml

huser@hadoop-lab:~$ mkdir /usr/local/hadoop/tmp

Contains configuration information for MapReduce properties.
huser@hadoop-lab$ vi /usr/local/hadoop/conf/mapred-site.xml

Contains server side configuration for Hadoop Distributed File System
huser@hadoop-lab$ vi /usr/local/hadoop/conf/hdfs-site.xml

huser@hadoop-lab:~$ sudo mkdir -p /hdfs/name
huser@hadoop-lab:~$ sudo mkdir /hdfs/data
huser@hadoop-lab:~$ sudo chown -R huser:hadoop /hdfs

Before we start adding files to HDFS we need to format it. Type 'Y' for the prompt. Y/N prompt is case-sensitive.
huser@hadoop-lab:~$ hadoop namenode -format

After the namenode has been formatted, it is time to launch hadoop.

As we all know that hadoop is written in java language, hence we can JPS (Java Process Status) tool to check the processes that are running in jvm.
huser@hadoop-lab:~$ jps
The output should look something like shown below in screenshot.

We can also have a look at the web interfaces of HDFS and MapReduce.
HDFS : http://localhost:50070

MapReduce : http://localhost:50030

That's it. I hope you enjoyed learning building up a Single-Node Hadoop Cluster on Ubuntu 14.04. If you have any suggestions, questions or any comments to make, please leave a comment.

Follow the below links to create:
Multi-Node Hadoop Cluster on Ubuntu 14.04