Docker配置Hadoop集羣並使用WordCount測試


本人由於畢業設計選題,開始接觸Hadoop,預期搭建一個具有分佈式存儲與高性能處理數據的框架,技術選型初步採用Docker進行分佈式集羣搭建,並且使用Docker封裝出一個靈活添加使用的算法層, 環境搭建中踩坑多次,特此記錄分享,有需求的小夥伴可以參考一下。

操作系統:Ubuntu20.04 默認Docker環境已經搭建完成

製作鏡像

1、拉取ubuntu鏡像

首先拉取一個ubuntu最新的鏡像作爲基礎映像docker pull ubuntu:latest,我這裏已經拉取過了,如果第一次pull會開始下載。下載後可以看一下docker images已經有一個ubuntu版本號爲latest的了。
圖1

2、使用Dockerfile構建包含jdk的ubuntu鏡像

去jdk官網下載jdk包。我下載的jdk1.8 jdk-8u281-linux-x64.tar.gz,在下載好的tar.gz文件所在的目錄,新建一個Dockerfile文件,並進入編輯狀態

vim Dockerfile

在這裏插入圖片描述
輸入下面的文件內容,其中jdk的版本號要根據你下載的版本適當修改:

FROM ubuntu:latest
MAINTAINER duanmu
ADD jdk-8u281-linux-x64.tar.gz /usr/local/
ENV JAVA_HOME /usr/local/jdk1.8.0_281
ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
ENV PATH $PATH:$JAVA_HOME/bin

編輯好後保存,開始build鏡像

docker build -t jdk-20210127 .

構建一個包含jdk的ubuntu鏡像命名爲jdk-20210127 ,注意最後的“.”

3、進入映像

新建一個以jdk-20210127爲基礎鏡像的容器命名爲ubuntu_hadoop並指定容器的hostname爲charlie,並進入容器。

docker run -it --name=ubuntu_hadoop -h charlie jdk-20210127

下圖爲進入容器後:在這裏插入圖片描述
此時輸入

java -version

已經可以顯示jdk的版本號了。

在這裏插入圖片描述

4、升級apt-get

apt-get update

5、安裝vim

apt-get install vim

6、更新apt-get鏡像源

默認的apt-get下載源速度太慢,更換下載源可以提升速度,進入下載源列表文件,按a進入insert模式

vim /etc/apt/sources.list

將其中內容全部替換爲以下內容

deb-src http://archive.ubuntu.com/ubuntu focal main restricted #Added by software-properties
deb http://mirrors.aliyun.com/ubuntu/ focal main restricted
deb-src http://mirrors.aliyun.com/ubuntu/ focal main restricted multiverse universe #Added by software-properties
deb http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted
deb-src http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted multiverse universe #Added by software-properties
deb http://mirrors.aliyun.com/ubuntu/ focal universe
deb http://mirrors.aliyun.com/ubuntu/ focal-updates universe
deb http://mirrors.aliyun.com/ubuntu/ focal multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-updates multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse #Added by software-properties
deb http://archive.canonical.com/ubuntu focal partner
deb-src http://archive.canonical.com/ubuntu focal partner
deb http://mirrors.aliyun.com/ubuntu/ focal-security main restricted
deb-src http://mirrors.aliyun.com/ubuntu/ focal-security main restricted multiverse universe #Added by software-properties
deb http://mirrors.aliyun.com/ubuntu/ focal-security universe
deb http://mirrors.aliyun.com/ubuntu/ focal-security multiverse

其中focal是ubuntu20.04的版本號,其他版本諸如xenial等修改版本號即可。

7、重新升級apt-get

apt-get update

8、安裝wget

apt-get install wget

9、創建並進入安裝hadoop的文件目錄

mkdir -p soft/apache/hadoop/
cd soft/apache/hadoop

10、通過wget下載hadoop安裝包

wget http://mirrors.ustc.edu.cn/apache/hadoop/common/hadoop-3.3.0/hadoop-3.3.0.tar.gz

本人使用的是比較新的hadoop3.3.0版本,其他版本也可以訪問這個網站的目錄樹去找。(3.3.0在後續配置中有一步驟略有差別,之後會提。)

11、解壓hadoop

tar -xvzf Hadoop-3.3.0.tar.gz

12、配置環境變量並重啓配置文件

vim ~/.bashrc

新增以下環境變量:

export JAVA_HOME=/usr/local/jdk1.8.0_281
export HADOOP_HOME=/soft/apache/hadoop/hadoop-3.3.0
export HADOOP_CONFIG_HOME=$HADOOP_HOME/etc/hadoop
export PATH=$PATH:$HADOOP_HOME/bin
export PATH=$PATH:$HADOOP_HOME/sbin

並重啓配置文件

source ~/.bashrc

13、創建文件夾並修改配置文件

cd $HADOOP_HOME
mkdir tmp
mkdir namenode
mkdir datanode

修改配置文件:

cd $HADOOP_CONFIG_HOME
vim core-site.xml

將下面內容替換:

<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<!--
  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License. See accompanying LICENSE file.
-->

<!-- Put site-specific property overrides in this file. -->
<configuration>
<property>
        <name>hadoop.tmp.dir</name>
        <value>/soft/apache/hadoop/hadoop-3.3.0/tmp</value>
</property>
<property>
        <name>fs.default.name</name>
        <value>hdfs://master:9000</value>
        <final>true</final>
</property>
</configuration>

更改hdfs-site.xml

vim hdfs-site.xml

用下面配置替換:

<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<!--
  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License. See accompanying LICENSE file.
-->

<!-- Put site-specific property overrides in this file. -->

<configuration>
<property>
        <name>dfs.replication</name>
        <value>2</value>
        <final>true</final>
