天天看點

Hadoop叢集安裝--Ubuntu

  我家寶最近在自學Hadoop,于是乎跟着一起玩玩,在此為她整理一篇基礎搭建的部落格,希望對她能有所幫助。同樣,開始之前先來了解下,什麼是Hadoop。

  Hadoop實作了一個分布式檔案系統(Hadoop Distributed File System),簡稱HDFS。HDFS有高容錯性的特點,并且設計用來部署在低廉的(low-cost)硬體上;而且它提供高吞吐量(high throughput)來通路應用程式的資料,适合那些有着超大資料集(large data set)的應用程式。HDFS放寬了(relax)POSIX的要求,可以以流的形式通路(streaming access)檔案系統中的資料。

  使用者可以在不了解分布式底層細節的情況下,開發分布式程式。充分利用叢集的威力進行高速運算和存儲。Hadoop的架構最核心的設計就是HDFS和MapReduce。HDFS為海量的資料提供了存儲,則MapReduce為海量的資料提供了計算。

搭建

  搭建叢集的話,需要最少兩台機器來搭建一個多節點的Hadoop環境,我這裡使用Hadoop最新穩定的2.7.3版本和三台雲主機(1主兩從,Ubuntu1404 LTS)。

修改hosts檔案

  保證三台機器的網絡是可達的前提下,更改主機名,并修改hosts檔案:

<code># hostnamectl  set-hostname master   // 在master節點上執行</code>

<code># hostnamectl  set-hostname slave-1  // 在slave-1節點上執行</code>

<code># hostnamectl  set-hostname slave-2  // 在slave-2節點上執行</code>

<code>分别把三台機器的hosts檔案進行修改:</code>

<code># vim /etc/hosts</code>

<code>192.168.1.2  master</code>

<code>192.168.1.3  slave-1</code>

<code>192.168.1.4  slave-2</code>

2.在master和slave節點上安裝java:

<code># add-apt-repository ppa:webupd8team/java  //添加ppa</code>

<code># apt-get update</code>

<code># apt-get installoracle-java8-installer </code>

<code># java -version  //檢驗Java版本</code>

<code>java version </code><code>"1.8.0_121"</code>

<code>Java(TM) SE Runtime Environment (build 1.8.0_121-b13)</code>

<code>Java HotSpot(TM) 64-Bit Server VM (build 25.121-b13, mixed mode)</code>

3.禁用IPv6

<code># vim /etc/sysctl.conf</code>

<code>net.ipv6.conf.all.disable_ipv6 = 1</code>

<code>net.ipv6.conf.default.disable_ipv6 = 1</code>

<code>net.ipv6.conf.lo.disable_ipv6 = 1</code>

<code># sysctl -p  //使其立即生效</code>

4.建立Hadoop User

  在master和slave節點上執行:

<code># addgroup hdgroup  //建立hadoop group</code>

<code># adduser —ingroup hdgroup hduser  //建立Hadoop User并加入Hadoop group</code>

<code>Adding user `hduser' ...</code>

<code>Adding new user `hduser</code><code>' (1001) with group `hdgroup'</code> <code>...</code>

<code>Creating home directory `</code><code>/home/hduser</code><code>' ...</code>

<code>Copying files from `</code><code>/etc/skel</code><code>' ...</code>

<code>Enter new UNIX password:            </code><code>//</code><code>輸入密碼之後一路回車即可</code>

<code>Retype new UNIX password:</code>

<code>passwd</code><code>: password updated successfully</code>

<code>Changing the user information </code><code>for</code> <code>hduser</code>

<code>Enter the new value, or press ENTER </code><code>for</code> <code>the default</code>

<code>         </code><code>Full Name []:</code>

<code>         </code><code>Room Number []:</code>

<code>         </code><code>Work Phone []:</code>

<code>         </code><code>Home Phone []:</code>

<code>         </code><code>Other []:</code>

<code>Is the information correct? [Y</code><code>/n</code><code>]</code>

  Hadoop要求無密碼登入,是以需要生成秘鑰,這裡注意要用剛才建立的普通hduser使用者,分别在master和slave上執行如下操作:

