P3768 簡單的數學題 題解
物理機環境
CPU: Intel® Xeon® CPU D-1581 @ 1.80GHz
記憶體: 64 GiB DDR3 Single-bit ECC
硬碟:HS-SSD-C2000Pro_2048G_30037121557
虛擬機器架構
hostname | IP |
---|---|
CentOS-01 | 10.10.10.31 |
CentOS-02 | 10.10.10.32 |
CentOS-03 | 10.10.10.33 |
安裝模板虛擬機器
主機名稱 CentOS-01、IP 地址 10.10.10.31、記憶體 4G、硬碟 100G、系統為CentOS8
使用 yum 安裝需要虛擬機器可以正常上網,yum 安裝前可以先測試下虛擬機器聯網情況
ping www.baidu.com
PING baidu.com (220.181.38.251) 56(84) bytes of data.
64 bytes from 220.181.38.251 (220.181.38.251): icmp_seq=1 ttl=50 time=40.7 ms
64 bytes from 220.181.38.251 (220.181.38.251): icmp_seq=2 ttl=50 time=40.7 ms
64 bytes from 220.181.38.251 (220.181.38.251): icmp_seq=3 ttl=50 time=40.0 ms
換國內源(以華為源為例)
sudo wget -O /etc/yum.repos.d/CentOS-Base.repo https://repo.huaweicloud.com/repository/conf/CentOS-8-reg.repo
安裝 epel-release
sudo yum install -y epel-release
關閉防火牆,關閉防火牆開機自啟
sudo systemctl stop firewalld
sudo systemctl disable firewalld.service
建立 owenxuan 使用者,並修改 owenxuan 使用者的密碼
sudo useradd owenxuan
sudo passwd owenxuan
配置 owenxaun 使用者具有 root 許可權,方便後期加 sudo 執行 root 許可權的命令
sudo vim /etc/sudoers
修改/etc/sudoers 檔案,在%wheel 這行下面新增一行,如下所示:
## Allow root to run any commands anywhere
root ALL=(ALL) ALL
## Allows people in group wheel to run all commands
%wheel ALL=(ALL) ALL
owenxuan ALL=(ALL) NOPASSWD:ALL
在/opt 目錄下建立資料夾,並修改所屬主和所屬組
在/opt 目錄下建立 module、software 資料夾
mkdir /opt/module
mkdir /opt/software
修改 module、software 資料夾的所有者和所屬組均為 owenxuan使用者
sudo chown owenxuan:owenxuan /opt/module
sudo chown owenxuan:owenxuan /opt/software
檢視 module、software 資料夾的所有者和所屬組
ll
總用量 4
drwxr-xr-x. 6 owenxuan owenxuan 80 12月 22 16:19 module
drwxr-xr-x. 2 owenxuan owenxuan 4096 12月 22 16:37 software
將ip地址設為靜態ip
vim /etc/sysconfig/network-scripts/ifcfg-enp1s0
修改/etc/sysconfig/network-scripts/ifcfg-enp1s0
新增MACADDR 並在路由器中設定靜態分配ip
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=dhcp
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
NAME=enp1s0
UUID=f2728000-045f-43f1-b2c3-4884e6a545f3
DEVICE=enp1s0
ONBOOT=yes
MACADDR=66:18:C7:F8:95:B9
安裝java8
使用官網提供的最新java8連結
-
登入oracle官網 Java下載地址
-
註冊oracle賬號 下載 x86 Compressed Archive
使用FinalShell 將 JDK 匯入到 opt 目錄下面的 software 資料夾下面
在 Linux 系統下的 opt 目錄中檢視軟體包是否匯入成功
ll /opt/software/
總用量 143360
-rw-rw-r--. 1 owenxuan owenxuan 146799982 12月 22 11:16 jdk-8u311-linux-x64.tar.gz
解壓 JDK 到/opt/module 目錄下
cd /opt/software/
tar -zxvf jdk-8u311-linux-x64.tar.gz -C ../module/
將JDK更名
cd ../module/
mv jdk1.8.0_311/ jdk
配置 JDK 環境變數
新建/etc/profile.d/my_env.sh 檔案
sudo vim /etc/profile.d/my_env.sh
新增如下內容
#JAVA_HOME
export JAVA_HOME=/opt/module/jdk
export PATH=$PATH:$JAVA_HOME/bin
儲存後退出
source 一下/etc/profile 檔案,讓新的環境變數 PATH 生效
source /etc/profile
測試 JDK 是否安裝成功
java -version
如果能看到以下結果,則代表 Java 安裝成功。
java version "1.8.0_311"
Java(TM) SE Runtime Environment (build 1.8.0_311-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.311-b11, mixed mode)
安裝 Hadoop
使用FinalShell 將 Hadoop匯入到 opt 目錄下面的 software 資料夾下面
在 Linux 系統下的 opt 目錄中檢視軟體包是否匯入成功
ll /opt/software
總用量 734364
-rw-rw-r--. 1 owenxuan owenxuan 605187279 12月 24 10:51 hadoop-3.3.1.tar.gz
解壓 Hadoop到/opt/module 目錄下
cd /opt/software/
tar -zxvf hadoop-3.3.1.tar.gz -C ../module/
將Hadoop更名
cd ../module/
mv hadoop-3.3.1/ hadoop
配置 Hadoop環境變數
新建/etc/profile.d/my_env.sh 檔案
sudo vim /etc/profile.d/my_env.sh
新增如下內容
#HADOOP_HOME
export HADOOP_HOME=/opt/module/hadoop
export PATH=$PATH:$HADOOP_HOME/bin
export PATH=$PATH:$HADOOP_HOME/sbin
儲存後退出
source 一下/etc/profile 檔案,讓新的環境變數 PATH 生效
source /etc/profile
測試 JDK 是否安裝成功
hadoop version
如果能看到以下結果,則代表 Java 安裝成功。
Hadoop 3.3.1
Source code repository https://github.com/apache/hadoop.git -r a3b9c37a397ad4188041dd80621bdeefc46885f2
Compiled by ubuntu on 2021-06-15T05:13Z
Compiled with protoc 3.7.1
From source with checksum 88a4ddb2299aca054416d6b7f81ca55
This command was run using /opt/module/hadoop/share/hadoop/common/hadoop-common-3.3.1.jar
叢集配置
叢集部署規劃
-
NameNode 和 SecondaryNameNode 不要安裝在同一臺伺服器
-
ResourceManager 也很消耗記憶體,不要和 NameNode、SecondaryNameNode 配置在 同一臺機器上。
CentOS-01 | CentOS-02 | CentOS-03 | |
---|---|---|---|
HDFS | NameNode DataNode | DataNode | SecondaryNameNode DataNode |
YARN | NodeManager | ResourceManager NodeManage | NodeManage |
配置叢集
進入Hadoop/etc/hadoop目錄
cd $HADOOP_HOME/etc/hadoop
配置 core-site.xml
<?xml version="1.0" encoding="UTF-8"?>
<?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>
<!-- 指定 NameNode 的地址 -->
<property>
<name>fs.defaultFS</name>
<value>hdfs://10.10.10.31:8020</value>
</property>
<!-- 指定 hadoop 資料的儲存目錄 -->
<property>
<name>hadoop.tmp.dir</name>
<value>/opt/module/hadoop/data</value>
</property>
<!-- 配置 HDFS 網頁登入使用的靜態使用者為 owenxuan -->
<property>
<name>hadoop.http.staticuser.user</name>
<value>owenxuan</value>
</property>
<property>
<name>hadoop.proxyuser.owenxuan.hosts</name>
<value>*</value>
</property>
<property>
<name>hadoop.proxyuser.owenxuan.groups</name>
<value>*</value>
</property>
</configuration>
配置 hdfs-site.xml
<?xml version="1.0" encoding="UTF-8"?>
<?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>
<!-- nn web 端訪問地址-->
<property>
<name>dfs.namenode.http-address</name>
<value>10.10.10.31:9870</value>
</property>
<!-- 2nn web 端訪問地址-->
<property>
<name>dfs.namenode.secondary.http-address</name>
<value>10.10.10.33:9868</value>
</property>
</configuration>
配置 yarn-site.xml
<?xml version="1.0"?>
<!--
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.
-->
<configuration>
<!-- 指定 MR 走 shuffle -->
<property>
<name>yarn.nodemanager.aux-services</name>
<value>mapreduce_shuffle</value>
</property>
<!-- 指定 ResourceManager 的地址-->
<property>
<name>yarn.resourcemanager.hostname</name>
<value>10.10.10.32</value>
</property>
<property>
<name>yarn.application.classpath</name>
<value>/opt/module/hadoop/etc/hadoop:/opt/module/hadoop/share/hadoop/common/lib/*:/opt/module/hadoop/share/hadoop/common/*:/opt/module/hadoop/share/hadoop/hdfs:/opt/module/hadoop/share/hadoop/hdfs/lib/*:/opt/module/hadoop/share/hadoop/hdfs/*:/opt/module/hadoop/share/hadoop/mapreduce/*:/opt/module/hadoop/share/hadoop/yarn:/opt/module/hadoop/share/hadoop/yarn/lib/*:/opt/module/hadoop/share/hadoop/yarn/*</value>
</property>
<!-- 開啟日誌聚集功能 -->
<property>
<name>yarn.log-aggregation-enable</name>
<value>true</value>
</property>
<!-- 設定日誌聚集伺服器地址 -->
<property>
<name>yarn.log.server.url</name>
<value>http://10.10.10.31:19888/jobhistory/logs</value>
</property>
<!-- 設定日誌保留時間為 7 天 -->
<property>
<name>yarn.log-aggregation.retain-seconds</name>
<value>604800</value>
</property>
</configuration>
配置 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>
<!-- 指定 MapReduce 程式執行在 Yarn 上 -->
<property>
<name>mapreduce.framework.name</name>
<value>yarn</value>
</property>
<!-- 歷史伺服器端地址 -->
<property>
<name>mapreduce.jobhistory.address</name>
<value>10.10.10.31:10020</value>
</property>
<!-- 歷史伺服器 web 端地址 -->
<property>
<name>mapreduce.jobhistory.webapp.address</name>
<value>10.10.10.31:19888</value>
</property>
</configuration>
複製虛擬機器
修改新虛擬機器hostname
hostnamectl set-hostname CentOS-02
hostnamectl set-hostname CentOS-03
修改新虛擬機器網絡卡MAC地址
vim /etc/sysconfig/network-scripts/ifcfg-enp1s0
修改網絡卡MAC地址
可隨機更換,保證不重複即可
在路由器設定靜態獲取IP
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=dhcp
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
NAME=enp1s0
UUID=f2728000-045f-43f1-b2c3-4884e6a545f3
DEVICE=enp1s0
ONBOOT=yes
MACADDR=66:18:C7:F8:95:B9
重啟所有虛擬機器
SSH 無密登入配置
在/home/owenxuan目錄下新建.ssh資料夾
mkdir ~/.ssh
生成公鑰和私鑰
cd ~/.ssh/
ssh-keygen -t rsa
一直按回車
直到出現
+---[RSA 3072]----+
|BB+*++. . |
|*+=+=. o . |
|B.=o.. = |
|.+ +. + . |
|. . o . S |
|.E . * . .. . |
|. . B . .+ |
| = o.. |
| .. .oo |
+----[SHA256]-----+
將公鑰拷貝到要免密登入的目標機器上
ssh-copy-id 10.10.10.31
ssh-copy-id 10.10.10.32
ssh-copy-id 10.10.10.33
在CentOS-02和CentOS-03執行同樣的操作
啟動叢集
如果叢集是第一次啟動
需要在 CentOS-01 節點格式化 NameNode(注意:格式化 NameNode,會產生新的叢集 id,導致 NameNode 和 DataNode 的叢集 id 不一致,叢集找不到已往資料。如果叢集在執行過程中報錯,需要重新格式化 NameNode 的話,一定要先停止 namenode 和 datanode 程序,並且要刪除所有機器的 data 和 logs 目錄,然後再進行格式化。)
cd /opt/module/hadoop/
hdfs namenode -format
啟動 HDFS
sbin/start-dfs.sh
在配置了 ResourceManager 的節點(CentOS-02)啟動 YARN
sbin/start-yarn.sh
啟動 HistoryServer
mapred --daemon start historyserver
Web 端檢視 HDFS 的 NameNode
- 瀏覽器中輸入:http://10.10.10.31:9870
- 檢視 HDFS 上儲存的資料資訊
Web 端檢視 YARN 的 ResourceManager
- 瀏覽器中輸入:http://10.10.10.32:8088
- 檢視 YARN 上執行的 Job 資訊
叢集基本測試
上傳檔案到叢集
hadoop fs -mkdir /input
hadoop fs -put $HADOOP_HOME/wcinput/word.txt /input
執行 wordcount 程式
hadoop jar share/hadoop/mapreduce/hadoop-mapreduce-examples-3.3.1.jar wordcount /input /output
編寫 Hadoop 叢集常用指令碼
Hadoop 叢集啟停指令碼(包含 HDFS,Yarn,Historyserver):myhadoop.sh
mkdir bin && cd bin
vim myhadoop.sh
輸入如下內容
#!/bin/bash
if [ # -lt 1 ]
then
echo "No Args Input..."
exit ;
fi
case1 in
"start")
echo " =================== 啟動 hadoop 叢集 ==================="
echo " --------------- 啟動 hdfs ---------------"
ssh 10.10.10.31 "/opt/module/hadoop/sbin/start-dfs.sh"
echo " --------------- 啟動 yarn ---------------"
ssh 10.10.10.32 "/opt/module/hadoop/sbin/start-yarn.sh"
echo " --------------- 啟動 historyserver ---------------"
ssh 10.10.10.31 "/opt/module/hadoop-3.1.3/bin/mapred --daemon start historyserver"
;;
"stop")
echo " =================== 關閉 hadoop 叢集 ==================="
echo " --------------- 關閉 historyserver ---------------"
ssh 10.10.10.31 "/opt/module/hadoop/bin/mapred --daemon stop historyserver"
echo " --------------- 關閉 yarn ---------------"
ssh 10.10.10.32 "/opt/module/hadoop/sbin/stop-yarn.sh"
echo " --------------- 關閉 hdfs ---------------"
ssh 10.10.10.31 "/opt/module/hadoop/sbin/stop-dfs.sh"
;;
*)
echo "Input Args Error..."
;;
esa
儲存後退出,然後賦予指令碼執行許可權
chmod +x myhadoop.sh
測試指令碼
啟動叢集
myhadoop.sh start
關閉叢集
myhadoop.sh stop
檢視三臺伺服器 Java 程序指令碼:jpsall
cd bin
vim jpsall
輸入如下內容
#!/bin/bash
for host in 10.10.10.31 10.10.10.32 10.10.10.33
do
echo =============== $host ===============
ssh $host jps
don
儲存後退出,然後賦予指令碼執行許可權
chmod +x jpsall
測試指令碼
jpsall
叢集分發指令碼:xsync
cd bin
vim xsync
輸入如下內容
#!/bin/bash
#1. 判斷引數個數
if [ $# -lt 1 ]
then
echo Not Enough Arguement!
exit;
fi
#2. 遍歷叢集所有機器
for host in 10.10.10.31 10.10.10.32 10.10.10.33
do
echo ==================== $host ====================
#3. 遍歷所有目錄,挨個傳送
for file in $@
do
#4. 判斷檔案是否存在
if [ -e $file ]
then
#5. 獲取父目錄
pdir=$(cd -P $(dirname $file); pwd)
#6. 獲取當前檔案的名稱
fname=$(basename $file)
ssh $host "mkdir -p $pdir"
rsync -av $pdir/$fname $host:$pdir
else
echo $file does not exists!
fi
done
don
儲存後退出,然後賦予指令碼執行許可權
chmod +x xsync
測試指令碼
xsync /home/owenxuan/bin
在其他機器上檢視~/目錄下是否存在bin目錄