Hadoop加zookeeper搭建高可靠集群

前期准备

1.修改Linux主机名,每台都得配置

vim /etc/sysconfig/network

NETWORKING=yes
HOSTNAME=hadoop-server1

2.修改IP /etc/sysconfig/network-scripts/ifcfg-eth0

3.修改主机名和IP的映射关系

vim /etc/hosts

192.168.146.181 hadoop-server1
192.168.146.182 hadoop-server2
192.168.146.183 hadoop-server3
192.168.146.184 hadoop-server4
192.168.146.185 hadoop-server5

######注意######如果你们公司是租用的服务器或是使用的云主机(如华为用主机、阿里云主机等)
/etc/hosts里面要配置的是内网IP地址和主机名的映射关系

4.关闭防火墙 

#查看防火墙状态
service iptables status
#关闭防火墙
service iptables stop
#查看防火墙开机启动状态
chkconfig iptables --list
#关闭防火墙开机启动
chkconfig iptables off

前4步用root用户操作,操作完后重启机器

5.ssh免登陆hadoop用户操作

#生成ssh免登陆密钥
#进入到我的home目录
cd ~/.ssh


ssh-keygen -t rsa (四个回车)
执行完这个命令后,会生成两个文件id_rsa(私钥)、id_rsa.pub(公钥)
将公钥拷贝到要免密登陆的目标机器上
ssh-copy-id hadoop-server2

6.安装JDK,配置环境变量等root用户操作

vim /etc/proflie
export JAVA_HOME=/usr/java/jdk1.7.0_65
export HADOOP_HOME=/itcast/hadoop-2.4.1
export PATH=$PATH:$JAVA_HOME/bin:$HADOOP_HOME/bin:$HADOOP_HOME/sbin


source /etc/profile


集群规划:

主机名 IP 安装软件 运行进程
hadoop-server1 192.168.146.181:jdk、hadoop namenode resourcemanage DFSZKFailoverController(zkfc)
hadoop-server2 192.168.146.182:jdk、hadoop namenode resourcemanage DFSZKFailoverController(zkfc)
hadoop-server3 192.168.146.183:jdk、hadoop、zookeeper datanode nodemanage journalnode QuorumPeerMain
hadoop-server4 192.168.146.184:jdk、hadoop、zookeeper datanode nodemanage journalnode QuorumPeerMain
hadoop-server5 192.168.146.185:jdk、hadoop、zookeeper datanode nodemanage journalnode QuorumPeerMain


安装步骤:
1.安装配置zooekeeper集群(在hadoop-server3上)
1.1解压
tar -zxvf zookeeper-3.4.5.tar.gz -C /home/hadoop/app/
1.2修改配置
cd /home/hadoop/app/zookeeper-3.4.5/conf/
cp zoo_sample.cfg zoo.cfg
vim zoo.cfg
修改:dataDir=/home/hadoop/app/zookeeper-3.4.5/data
在最后添加:
server.1=hadoop-server3:2888:3888
server.2=hadoop-server4:2888:3888
server.3=hadoop-server5:2888:3888
保存退出
然后创建一个tmp文件夹
mkdir /home/hadoop/app/zookeeper-3.4.5/data
再创建一个空文件
touch /home/hadoop/app/zookeeper-3.4.5/data/myid
最后向该文件写入ID
echo 1 > /home/hadoop/app/zookeeper-3.4.5/data/myid
1.3将配置好的zookeeper拷贝到其他节点
scp -r /home/hadoop/app/zookeeper-3.4.5/ weekend06:/home/hadoop/app/
scp -r /home/hadoop/app/zookeeper-3.4.5/ weekend07:/home/hadoop/app/

注意:修改hadoop-server4、hadoop-server5对应/home/hadoop/app/zookeeper-3.4.5/data/myid内容
hadoop-server4:
echo 2 > /home/hadoop/app/zookeeper-3.4.5/data/myid
hadoop-server5:
echo 3 > /home/hadoop/app/zookeeper-3.4.5/data/myid