<code># su - hduser</code>

<code>$ </code><code>ssh</code><code>-keygen -N </code><code>''</code>

<code>Generating public</code><code>/private</code> <code>rsa key pair.</code>

<code>Enter </code><code>file</code> <code>in</code> <code>which</code> <code>to save the key (</code><code>/home/hduser/</code><code>.</code><code>ssh</code><code>/id_rsa</code><code>):</code>

<code>Created directory </code><code>'/home/hduser/.ssh'</code><code>.</code>

<code>Your identification has been saved </code><code>in</code> <code>/home/hduser/</code><code>.</code><code>ssh</code><code>/id_rsa</code><code>.</code>

<code>Your public key has been saved </code><code>in</code> <code>/home/hduser/</code><code>.</code><code>ssh</code><code>/id_rsa</code><code>.pub.</code>

<code>The key fingerprint is:</code>

<code>5b:ae:c6:5a:ce:66:51:d3:6c:6c:14:9b:b2:8a:da:e9 hduser@master</code>

<code>The key's randomart image is:</code>

<code>+--[ RSA 2048]----+</code>

<code>|            ..   |</code>

<code>|            .o   |</code>

<code>|          .=o    |</code>

<code>|          oo*    |</code>

<code>|        S.o+     |</code>

<code>|       ..=       |</code>

<code>|      ..+..      |</code>

<code>|     o ==.       |</code>

<code>|    ..E=+        |</code>

<code>+-----------------+</code>

<code>$ </code><code>ssh</code><code>-copy-</code><code>id</code> <code>hduser@master</code>

<code>$ </code><code>ssh</code><code>-copy-</code><code>id</code> <code>hduser@slave-1</code>

<code>$ </code><code>ssh</code><code>-copy-</code><code>id</code> <code>hduser@slave-2</code>

5.下載下傳和安裝Hadoop

<a href="https://s3.51cto.com/wyfs02/M02/8D/81/wKiom1iezuazT-lkAAL2XegG4Yk005.png-wh_500x0-wm_3-wmp_4-s_1622907005.png" target="_blank"></a>

 打開連結之後,右鍵複制連結位址:

<a href="https://s2.51cto.com/wyfs02/M00/8D/7F/wKioL1iezzOBIOACAAI4HvzbjQU970.png-wh_500x0-wm_3-wmp_4-s_1431648937.png" target="_blank"></a>

在master和slave上分别執行(你也可以在一台機器上下載下傳完之後拷貝到另外兩台上):

<code>$ </code><code>cd</code> <code>/home/hduser</code>

<code>$ wget -c </code>

<code>$ </code><code>tar</code> <code>-zxvf hadoop-2.7.3.</code><code>tar</code><code>.gz</code>

<code>$ </code><code>mv</code> <code>hadoop-2.7.3 hadoop</code>

6.更改環境變量

  首先确定之前安裝的java home目錄,查找辦法如下(在任意一台機器上執行):

<code>hduser@master:~$ </code><code>env</code> <code>| </code><code>grep</code> <code>-i java</code>

<code>JAVA_HOME=</code><code>/usr/lib/jvm/java-8-oracle</code>

  分别在master和slave節點上執行以下操作,編輯".bashrc"檔案,添加如下幾行:

<code>$ vim .bashrc   </code><code>//</code><code>編輯檔案,添加如下幾行</code>

<code>export</code> <code>HADOOP_HOME=</code><code>/home/hduser/hadoop</code>

<code>export</code> <code>JAVA_HOME=</code><code>/usr/lib/jvm/java-8-oracle</code>

<code>PATH=$PATH:$HADOOP_HOME</code><code>/bin</code><code>:$HADOOP_HOME</code><code>/sbin</code>

<code>$ </code><code>source</code>  <code>.bashrc   </code><code>//source</code><code>使其立即生效</code>

  分别在master和slave節點上執行以下操作,更改Hadoop-env的JAVA_HOME:

<code>$ vim </code><code>/home/hduser/hadoop/etc/hadoop/hadoop-env</code><code>.sh</code>

<code>#export JAVA_HOME=${JAVA_HOME}   //更改此行,或者注釋掉新加以下一行</code>

