集羣四部曲(二):完美的Hadoop集羣搭建

    繼上次Zookeeper集羣的搭建成功後,如今來講下Hadoop的集羣搭建。好了,閒話很少說,讓咱們開始今天的冒險之旅吧。java

    1、環境:虛擬機CentOs7系統,完整的環境,請確認已安裝JDK,及Hadoop安裝包(本人用的是2.9.0),節點仍然使用上次克隆的兩個,下面先進行其中一個的環境搭建。node

    2、Hadoop配置(本身解壓哈)apache

    配置前說明一下(很重要,拿本子記一下):首先,namenode,datanode,ResourceManager瞭解一下,這裏準備將slave01做爲namenode,slave02和slave03做爲datanode,下面的操做都是以這個爲前提展開的,請你們作好戰前溫習,以防不知下面的操做的意義。開始表演吧:vim

    解壓後文件目錄結構基本以下:windows

[hadoop@slave01 hadoop]$ pwd
/usr/local/hadoop
[hadoop@slave01 hadoop]$ ls
bin  include  libexec      logs        README.txt  share
etc  lib      LICENSE.txt  NOTICE.txt  sbin        tmp
[hadoop@slave01 hadoop]$ cd etc/hadoop/
[hadoop@slave01 hadoop]$ ls
capacity-scheduler.xml      httpfs-env.sh            mapred-env.sh
configuration.xsl           httpfs-log4j.properties  mapred-queues.xml.template
container-executor.cfg      httpfs-signature.secret  mapred-site.xml.template
core-site.xml               httpfs-site.xml          slaves
hadoop-env.cmd              kms-acls.xml             ssl-client.xml.example
hadoop-env.sh               kms-env.sh               ssl-server.xml.example
hadoop-metrics2.properties  kms-log4j.properties     yarn-env.cmd
hadoop-metrics.properties   kms-site.xml             yarn-env.sh
hadoop-policy.xml           log4j.properties         yarn-site.xml
hdfs-site.xml               mapred-env.cmd

    關閉防火牆,你們瞭解一下:服務器

systemctl stop firewalld    #只在本次運用時生效,下次開啓機器時需重複此操做
或
systemctl disable firewalld   #此命令在下次重啓時生效,將永久關閉防火牆

    (1)將Hadoop添加到環境變量中框架

vim /etc/profile

    添加HADOOP_HOME,修改以下:dom

JAVA_HOME=/usr/java/jdk1.8.0_161
JRE_HOME=/usr/java/jdk1.8.0_161/jre
SCALA_HOME=/usr/local/scala
HADOOP_HOME=/usr/local/hadoop
ZOOKEEPER_HOME=/usr/local/zookeeper
KAFKA_HOME=/usr/local/kafka
PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin:$SCALA_HOME/bin:$HADOOP_HOME/bin:$HADOOP_HOME/sbin:$ZOOKEEPER_HOME/bin:$KAFKA_HOME/bin
CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib
export JAVA_HOME JRE_HOME SCALA_HOME HADOOP_HOME ZOOKEEPER_HOME KAFKA_HOME PATH CLASSPATH

    查看Hadoop解壓目錄下:/etc/hadoop/hadoop-env.sh文件中配置是否正常:ide

# The java implementation to use.
export JAVA_HOME=/usr/java/jdk1.8.0_161

# The jsvc implementation to use. Jsvc is required to run secure datanodes
# that bind to privileged ports to provide authentication of data transfer
# protocol.  Jsvc is not required if SASL is configured for authentication of
# data transfer protocol using non-privileged ports.
#export JSVC_HOME=${JSVC_HOME}

export HADOOP_CONF_DIR=${HADOOP_CONF_DIR:-"/etc/hadoop"}

    JAVA_HOME改成本身本機jdk安裝地址oop

    (2)修改core-site.xml文件

    從上面的目錄結構中找到該文件,打開後顯示的初始配置以下:

<configuration>
    <property>
        <name>hadoop.tmp.dir</name>
        <value>file:/usr/local/hadoop/tmp</value>
        <description>Abase for other temporary directories.</description>
    </property>
    <property>
        <name>fs.defaultFS</name>
        <value>hdfs://localhost:9000</value>
    </property>
</configuration>

    進行以下修改:

