用Linux Container在单机上部署完全分布式的Hadoop集群

在Hadoop开发和学习过程中,经常需要部署一个完全分布式的Hadoop集群。而目前在单机上用得最多的方法就是用Vmware建虚拟机,不过一般的机器开3-4个虚拟机就差不多了。如果想在单机上(而且是虚拟机里面)跑10个节点Hadoop集群,那么可以考虑一下Linux Container。

阅读本文你将得到:

1. 如何在Centos上创建和使用Linux Container (RHEL上也是类似的,不过没有验证过)

2. 简要的Linux Container使用方法


注:
1. 本文将不会介绍如何部署Hadoop集群,相关文章网络上已经很多了。
2. 如果你在用Ubuntu,请移步这里(需自带梯子),本文也是受其启发:http://ofirm.wordpress.com/2014/01/05/creating-a-virtualized-fully-distributed-hadoop-cluster-using-linux-containers/

Linux Container是什么?

Linux Container是一种轻量的虚拟化解决方案,资源占用小,隔离性一般。从使用者的角度上来看,每个Container看起来就是一台虚拟机,有自己的机器名,有自己的IP地址,可以安装自己的软件,但是它们是跑在同一个Host上的不同进程而已。如果想要了解更多关于Linux Container的话,请自行查阅相关资料,比如:http://www.stgraber.org/2013/12/20/lxc-1-0-blog-post-series/

在CentOS上安装Linux Container

1. 准备工作

先把Linux Container依赖的一些软件包给装上,并启动相关的服务。其中git将在第二步中用来下载源代码。
yum install -y automake gcc libvirt libcgroup git
service cgconfig start
service libvirtd start 

2. 从github中下载Linux Container的代码

 git clone https://github.com/jewes/lxc.git

上面的git地址是笔者从官方源代码中fork出来的,对centos的模板做了些小修改,使得创建CentOS的Linux Container更加简单。

3. 编译

 ./autogen.sh && ./configure && make && sudo make install

4. 设置环境变量,可以考虑将其写到/etc/bashrc中去

 export LD_LIBRARY_PATH=/usr/local/lib/:$LD_LIBRARY_PATH

经过以上几步后,Linux Container就安装到你的机器上了。下面将介绍如何使用Linux Container。

使用Linux Container

1. 创建Linux Container

Linux Container自带了命令lxc-create来创建Container。不过,在构建Hadoop集群的时候,我们有如下需求:
  • 需要创建若干个Container,对应于集群中节点的数量
  • 每个Container要分配一个固定的IP地址
  • 每个Container要配置FQDN,也就是在运行hostname -f的时候,输出的内容要带有域名的,这在配置Kerberos的Hadoop集群中是必须的。
  • Container之间通过机器名能互相访问

为此,笔者在lxc-create命令的基础上写了小脚本create_containers.sh(该脚本位于源代码的根目录下),使得能用一条命令就创建出我们需要数量的Container。
使用方法举例:

# 需要root权限,因为lxc-create需要root权限
./create_containers.sh -n 5 -s 111 

上述命令将创建5个Container,Container的IP地址从192.168.122.111开始,相关信息如下:
hostname/container name
fqdn
ip address
lxc111 lxc111.example.com 192.168.122.111
lxc112 lxc112.example.com 192.168.122.112
lxc113 lxc113.example.com 192.168.122.113
lxc114 lxc114.example.com 192.168.122.114
lxc115 lxc115.example.com 192.168.122.115
Container默认的root密码是welcome

2. 常用的Linux Container的管理工具:

lxc-start: 启动一个Linux Container
# -d 表示以daemon来启动该Container
lxc-start -n <container name> -d

lxc-info:显示一个Linux Container的信息
lxc-info -n <container name>

lxc-stop: 停止一个Container
lxc-stop -n <container name>

lxc-destroy: 销毁一个Linux Container
lxc-destroy -n <container name>

你也可以用ssh方式登入Container,这样就和使用一台普通Linux机器没有区别了,你可以在里面用yum来安装需要的软件,当然也可以部署Hadoop了。

3. 在Linux Container中部署Hadoop集群

笔者在一台有4GB内存的虚拟机中,建了11个Container,部署了Pivotal的最新Hadoop发行版 PHD2.0,其中两个Container分别用来跑Namenode和ResourceManager,剩下9台用了跑Datanode和NodeManager,跑些数据量不大的任务没有问题,大数据量的任务目前还没有跑过,用来学习Hadoop完全够了。有图有真相!

Datanodes:


NodeManagers:


用Linux Container在单机上部署完全分布式的Hadoop集群,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。