7.Hadoop配置

 Hadoop的配置這裡主要涉及四個配置檔案:etc/hadoop/core-site.xml,etc/hadoop/hdfs-site.xml, etc/hadoop/yarn-site.xml and etc/hadoop/mapred-site.xml.

  這裡摘錄網絡上的一段話,在繼續下面的操作之前一定要閱讀這段,以便更好的了解:

Hadoop Distributed File System: A distributed file system that provides high-throughput access to application data. A HDFS cluster primarily consists of a NameNode that manages the file system metadata and DataNodes that store the actual data. If you compare HDFS to a traditional storage structures ( e.g. FAT, NTFS), then NameNode is analogous to a Directory Node structure, and DataNode is analogous to actual file storage blocks.

Hadoop YARN: A framework for job scheduling and cluster resource management.

Hadoop MapReduce: A YARN-based system for parallel processing of large data sets.

①在master和slave節點上更改"core-site.xml"檔案,master和slave節點應該使用相同"fs.defaultFS"值,而且必須指向master節點;在“configuration”中間添加如下配置:

<code>&lt;property&gt;</code>

<code>  </code><code>&lt;name&gt;hadoop.tmp.</code><code>dir</code><code>&lt;</code><code>/name</code><code>&gt;</code>

<code>  </code><code>&lt;value&gt;</code><code>/home/hduser/tmp</code><code>&lt;</code><code>/value</code><code>&gt;</code>

<code>  </code><code>&lt;description&gt;Temporary Directory.&lt;</code><code>/description</code><code>&gt;</code>

<code>&lt;</code><code>/property</code><code>&gt;</code>

<code>  </code><code>&lt;name&gt;fs.defaultFS&lt;</code><code>/name</code><code>&gt;</code>

<code>  </code><code>&lt;value&gt;hdfs:</code><code>//master</code><code>:54310&lt;</code><code>/value</code><code>&gt;</code>

<code>  </code><code>&lt;description&gt;Use HDFS as </code><code>file</code> <code>storage engine&lt;</code><code>/description</code><code>&gt;</code>

最終core-site.xml配置檔案如下圖所示:

<a href="https://s3.51cto.com/wyfs02/M00/8D/7F/wKioL1ie2-2SHfPcAAL8qN5RLB4952.png-wh_500x0-wm_3-wmp_4-s_2870729140.png" target="_blank"></a>

如果tmp目錄不存在,需要手動建立一個:

<code>$ </code><code>mkdir</code> <code>/home/hduser/tmp</code>

<code>$ </code><code>chown</code> <code>-R hduser:hdgroup </code><code>/home/hduser/tmp</code> <code>//</code><code>非hduser使用者建立虛賦權</code>

②隻在master節點上更改"mapred-site.xml"檔案,由于沒有這個檔案,需要需要複制那個template檔案生成一個:

<code>$ </code><code>cd</code> <code>/home/hduser/hadoop/</code>

<code>$ </code><code>cp</code> <code>-av etc</code><code>/hadoop/mapred-site</code><code>.xml.template etc</code><code>/hadoop/mapred-site</code><code>.xml</code>

編輯xml配置檔案,在“configuration”中間添加如下配置:

<code> </code><code>&lt;name&gt;mapreduce.jobtracker.address&lt;</code><code>/name</code><code>&gt;</code>

<code> </code><code>&lt;value&gt;master:54311&lt;</code><code>/value</code><code>&gt;</code>

<code> </code><code>&lt;description&gt;The host and port that the MapReduce job tracker runs</code>

<code>  </code><code>at. If “</code><code>local</code><code>”, </code><code>then</code> <code>jobs are run </code><code>in</code><code>-process as a single map</code>

<code>  </code><code>and reduce task.</code>

<code>&lt;</code><code>/description</code><code>&gt;</code>

<code> </code><code>&lt;name&gt;mapreduce.framework.name&lt;</code><code>/name</code><code>&gt;</code>

<code> </code><code>&lt;value&gt;yarn&lt;</code><code>/value</code><code>&gt;</code>