</property>
<property>
        <name>dfs.namenode.name.dir</name>
        <value>/soft/apache/hadoop/hadoop-3.3.0/namenode</value>
        <final>true</final>
</property>
<property>
        <name>dfs.datanode.name.dir</name>
        <value>/soft/apache/hadoop/hadoop-3.3.0/datanode</value>
        <final>true</final>
</property>
</configuration>

接下來

cp marred-site.xml.template marred-site.xml
vim mapred-site.xml
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<!--
  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License. See accompanying LICENSE file.
-->

<!-- Put site-specific property overrides in this file. -->

<configuration>
<property>
        <name>mapred.job.tarcker</name>
        <value>master:9001</value>
</property>
</configuration>

14、修改hadoop環境變量

在hadoop的安裝目錄下,找到hadoop-env,sh文件

vim hadoop-env.sh

在最後添加

export JAVA_HOME=/usr/local/jdk1.8.0_281

刷新

hadoop namenode -format

15、安裝SSH

hadoop的環境必須滿足ssh免密登陸,先安裝ssh

apt-get install net-tools
apt-get install ssh

創建sshd目錄

mkdir -p ~/var/run/sshd

生成訪問密鑰

cd ~/
ssh-keygen -t rsa -P '' -f ~/.ssh/id_dsa
cd .ssh
cat id_dsa.pub >> authorized_keys

這一步驟提示安裝路徑與設置密碼時全布直接按回車即可設置成免密。

修改ssh配置

vim /etc/ssh/ssh_config

添加,將下面這句話直接添加即可,也可以在文件中找到被註釋的這句話去修改。

StrictHostKeyChecking no #將ask改爲no
vim etc/ssh/sshd_config

在末尾添加:

#禁用密碼驗證
PasswordAuthentication no
#啓用密鑰驗證
RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys

最後使用下面語句測試是否免密登陸,

ssh localhost

出現下圖即免密配置成功。
在這裏插入圖片描述
最後在在高版本hadoop配置過程中,最後啓動時常常報如下的錯:
在這裏插入圖片描述爲了避免踩坑,先提前設置,進入環境變量


vim /etc/profile

增加如下內容並保存:

export HDFS_NAMENODE_USER=root
export HDFS_DATANODE_USER=root
export HDFS_SECONDARYNAMENODE_USER=root
export YARN_RESOURCEMANAGER_USER=root
export YARN_NODEMANAGER_USER=root

使配置生效

source /etc/profile

16、導出鏡像

至此鏡像已經配置完成,退出容器,將配置好的鏡像保存,其中xxxx爲剛剛操作的容器的id,可以使用docker ps -a查看

docker commit xxxx ubuntu:hadoop

此時ubuntu_hadoop就是最終配置好的包含hadoop的鏡像。

17、集羣測試

依次構建並啓動三個以剛剛生成的鏡像爲基本鏡像的容器,依次命名爲master 、slave1、slave2,並將master做端口映射(提示:容器要處於運行狀態,生成容器後使用ctrl+P+Q退出可以使容器保持後臺運行。)

docker run -it  -h master --name=master -p 9870:9870 -p 8088:8088 ubuntu:hadoop 
docker run -it  -h slave1 --name=slave1 ubuntu:hadoop 
docker run -it  -h slave2 --name=slave2 ubuntu:hadoop 

修改每個容器的host文件
對matser、slave1、slave2裏的host文件,分別加入其他兩個容器的ip

vim /etc/hosts
127.0.0.1       localhost
::1     localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.2      master
172.17.0.3      slave1
172.17.0.4      slave2

修改master中slaves文件

注意,在hadoop3.3.0版本中並不是修改slaves文件,而是修改workers文件。此處爲3.3.0版本的一些變化。
老版本(自行查找hadoop版本中已存在文件是slaves還是iworkers)

cd $HADOOP_CONFIG_HOME/
vim slaves 

3.3.0

cd $HADOOP_CONFIG_HOME/
vim workers

將其他兩個節點名稱加入文件

slave1
slave2

啓動hadoop

start-all.sh

出現下圖即爲配置成功,
在這裏插入圖片描述此時可以訪問localhost:9870和localhost:8088 ,去監控集羣運行狀態了,如下圖
在這裏插入圖片描述

在這裏插入圖片描述

18、使用wordCount測試集羣

進入hadoop目錄,查看一下所有文件,以文件中的LICENSE.txt爲輸入文件,來統計其中單詞出現頻率作爲測試。

cd $HADOOP_HOME
ll
首先在HDFS文件存儲系統中新建一個inputs文件夾。
hadoop fs -mkdir /input 

可以使用下面命令看到文件夾創建成功

hadoop fs -ls / 

把license.txt放進input文件夾

hadoop fs -put LICENSE.txt /input 

查看已經放入

hadoop fs -ls /input

使用示例程序進行統計(mapreduce示例包不同版本路徑不同需要自行查找,下面是3.3.0版本)

hadoop jar /soft/apache/hadoop/hadoop-3.3.0/share/hadoop/mapreduce/hadoop-mapreduce-examples-3.3.0.jar wordcount /input /output

在這裏插入圖片描述

運行後查看文件夾,發現多了一個output文件夾,打開output文件夾,多了兩個文件,_SUCCESS和part-r-00000,說明運行成功。

hadoop fs -ls /
hadoop fs -ls /output

在這裏插入圖片描述打開part -r-00000

hadoop fs -cat /output/part-r-00000

詞頻已經統計完畢了:
在這裏插入圖片描述

至此,已經完成了基本的Docker搭建hadoop分佈式集羣。並進行簡單的測試,接下來我也會在學習過程中持續更新,兄弟萌覺得有用點個贊奧!!!

																					20210129 duan

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章