本文旨在使用一个全新安装好的Linux系统从0开始进行Hadoop伪分布式环境的搭建,以达到快速搭建的目的,从而体验Hadoop的魅力所在,为后面的继续学习提供基础环境。
对使用的系统环境作如下说明:
可以看到,这里直接使用root用户,而不是按照大多数的教程创建一个hadoop用户来进行操作,就是为了达到快速搭建Hadoop环境以进行体验的目的。
为了保证后面的操作能够正常完成,请先确认本机是否可以解析到主机名leaf,如果不能,请手动添加解析到/etc/hosts目录中:
[root@leaf ~]# echo "127.0.0.1 leaf" >> /etc/hosts
[root@leaf ~]# ping leaf
PING leaf (127.0.0.1) 56(84) bytes of data.
64 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64 time=0.043 ms
64 bytes from localhost (127.0.0.1): icmp_seq=2 ttl=64 time=0.048 ms
64 bytes from localhost (127.0.0.1): icmp_seq=3 ttl=64 time=0.046 ms
1.rsync软件安装
使用下面命令安装:
[root@leaf ~]# yum install -y rsync
2.ssh安装与免密码登陆配置
(1)ssh安装
使用下面命令安装
[root@leaf ~]# yum install -y openssh-server openssh-clients
(2)ssh免密码登陆配置
因为Hadoop使用ssh协议来管理远程守护进程,所以需要配置免密码登陆。
关闭防火墙和selinux
为了确保能够成功配置,在配置前,先把防火墙和selinux关闭:
# 关闭防火墙
[root@leaf ~]# /etc/init.d/iptables stop
[root@leaf ~]# chkconfig --level 3 iptables off
# 关闭selinux
[root@leaf ~]# setenforce 0
[root@leaf ~]# sed -i s/SELINUX=enforcing/SELINUX=disabled/g /etc/selinux/config
[root@leaf ~]# cat /etc/selinux/config | grep disabled
# disabled - No SELinux policy is loaded.
SELINUX=disabled
生成密钥对
[root@leaf ~]# mkdir .ssh
[root@leaf ~]# ssh-keygen -t dsa -P '' -f .ssh/id_dsa
Generating public/private dsa key pair.
Your identification has been saved in .ssh/id_dsa.
Your public key has been saved in .ssh/id_dsa.pub.
The key fingerprint is:
5b:af:7c:45:f3:ff:dc:50:f5:81:4b:1e:5c:c1:86:90 root@leaf
The key's randomart image is:
+--[ DSA 1024]----+
| .o oo.|
| E..oo |
| =...|
| o = +|
| S . + oo|
| o . ...|
| . ... .|
| . .. oo|
| o. =|
+-----------------+
将公钥添加到本地信任列表
[root@leaf ~]# cat .ssh/id_dsa.pub >> .ssh/authorized_keys
验证
上面三步完成后就完成了免密码登陆的配置,可以使用下面的命令进行验证:
[root@leaf ~]# ssh localhost
The authenticity of host 'localhost (::1)' can't be established.
RSA key fingerprint is d1:0d:ed:eb:e7:d1:2f:02:23:70:ef:11:14:4e:fa:42.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'localhost' (RSA) to the list of known hosts.
Last login: Wed Aug 30 04:28:01 2017 from 10.0.0.1
[root@leaf ~]#
在第一次登陆的时候需要输入yes,之后再登陆时就可以直接登陆了:
[root@leaf ~]# ssh localhost
Last login: Wed Aug 30 04:44:02 2017 from localhost
[root@leaf ~]#
3.jdk安装与配置
(1)jdk下载
这里使用的是jdk1.7版本,可以到下面的网站进行下载:
下载完成后,可以使用winscp上传到/root目录下,如下:
[root@leaf ~]# ls -lh jdk-7u80-linux-x64.tar.gz
-rw-r--r--. 1 root root 147M Aug 29 12:05 jdk-7u80-linux-x64.tar.gz
(2)jdk安装
将jdk解压到/usr/local目录下,并创建软链接:
[root@leaf ~]# cp jdk-7u80-linux-x64.tar.gz /usr/local/
[root@leaf ~]# cd /usr/local/
[root@leaf local]# tar -zxf jdk-7u80-linux-x64.tar.gz
[root@leaf local]# ls -ld jdk1.7.0_80/
drwxr-xr-x. 8 uucp 143 4096 Apr 11 2015 jdk1.7.0_80/
[root@leaf local]# ln -s jdk1.7.0_80/ jdk
[root@leaf local]# ls -ld jdk
lrwxrwxrwx. 1 root root 12 Aug 30 04:56 jdk -> jdk1.7.0_80/
(3)JAVA_HOME环境变量配置
java命令在/usr/local/jdk/bin目录下:
[root@leaf local]# cd jdk/bin/
[root@leaf bin]# ls -lh java
-rwxr-xr-x. 1 uucp 143 7.6K Apr 11 2015 java
配置java环境变量:
[root@leaf bin]# echo 'export JAVA_HOME=/usr/local/jdk/bin' >> /etc/profile
[root@leaf bin]# echo 'export PATH=$PATH:$JAVA_HOME' >> /etc/profile
[root@leaf bin]# source /etc/profile
这样我们就可以在任何一个目录下使用java相关的命令:
[root@leaf ~]# java -version
java version "1.7.0_80"
Java(TM) SE Runtime Environment (build 1.7.0_80-b15)
Java HotSpot(TM) 64-Bit Server VM (build 24.80-b11, mixed mode)
[root@leaf ~]# javac -version
javac 1.7.0_80
4.hadoop安装与配置
(1)hadoop下载
这里使用hadoop 2.6.5版本,可以到下面的网站进行下载:
选择2.6.5的binary进入相应的页面便可以下载,然后使用winscp上传到/root目录下,如下:
[root@leaf ~]# ls -lh hadoop-2.6.5.tar.gz
-rw-r--r--. 1 root root 191M Aug 29 19:09 hadoop-2.6.5.tar.gz
(2)hadoop安装
将hadoop解压到/usr/local目录下,并创建软链接:
[root@leaf ~]# cp hadoop-2.6.5.tar.gz /usr/local
[root@leaf ~]# cd /usr/local
[root@leaf local]# tar -zxf hadoop-2.6.5.tar.gz
[root@leaf local]# ls -ld hadoop-2.6.5
drwxrwxr-x. 9 1000 1000 4096 Oct 3 2016 hadoop-2.6.5
[root@leaf local]# ln -s hadoop-2.6.5 hadoop
[root@leaf local]# ls -ld hadoop
lrwxrwxrwx. 1 root root 12 Aug 30 05:05 hadoop -> hadoop-2.6.5
(3)hadoop环境变量配置
hadoop相关命令在/usr/local/hadoop/bin和/usr/local/hadoop/sbin目录下,如下所示:
[root@leaf local]# cd hadoop/bin/
[root@leaf bin]# ls -lh hadoop
-rwxr-xr-x. 1 1000 1000 5.4K Oct 3 2016 hadoop
配置hadoop环境变量:
1 2
[root@leaf bin]# echo 'export HADOOP_HOME=/usr/local/hadoop/bin:/usr/local/hadoop/sbin' >> /etc/profile [root@leaf bin]# echo 'export PATH=$PATH:$HADOOP_HOME' >> /etc/profile
这样我们就可以在任何一个目录下使用hadoop相关的命令:
[root@leaf ~]# hadoop
Usage: hadoop [--config confdir] COMMAND
where COMMAND is one of:
fs run a generic filesystem user client
version print the version
jar <jar> run a jar file
checknative [-a|-h] check native hadoop and compression libraries availability
distcp <srcurl> <desturl> copy file or directories recursively
archive -archiveName NAME -p <parent path> <src>* <dest> create a hadoop archive
classpath prints the class path needed to get the
credential interact with credential providers
Hadoop jar and the required libraries
daemonlog get/set the log level for each daemon
trace view and modify Hadoop tracing settings
or
CLASSNAME run the class named CLASSNAME
Most commands print help when invoked w/o parameters.
(4)hadoop配置
hadoop的配置文件在/usr/local/hadoop/etc/hadoop目录下:
[root@leaf ~]# cd /usr/local/hadoop/etc/hadoop/
[root@leaf hadoop]# ls
capacity-scheduler.xml hadoop-policy.xml kms-log4j.properties ssl-client.xml.example