<code> </code><code>&lt;description&gt;The framework </code><code>for</code> <code>running mapreduce jobs&lt;</code><code>/description</code><code>&gt;</code>

③在master和slave節點上更改"hdfs-site.xml"檔案,在“configuration”中間添加如下配置:

<code> </code><code>&lt;name&gt;dfs.replication&lt;</code><code>/name</code><code>&gt;</code>

<code> </code><code>&lt;value&gt;2&lt;</code><code>/value</code><code>&gt;</code>

<code> </code><code>&lt;description&gt;Default block replication.</code>

<code>  </code><code>The actual number of replications can be specified when the </code><code>file</code> <code>is created.</code>

<code>  </code><code>The default is used </code><code>if</code> <code>replication is not specified </code><code>in</code> <code>create </code><code>time</code><code>.</code>

<code> </code><code>&lt;</code><code>/description</code><code>&gt;</code>

<code> </code><code>&lt;name&gt;dfs.namenode.name.</code><code>dir</code><code>&lt;</code><code>/name</code><code>&gt;</code>

<code> </code><code>&lt;value&gt;</code><code>/data/hduser/hdfs/namenode</code><code>&lt;</code><code>/value</code><code>&gt;</code>

<code> </code><code>&lt;description&gt;Determines where on the </code><code>local</code> <code>filesystem the DFS name node should store the name table(fsimage). If this is a comma-delimited list of directories </code><code>then</code> <code>the name table is replicated </code><code>in</code> <code>all of the directories, </code><code>for</code> <code>redundancy.</code>

<code> </code><code>&lt;name&gt;dfs.datanode.data.</code><code>dir</code><code>&lt;</code><code>/name</code><code>&gt;</code>

<code> </code><code>&lt;value&gt;</code><code>/data/hduser/hdfs/datanode</code><code>&lt;</code><code>/value</code><code>&gt;</code>

<code> </code><code>&lt;description&gt;Determines where on the </code><code>local</code> <code>filesystem an DFS data node should store its blocks. If this is a comma-delimited list of directories, </code><code>then</code> <code>data will be stored </code><code>in</code> <code>all named directories, typically on different devices. Directories that </code><code>do</code> <code>not exist are ignored.</code>

并建立剛才配置檔案中指定的目錄:

<code>$ </code><code>mkdir</code> <code>-p </code><code>/home/hduser/data/hduser/hdfs/</code><code>{namenode,datanode}</code>

<code>$ </code><code>chown</code> <code>-R hduser:hdgroup </code><code>/home/hduser/data/</code>  <code>//</code><code>如果非hduser使用者建立的需要賦權</code>

1).這裡dfs.replication的預設值為3,這裡我設定了2個副本,代表每個存儲到HDFS的檔案都有額外一個副本,這裡的值可以視叢集規模而定。

2)dfs.namenode.name.dir和dfs.datanode.name.dir是namenode和datanode存放HDFS資料塊檔案的位置,如果沒有需要手動建立。

④在master和slave節點上更改"yarn-site.xml"檔案,master和slave節點應該使用相同的值,并且指向master節點。在“configuration”中間添加如下配置:

<code> </code><code>&lt;name&gt;yarn.nodemanager.aux-services&lt;</code><code>/name</code><code>&gt;</code>

<code> </code><code>&lt;value&gt;mapreduce_shuffle&lt;</code><code>/value</code><code>&gt;</code>

<code> </code><code>&lt;name&gt;yarn.resourcemanager.scheduler.address&lt;</code><code>/name</code><code>&gt;</code>

<code> </code><code>&lt;value&gt;master:8030&lt;</code><code>/value</code><code>&gt;</code>

<code> </code><code>&lt;name&gt;yarn.resourcemanager.address&lt;</code><code>/name</code><code>&gt;</code>

<code> </code><code>&lt;value&gt;master:8032&lt;</code><code>/value</code><code>&gt;</code>

<code>  </code><code>&lt;name&gt;yarn.resourcemanager.webapp.address&lt;</code><code>/name</code><code>&gt;</code>

<code>  </code><code>&lt;value&gt;master:8088&lt;</code><code>/value</code><code>&gt;</code>

