最近一直在研究大数据,这里记录一下自己创建Docker集群并且使用ansible搭建Hadoop大数据平台的过程,其中主要借鉴了一下网上的一篇较为全面的文章,不过到时遇到蛮多坑的,所以将那篇文章做了改动,添加或者减少一些步骤,至少能够保证搭建这样的Hadoop集群顺手即来。
基础环境
Centos7 ; Daocloud加速器 ; 163 yum源 ; 一台虚拟机或者云主机 ;
不用杀程序员祭天,但是要有一颗完成到底的心
集群架构
集群包含4台采用Docker创建的容器环境,无需多余的虚拟机。
OS | hostname | IP | |
---|---|---|---|
Centos7 | cluster-master | 172.18.0.2 | |
Centos7 | cluster-slave1 | 172.18.0.3 | |
Centos7 | cluster-slave2 | 172.18.0.4 | |
Centos7 | cluster-slave3 | 172.18.0.5 |
Docker安装
Docker安装可以看我的文章:Docker快速安装以及换镜像源
curl -sSL https://get.daocloud.io/docker | sh
##换源
###这里可以参考这篇文章http://www.jianshu.com/p/34d3b4568059
curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s http://67e93489.m.daocloud.io
systemctl restart docker
拉取Centos7:latest镜像
笔者此时的latest镜像的版本为Centos7.4
docker pull daocloud.io/library/centos:latest
拉取完成之后可以使用这条命令查看images是否下载成功(duoyu)
docker image`
创建容器
按照集群的架构,创建容器时需要设置固定IP,所以先要在docker使用如下命令创建固定IP的子网
docker network create --subnet=172.18.0.0/16 netgroup
docker的子网创建完成之后就可以创建固定IP的容器了
#cluster-master
docker run -d --privileged -ti -v /sys/fs/cgroup:/sys/fs/cgroup --name cluster-master -h cluster-master --net netgroup --ip 172.18.0.2 daocloud.io/library/centos /usr/sbin/init
#cluster-slaves
docker run -d --privileged -ti -v /sys/fs/cgroup:/sys/fs/cgroup --name cluster-slave1 -h cluster-slave1 --net netgroup --ip 172.18.0.3 daocloud.io/library/centos /usr/sbin/init
docker run -d --privileged -ti -v /sys/fs/cgroup:/sys/fs/cgroup --name cluster-slave2 -h cluster-slave2 --net netgroup --ip 172.18.0.4 daocloud.io/library/centos /usr/sbin/init
docker run -d --privileged -ti -v /sys/fs/cgroup:/sys/fs/cgroup --name cluster-slave3 -h cluster-slave3 --net netgroup --ip 172.18.0.5 daocloud.io/library/centos /usr/sbin/init
那位博主还很贴心的解释了在Centos7下使用简单方式创建容器后遇到sshd启动失败的问题,所以需要添加参数--privileged和-v /sys/fs/cgroup:/sys/fs/cgroup,并在启动的时候运行/usr/sbin/init.
在每一容器上部署Openssh
#cluster-master需要修改配置文件(特殊)
#cluster-master
#换源
[root@cluster-master /]# yum -y install wget
[root@cluster-master /]# mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
[root@cluster-master /]# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.163.com/.help/CentOS7-Base-163.repo
[root@cluster-master /]# yum makecache
#安装openssh
[root@cluster-master /]# yum -y install openssh openssh-server openssh-clients
[root@cluster-master /]# systemctl start sshd
####ssh自动接受新的公钥
####master设置ssh登录自动添加kown_hosts
[root@cluster-master /]# vi /etc/ssh/ssh_config
将原来的StrictHostKeyChecking ask
设置StrictHostKeyChecking为no
保存
[root@cluster-master /]# systemctl restart sshd
接着分别对slaves安装openssh
[root@cluster-slave1 /]# yum -y install wget
[root@cluster-slave1 /]# mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
[root@cluster-slave1 /]# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.163.com/.help/CentOS7-Base-163.repo
[root@cluster-slave1 /]# yum makecache
#安装openssh
[root@cluster-slave1 /]#yum -y install openssh openssh-server openssh-clients
[root@cluster-slave1 /]# systemctl start sshd
分别在cluster-slave2,cluster-slave3重复以上步骤,还是贴下代码把
#cluster-slave2
[root@cluster-slave2 /]# yum -y install wget
[root@cluster-slave2 /]# mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
[root@cluster-slave2 /]# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.163.com/.help/CentOS7-Base-163.repo
[root@cluster-slave2 /]# yum makecache
#安装openssh
[root@cluster-slave2 /]#yum -y install openssh openssh-server openssh-clients
[root@cluster-slave2 /]# systemctl start sshd
[root@cluster-slave3 /]# yum -y install wget
[root@cluster-slave3 /]# mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
[root@cluster-slave3 /]# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.163.com/.help/CentOS7-Base-163.repo
[root@cluster-slave3 /]# yum makecache
#安装openssh
[root@cluster-slave3 /]#yum -y install openssh openssh-server openssh-clients
[root@cluster-slave3 /]# systemctl start sshd
有些人可能会问,为什么不用ansible进行操作呢?
这里有必要进行解释一下,ansible是给予sshd服务对若干个slave子主机进行操作的,如果没有openssh的免密码登录,它什么都不是;如果觉得麻烦,可以去docker pull一个包含sshd服务centos-sshd的镜像,或者自己写Dockerfile进行构建顺便换源,这里仅仅介绍一下原生的镜像搭建Hadoop集群.
cluster-master公钥分发
在master机上执行ssh-keygen -t rsa并一路回车,完成之后会生成~/.ssh目录,目录下有id_rsa(私钥文件)和id_rsa.pub(公钥文件),再将id_rsa.pub重定向到文件authorized_keys
ssh-keygen -t rsa
#一路回车
[root@cluster-master /]# cat ~/.ssh/id_rsa.pub > ~/.ssh/authorized_keys
文件生成之后用scp将公钥文件分发到集群slave主机
[root@cluster-master /]# ssh root@cluster-slave1 'mkdir ~/.ssh'
[root@cluster-master /]# scp ~/.ssh/authorized_keys root@cluster-slave1:~/.ssh
[root@cluster-master /]# ssh root@cluster-slave2 'mkdir ~/.ssh'
[root@cluster-master /]# scp ~/.ssh/authorized_keys root@cluster-slave2:~/.ssh
[root@cluster-master /]# ssh root@cluster-slave3 'mkdir ~/.ssh'
[root@cluster-master /]# scp ~/.ssh/authorized_keys root@cluster-slave3:~/.ssh
分发完成之后测试(ssh root@cluster-slave1)是否已经可以免输入密码登录。另外本次实验使用到了root用户,如果在其他用户下使用免密码登录,需要确保用户对~/.ssh/authorized_keys文件有可操作权限。
Ansible安装
为什么不用源码安装呢?
原文博主使用了源码安装,并安装到/opt/目录下,但是没有给出正确的搭建过程,缺少了很多ansible使用必要的文件(只是提到了hosts)比如 inventory文件需要配置[default]hosts文件目录需要设置。
这里使用官方自带安装:
[root@cluster-master /]# yum -y install epel-release
[root@cluster-master /]# yum -y install ansible
#这样的话ansible会被安装到/etc/ansible目录下
此时我们再去编辑ansible的hosts文件
vi /etc/ansible/hosts
hosts文件内容如下
[cluster]
cluster-master
cluster-slave1
cluster-slave2
cluster-slave3
[master]
cluster-master
[slaves]
cluster-slave1
cluster-slave2
cluster-slave3
配置docker容器hosts
由于/etc/hosts文件在容器启动时被重写,直接修改内容在容器重启后不能保留,为了让容器在重启之后获取集群hosts,使用了一种启动容器后重写hosts的方法。
需要在~/.bashrc中追加以下指令
:>/etc/hosts
cat >>/etc/hosts<<EOF
127.0.0.1 localhost
172.18.0.2 cluster-master
172.18.0.3 cluster-slave1
172.18.0.4 cluster-slave2
172.18.0.5 cluster-slave3
EOF
source ~/.bashrc
使配置文件生效,可以看到/etc/hosts文件已经被改为需要的内容
[root@cluster-master ansible]# cat /etc/hosts
127.0.0.1 localhost
172.18.0.2 cluster-master
172.18.0.3 cluster-slave1
172.18.0.4 cluster-slave2
172.18.0.5 cluster-slave3
用ansible分发.bashrc至集群slave下
ansible cluster -m copy -a "src=~/.bashrc dest=~/"
到这里好不容易到了搭建Hadoop这个步骤,感觉博主理解的东西真的是好多啊从docker一直到ansible-playbook,真是不容易啊.
Hadoop
在集群中安装openjdk
使用ansible在在集群中安装openjdk
[root@cluster-master ansible]# ansible cluster -m yum -a "name=java-1.8.0-openjdk,java-1.8.0-openjdk-devel state=latest"
在cluster-master上安装hadoop
将hadoop安装包下载至/opt目录下
这里采用Hadoop 2.x系列最稳定的stable版本2.7.4 你可以选择更新成hadoop-3.x beta版本 或者 hadoop 2.8.2,只要你能驾驭的了
[root@cluster-master opt]# wget http://mirrors.tuna.tsinghua.edu.cn/apache/hadoop/common/hadoop-2.7.4/hadoop-2.7.4.tar.gz
下载完成之后解压安装包,并创建链接文件
[root@cluster-master opt]# tar -xzvf hadoop-2.7.4.tar.gz [root@cluster-master opt]# ln -s hadoop-2.7.4 hadoop
设置java和hadoop环境变量(.bashrc)
# hadoop
export HADOOP_HOME=/opt/hadoop
export PATH=$HADOOP_HOME/bin:$HADOOP_HOME/sbin:$PATH
#java
export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.151-1.b12.el7_4.x86_64/
export PATH=$HADOOP_HOME/bin:$PATH
注意一下,这里的JAVA_HOME的版本java-1.8.0-openjdk-1.8.0.151-1.b12.el7_4.x86_64/需要以实际情况为准,你可以打开你的/usr/lib/jvm/目录进行自行确认,此处为实际设置。
修改hadoop运行所需配置文件
掌握过Hadoop的朋友都知道,Hadoop集群搭建需要修改几个必要的配置文件以及xml参数设置,此处不深究.
[root@cluster-master opt]# cd $HADOOP_HOME/etc/hadoop/
首先我们修改**core-site.xml**这个文件:vi core-site.xml
```bash
<configuration>
<property>
<name>hadoop.tmp.dir</name>
<value>/home/hadoop/tmp</value>
<description>A base for other temporary directories.</description>
</property>
<!-- file system properties -->
<property>
<name>fs.default.name</name>
<value>hdfs://cluster-master:9000</value>
</property>
<property>
<name>fs.trash.interval</name>
<value>4320</value>
</property>
</configuration>
hdfs-site.xml
<property>
<name>dfs.namenode.name.dir</name>
<value>/home/hadoop/tmp/dfs/name</value>
</property>
<property>
<name>dfs.datanode.data.dir</name>
<value>/home/hadoop/data</value>
</property>
<property>
<name>dfs.replication</name>
<value>3</value>
</property>
<property>
<name>dfs.webhdfs.enabled</name>
<value>true</value>
</property>
<property>
<name>dfs.permissions.superusergroup</name>
<value>staff</value>
</property>
<property>
<name>dfs.permissions.enabled</name>
<value>false</value>
</property>
mapred-site.xml
<configuration>
<property>
<name>mapreduce.framework.name</name>
<value>yarn</value>
</property>
<property>
<name>mapred.job.tracker</name>
<value>cluster-master:9001</value>
</property>
<property>
<name>mapreduce.jobtracker.http.address</name>
<value>cluster-master:50030</value>
</property>
<property>
<name>mapreduce.jobhisotry.address</name>
<value>cluster-master:10020</value>
</property>
<property>
<name>mapreduce.jobhistory.webapp.address</name>
<value>cluster-master:19888</value>
</property>
<property>
<name>mapreduce.jobhistory.done-dir</name>
<value>/jobhistory/done</value>
</property>
<property>
<name>mapreduce.intermediate-done-dir</name>
<value>/jobhisotry/done_intermediate</value>
</property>
<property>
<name>mapreduce.job.ubertask.enable</name>
<value>true</value>
</property>
</configuration>
yarn-site.xml
<configuration>
<property>
<name>yarn.resourcemanager.hostname</name>
<value>cluster-master</value>
</property>
<property>
<name>yarn.nodemanager.aux-services</name>
<value>mapreduce_shuffle</value>
</property>
<property>
<name>yarn.nodemanager.aux-services.mapreduce.shuffle.class</name>
<value>org.apache.hadoop.mapred.ShuffleHandler</value>
</property>
<property>
<name>yarn.resourcemanager.address</name>
<value>cluster-master:18040</value>
</property>
<property>
<name>yarn.resourcemanager.scheduler.address</name>
<value>cluster-master:18030</value>
</property>
<property>
<name>yarn.resourcemanager.resource-tracker.address</name>
<value>cluster-master:18025</value>
</property> <property>
<name>yarn.resourcemanager.admin.address</name>
<value>cluster-master:18141</value>
</property>
<property>
<name>yarn.resourcemanager.webapp.address</name>
<value>cluster-master:18088</value>
</property>
<property>
<name>yarn.log-aggregation-enable</name>
<value>true</value>
</property>
<property>
<name>yarn.log-aggregation.retain-seconds</name>
<value>86400</value>
</property>
<property>
<name>yarn.log-aggregation.retain-check-interval-seconds</name>
<value>86400</value>
</property>
<property>
<name>yarn.nodemanager.remote-app-log-dir</name>
<value>/tmp/logs</value>
</property>
<property>
<name>yarn.nodemanager.remote-app-log-dir-suffix</name>
<value>logs</value>
</property>
</configuration>
打包hadoop文件
将hadoop链接文件和hadoop-2.7.4打包成一个文件方便ansible分发到slave主机
[root@cluster-master opt]# tar -cvf hadoop-dis.tar hadoop hadoop-2.7.4
使用ansible-playbook分发.bashrc和hadoop-dis.tar至slave主机
---
- hosts: cluster
tasks:
- name: copy .bashrc to slaves
copy: src=~/.bashrc dest=~/
notify:
- exec source
- name: copy hadoop-dis.tar to slaves
unarchive: src=/opt/hadoop-dis.tar dest=/opt
handlers:
- name: exec source
shell: source ~/.bashrc
不得不提的是,博主这个yaml文件写的真的是不错
将以上yaml保存为hadoop-dis.yaml,并执行
[root@cluster-master opt]# ansible-playbook hadoop-dis.yaml
hadoop-dis.tar会自动解压到slave主机的/opt目录下
格式化namenode
[root@cluster-master opt]# hadoop namenode -format
此时如果你看到return 0 ;Sucessfully formatted等字样说明HDFS集群格式化成功了.如有不成功,可先自行查找错误,实在不行可以在评论区留言.
启动hadoop集群
到这一步已经可以开始hadoop之旅了,启动比较简单,在$HADOOP_HOME/sbin下有几个启动和停止的脚本如下:
[root@cluster-master opt]# cd $HADOOP_HOME/sbin
[root@cluster-master sbin]# ls -l
total 120
-rwxr-xr-x. 1 20415 101 2752 Aug 1 00:35 distribute-exclude.sh
-rwxr-xr-x. 1 20415 101 6452 Aug 1 00:35 hadoop-daemon.sh
-rwxr-xr-x. 1 20415 101 1360 Aug 1 00:35 hadoop-daemons.sh
-rwxr-xr-x. 1 20415 101 1640 Aug 1 00:35 hdfs-config.cmd
-rwxr-xr-x. 1 20415 101 1427 Aug 1 00:35 hdfs-config.sh
-rwxr-xr-x. 1 20415 101 2291 Aug 1 00:35 httpfs.sh
-rwxr-xr-x. 1 20415 101 3128 Aug 1 00:35 kms.sh
-rwxr-xr-x. 1 20415 101 4080 Aug 1 00:35 mr-jobhistory-daemon.sh
-rwxr-xr-x. 1 20415 101 1648 Aug 1 00:35 refresh-namenodes.sh
-rwxr-xr-x. 1 20415 101 2145 Aug 1 00:35 slaves.sh
-rwxr-xr-x. 1 20415 101 1779 Aug 1 00:35 start-all.cmd
-rwxr-xr-x. 1 20415 101 1471 Aug 1 00:35 start-all.sh
-rwxr-xr-x. 1 20415 101 1128 Aug 1 00:35 start-balancer.sh
-rwxr-xr-x. 1 20415 101 1401 Aug 1 00:35 start-dfs.cmd
-rwxr-xr-x. 1 20415 101 3734 Aug 1 00:35 start-dfs.sh
-rwxr-xr-x. 1 20415 101 1357 Aug 1 00:35 start-secure-dns.sh
-rwxr-xr-x. 1 20415 101 1571 Aug 1 00:35 start-yarn.cmd
-rwxr-xr-x. 1 20415 101 1347 Aug 1 00:35 start-yarn.sh
-rwxr-xr-x. 1 20415 101 1770 Aug 1 00:35 stop-all.cmd
-rwxr-xr-x. 1 20415 101 1462 Aug 1 00:35 stop-all.sh
-rwxr-xr-x. 1 20415 101 1179 Aug 1 00:35 stop-balancer.sh
-rwxr-xr-x. 1 20415 101 1455 Aug 1 00:35 stop-dfs.cmd
-rwxr-xr-x. 1 20415 101 3206 Aug 1 00:35 stop-dfs.sh
-rwxr-xr-x. 1 20415 101 1340 Aug 1 00:35 stop-secure-dns.sh
-rwxr-xr-x. 1 20415 101 1642 Aug 1 00:35 stop-yarn.cmd
-rwxr-xr-x. 1 20415 101 1340 Aug 1 00:35 stop-yarn.sh
-rwxr-xr-x. 1 20415 101 4295 Aug 1 00:35 yarn-daemon.sh
-rwxr-xr-x. 1 20415 101 1353 Aug 1 00:35 yarn-daemons.sh
到目前为止,集群已经搭建完毕,你可以尝试./start-all.sh脚本启动整个集群然后用jps看看集群所需服务是否全都启动了,这里不在赘述.
我已经打包好在dockerhub上面,欢迎下载.
docker pull awedocker/hadoop-master
docker pull awedocker/hadoop-slave1
docker pull awedocker/hadoop-slave2
docker pull awedocker/hadoop-slave3
记得按照教程划分子网.
转载自:http://www.jianshu.com/p/0c7b6de487ce 有大量改动 不喜勿喷