主要讲解在虚拟机中安装Hadoop的方法
在本示例中,使用VBOX模拟3台Centos系统,分别是1台master和2台slaver。IP分别是:
每台均安装下面章节安装。
(略)
从阿里镜像网站下载镜像文件
(略)
ip addr
检查网卡信息,显示有两个网卡eno0s3
和enp0s8
,如下图:cd /etc/sysconfig/network-scripts/
修改对应ifcfg-enp0s3
和ifcfg-enp0s8
文件,如下图:service network restart
重启网络后,在xshell中配置静态IP即可访问,如下图:访问阿里镜像网站,点击centos右侧帮助按钮
# 备份
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
# 下载新的CentOS-Base.repo 到/etc/yum.repos.d/
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
# 运行yum makecache生成缓存
安装一些基础工具
yum install -y lrzsz
命令 | 备注 |
---|---|
service iptable status | 查看防火墙状态 |
service iptables stop | 临时关闭防火墙 |
chkconfig iptables off | 永久关闭防火墙 |
命令 | 备注 |
---|---|
firewall-cmd --state | 查看防火墙状态(关闭后显示notrunning,开启后显示running) |
systemctl list-unit-files|grep firewalld.service 或 systemctl status firewalld.service | 从centos7开始使用systemctl来管理服务和程序,包括了service和chkconfig |
systemctl stop firewalld.service | 停止firewall |
systemctl disable firewalld.service | 禁止firewall开机启动 |
systemctl start firewalld.service | 启动一个服务 |
systemctl stop firewalld.service | 关闭一个服务 |
systemctl restart firewalld.service | 重启一个服务 |
systemctl status firewalld.service | 显示一个服务的状态 |
systemctl enable firewalld.service | 在开机时启用一个服务 |
systemctl disable firewalld.service | 在开机时禁用一个服务 |
systemctl is-enabled firewalld.service | 查看服务是否开机启动 |
systemctl list-unit-files|grep enabled | 查看已启动的服务列表 |
临时修改 hostname | |
---|---|
hostname bigdata01 | 这种修改方式,系统重启后就会失效 |
永久修改 hostname | |
---|---|
想永久修改,应该修改配置文件 | 写入以下内容: |
vi /etc/hosts | 修改 127.0.0.1这行中的 localhost.localdomain为 bigdata01 |
vi /etc/hostname | 删除文件中的所有文字,在第一行添加slave3 |
重启并验证 | #>reboot -f |
将所有主机的vi /etc/hosts
都配置如下:
192.168.56.3 bigdata01
192.168.56.4 bigdata02
192.168.56.5 bigdata03
vi /etc/sysconfig/selinux
修改为
注意: Hadoop机器上的JDK, 最好是Oracle的JavaJDK, 不然会有一些问题,比如可能没有 JPS 命令。如果安装了其他版本的JDK, 卸载掉。
删除自带 java
[root@server media]# rpm -qa |grep java
tzdata-java-2014g-1.el6.noarch
java-1.7.0-openjdk-1.7.0.65-2.5.1.2.el6_5.x86_64
java-1.6.0-openjdk-1.6.0.0-11.1.13.4.el6.x86_64
[root@server media]# rpm -e java-1.7.0-openjdk-1.7.0.65-2.5.1.2.el6_5.x86_64
[root@server media]# rpm -e java-1.6.0-openjdk-1.6.0.0-11.1.13.4.el6.x86_64 --nodeps
[root@server media]# rpm -qa|grep java
tzdata-java-2014g-1.el6.noarch
上传tar.gz包只安装目录
# 将jdk-7u67-linux-x64.tar.gz解压到/opt/modules目录下
[root@bigdata-senior01 /]# tar -zxvf jdk-7u67-linux-x64.tar.gz -C /opt/modules
添加环境变量
# 设置JDK的环境变量JAVA_HOME。需要修改配置文件/etc/profile,追加
export JAVA_HOME="/opt/modules/jdk1.7.0_67"
export PATH=$JAVA_HOME/bin:$PATH
# 修改完毕后,执行 source /etc/profile
rpm -ivh jdk-8u261-linux-x64.rpm
添加环境变量
vi /etc/profile
export JAVA\_HOME="/usr/java/jdk1.7.0\_80"
export PATH=`$JAVA_HOME/bin:$`PATH
# 修改完毕后,执行 source /etc/profile
yum -y install expect
[root@bigdata01 ~]# which expect
/usr/bin/expect
bigdata01
bigdata02
bigdata03
# 这里使用hostname,是因为之前的步骤在主机已经配置了hosts文件。也可以直接使用IP
# cat sshkey.sh
#!/bin/bash
# 这里替换为真实密码
password=mage
#2.keys
ssh-keygen -t rsa -P "" -f /root/.ssh/id_rsa &> /dev/null
#3.install expect
rpm -q expect &> /dev/null || yum install expect -y &> /dev/null
#4.send key
while read ip;do
expect << EOF
set timeout 20
spawn ssh-copy-id -i /root/.ssh/id_rsa.pub root@$ip
expect {
"yes/no" { send "yes\n";exp_continue }
"password" { send "$password\n" }
}
expect eof
EOF
done < /tmp/hadoop_hosts.txt
(略)
bigdata01 ntpd服务器,用于与外部公共ntpd同步标准时间
bigdata02 ntpd客户端,用于与ntpd同步时间
bigdata03 ntpd客户端,用于与ntpd同步时间
rpm -q ntp
yum -y install ntp
systemctl enable ntpd
systemctl start ntpd
ntpdate -u cn.pool.ntp.org
,同步服务器vi /etc/ntp.conf
文件,红色字体是修改的内容# For more information about this file, see the man pages
# ntp.conf(5), ntp_acc(5), ntp_auth(5), ntp_clock(5), ntp_misc(5), ntp_mon(5).
driftfile /var/lib/ntp/drift
# Permit time synchronization with our time source, but do not
# permit the source to query or modify the service on this system.
restrict default nomodify notrap nopeer noquery
# Permit all access over the loopback interface. This could
# be tightened as well, but to do so would effect some of
# the administrative functions.
restrict 127.0.0.1
restrict ::1
# Hosts on local network are less restricted.
#restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap
restrict 192.168.56.0 mask 255.255.255.0 nomodify notrap
#允许内网其他机器同步时间,如果不添加该约束默认允许所有IP访问本机同步服务
# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
#server 0.centos.pool.ntp.org iburst
#server 101.201.72.121 # 中国国家授时中心
#server 133.100.11.8 #日本[福冈大学]
#server 3.cn.pool.ntp.org
#server 1.asia.pool.ntp.org
#server 3.asia.pool.ntp.org
#配置和上游标准时间同步,时间服务器的域名,如不需要连接互联网,就将他们注释掉
#broadcast 192.168.1.255 autokey # broadcast server
#broadcastclient # broadcast client
#broadcast 224.0.1.1 autokey # multicast server
#multicastclient 224.0.1.1 # multicast client
#manycastserver 239.255.254.254 # manycast server
#manycastclient 239.255.254.254 autokey # manycast client
#配置允许上游时间服务器主动修改本机(内网ntp Server)的时间
#restrict 101.201.72.121 nomodify notrap noquery
#restrict 133.100.11.8 nomodify notrap noquery
#restrict 3.cn.pool.ntp.org nomodify notrap noquery
#restrict 1.asia.pool.ntp.org nomodify notrap noquery
#restrict 3.asia.pool.ntp.org nomodify notrap noquery
#确保localhost有足够权限,使用没有任何限制关键词的语法。
#外部时间服务器不可用时,以本地时间作为时间服务。
#注意:这里不能改,必须使用127.127.1.0,否则会导致无法!
#在ntp客户端运行ntpdate serverIP,出现no server suitable for synchronization found的错误。
#在ntp客户端用ntpdate –d serverIP查看,发现有“Server dropped: strata too high”的错误,并且显示“stratum 16”。而正常情况下stratum这个值得范围是“0~15”。
#这是因为NTP server还没有和其自身或者它的server同步上。
#以下的定义是让NTP Server和其自身保持同步,如果在ntp.conf中定义的server都不可用时,将使用local时间作为ntp服务提供给ntp客户端。
server 127.127.1.0 # local clock
fudge 127.127.1.0 stratum 8
# Enable public key cryptography.
#crypto
includefile /etc/ntp/crypto/pw
# Key file containing the keys and key identifiers used when operating
# with symmetric key cryptography.
keys /etc/ntp/keys
# Specify the key identifiers which are trusted.
#trustedkey 4 8 42
# Specify the key identifier to use with the ntpdc utility.
#requestkey 8
# Specify the key identifier to use with the ntpq utility.
#controlkey 8
# Enable writing of statistics records.
#statistics clockstats cryptostats loopstats peerstats
# Disable the monitoring facility to prevent amplification attacks using ntpdc
# monlist command when default restrict does not include the noquery flag. See
# CVE-2013-5211 for more details.
# Note: Monitoring will not be disabled with the limited restriction flag.
disable monitor
systemctl restart ntpd
ntpq -p
ntpstat
lsof -i:123
#配置上游时间服务器为本地的ntpd Server服务器
server 192.168.56.3
# 配置允许上游时间服务器主动修改本机的时间
restrict 192.168.56.3 nomodify notrap noquery
server 127.127.1.0
fudge 127.127.1.0 stratum 10
伪分布式模式,只需要使用一台主机,以bigdata01为例。其即作为master也作为slaver使用。
tar -zxvf hadoop-1.2.1-bin.tar.gz
ln -s hadoop-1.2.1 hadoop
如下图配置/etc/profile
,并source
生效
其中export HADOOP_HOME_WARN_SUPPRESS=1
配置后是为了解决Hadoop1.2.1出现==Warning: $HADOOP_HOME is deprecated.==的问题
# The java implementation to use. Required.
# export JAVA_HOME=/usr/lib/j2sdk1.5-sun
export JAVA_HOME=/usr/java/jdk1.7.0_80
xml version="1.0"/>
xml-stylesheet type="text/xsl" href="configuration.xsl"/>
<configuration>
<property>
<name>fs.default.namename>
<value>hdfs://bigdata01:9000value>
<description>用于dfs命令模块中指定默认的文件系统协议description>
property>
<property>
<name>hadoop.tmp.dirname>
<value>/tmp/hadoop1xlocalmvalue>
<description>临时目录,其他临时目录的父目录description>
property>
configuration>
xml version="1.0"/>
xml-stylesheet type="text/xsl" href="configuration.xsl"/>
<configuration>
<property>
<name>dfs.name.dirname>
<value>/hadoop/1x/localm/hdfs/namevalue>
<description>name node的元数据,以,号隔开,hdfs会把元数据冗余复制到这些目录,一般这些目录是不同的块设备,不存在的目录会被忽略掉description>
property>
<property>
<name>dfs.data.dirname>
<value>/hadoop/1x/localm/hdfs/datavalue>
<description>data node的数据目录,以,号隔开,hdfs会把数据存在这些目录下,一般这些目录是不同的块设备,不存在的目录会被忽略掉description>
property>
<property>
<name>dfs.replicationname>
<value>1value>
<description>文件复制的副本数,如果创建时不指定这个参数,就使用这个默认值作为复制的副本数description>
property>
configuration>
xml version="1.0"/>
xml-stylesheet type="text/xsl" href="configuration.xsl"/>
<configuration>
<property>
<name>mapred.job.trackername>
<value>bigdata01:9001value>
<description>job tracker交互端口description>
property>
configuration>
bigdata01
bigdata01
hadoop namenode –format
# 启动所有的Hadoop守护。包括namenode, datanode, jobtracker, tasktrack
start-all.sh
# 停止所有的Hadoop
stop-all.sh
# 启动Map/Reduce守护。包括Jobtracker和Tasktrack
start-mapred.sh
# 停止Map/Reduce守护
stop-mapred.sh
# 启动Hadoop DFS守护Namenode和Datanode
start-dfs.sh
# 停止DFS守护
stop-dfs.sh
http://192.168.56.3:50070/dfshealth.jsp
http://192.168.56.3:50030/jobtracker.jsp
安装部署完全分布式集群,现有3台机器,分别规划为
# The java implementation to use. Required.
# export JAVA_HOME=/usr/lib/j2sdk1.5-sun
export JAVA_HOME=/usr/java/jdk1.7.0_80
xml version="1.0"/>
xml-stylesheet type="text/xsl" href="configuration.xsl"/>
<configuration>
<property>
<name>fs.default.namename>
<value>hdfs://bigdata01:9000value>
<description>用于dfs命令模块中指定默认的文件系统协议description>
property>
<property>
<name>hadoop.tmp.dirname>
<value>/tmp/hadoop1xclustermvalue>
<description>临时目录,其他临时目录的父目录description>
property>
configuration>
xml version="1.0"/>
xml-stylesheet type="text/xsl" href="configuration.xsl"/>
<configuration>
<property>
<name>dfs.name.dirname>
<value>/hadoop/1x/clusterm/hdfs/namevalue>
<description>name node的元数据,以,号隔开,hdfs会把元数据冗余复制到这些目录,一般这些目录是不同的块设备,不存在的目录会被忽略掉description>
property>
<property>
<name>dfs.data.dirname>
<value>/hadoop/1x/clusterm/hdfs/datavalue>
<description>data node的数据目录,以,号隔开,hdfs会把数据存在这些目录下,一般这些目录是不同的块设备,不存在的目录会被忽略掉description>
property>
<property>
<name>dfs.replicationname>
<value>2value>
<description>文件复制的副本数,如果创建时不指定这个参数,就使用这个默认值作为复制的副本数description>
property>
configuration>
xml version="1.0"/>
xml-stylesheet type="text/xsl" href="configuration.xsl"/>
<configuration>
<property>
<name>mapred.job.trackername>
<value>bigdata01:9001value>
<description>job tracker交互端口description>
property>
configuration>
bigdata01
bigdata02
bigdata03
cluster_copy_all_nodes 用来集群间同步拷贝文件:
#!/bin/bash
SELF=`hostname`
if [ -z "$NODE_LIST" ]; then
echo
echo Error: NODE_LIST environment variable must be set in .bash_profile
exit 1
fi
for i in $NODE_LIST; do
if [ ! $i = $SELF ]; then
if [ $1 = "-r" ]; then
scp -oStrictHostKeyChecking=no -r $2 $i:$3
else
scp -oStrictHostKeyChecking=no $1 $i:$2
fi
fi
done
wait
cluster_run_all_nodes 用来集群间同步运行命令:
#!/bin/bash
if [ -z "$NODE_LIST" ]; then
echo
echo Error: NODE_LIST environment variable must be set in .bash_profile
exit 1
fi
if [[ $1 = '--background' ]]; then
shift
for i in $NODE_LIST; do
ssh -oStrictHostKeyChecking=no -n $i "$@" &
done
else
for i in $NODE_LIST; do
ssh -oStrictHostKeyChecking=no $i "$@"
done
fi
wait
授予两个脚本的可执行权限:
chmod +x /usr/local/bin/cluster_*
tar -zcvf hadoop-1.2.1.tar.gz hadoop-1.2.1
cluster_copy_all_nodes.sh "bigdata02 bigdata03" /opt/hadoop-1.2.1.tar.gz /opt/hadoop-1.2.1.tar.gz
cluster_run_all_nodes.sh "bigdata02 bigdata03" "tar -zxvf /opt/hadoop-1.2.1.tar.gz"
cluster_run_all_nodes.sh "bigdata02 bigdata03" "cd /opt;ln -s hadoop-1.2.1 hadoop"
hadoop namenode -format
start-all.sh
在各台机器上执行jps
(略)
BigData01 | BigData02 | BigData03 |
---|---|---|
NameNode | ResourceManager | |
DataNode | DataNode | DataNode |
NodeManager | NodeManager | NodeManager |
HistoryServer | SecondaryNameNode |
mkdir -p /opt/modules/app/
tar -zxf /root/hadoop-2.6.5.tar.gz -C /opt/modules/app/
/etc/profile
JAVA_HOME=/usr/java/jdk1.7.0_80
CLASSPATH=.:$JAVA_HOME/lib.tools.jar
export HADOOP_HOME=/opt/modules/app/hadoop-2.6.5
export HADOOP_MAPRED_HOME=$HADOOP_HOME
export HADOOP_COMMON_HOME=$HADOOP_HOME
export HADOOP_HDFS_HOME=$HADOOP_HOME
export YARN_HOME=$HADOOP_HOME
export HADOOP_COMMON_LIB_NATIVE_DIR=$HADOOP_HOME/lib/native
export HADOOP_OPTS="-Djava.library.path=$HADOOP_HOME/lib:$HADOOP_COMMON_LIB_NATIVE_DIR"
export PATH=$PATH:$HADOOP_HOME/sbin:$HADOOP_HOME/bin:$JAVA_HOME/bin
# 进入Hadoop目录
cd /opt/modules/app/hadoop-2.6.5/etc/hadoop
# 修改 hadoop-env.sh、 mapred-env.sh、 yarn-env.sh 文件中的JDK路径,必须写绝对路径,不能写环境变量
# The java implementation to use.
export JAVA_HOME="/usr/java/jdk1.7.0_80"
# 最后加上
export HADOOP_OPTS="-Djava.library.path=$HADOOP_HOME/lib:$HADOOP_COMMON_LIB_NATIVE_DIR"
core-site.xml
<configuration>
<property>
<name>fs.defaultFSname>
<value>hdfs://BigData01:8020value>
<description>NameNode的地址description>
property>
<property>
<name>hadoop.tmp.dirname>
<value>/opt/modules/app/hadoop-2.6.5/data/tmpvalue>
<description>hadoop临时目录的地址,默认情况下,NameNode和DataNode的数据文件都会存在这个目录下的对应子目录下.应该保证此目录是存在的,如果不存在,先创建description>
property>
<property>
<name>dfs.namenode.name.dirname>
<value>file://${hadoop.tmp.dir}/dfs/namevalue>
<description>NameNode的目录,该目录必须手动创建,不然启动报错description>
property>
<property>
<name>dfs.datanode.data.dirname>
<value>file://${hadoop.tmp.dir}/dfs/datavalue>
<description>DataNode的目录,该目录可以由程序自动创建description>
property>
configuration>
hdfs-site.xml
<configuration>
<property>
<name>dfs.namenode.secondary.http-addressname>
<value>BigData03:50090value>
<description>secondaryNameNode的http访问地址和端口号description>
property>
configuration>
slaves:slaves文件是指定HDFS上有哪些DataNode节点
BigData01
BigData02
BigData03
yarn-site.xml
<configuration>
<property>
<name>yarn.nodemanager.aux-servicesname>
<value>mapreduce_shufflevalue>
<description>附属服务AuxiliaryService是由YARN中节点管理器NM启动的通用服务description>
property>
<property>
<name>yarn.resourcemanager.hostnamename>
<value>BigData02value>
<description>resourcemanager服务器description>
property>
<property>
<name>yarn.log-aggregation-enablename>
<value>truevalue>
<description>配置是否启用日志聚集功能description>
property>
<property>
<name>yarn.log-aggregation.retain-secondsname>
<value>106800value>
<description>配置聚集的日志在HDFS上最多保存多长时间description>
property>
configuration>
mapred-site.xml
<configuration>
<property>
<name>mapreduce.framework.namename>
<value>yarnvalue>
<description>设置mapreduce任务运行在yarndescription>
property>
<property>
<name>mapreduce.jobhistory.addressname>
<value>BigData01:10020value>
<description>设置mapreduce的历史服务器description>
property>
<property>
<name>mapreduce.jobhistory.webapp.addressname>
<value>BigData01:19888value>
<description>设置历史服务器的web页面地址和端口号description>
property>
configuration>
mkdir -p /opt/modules/app
scp -r /opt/modules/app/hadoop-2.6.5/ BigData02:/opt/modules/app
scp -r /opt/modules/app/hadoop-2.6.5/ BigData03:/opt/modules/app
在NameNode机器上执行格式化:
/opt/modules/app/hadoop-2.6.5/bin/hdfs namenode -format
启动 HDFS 在BigData01主机上执行:start-dfs.sh
启动YARN 在BigData01主机执行 start-yarn.sh
启动ResourceManager 在BigData02主机执行 yarn-daemon.sh start resourcemanager
启动日志服务器 在BigData03主机执行 mr-jobhistory-daemon.sh start historyserver
检查进程
直接启动,无需执行上面多个命令 start-all.sh
查看 HDFS Web 页面
http://192.168.137.10:50070/
查看 YARN Web 页面
http://192.168.137.12:8088/cluster
此处未做测试,可以参考文章:最新Hadoop3.2.1完全分布式集群安装教程
通过Docker可以更方便的安装Hadoop集群,而无需配置如此多的机器。具体安装方法,请参见:Docker章节。
2018-08-09 11:06:46,081 INFO org.apache.hadoop.service.AbstractService: Service ResourceManager failed in state STARTED; cause: org.apache.hadoop.yarn.exceptions.YarnRuntimeException: java.net.BindException: Problem binding to [BigData02:8031] java.net.BindException: 无法指定被请求的地址; For more details see: http://wiki.apache.org/hadoop/BindException
org.apache.hadoop.yarn.exceptions.YarnRuntimeException: java.net.BindException: Problem binding to [BigData02:8031] java.net.BindException: 无法指定被请求的地址; For more details see: http://wiki.apache.org/hadoop/BindException
at org.apache.hadoop.yarn.factories.impl.pb.RpcServerFactoryPBImpl.getServer(RpcServerFactoryPBImpl.java:139)
at org.apache.hadoop.yarn.ipc.HadoopYarnProtoRPC.getServer(HadoopYarnProtoRPC.java:65)
at org.apache.hadoop.yarn.ipc.YarnRPC.getServer(YarnRPC.java:54)
at org.apache.hadoop.yarn.server.resourcemanager.ResourceTrackerService.serviceStart(ResourceTrackerService.java:162)
at org.apache.hadoop.service.AbstractService.start(AbstractService.java:193)
at org.apache.hadoop.service.CompositeService.serviceStart(CompositeService.java:120)
yarn-site.xml 按照以下配置也不行
<configuration>
<property>
<name>yarn.nodemanager.aux-servicesname>
<value>mapreduce_shufflevalue>
<description>附属服务AuxiliaryService是由YARN中节点管理器NM启动的通用服务description>
property>
<property>
<name>yarn.nodemanager.aux-services.mapreduce.shuffle.classname>
<value>org.apache.hadoop.mapred.ShuffleHandlervalue>
property>
<property>
<name>yarn.resourcemanager.addressname>
<value>192.168.137.12:8032value>
property>
<property>
<name>yarn.resourcemanager.scheduler.addressname>
<value>192.168.137.12:8030value>
property>
<property>
<name>yarn.resourcemanager.resource-tracker.addressname>
<value>192.168.137.12:8031value>
property>
<property>
<name>yarn.resourcemanager.admin.addressname>
<value>192.168.137.12:8033value>
property>
<property>
<name>yarn.resourcemanager.webapp.addressname>
<value>192.168.137.12:8088value>
property>
<property>
<name>yarn.log-aggregation-enablename>
<value>truevalue>
<description>配置是否启用日志聚集功能description>
property>
<property>
<name>yarn.log-aggregation.retain-secondsname>
<value>106800value>
<description>配置聚集的日志在HDFS上最多保存多长时间description>
property>
configuration>
原因暂未找到
[root@bigdata01 ~]# hadoop fs -ls /
18/09/13 10:45:34 WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
Found 1 items
drwxr-xr-x - root supergroup 0 2018-09-13 10:36 /input
[root@bigdata01 ~]#
问题处理参见:
util.NativeCodeLoader: Unable to load native-hadoop library for your platform