一、简介 当今只要谈到大数据,自然想到Hadoop,以前Hadoop还只是个软件、系统,而如今更多代表的是一个大数据生态圈。 本文谈的 Hadoop 特指一个软件,它是 Apache 基金会的顶级项目之一,它本身主要解决了大数据领域的两大核心问题,如何存储(hdfs)、如何计算(mapreduce)。 官方文档:http://hadoop.apache.org 闲言少叙,下面进入正题。 二、部署环境的准备工作 1、服务器 本次选择使用4台物理机来搭建Hadoop集群,配置如下 Master CPU(24核) MEM(48G) DISK(2T*1 -> RAID10) Slave01 CPU(24核) MEM(12G) DISK(1T*4) Slave02 CPU(12核) MEM(24G) DISK(1T*4) Slave03 CPU(24核) MEM(16G) DISK(1T*4) # 还是挺惨的~ 2、系统选择的是 CentOS 6.9 x86_64 三、系统初始化 1、配置主机名、停用多余的服务、关闭SELinux、Iptables 2、修改最大打开文件描述符 shell > tail -4 /etc/security/limits.conf * - nofile 65536 # End of file 3、关闭透明大页 shell > tail -1 /etc/rc.local echo never > /sys/kernel/mm/redhat_transparent_hugepage/defrag 4、创建 Unix 用户账号 shell > useradd hadoop | echo hadoop | passwd --stdin hadoop # 所有服务器都需要创建 5、格式化硬盘 shell > mkdir /dfs && chown -R hadoop.hadoop /dfs # Master 执行 shell > mkdir -p /dfs/{disk1,disk2,disk3} shell > mkfs.ext4 /dev/sdb、/dev/sdc、/dev/sdd shell > tail -3 /etc/fstab /dev/sdb /dfs/disk1 ext4 defaults,noatime 0 0 /dev/sdc /dfs/disk2 ext4 defaults,noatime 0 0 /dev/sdd /dfs/disk3 ext4 defaults,noatime 0 0 shell > mount -a shell > chown -R hadoop.hadoop /dfs # 所有 Slave 都需要执行 6、时间同步 shell > crontab -l 0 */2 * * * /usr/sbin/ntpdate us.pool.ntp.org | /sbin/hwclock -w > /dev/null # 时间同步挺重要的, 上次遇到过由于时间不同步导致 HBase 的 RegionServer 无法启动 # init 6 四、搭建 Hadoop 集群 1、配置用户免密登陆 root shell > ssh-keygen # 生成密钥 root shell > ssh-copy-id -i ~/.ssh/id_rsa.pub slave01 root shell > ssh-copy-id -i ~/.ssh/id_rsa.pub slave02 root shell > ssh-copy-id -i ~/.ssh/id_rsa.pub slave03 hadoop shell > ssh-keygen # 生成密钥 hadoop shell > ssh-copy-id -i ~/.ssh/id_rsa.pub master hadoop shell > ssh-copy-id -i ~/.ssh/id_rsa.pub slave01 hadoop shell > ssh-copy-id -i ~/.ssh/id_rsa.pub slave02 hadoop shell > ssh-copy-id -i ~/.ssh/id_rsa.pub slave03 # Hadoop 控制脚本(不是 Hadoop 守护进程)依赖 SSH 来管理服务的启停。 # Tips: ssh-copy-id -i ~/.ssh/id_rsa.pub -p 22 root@slave01 2、安装 jdk shell > rpm -ivh jdk-8u161-linux-x64.rpm shell > ansible slave -m shell -a 'rpm -ivh jdk-8u161-linux-x64.rpm' shell > vim /etc/profile export JAVA_HOME=/usr/java/default shell > source /etc/profile shell > java -version java version "1.8.0_161" Java(TM) SE Runtime Environment (build 1.8.0_161-b12) Java HotSpot(TM) 64-Bit Server VM (build 25.161-b12, mixed mode) # jdk 是需要所有机器都安装的, 且配置环境变量 3、下载、安装 Hadoop shell > wget http://mirrors.hust.edu.cn/apache/hadoop/common/hadoop-2.9.1/hadoop-2.9.1.tar.gz shell > tar xf hadoop-2.9.1.tar.gz -C /usr/local shell > chown -R hadoop.hadoop /usr/local/hadoop-2.9.1 shell > tail -2 /etc/profile export HADOOP_HOME=/usr/local/hadoop-2.9.1 export PATH=$PATH:$HADOOP_HOME/bin:$HADOOP_HOME/sbin shell > source /etc/profile 4、配置 Hadoop 1> hadoop-env.sh 一个全局环境变量控制文件, 该文件中的值会被 yarn-env.sh、mapred-env.sh 覆盖 shell > vim /usr/local/hadoop-2.9.1/etc/hadoop/hadoop-env.sh export JAVA_HOME=/usr/java/default # JAVA_HOME export HADOOP_HEAPSIZE=1000 # 内存堆大小 # Hadoop 默认为每个守护进程分配1000MB内存, 资料显示, 以 NameNode 进程来说, 保守计算每100万个数据块需要1000MB内存, # 我们集群为3个节点, 每个节点有3T硬盘, 数据块大小为256MB, 每个数据块有3个复本, 大概有12000个数据块 -> 3*3000000MB/(256MB*3), 默认值足够了。 # Tips: 可以单独为每个进程设置不同的内存大小 2> core-site.xml shell > vim /usr/local/hadoop-2.9.1/etc/hadoop/core-site.xml <configuration> <property> <name>fs.defaultFS</name> <value>hdfs://192.168.10.50</value> </property> <property> <name>hadoop.tmp.dir</name> <value>file:///dfs/tmp/hadoop-${user.name}</value> </property> <property> <name>io.file.buffer.size</name> <value>131072</value> </property> <property> <name>fs.trash.interval</name> <value>1440</value> </property> </configuration> # 模板文件:HADOOP_HOME/share/doc/hadoop/hadoop-project-dist/hadoop-common/core-default.xml 3> hdfs-site.xml shell > vim /usr/local/hadoop-2.9.1/etc/hadoop/hdfs-site.xml <configuration> <property> <name>dfs.blocksize</name> <value>134217728</value> </property> <property> <name>dfs.replication</name> <value>3</value> </property> <property> <name>dfs.namenode.handler.count</name> <value>100</value> </property> <property> <name>dfs.namenode.name.dir</name> <value>file:///dfs/name</value> </property> <property> <name>dfs.datanode.data.dir</name> <value>file:///dfs/disk1/data,/dfs/disk2/data,/dfs/disk3/data</value> </property> <property> <name>dfs.namenode.checkpoint.dir</name> <value>file:///dfs/namesecondary</value> </property> </configuration> # 模板文件:HADOOP_HOME/share/doc/hadoop/hadoop-project-dist/hadoop-hdfs/hdfs-default.xml 4> yarn-site.xml shell > vim /usr/local/hadoop-2.9.1/etc/hadoop/yarn-site.xml <configuration> <property> <name>yarn.resourcemanager.hostname</name> <value>192.168.10.50</value> </property> <property> <name>yarn.nodemanager.local-dirs</name> <value>file:///dfs/disk1/nm-local-dir,/dfs/disk2/nm-local-dir,/dfs/disk3/nm-local-dir</value> </property> <property> <name>yarn.nodemanager.aux-services</name> <value>mapreduce_shuffle</value> </property> <property> <name>yarn.nodemanager.resource.memory-mb</name> <value>10240</value> </property> <property> <name>yarn.nodemanager.resource.cpu-vcores</name> <value>10</value> </property> </configuration> # 模板文件:HADOOP_HOME/share/doc/hadoop/hadoop-yarn/hadoop-yarn-common/yarn-default.xml 5> mapred-site.xml shell > vim /usr/local/hadoop-2.9.1/etc/hadoop/mapred-site.xml <configuration> <property> <name>mapreduce.framework.name</name> <value>yarn</value> </property> <property> <name>mapreduce.map.memory.mb</name> <value>2048</value> </property> <property> <name>mapreduce.reduce.memory.mb</name> <value>2048</value> </property> <property> <name>mapred.child.java.opts</name> <value>-Xmx1024m</value> </property> </configuration> # 模板文件:HADOOP_HOME/share/doc/hadoop/hadoop-mapreduce-client/hadoop-mapreduce-client-core/mapred-default.xml 6> slaves shell > tail /usr/local/hadoop-2.9.1/etc/hadoop/slaves 192.168.10.51 192.168.10.52 192.168.10.53 5、同步配置文件 shell > ansible slave -m synchronize -a 'src=/usr/local/hadoop-2.9.1 dest=/usr/local/' 6、启动 Hadoop shell > su - hadoop -c "hdfs namenode -format" # 格式化文件系统 18/06/03 16:24:39 INFO common.Storage: Storage directory /dfs/name has been successfully formatted. # 表示成功 shell > su - hadoop hadoop shell > start-dfs.sh # 启动 hdfs hadoop shell > start-yarn.sh # 启动 yarn hadoop shell > jps # Master 节点服务启动成功 14032 ResourceManager 13745 SecondaryNameNode 14364 Jps 13406 NameNode hadoop shell > ansible slave -m shell -a 'jps' # Slave 节点服务启动成功 slave02 | SUCCESS | rc=0 >> 4324 DataNode 4936 Jps 4572 NodeManager slave01 | SUCCESS | rc=0 >> 4807 DataNode 5065 NodeManager 5455 Jps slave03 | SUCCESS | rc=0 >> 4720 DataNode 5365 Jps 4975 NodeManager 五、附加 1、hdfs dfs -ls # 列出当前用户家目录 18/06/03 17:16:30 WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable ls: `.': No such file or directory 解决方法: 1> 根据你 hadoop 的版本,来 http://dl.bintray.com/sequenceiq/sequenceiq-bin 下载一个对应版本的 hadoop-native-64 包 2> 解压压缩包,覆盖到 HADOOP_HOME/lib/native/ 目录下即可! shell > tar xf hadoop-native-64-2.7.0.tar -C /usr/local/hadoop-2.9.1/lib/native/ hadoop shell > hdfs dfs -ls ls: `.': No such file or directory # 创建用户家目录即可 hdfs -mkdir -p /user/hadoop # 如果没有对应版本就下载个最接近的也行,我的环境 hadoop 2.9.1,下载 2.7.0 没有问题。
原创文章,作者:Maggie-Hunter,如若转载,请注明出处:https://blog.ytso.com/196513.html