2.安装配置hadoop集群(在hadoop-server1上操作)
2.1解压
tar -zxvf hadoop-2.4.1.tar.gz -C /weekend/
2.2配置HDFS(hadoop2.0所有的配置文件都在$HADOOP_HOME/etc/hadoop目录下)
#将hadoop添加到环境变量中
vim /etc/profile
export JAVA_HOME=/hadoop/home/app/jdk1.7.0_55
export HADOOP_HOME=/home/hadoop/app/hadoop-2.4.1
export PATH=$PATH:$JAVA_HOME/bin:$HADOOP_HOME/bin

#hadoop2.0的配置文件全部在$HADOOP_HOME/etc/hadoop下
cd /home/hadoop/app/hadoop-2.4.1/etc/hadoop

2.2.1修改hadoo-env.sh
export JAVA_HOME=/home/hadoop/app/jdk1.7.0_55

2.2.2修改core-site.xml



fs.defaultFS
hdfs://ns1/



hadoop.tmp.dir
/home/hadoop/app/hadoop-2.4.1/tmp




ha.zookeeper.quorum
hadoop-server3:2181,hadoop-server3:2181,hadoop-server3:2181



2.2.3修改hdfs-site.xml



dfs.nameservices
ns1



dfs.ha.namenodes.ns1
nn1,nn2



dfs.namenode.rpc-address.ns1.nn1
hadoop-server1:9000



dfs.namenode.http-address.ns1.nn1
hadoop-server1:50070



dfs.namenode.rpc-address.ns1.nn2
weekend02:9000



dfs.namenode.http-address.ns1.nn2
hadoop-server2:50070



dfs.namenode.shared.edits.dir
qjournal://hadoop-server3:8485;hadoop-server4:8485;hadoop-server5:8485/ns1



dfs.journalnode.edits.dir
/home/hadoop/app/hadoop-2.4.1/journaldata



dfs.ha.automatic-failover.enabled
true



dfs.client.failover.proxy.provider.ns1
org.apache.hadoop.hdfs.server.namenode.ha.ConfiguredFailoverProxyProvider



dfs.ha.fencing.methods

sshfence
shell(/bin/true)




dfs.ha.fencing.ssh.private-key-files
/home/hadoop/.ssh/id_rsa



dfs.ha.fencing.ssh.connect-timeout
30000



2.2.4修改mapred-site.xml



mapreduce.framework.name
yarn



2.2.5修改yarn-site.xml



   yarn.resourcemanager.ha.enabled
   true



   yarn.resourcemanager.cluster-id
   yrc



   yarn.resourcemanager.ha.rm-ids
   rm1,rm2



   yarn.resourcemanager.hostname.rm1
   hadoop-server1


   yarn.resourcemanager.hostname.rm2
   hadoop-server2



   yarn.resourcemanager.zk-address
   hadoop-server3:2181,hadoop-server4:2181,hadoop-server5:2181


   yarn.nodemanager.aux-services
   mapreduce_shuffle




2.2.6修改slaves(slaves是指定子节点的位置)
hadoop-server3
hadoop-server4
hadoop-server5


2.2.7配置免密码登陆
#首先要配置hadoop-server1到hadoop-server2、hadoop-server3、hadoop-server4、hadoop-server5的免密码登陆
#在hadoop-server1上生产一对钥匙
ssh-keygen -t rsa
#将公钥拷贝到其他节点,包括自己
ssh-coyp-id hadoop-server1
ssh-coyp-id hadoop-server2
ssh-coyp-id hadoop-server3
ssh-coyp-id hadoop-server4
ssh-coyp-id hadoop-server5
#注意:resourcemanager到nodemanager要配置免密登录
#注意:两个namenode之间要配置ssh免密码登陆,别忘了配置hadoop-server2到hadoop-server1的免登陆
在hadoop-server2上生产一对钥匙
ssh-keygen -t rsa
ssh-coyp-id -i hadoop-server1

