• 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

    ######注意######假设你们公司是租用的server或是使用的云主机(如华为用主机、阿里云主机等)
    /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
    <configuration>
    <!-- 指定hdfs的nameservice为ns1 -->
    <property>
    <name>fs.defaultFS</name>
    <value>hdfs://ns1/</value>
    </property>
    <!-- 指定hadoop暂时文件夹 -->
    <property>
    <name>hadoop.tmp.dir</name>
    <value>/home/hadoop/app/hadoop-2.4.1/tmp</value>
    </property>

    <!-- 指定zookeeper地址 -->
    <property>
    <name>ha.zookeeper.quorum</name>
    <value>hadoop-server3:2181,hadoop-server3:2181,hadoop-server3:2181</value>
    </property>
    </configuration>

    2.2.3改动hdfs-site.xml
    <configuration>
    <!--指定hdfs的nameservice为ns1。须要和core-site.xml中的保持一致 -->
    <property>
    <name>dfs.nameservices</name>
    <value>ns1</value>
    </property>
    <!-- ns1以下有两个NameNode。各自是nn1,nn2 -->
    <property>
    <name>dfs.ha.namenodes.ns1</name>
    <value>nn1,nn2</value>
    </property>
    <!-- nn1的RPC通信地址 -->
    <property>
    <name>dfs.namenode.rpc-address.ns1.nn1</name>
    <value>hadoop-server1:9000</value>
    </property>
    <!-- nn1的http通信地址 -->
    <property>
    <name>dfs.namenode.http-address.ns1.nn1</name>
    <value>hadoop-server1:50070</value>
    </property>
    <!-- nn2的RPC通信地址 -->
    <property>
    <name>dfs.namenode.rpc-address.ns1.nn2</name>
    <value>weekend02:9000</value>
    </property>
    <!-- nn2的http通信地址 -->
    <property>
    <name>dfs.namenode.http-address.ns1.nn2</name>
    <value>hadoop-server2:50070</value>
    </property>
    <!-- 指定NameNode的元数据在JournalNode上的存放位置 -->
    <property>
    <name>dfs.namenode.shared.edits.dir</name>
    <value>qjournal://hadoop-server3:8485;hadoop-server4:8485;hadoop-server5:8485/ns1</value>
    </property>
    <!-- 指定JournalNode在本地磁盘存放数据的位置 -->
    <property>
    <name>dfs.journalnode.edits.dir</name>
    <value>/home/hadoop/app/hadoop-2.4.1/journaldata</value>
    </property>
    <!-- 开启NameNode失败自己主动切换 -->
    <property>
    <name>dfs.ha.automatic-failover.enabled</name>
    <value>true</value>
    </property>
    <!-- 配置失败自己主动切换实现方式 -->
    <property>
    <name>dfs.client.failover.proxy.provider.ns1</name>
    <value>org.apache.hadoop.hdfs.server.namenode.ha.ConfiguredFailoverProxyProvider</value>
    </property>
    <!-- 配置隔离机制方法,多个机制用换行切割。即每一个机制暂用一行-->
    <property>
    <name>dfs.ha.fencing.methods</name>
    <value>
    sshfence
    shell(/bin/true)
    </value>
    </property>
    <!-- 使用sshfence隔离机制时须要ssh免登陆 -->
    <property>
    <name>dfs.ha.fencing.ssh.private-key-files</name>
    <value>/home/hadoop/.ssh/id_rsa</value>
    </property>
    <!-- 配置sshfence隔离机制超时时间 -->
    <property>
    <name>dfs.ha.fencing.ssh.connect-timeout</name>
    <value>30000</value>
    </property>
    </configuration>

    2.2.4改动mapred-site.xml
    <configuration>
    <!-- 指定mr框架为yarn方式 -->
    <property>
    <name>mapreduce.framework.name</name>
    <value>yarn</value>
    </property>
    </configuration>

    2.2.5改动yarn-site.xml
    <configuration>
    <!-- 开启RM高可用 -->
    <property>
       <name>yarn.resourcemanager.ha.enabled</name>
       <value>true</value>
    </property>
    <!-- 指定RM的cluster id -->
    <property>
       <name>yarn.resourcemanager.cluster-id</name>
       <value>yrc</value>
    </property>
    <!-- 指定RM的名字 -->
    <property>
       <name>yarn.resourcemanager.ha.rm-ids</name>
       <value>rm1,rm2</value>
    </property>
    <!-- 分别指定RM的地址 -->
    <property>
       <name>yarn.resourcemanager.hostname.rm1</name>
       <value>hadoop-server1</value>
    </property>
    <property>
       <name>yarn.resourcemanager.hostname.rm2</name>
       <value>hadoop-server2</value>
    </property>
    <!-- 指定zk集群地址 -->
    <property>
       <name>yarn.resourcemanager.zk-address</name>
       <value>hadoop-server3:2181,hadoop-server4:2181,hadoop-server5:2181</value>
    </property>
    <property>
       <name>yarn.nodemanager.aux-services</name>
       <value>mapreduce_shuffle</value>
    </property>
    </configuration>


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


    2.2.7配置免password登陆
    #首先要配置hadoop-server1到hadoop-server2、hadoop-server3、hadoop-server4、hadoop-server5的免password登陆
    #在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免password登陆,别忘了配置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 <pid of NN>
    通过浏览器訪问: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流程


    版权声明:本文博主原创文章。博客,未经同意不得转载。

  • 相关阅读:
    053(四十四)
    053(四十三)
    053(四十二)
    053(四十一)
    053(四十)
    053(三十九)
    053(三十八)
    053(三十七)
    053(三十六)
    【leetcode❤python】231. Power of Two
  • 原文地址:https://www.cnblogs.com/bhlsheji/p/4885002.html
Copyright © 2020-2023  润新知