环境搭建-Hadoop集群搭建
环境搭建-Hadoop集群搭建
写在前面,前面我们快速搭建好了centos的集群环境,接下来,我们就来开始hadoop的集群的搭建工作
实验环境
Hadoop版本:CDH 5.7.0
这里,我想说一下,我们我没有选择官方版本,是因为,cdh版本已经解决好了各个组件之间的依赖。因为,后面,我们还会使用更多hadoop家族里面的组件。cdh版本目前也是国内成产环境中使用最多的一个版本。
环境所需要的安装包我可以在我的百度云分享中获取到:
链接:http://pan.baidu.com/s/1c24gbUK 密码:8h1r
在开始正式安装hadoop之前,我们得配置集群SSH免密码登陆
配置/etc/hosts文件
添加上:
192.168.1.61 hadoop000
192.168.1.62 hadoop001
192.168.1.63 hadoop002
[root@localhost ~]# vim /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.
localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.
localdomain6
192.168.1.61 hadoop000
192.168.1.62 hadoop001
192.168.1.63 hadoop002
每一台机器都要添加上哟!
这样的话,我们的服务器之间就是可以相互ping主机名ping通的了
开始配置集群SSH免密码登陆
在三台机器上配置对本机的ssh免密码登录
ssh-keygen -t rsa
生成本机的公钥,过程中不断敲回车即可
[root@localhost app]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
0b:9a:fb:86:9c:97:b8:6f:2a:d9:2e:6e:a2:3b:49:95 root@localhost.localdomain
The key's randomart image is:
+--[ RSA 2048]----+
| |
| |
| . |
| E |
| . . S |
| . o . . |
|.. +o+ .. |
|+ = *.= |
|+*.+=Oo |
+-----------------+
[root@localhost app]#
公钥复制为authorized_keys文件,
[root@localhost ~]# cd .ssh/
[root@localhost .ssh]# ls
id_rsa id_rsa.pub
[root@localhost .ssh]# touch authorized_keys
[root@localhost .ssh]# cp id_rsa.pub authorized_keys
cp: overwrite `authorized_keys'? yes
[root@localhost .ssh]# ls
authorized_keys id_rsa id_rsa.pub
[root@localhost .ssh]# cat authorized_keys
ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAxwB29J6IeubQq986jvCOss7luE0Kq1l5ayguovC7AzXtxVVzc8Tls0OmZ3UFddGI9YGPQSHn4Vlgh5LltmIlCWEz01s2sHXaIMA3hx6dMK9jYeOY1qJPpKMb+TyM5p2qkfFUj/uFYfW/jTLohQlXZpp5pGEH9bSsh+sS5EmLDRPYFFH89NU/fhUBmNrbY3QqWlBcM+dmxdHoAK/sVeMxurYolQ3Ws8DzGo0IhbOoMTkxEACiTkf72Nw+2ZtF1Bkv1gYRa6fEqm2GvalOjvDkgFhN6DiT12JtTOQ3B0ZR1o/94koQtRzhU0IMcrLMfFcGKqUisLS6mguO+0sCKqDXlQ== root@localhost.localdomain
[root@localhost .ssh]#
#这里的,每一步都是很重要的
此时使用ssh连接本机就不需要输入密码了(第一次例外)
[root@localhost .ssh]# ssh hadoop000
The authenticity of host 'hadoop000 (192.168.1.61)' can't be established.
RSA key fingerprint is d7:1b:23:6b:0f:80:26:cd:da:9f:89:75:f6:4d:50:4c.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'hadoop000,192.168.1.61' (RSA) to the list of known hosts.
Last login: Thu Nov 23 15:01:36 2017 from 192.168.1.9
[root@localhost ~]# logout
Connection to hadoop000 closed.
[root@localhost .ssh]# ssh hadoop000
Last login: Thu Nov 23 15:32:58 2017 from hadoop000
[root@localhost ~]#
其他两个服务器执行同样上述操作
配置hadoop000节点ssh免密码登录其余节点
ssh-copy-id -i hadoop001
[root@localhost .ssh]# ssh-copy-id -i hadoop001
The authenticity of host 'hadoop001 (192.168.1.62)' can't be established.
RSA key fingerprint is d3:ca:00:af:e5:40:0a:a6:9b:0d:a6:42:bc:22:48:66.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'hadoop001,192.168.1.62' (RSA) to the list of known hosts.
root@hadoop001's password:
Now try logging into the machine, with "ssh 'hadoop001'", and check in:
.ssh/authorized_keys
to make sure we haven't added extra keys that you weren't expecting.
[root@localhost .ssh]#
测试:
[root@localhost .ssh]# ssh hadoop001
Last login: Thu Nov 23 14:25:48 2017 from 192.168.1.9
[root@localhost ~]# logout
Connection to hadoop001 closed.
[root@localhost .ssh]#
#免密码登录成功
好啦,前面讲了这么多,下面正式开始搭建Hadoop环境。
安装 Hadoop
1.我们首先得先下载软件,是把,这里我们直接从本地的软件包里面上传上去就好,当然,从官网下载也是可以的。
2.接着,就是解压到app目录下面去,修改文件名为hadoop
[root@localhost softwares]# ls
hadoop-2.6.0-cdh5.7.0.tar.gz jdk-8u144-linux-x64.tar.gz
[root@localhost softwares]# tar -zxvf hadoop-2.6.0-cdh5.7.0.tar.gz -C ../app/
3.设置系统环境变量
[root@localhost hadoop]# vim ~/.bash_profile
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
export JAVA_HOME=/root/app/jdk1.8.0_144
export PATH=$JAVA_HOME/bin:$PATH
export HADOOP_HOME=/root/app/hadoop
export PATH=$HADOOP_HOME/bin:$PATH
export PATH=$HADOOP_HOME/sbin:$PATH
检验:
[root@localhost hadoop]# source ~/.bash_profile
[root@localhost hadoop]#
[root@localhost hadoop]# hadoop version
Hadoop 2.6.0-cdh5.7.0
Subversion http://github.com/cloudera/hadoop -r c00978c67b0d3fe9f3b896b5030741bd40bf541a
Compiled by jenkins on 2016-03-23T18:41Z
Compiled with protoc 2.5.0
From source with checksum b2eabfa328e763c88cb14168f9b372
This command was run using /root/app/hadoop/share/hadoop/common/hadoop-common-2.6.0-cdh5.7.0.jar
[root@localhost hadoop]#
#可以看见上面的输出结果,就说明没有问题了
如果你还不放心,可以检查一下yarn
[root@localhost hadoop]# yarn version
Hadoop 2.6.0-cdh5.7.0
Subversion http://github.com/cloudera/hadoop -r c00978c67b0d3fe9f3b896b5030741bd40bf541a
Compiled by jenkins on 2016-03-23T18:41Z
Compiled with protoc 2.5.0
From source with checksum b2eabfa328e763c88cb14168f9b372
This command was run using /root/app/hadoop/share/hadoop/common/hadoop-common-2.6.0-cdh5.7.0.jar
[root@localhost hadoop]#
#可以啦,到位
4.接下来,就是配置文件的书写
配置文件在:$HADOOP_HOME/etc/hadoop下
主要的修改的配置文件包括:core-site.xml、hdfs-site.xml、mapred-site.xml、yarn-site.xml、slaves、hadoop-env.sh等
step4.1:hadoop-env.sh
配置hadoop的jdk环境
[root@localhost hadoop]# vim hadoop-env.sh
修改为# The java implementation to use.
export JAVA_HOME=/root/app/jdk1.8.0_144
保存退出
step4.2:core-site.xml
[root@localhost hadoop]# vim core-site.xml
添加上:
<property>
<name>fs.defaultFS</name>
<value>hdfs://hadoop000:8020</value>
</property>
<property>
<name>hadoop.tmp.dir</name> <value>/root/app/hadoop/data/tmp</value>
</property>
上面的保存退出就好
step4.3:hdfs-site.xml
添加上:
<property>
<name>dfs.name.dir</name>
<value>/root/app/hadoop/data/namenode</value>
</property>
<property>
<name>dfs.data.dir</name>
<value>/root/app/hadoop/data/datanode</value>
</property>
<property>
<name>dfs.tmp.dir</name>
<value>/root/app/hadoop/data/dfstmp</value>
</property>
<property>
<name>dfs.replication</name>
<value>3</value>
</property>
保存退出
step4.4:mapred-site.xml
[root@localhost hadoop]# cp mapred-site.xml.template mapred-site.xml
[root@localhost hadoop]#
[root@localhost hadoop]# vim mapred-site.xml
进去添加:
<property>
<name>mapreduce.framework.name</name>
<value>yarn</value>
</property>
保存退出
step4.5:yarn-site.xml
进入添加:
<property>
<name>yarn.resourcemanager.hostname</name>
<value>sparkproject1</value>
</property>
<property>
<name>yarn.nodemanager.aux-services</name>
<value>mapreduce_shuffle</value>
</property>
保存退出
step4.6:slaves
配置我们的子节点:
[root@localhost hadoop]# vim slaves
hadoop000
hadoop001
hadoop002
说明:hadoop000在这里既是主节点,又是从节点
在另外两台服务器上搭建Hadoop环境
这里我们可以直接使用scp命令,将hadoop000的配置全部拷贝到另外的两台机器上去
[root@localhost ~]# scp -r ./* root@hadoop001:/root
[root@localhost ~]# scp -r ./* root@hadoop002:/root
#会比较慢,稍等一下
[root@localhost ~]# scp -r ~/.bash_profile root@hadoop001:~/
.bash_profile 100% 359 0.4KB/s 00:00
[root@localhost ~]# scp -r ~/.bash_profile root@hadoop002:~/
.bash_profile 100% 359 0.4KB/s 00:00
[root@localhost ~]#
在hadoop001和hadoop002分别source一下我们发送过去的配合文件
[root@localhost ~]# source ~/.bash_profile
[root@localhost ~]#
接下来,我们就可以开始我们的集群了
启动Hdfs集群
Hdfs是hadoop的分布式文件系统,简单来说,就是存放数据的。是海量数据!
1.格式化namenode
[root@localhost ~]# hdfs namenode -format
2.启动集群
使用jps命令查看一下:
*可以看见hadoop主节点上开启了一个namenode和datanode
另外两个节点的datanode都也启起了*
这时,我们也可以在windows机器上通过浏览器来查看集群的情况:
在浏览器中输入hadoop000:50070即可:(因为我在本机c盘的hosts文件中添加了主机名与ip地址的对应关系,你也可以直接使用ip+端口的形式)
点开上面:
就来到集群的根目录,这时,上面什么都没有,我们传一个东西上去
本地上传一个test1.txt的文件
[root@localhost ~]# cat test1.txt
Hello Hadoop
Hello BigData
Hello Tomorrow
[root@localhost ~]# hdfs dfs -put test1.txt /
17/11/23 17:16:29 WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
[root@localhost ~]#
刷新页面,文件就可以被看见啦;
要想查看文件的内容,可以把文件下在下来,关于hdfs就简单介绍到这儿,下面开始介绍yarn….
启动Yarn集群
在hadoop集群中,yarn扮演的是一个集群资源的管理与调度这么一个角色
输入命令:
[root@localhost hadoop]# start-yarn.sh
starting yarn daemons
starting resourcemanager, logging to /root/app/hadoop/logs/yarn-root-resourcemanager-localhost.localdomain.out
hadoop002: starting nodemanager, logging to /root/app/hadoop/logs/yarn-root-nodemanager-localhost.localdomain.out
hadoop001: starting nodemanager, logging to /root/app/hadoop/logs/yarn-root-nodemanager-localhost.localdomain.out
hadoop000: starting nodemanager, logging to /root/app/hadoop/logs/yarn-root-nodemanager-localhost.localdomain.out
[root@localhost hadoop]#
同样,jps查看进程:
可以看见,主节点多了两个进程出来,resourcemanager 和nodemanager,其余从节点多了nodemanger,,这个就是管理各个节点的资源的进程,都出现了,就说明启动成功,yarn也提供了web端,端口是8088,
在浏览器输入hadoop000:8088即可:
可以重点关注一下我上面的圈出的内容
我们可以开始一个简单的作业,测试一下
[root@localhost hadoop]# pwd
/root/app/hadoop/share/hadoop
[root@localhost hadoop]# hadoop jar mapreduce/hadoop-mapreduce-examples-2.6.0-cdh5.7.0.jar pi 2 3
上面的命令执行之后,计算pi的值,然后开始了一个mapreduce作业
过一会,就会输出结果
这个时候,你如果查看集群 ,会看见一个作业正在被执行:
输出结果:
可以看见pi的输出结果为4,虽然误差有点大。但是也是跑成功了一个作业。
到这里,我们的hadoop集群的搭建测试工作就已经完成了。不算是特别麻烦。你可以倒回去,再仔细看看,想想。慢慢的就会了。目前,我也只是刚接触大数据,有什么的错误的地方,可以给我留言。。祝你学习愉快!!