2.4将配置好的hadoop拷贝到其他节点
scp -r /home/hadoop/app/hadoop-2.4.1/ hadoop@hadoop-server2:/home/hadoop/app/
scp -r /home/hadoop/app/hadoop-2.4.1/ hadoop@hadoop-server3:/home/hadoop/app/

scp -r /home/hadoop/app/hadoop-2.4.1/ hadoop@hadoop-server4:/home/hadoop/app/
scp -r /home/hadoop/app/hadoop-2.4.1/ hadoop@hadoop-server
5:/home/hadoop/app/

###注意:严格按照下面的步骤
2.5启动zookeeper集群(分别在hadoop-server3、hadoop-server4、hadoop-server5上启动zk)
cd /home/hadoop/app/zookeeper-3.4.5/bin/
./zkServer.sh start
#查看状态:一个leader,两个follower
./zkServer.sh status

2.6启动journalnode(分别在在hadoop-server3、hadoop-server4、hadoop-server5上执行)
cd /home/hadoop/app/hadoop-2.4.1
sbin/hadoop-daemon.sh start journalnode
#运行jps命令检验,hadoop-server3、hadoop-server4、hadoop-server5上多了JournalNode进程

2.7格式化HDFS
#在hadoop-server1上执行命令:
hdfs namenode -format
#格式化后会在根据core-site.xml中的hadoop.tmp.dir配置生成个文件,这里我配置的是/home/hadoop/app/hadoop-2.4.1/tmp,然后将/home/hadoop/app/hadoop-2.4.1/tmp拷贝到hadoop-server2的/home/hadoop/app/hadoop-2.4.1/下。
scp -r tmp/ hadoop-server2:/home/hadoop/app/hadoop-2.4.1/
##也可以这样,在hadoop-server2上执行命令:建议hdfs namenode -bootstrapStandby

2.8格式化ZKFC(在hadoop-server1上执行即可)
hdfs zkfc -formatZK

2.9启动HDFS(在hadoop-server1上执行)
sbin/start-dfs.sh


2.10启动YARN(#####注意#####:是在hadoop-server1上执行start-yarn.sh)
sbin/start-yarn.sh

hadoop-server2上启动 yyarn-daemon.sh start rescourcemanager



到此,hadoop-2.4.1配置完毕,可以统计浏览器访问:
http://192.168.146.181:50070
NameNode 'hadoop-server1:9000' (active)
http://192.168.146.182:50070
NameNode 'hadoop-server2:9000' (standby)

验证HDFS HA
首先向hdfs上传一个文件
hadoop fs -put /etc/profile /profile
hadoop fs -ls /
然后再kill掉active的NameNode
kill -9
通过浏览器访问:http://192.168.146.182:50070
NameNode 'hadoop-server2:9000' (active)
这个时候hadoop-server2上的NameNode变成了active
在执行命令:
hadoop fs -ls /
-rw-r--r--   3 root supergroup       1926 2015-06-24 15:36 /profile
刚才上传的文件依然存在!!!
手动启动那个挂掉的NameNode
sbin/hadoop-daemon.sh start namenode
通过浏览器访问:http://192.168.146.181:50070
NameNode 'hadoop-server1:9000' (standby)

验证YARN:
运行一下hadoop提供的demo中的WordCount程序:
hadoop jar share/hadoop/mapreduce/hadoop-mapreduce-examples-2.4.1.jar wordcount /profile /out

OK,大功告成!!!





测试集群工作状态的一些指令 :
bin/hdfs dfsadmin -report  查看hdfs的各节点状态信息




bin/hdfs haadmin -getServiceState nn1  获取一个namenode节点的HA状态


sbin/hadoop-daemon.sh start namenode  单独启动一个namenode进程




./hadoop-daemon.sh start zkfc   单独启动一个zkfc进程


你可能感兴趣的:(Hadoop)