Hadoop集群搭建常用命令、目录及注意

时间:2024-05-31 16:19:37

目录:(1)/usr/local/hadoop/(2)hadoop配置目录:/usr/local/hadoop/etc/hadoop(3)/usr/local/hadoop/sbin(4)share目录在/usr/local/hadoop下(5)将HDFS所有文件副本数调整为2,数据块大小为512MB:在/usr/local/hadoop/etc/hadoop/hdfs-site.xml文件中修改(6)将指定HDFS上某个文件(比如/home/hadoop/test.txt)的副本数调整成2:hadoop dfs -setrep -w 2 -R /home/hadoop/test.txt

命令:(1)①重新编译环境变量使配置生效,source /etc/profile

②开启namenode:hadoop-daemon.sh start namenode 关闭namenode:hadoop-daemon.sh stop namenode

③开启datanode:hadoop-daemon.sh start datanode 关闭datanode:hadoop-daemon.sh stop datanode

④开启hdfs:start-dfs.sh 关闭hdfs:stop-dfs.sh

⑤开启yarn:start-yarn.sh 关闭yarn:stop-yarn.sh

(2)hadoop完全分布式 HA高可用搭建过程:

1、使用hadoop用户解压并安装到apps路径下
1.1使用hadoop用户进入到在/home/hadoop/apps目录下
cd /home/hadoop/apps

注意:如果没有/home/hadoop/apps路径,自行在/home/hadoop路径下创建apps文件夹:mkdir /home/Hadoop/apps
1.2使用rz将本机的hadoop安装包上传到/home/hadoop/apps目录下
1.3解压安装文件
tar -zxvf hadoop-2.7.4.tar.gz
1.4使用root用户创建软链接
ln -s /home/hadoop/apps/hadoop-2.7.4 /usr/local/hadoop
1.5使用root用户修改软链接属主
chown -R hadoop:hadoop /usr/local/hadoop


1.6使用root用户将hadoop相关内容添加到环境变量中
注意:Hadoop配置文件路径是/usr/local/hadoop/etc/hadoop
vim /etc/profile
添加内容如下:
export HADOOP_HOME=/usr/local/hadoop
export HADOOP_CONF_DIR=$HADOOP_HOME/etc/hadoop
export YARN_HOME=$HADOOP_HOME
export YARN_CONF_DIR=$HADOOP_HOME/etc/hadoop
export PATH=$PATH:${HADOOP_HOME}/bin:${HADOOP_HOME}/sbin
1.7使用root用户重新编译环境变量使配置生效
source /etc/profile

2. 配置HDFS
2.1使用hadoop用户进入到Hadoop配置文件路径
     cd /usr/local/hadoop/etc/hadoop