<configuration>
    <!-- 指定Hadoop臨時目錄 -->
    <property>
        <name>hadoop.tmp.dir</name>
        <value>file:/usr/local/hadoop/tmp</value>
        <description>Abase for other temporary directories.</description>
    </property>
    <!-- 指定HDFS的namenode的通訊地址 -->
    <property>
        <name>fs.defaultFS</name>
        <value>hdfs://slave01:9000</value>
    </property>
</configuration>

    (3)修改hdfs-site.xml文件

     從上面的目錄結構中找到該文件,打開後顯示的初始配置以下:

<configuration>
    <property>
        <name>dfs.replication</name>
        <value>1</value>
    </property>
    <property>
        <name>dfs.namenode.name.dir</name>
        <value>file:/usr/local/hadoop/tmp/dfs/name</value>
    </property>
    <property>
        <name>dfs.datanode.data.dir</name>
        <value>file:/usr/local/hadoop/tmp/dfs/data</value>
    </property>
</configuration>

    進行以下修改:

<configuration>
    <!-- 設置namenode的http通訊地址 -->
    <property>
        <name>dfs.namenode.http-address</name>
        <value>slave01:50070</value>
    </property>
    <!-- 設置hdfs副本數量 --> 
    <property>
        <name>dfs.replication</name>
        <value>2</value>
    </property>
    <!-- 設置namenode存放路徑 -->
    <property>
        <name>dfs.namenode.name.dir</name>
        <value>file:/usr/local/hadoop/tmp/dfs/name</value>
    </property>
    <!--設置datanode存放路徑  -->
    <property>
        <name>dfs.datanode.data.dir</name>
        <value>file:/usr/local/hadoop/tmp/dfs/data</value>
    </property>
</configuration>

    此處可添加其餘的配置參數,在此不作介紹,可自行百度查看,注意設置的存放路徑的文件夾的建立,若不存在,則建立

    (4)修改mapred-site.xml文件

    首先複製一份該文件的temple,從新命名:

cp mapred-site.xml.template mapred-site.xml

    初始內容爲空,修改後以下:

<configuration>
      <!-- 指定mapreduce框架爲yarn方式 -->
      <property>
          <name>mapreduce.framework.name</name>
          <value>yarn</value>
      </property>
</configuration>

    (5)修改yarn-site.xml文件

    初始內容爲空,修改後以下:

<configuration>

<!-- Site specific YARN configuration properties -->
        <!-- 設置 resourcemanager 在哪一個節點-->
        <property>
                <name>yarn.resourcemanager.hostname</name>
                <value>slave01</value>
        </property>
         <!-- reducer取數據的方式是mapreduce_shuffle -->
        <property>
                <name>yarn.nodemanager.aux-services</name>
                <value>mapreduce_shuffle</value>
        </property>