<code>  </code><code>&lt;name&gt;yarn.resourcemanager.resource-tracker.address&lt;</code><code>/name</code><code>&gt;</code>

<code>  </code><code>&lt;value&gt;master:8031&lt;</code><code>/value</code><code>&gt;</code>

<code>  </code><code>&lt;name&gt;yarn.resourcemanager.admin.address&lt;</code><code>/name</code><code>&gt;</code>

<code>  </code><code>&lt;value&gt;master:8033&lt;</code><code>/value</code><code>&gt;</code>

⑤更新slave檔案

  在master節點上修改slave檔案,添加master和slave節點的主機名或者ip位址,并去掉"localhost":

<code>$ vim </code><code>/home/hduser/hadoop/etc/hadoop/slaves</code>

<code>master</code>

<code>slave-1</code>

<code>slave-2</code>

⑥格式化namenode:

  在啟動cluster之前需要先格式化namenode,在master上執行:

<code>$ hdfs namenode -</code><code>format</code>

看到類似提示INFO:"Storage directory /home/hduser/data/hduser/hdfs/namenode has been successfully formatted."表示格式化成功。

⑦啟動服務

  可以直接使用Hadoop提供的腳本"start-all.sh"啟動所有服務,也可以把dfs和yarn分别啟動。可以使用絕對路徑:/home/hduser/hadoop/sbin/start-all.sh,也可以直接調用start-all.sh腳本(因為前面已經改過PATH的路徑):

<code>$ start-all.sh</code>

如下圖所示沒有看到任何錯誤資訊,表示叢集已成功啟動:

<a href="https://s5.51cto.com/wyfs02/M01/8D/80/wKioL1ifAi6CHKv1AALFSxwfsBE953.png-wh_500x0-wm_3-wmp_4-s_646365465.png" target="_blank"></a>

⑧驗證檢視

  使用jps指令分别在master和slave上檢視啟動的服務

<a href="https://s4.51cto.com/wyfs02/M02/8D/80/wKioL1ifAuywLnUvAACUdLK8-wk339.png-wh_500x0-wm_3-wmp_4-s_1068650777.png" target="_blank"></a>

<a href="https://s1.51cto.com/wyfs02/M00/8D/80/wKioL1ifAwuBfAE7AABg8r7Ndd4766.png-wh_500x0-wm_3-wmp_4-s_556833938.png" target="_blank"></a>

  網頁驗證:

<a href="https://s1.51cto.com/wyfs02/M02/8D/80/wKioL1ifA_OxKaJlAAK5gJWhOxA586.png-wh_500x0-wm_3-wmp_4-s_2956240976.png" target="_blank"></a>

如果所有node均正常啟動,這裡能夠全部顯示:

<a href="https://s5.51cto.com/wyfs02/M02/8D/81/wKioL1ifBLewUbRbAAMYZLw9g7U228.png-wh_500x0-wm_3-wmp_4-s_552914878.png" target="_blank"></a>

  Hadoop解壓的share目錄裡給我們提供了幾個example的jar包,我們執行一個看下效果:

<code>$ hadoop jar </code><code>/home/hduser/hadoop/share/hadoop/mapreduce/hadoop-mapreduce-examples-2</code><code>.7.3.jar  pi 30 100</code>

能夠看到目前執行的任務:

<a href="https://s5.51cto.com/wyfs02/M02/8D/83/wKiom1ifBniTbJrxAAKjzYi8iq4349.png-wh_500x0-wm_3-wmp_4-s_3750754622.png" target="_blank"></a>

寫在最後:

如果在新增了節點或者删除節點的時候出現了問題,首先删除slave的hadoop.tmp.dir,然後重新啟動試試看,如果還是不行,嘗試把Master的hadoop.tmp.dir删除(意味着dfs上的資料也會丢失)然後需要重新namenode –format。

如果有任何報錯資訊記得去查log日志,檔案位置在Hadoop的安裝目錄logs檔案夾下。

      本文轉自Jx戰壕  51CTO部落格,原文連結:http://blog.51cto.com/xujpxm/1896964,如需轉載請自行聯系原作者