2.2修改hadoop-env.sh
修改JDK路径export JAVA_HOME=/usr/local/jdk
2.3 配置core-site.xml(具体配置文件在最下面
2.4 配置hdfs-site.xml

3. 配置YARN
3.1 修改yarn-site.xml
3.2 修改mapred-site.xml(在配置mapred-site.xml时,千万注意文件名字一定是mapred-site.xml,不是mapred-site.xml.template,要用命令:cp mapred-site.xml.template mapred-site.xml复制一份然后在配置)
3.3 在/usr/local/hadoop路径下创建hdpdata文件夹
cd /usr/local/hadoop
mkdir hdpdata

4. 修改slaves文件,设置datanode和nodemanager启动节点主机名称
在slaves文件中添加节点的主机名称
node03
node04
node05

注意:node03,node04,node05是我的虚拟机主机名称,在这三台机器上启动datanode和nodemanager,同学根据自己集群主机名称情况自行修改。

5. 配置hadoop用户免密码登陆
配置node01到node01、node02、node03、node04、node05的免密码登陆
在node01上生产一对钥匙
ssh-****** -t rsa
将公钥拷贝到其他节点,包括自己本机
ssh-copy-id -i node01
ssh-copy-id -i node02
ssh-copy-id -i node03
ssh-copy-id -i node04
ssh-copy-id -i node05

配置node02到node01、node02、node03、node04、node05的免密码登陆
在node02上生产一对钥匙
ssh-****** -t rsa
将公钥拷贝到其他节点,包括自己本机
ssh-copy-id -i node01
ssh-copy-id -i node02
ssh-copy-id -i node03
ssh-copy-id -i node04
ssh-copy-id -i node05
注意:两个namenode之间要配置ssh免密码登陆

6. 将配置好的hadoop拷贝到其他节点
scp -r hadoop-2.7.4 [email protected]:/home/hadoop/apps
scp -r hadoop-2.7.4 [email protected]:/home/hadoop/apps
scp -r hadoop-2.7.4 [email protected]:/home/hadoop/apps
scp -r hadoop-2.7.4 [email protected]:/home/hadoop/apps

在每个节点分别执行如下四步操作
第一步:使用root用户创建软链接
ln -s /home/hadoop/apps/hadoop-2.7.4 /usr/local/hadoop
第二步:使用root用户修改软链接属主
chown -R hadoop:hadoop /usr/local/hadoop
第三步:使用root用户添加环境变量
vim /etc/profile
添加内容:
export HADOOP_HOME=/usr/local/hadoop
export HADOOP_CONF_DIR=$HADOOP_HOME/etc/hadoop
export YARN_HOME=$HADOOP_HOME
export YARN_CONF_DIR=$HADOOP_HOME/etc/hadoop

export PATH=$PATH:${HADOOP_HOME}/bin:${HADOOP_HOME}/sbin
第四步:使用root用户重新编译环境变量使配置生效
source /etc/profile

(3)集群规划

Hadoop集群搭建常用命令、目录及注意Hadoop集群搭建常用命令、目录及注意

(4)启动hadoop集群命令(注意使用hadoop用户启动,严格按照顺序启动):

①启动journalnode,在node03,node04,node05机器上,输入/usr/local/hadoop/sbin/hadoop-daemon.sh start journalnode 然后输入jps命令检验多了journalnode进程

②格式化HDFS,在node01上执行命令:hdfs namenode -format,格式化成功之后会在core-site.xml中的hadoop.tmp.dir制定的路径下生成dfs文件夹,将该文件夹拷贝到node02的相同路径下,scp -r hdpdata [email protected]:/usr/local/hadoop

③在node01上执行格式化ZKFC操作:hdfs zkfc -formatZK,执行成功后,日志输出如下信息:INFO ha.ActiveStandbyElector: Successfully created /hadoop-ha/ns in ZK

④在node01上启动HDFS:sbin/start-dfs.sh

⑤在node02上启动YARN:sbin/start-yarn.sh,在node01单独启动一个ResourceManager作为备份节点ResourceManager(Standby):sbin/yarn-daemon.sh start resourcemanager

⑥在node02上启动JobHistoryServer:sbin/mr-jobhistory-daemon.sh start historyserver,启动完成node02会增加一个JobHistoryServer进程

⑦hadoop安装启动完成

HDFS HTTP访问地址:

NameNode (active):http://192.168.183.100:50070
NameNode (standby):http://192.168.183.101:50070

ResourceManager HTTP访问地址:

ResourceManager :http://192.168.183.101:8088

历史日志HTTP访问地址:

JobHistoryServer:http://192.168.183.101:19888

(5)集群验证

①验证HDFS是否正常工作及HA高可用

首先向hdfs上传一个文件:hadoop fs -put /usr/local/hadoop/README.txt /

在active节点手动关闭active的namenode:sbin/hadoop-daemon.sh stop namenode

通过HTTP 50070端口查看standby namenode的状态是否转换为active,手动启动上一步关闭的namenode:sbin/hadoop-daemon.sh start namenode

②验证YARN是否正常工作及ResourceManager HA高可用

运行测试hadoop提供的demo中的WordCount程序:

hadoop fs -mkdir /wordcount

hadoop fs -mkdir /wordcount/input

hadoop fs -mv /README.txt /wordcount/input

hadoop jar share/hadoop/mapreduce/hadoop-mapreduce/hadoop-mapreduce-examples-2.7.4.jar wordcount /wordcount/input /wordcount/output。share目录在/usr/local/hadoop下

验证ResourceManager HA

手动关闭node02的ResourceManager:sbin/hadoop-daemon.sh stop resourcemanager

通过HTTP 8088端口访问node01的ResourceManager查看状态

手动启动node02的ResourceManager:sbin/yarn-daemon.sh start resourcemanager

 

配置文件(1)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>
    <!-- 指定hdfs的nameservice名称空间为ns -->
    <property>
        <name>fs.defaultFS</name>
        <value>hdfs://ns</value>
    </property>
    <!-- 指定hadoop临时目录,默认在/tmp/{$user}目录下,不安全,每次开机都会被清空-->
    <property>
        <name>hadoop.tmp.dir</name>
        <value>/usr/local/hadoop/hdpdata/</value>
        <description>需要手动创建hdpdata目录</description>
    </property>
    <!-- 指定zookeeper地址 -->
    <property>
        <name>ha.zookeeper.quorum</name>
        <value>node01:2181,node02:2181,node03:2181</value>
        <description>zookeeper地址,多个用逗号隔开</description>
    </property>
</configuration>

 

(2)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>
    <!-- NameNode HA配置 -->
    <property>
        <name>dfs.nameservices</name>
        <value>ns</value>
        <description>指定hdfs的nameservice为ns,需要和core-site.xml中的保持一致</description>
    </property>
    <property>
        <name>dfs.ha.namenodes.ns</name>
        <value>nn1,nn2</value>
        <description>ns命名空间下有两个NameNode,逻辑代号,随便起名字,分别是nn1,nn2</description>
    </property>
    <property>
        <name>dfs.namenode.rpc-address.ns.nn1</name>
        <value>node01:9000</value>
        <description>nn1的RPC通信地址</description>
    </property>
    <property>
        <name>dfs.namenode.http-address.ns.nn1</name>
        <value>node01:50070</value>
        <description>nn1的http通信地址</description>
    </property>
    <property>
        <name>dfs.namenode.rpc-address.ns.nn2</name>
        <value>node02:9000</value>
        <description>nn2的RPC通信地址</description>
    </property>
    <property>
        <name>dfs.namenode.http-address.ns.nn2</name>
        <value>node02:50070</value>
        <description>nn2的http通信地址</description>
    </property>
    <!--JournalNode配置 -->
    <property>
        <name>dfs.namenode.shared.edits.dir</name>
        <value>qjournal://node03:8485;node04:8485;node05:8485/ns</value>
        <description>指定NameNode的edits元数据在JournalNode上的存放位置</description>
    </property>
    <property>
        <name>dfs.journalnode.edits.dir</name>
        <value>/usr/local/hadoop/journaldata</value>
        <description>指定JournalNode在本地磁盘存放数据的位置</description>
    </property>
    <!--namenode高可用主备切换配置 -->
    <property>
        <name>dfs.ha.automatic-failover.enabled</name>
        <value>true</value>
        <description>开启NameNode失败自动切换</description>
    </property>
    <property>
        <name>dfs.client.failover.proxy.provider.ns</name>
        <value>org.apache.hadoop.hdfs.server.namenode.ha.ConfiguredFailoverProxyProvider</value>
        <description>配置失败自动切换实现方式,使用内置的zkfc</description>
    </property>
    <property>
        <name>dfs.ha.fencing.methods</name>
        <value>
            sshfence
            shell(/bin/true)
        </value>
        <description>配置隔离机制,多个机制用换行分割,先执行sshfence,执行失败后执行shell(/bin/true),/bin/true会直接返回0表示成功</description>
    </property>
    <property>
        <name>dfs.ha.fencing.ssh.private-key-files</name>
        <value>/home/hadoop/.ssh/id_rsa</value>
        <description>使用sshfence隔离机制时需要ssh免登陆</description>
    </property>
    <property>
        <name>dfs.ha.fencing.ssh.connect-timeout</name>
        <value>30000</value>
        <description>配置sshfence隔离机制超时时间</description>
    </property>
    <!--dfs文件属性设置-->
    <property>
        <name>dfs.replication</name>
        <value>3</value>
        <description>设置block副本数为3</description>
    </property>

    <property>
        <name>dfs.block.size</name>
        <value>134217728</value>
        <description>设置block大小是128M</description>
    </property>
</configuration>

 

(3)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>mapreduce.framework.name</name>
        <value>yarn</value>
        <description>指定mr框架为yarn方式 </description>
    </property>
    <!-- 历史日志服务jobhistory相关配置 -->
    <property>
        <name>mapreduce.jobhistory.address</name>
        <value>node02:10020</value>
        <description>历史服务器端口号</description>
    </property>
    <property>
        <name>mapreduce.jobhistory.webapp.address</name>
        <value>node02:19888</value>
        <description>历史服务器的WEB UI端口号</description>
    </property>
    <property>
        <name>mapreduce.jobhistory.joblist.cache.size</name>
        <value>2000</value>
        <description>内存中缓存的historyfile文件信息(主要是job对应的文件目录)</description>
    </property>
</configuration>


(4)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>
    <!-- 开启RM高可用 -->
    <property>
        <name>yarn.resourcemanager.ha.enabled</name>
        <value>true</value>
    </property>
    <!-- 指定RM的cluster id,一组高可用的rm共同的逻辑id -->
    <property>
        <name>yarn.resourcemanager.cluster-id</name>
        <value>yarn-ha</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>node01</value>
    </property>
    <property>
        <name>yarn.resourcemanager.webapp.address.rm1</name>
        <value>${yarn.resourcemanager.hostname.rm1}:8088</value>
        <description>HTTP访问的端口号</description>
    </property>
    <property>
        <name>yarn.resourcemanager.hostname.rm2</name>
        <value>node02</value>
    </property>
    <property>
        <name>yarn.resourcemanager.webapp.address.rm2</name>
        <value>${yarn.resourcemanager.hostname.rm2}:8088</value>
    </property>
    <!-- 指定zookeeper集群地址 -->
    <property>
        <name>yarn.resourcemanager.zk-address</name>
        <value>node01:2181,node02:2181,node03:2181</value>
    </property>
    <!--NodeManager上运行的附属服务,需配置成mapreduce_shuffle,才可运行MapReduce程序-->
    <property>
        <name>yarn.nodemanager.aux-services</name>
        <value>mapreduce_shuffle</value>
    </property>
    <!-- 开启日志聚合 -->
    <property>
        <name>yarn.log-aggregation-enable</name>
        <value>true</value>
    </property>
    <!-- 日志聚合HDFS目录 -->
    <property>
        <name>yarn.nodemanager.remote-app-log-dir</name>
        <value>/data/hadoop/yarn-logs</value>
    </property>
    <!-- 日志保存时间3days,单位秒 -->
    <property>
        <name>yarn.log-aggregation.retain-seconds</name>
        <value>259200</value>
    </property>
</configuration>