</configuration>

    (6)修改slaves文件(只需將做爲datanode的服務器名加上便可

slave02
slave03

    (7)拷貝配置文件到其餘服務器

    若不想一個一個的進行文件配置,能夠將已經配置好的文件拷貝到其餘須要的服務器上,注意拷貝成功後執行命令:source /etc/profile使之生效

//slave01上的/etc/profile文件拷貝到slave02
scp -r /etc/profile slave02:/etc/profile
//slave01上的/usr/local/hadoop文件夾整個目錄拷貝到slave02
scp -r /usr/local/hadoop slave02:/usr/local/

    (8)格式化HDFS

    在namenode節點的服務器即slave01上執行命令:

hdfs namenode -format

    格式化成功則顯示以下:

8/03/16 15:40:23 INFO namenode.FSDirectory: XAttrs enabled? true
18/03/16 15:40:23 INFO namenode.NameNode: Caching file names occurring more than 10 times
18/03/16 15:40:23 INFO snapshot.SnapshotManager: Loaded config captureOpenFiles: falseskipCaptureAccessTimeOnlyChange: false
18/03/16 15:40:23 INFO util.GSet: Computing capacity for map cachedBlocks
18/03/16 15:40:23 INFO util.GSet: VM type       = 64-bit
18/03/16 15:40:23 INFO util.GSet: 0.25% max memory 889 MB = 2.2 MB
18/03/16 15:40:23 INFO util.GSet: capacity      = 2^18 = 262144 entries
18/03/16 15:40:23 INFO metrics.TopMetrics: NNTop conf: dfs.namenode.top.window.num.buckets = 10
18/03/16 15:40:23 INFO metrics.TopMetrics: NNTop conf: dfs.namenode.top.num.users = 10
18/03/16 15:40:23 INFO metrics.TopMetrics: NNTop conf: dfs.namenode.top.windows.minutes = 1,5,25
18/03/16 15:40:23 INFO namenode.FSNamesystem: Retry cache on namenode is enabled
18/03/16 15:40:23 INFO namenode.FSNamesystem: Retry cache will use 0.03 of total heap and retry cache entry expiry time is 600000 millis
18/03/16 15:40:23 INFO util.GSet: Computing capacity for map NameNodeRetryCache
18/03/16 15:40:23 INFO util.GSet: VM type       = 64-bit
18/03/16 15:40:23 INFO util.GSet: 0.029999999329447746% max memory 889 MB = 273.1 KB
18/03/16 15:40:23 INFO util.GSet: capacity      = 2^15 = 32768 entries
Re-format filesystem in Storage Directory /usr/local/hadoop/tmp/dfs/name ? (Y or N) y
18/03/16 15:40:26 INFO namenode.FSImage: Allocated new BlockPoolId: BP-1094714660-127.0.0.1-1521186026480
18/03/16 15:40:26 INFO common.Storage: Will remove files: [/usr/local/hadoop/tmp/dfs/name/current/VERSION, /usr/local/hadoop/tmp/dfs/name/current/seen_txid, /usr/local/hadoop/tmp/dfs/name/current/fsimage_0000000000000000000.md5, /usr/local/hadoop/tmp/dfs/name/current/fsimage_0000000000000000000, /usr/local/hadoop/tmp/dfs/name/current/edits_0000000000000000001-0000000000000000004, /usr/local/hadoop/tmp/dfs/name/current/fsimage_0000000000000000004.md5, /usr/local/hadoop/tmp/dfs/name/current/fsimage_0000000000000000004, /usr/local/hadoop/tmp/dfs/name/current/edits_0000000000000000005-0000000000000000005, /usr/local/hadoop/tmp/dfs/name/current/edits_inprogress_0000000000000000006]
18/03/16 15:40:26 INFO common.Storage: Storage directory /usr/local/hadoop/tmp/dfs/name has been successfully formatted.
18/03/16 15:40:26 INFO namenode.FSImageFormatProtobuf: Saving image file /usr/local/hadoop/tmp/dfs/name/current/fsimage.ckpt_0000000000000000000 using no compression
18/03/16 15:40:26 INFO namenode.FSImageFormatProtobuf: Image file /usr/local/hadoop/tmp/dfs/name/current/fsimage.ckpt_0000000000000000000 of size 323 bytes saved in 0 seconds.
18/03/16 15:40:26 INFO namenode.NNStorageRetentionManager: Going to retain 1 images with txid >= 0
18/03/16 15:40:26 INFO namenode.NameNode: SHUTDOWN_MSG: 
/************************************************************
SHUTDOWN_MSG: Shutting down NameNode at slave01/127.0.0.1
************************************************************/
[hadoop@slave01 hadoop]$

    中間有要操做的直接按提示便可,注意,下面說下報錯的狀況,可能不少同窗在配置的時候均可能遇到過,折騰很多時間吧:

18/03/16 15:54:14 WARN namenode.NameNode: Encountered exception during format: 
java.io.IOException: Cannot remove current directory: /usr/local/hadoop/tmp/dfs/name/current
	at org.apache.hadoop.hdfs.server.common.Storage$StorageDirectory.clearDirectory(Storage.java:358)
	at org.apache.hadoop.hdfs.server.namenode.NNStorage.format(NNStorage.java:571)
	at org.apache.hadoop.hdfs.server.namenode.NNStorage.format(NNStorage.java:592)
	at org.apache.hadoop.hdfs.server.namenode.FSImage.format(FSImage.java:166)
	at org.apache.hadoop.hdfs.server.namenode.NameNode.format(NameNode.java:1172)
	at org.apache.hadoop.hdfs.server.namenode.NameNode.createNameNode(NameNode.java:1614)
	at org.apache.hadoop.hdfs.server.namenode.NameNode.main(NameNode.java:1741)
18/03/16 15:54:14 ERROR namenode.NameNode: Failed to start namenode.
java.io.IOException: Cannot remove current directory: /usr/local/hadoop/tmp/dfs/name/current
	at org.apache.hadoop.hdfs.server.common.Storage$StorageDirectory.clearDirectory(Storage.java:358)
	at org.apache.hadoop.hdfs.server.namenode.NNStorage.format(NNStorage.java:571)
	at org.apache.hadoop.hdfs.server.namenode.NNStorage.format(NNStorage.java:592)
	at org.apache.hadoop.hdfs.server.namenode.FSImage.format(FSImage.java:166)
	at org.apache.hadoop.hdfs.server.namenode.NameNode.format(NameNode.java:1172)
	at org.apache.hadoop.hdfs.server.namenode.NameNode.createNameNode(NameNode.java:1614)
	at org.apache.hadoop.hdfs.server.namenode.NameNode.main(NameNode.java:1741)
18/03/16 15:54:14 INFO util.ExitUtil: Exiting with status 1: java.io.IOException: Cannot remove current directory: /usr/local/hadoop/tmp/dfs/name/current
18/03/16 15:54:14 INFO namenode.NameNode: SHUTDOWN_MSG: 
/************************************************************
SHUTDOWN_MSG: Shutting down NameNode at slave02/127.0.0.1
************************************************************/
[hadoop@slave02 hadoop]$

    報錯的緣由呢,只是由於沒有設置 /usr/local/hadoop/tmp文件夾的權限,悲傷辣麼大,我竟無言以對。解決方法,固然是給這個目錄加上權限了,chmod瞭解一下,在三個服務器上都進行該操做:

sudo chmod -R a+w /usr/local/hadoop

    到此呢,基本的配置已經完成了。固然,還有許多的配置沒有使用,這裏只是作個初步簡單的集羣。

    (三)啓動hadoop集羣

    (1)在slave01上執行以下命令

start-dfs.sh
start-yarn.sh
jps

    啓動成功顯示以下:

[hadoop@slave01 sbin]$ ./start-dfs.sh 
Starting namenodes on [slave01]
slave01: starting namenode, logging to /usr/local/hadoop/logs/hadoop-hadoop-namenode-slave01.out
slave02: starting datanode, logging to /usr/local/hadoop/logs/hadoop-hadoop-datanode-slave02.out
slave03: starting datanode, logging to /usr/local/hadoop/logs/hadoop-hadoop-datanode-slave03.out
Starting secondary namenodes [0.0.0.0]
0.0.0.0: starting secondarynamenode, logging to /usr/local/hadoop/logs/hadoop-hadoop-secondarynamenode-slave01.out
[hadoop@slave01 sbin]$ ./start-yarn.sh 
starting yarn daemons
starting resourcemanager, logging to /usr/local/hadoop/logs/yarn-hadoop-resourcemanager-localhost.localdomain.out
slave03: starting nodemanager, logging to /usr/local/hadoop/logs/yarn-hadoop-nodemanager-slave03.out
slave02: starting nodemanager, logging to /usr/local/hadoop/logs/yarn-hadoop-nodemanager-slave02.out
[hadoop@slave01 sbin]$ jps
7141 Jps
6601 SecondaryNameNode
6878 ResourceManager
6367 NameNode
[hadoop@slave01 sbin]$

    中間須要操做的直接按提示來,從jps命令顯示的結果能夠看出,NameNode和ResourceManager等都已經正常啓動了,很欣慰啊。

    (2)在slave02和slave03上執行jps命令

#slave02
[hadoop@slave02 hadoop]$ jps
12296 DataNode
13226 Jps
12446 NodeManager
[hadoop@slave02 hadoop]$ 

#slave03
[hadoop@slave03 hadoop]$ jps
12122 NodeManager
11978 DataNode
12796 Jps
[hadoop@slave03 hadoop]$

    能夠看到DataNode和NodeManager都正常啓動了。若沒有成功啓動,須要關掉slave01中的進程,找尋緣由,運行以下命令關閉hadoop相關的全部進程:

[hadoop@slave01 sbin]$ ./stop-all.sh

或者從新啓動虛擬機(這個成功概率很大,在其餘配置正確的狀況下)。我也出現該問題了,後來將三個服務器上/hadoop/tmp/dfs目錄下的data文件夾刪掉了,從新啓動則成功了。固然視狀況而定,可參考以下:http://www.javashuo.com/article/p-pylzchgq-eb.html

    (3)查看hadoop運行管理界面

    訪問http://slave01:8088(默認8088端口,若被佔用,請修改):

    點擊左邊菜單的Nodes能夠查看當前的node節點,以下:

    點開其中一個node節點,如:slave02:8042,可查看該node節點的具體信息,以下:

    訪問http://slave01:50070:

    好了,如今Hadoop集羣的搭建已經完成了,O(∩_∩)O。

相關文章
相關標籤/搜索