macOS使用docker搭建hadoop集群
安装docker
下载网址:https://www.docker.com/products/docker-desktop/
构建基础镜像
拉取centos镜像
docker pull centos:7.5.1804
创建文件夹,将jdk安装包放入,并配置Dockerfile文件
mkdir docker-hadoop
cd docker-hadoop
mv ~/Downloads/jdk-8u341-linux-x64.tar.gz ./jdk-8u341-linux-x64.tar.gz
vim Dockerfile
FROM centos:7.5.1804
#安装JDK
RUN mkdir -p /opt/software && mkdir -p /opt/service
ADD jdk-8u141-linux-x64.tar.gz /opt/service
#安装语言包
RUN yum -y install kde-l10n-Chinese glibc-common vim
RUN localedef -c -f UTF-8 -i zh_CN zh_CN.utf8
RUN echo "LANG=zh_CN.UTF-8" >>/etc/locale.conf
#解决login环境变量失效问题
RUN touch /etc/profile.d/my_env.sh
RUN echo -e "export LC_ALL=zh_CN.UTF-8\nexport JAVA_HOME=/opt/service/jdk1.8.0_141\nexport PATH=\$PATH:\$JAVA_HOME/bin" >>/etc/profile.d/my_env.sh
#安装ssh服务
# 更换国内阿里云yum源
RUN curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
RUN sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
RUN yum makecache
# 安装sshd
RUN yum install -y openssh-server openssh-clients vim net-tools lrzsz
RUN sed -i '/^HostKey/'d /etc/ssh/sshd_config
RUN echo 'HostKey /etc/ssh/ssh_host_rsa_key'>>/etc/ssh/sshd_config
# 生成 ssh-key
RUN ssh-keygen -t rsa -b 2048 -f /etc/ssh/ssh_host_rsa_key
# 更改 root 用户登录密码为
RUN echo 'root:123456' | chpasswd
# 声明 22 端口
EXPOSE 22
# 容器运行时启动sshd
RUN mkdir -p /opt
RUN echo '#!/bin/bash' >> /opt/run.sh
RUN echo '/usr/sbin/sshd -D' >> /opt/run.sh
RUN chmod +x /opt/run.sh
CMD ["/opt/run.sh"]
构建基础镜像
docker build -t hadoopbase:1.0 ./
解决macOS宿主机无法ping通Docker容器的问题
1.首先 Mac 端通过 brew 安装 docker-connector
brew install wenjunxiao/brew/docker-connector
2.手动修改 /usr/local/etc/docker-connector.conf 文件中的路由
route 172.172.0.0/16
3.配置完成,直接启动服务(需要 sudo,路由配置启动之后仍然可以修改,并且无需重启服务立即生效),需要输入本机密码
sudo brew services start docker-connector
4.使用一下命令在docker 端运行 wenjunxiao/mac-docker-connector
docker run -it -d --restart always --net host --cap-add NET_ADMIN --name connector wenjunxiao/mac-docker-connector
启动镜像 基础配置
创建自定义网络
docker network create --subnet=172.172.0.0/24 hadoop
固定容器ip启动
docker run -itd --network hadoop --ip 172.172.0.2 -h hadoop01 --name hadoop01 hadoopbase:1.0 /bin/bash
docker run -itd --network hadoop --ip 172.172.0.3 -h hadoop02 --name hadoop02 hadoopbase:1.0 /bin/bash
docker run -itd --network hadoop --ip 172.172.0.4 -h hadoop03 --name hadoop03 hadoopbase:1.0 /bin/bash
分别进入hadoop01、hadoop02、hadoop03配置主机和映射
vim /etc/hosts
172.172.0.2 hadoop01
172.172.0.3 hadoop02
172.172.0.4 hadoop03
分别进入hadoop01、hadoop02、hadoop03配置免密登录
ssh-keygen -t rsa
ssh-copy-id hadoop01
ssh-copy-id hadoop02
ssh-copy-id hadoop03
Hadoop安装与部署
hadoop安装
1)将hadoop导入到opt/software文件夹下
[root@hadoop01 ~]$ cd /opt/software
[root@hadoop01 software]$ rz -E
2)将安装文件解压到/opt/service文件夹下
[root@hadoop01 software]$ tar -zxvf hadoop-3.1.3.tar.gz -C /opt/service/
3)检查是否解压成功
[root@hadoop01 software]$ ll /opt/service/hadoop-3.1.3/
4)将hadoop添加到环境变量
第一步:获取Hadoop的安装路径
[root@hadoop01 hadoop-3.1.3]$ pwd
/opt/service/hadoop-3.1.3
第二步:打开/etc/profile.d/my_env.sh文件
[root@hadoop01 hadoop-3.1.3]$ sudo vim /etc/profile.d/my_env.sh
第三步:在my_env.sh文件中增加如下配置信息
#HADOOP_HOME
export HADOOP_HOME=/opt/service/hadoop-3.1.3
export PATH=$PATH:$HADOOP_HOME/bin
export PATH=$PATH:$HADOOP_HOME/sbin
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
第四步:刷新配置文件
[root@hadoop01 hadoop-3.1.3]$ source /etc/profile.d/my_env.sh
修改配置文件
(1) 修改配置文件core-site.xml
第一步:进入core-site.xml所在的位置(hadoop的配置文件在hadoop根目录/etc/hadoop文件夹中)
[root@hadoop01 hadoop]$ cd $HADOOP_HOME/etc/hadoop
第二步:修改core-site.xml
[root@hadoop01 hadoop]$ vim core-site.xml
内容修改为:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<configuration>
<!-- 指定NameNode的地址 -->
<property>
<name>fs.defaultFS</name>
<value>hdfs://hadoop01:8020</value>
</property>
<!-- 指定hadoop数据的存储目录 -->
<property>
<name>hadoop.tmp.dir</name>
<value>/opt/service/hadoop-3.1.3/data</value>
</property>
<!-- 配置HDFS网页登录使用的静态用户为root -->
<property>
<name>hadoop.http.staticuser.user</name>
<value>root</value>
</property>
</configuration>
(2) 修改配置文件hdfs-site.xml
编辑hdfs-site.xml
[root@hadoop01 hadoop]$ vim hdfs-site.xml
内容为:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<configuration>
<!-- nn web端访问地址-->
<property>
<name>dfs.namenode.http-address</name>
<value>hadoop01:9870</value>
</property>
<!-- 2nn web端访问地址-->
<property>
<name>dfs.namenode.secondary.http-address</name>
<value>hadoop03:9868</value>
</property>
<!-- 测试环境指定HDFS副本的数量1 -->
<property>
<name>dfs.replication</name>
<value>1</value>
</property>
</configuration>
(3) 修改配置文件yarn-site.xml
编辑yarn-site.xml
[root@hadoop01 hadoop]$ vim yarn-site.xml
内容为:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<configuration>
<!-- 指定MR走shuffle -->
<property>
<name>yarn.nodemanager.aux-services</name>
<value>mapreduce_shuffle</value>
</property>
<!-- 指定ResourceManager的地址-->
<property>
<name>yarn.resourcemanager.hostname</name>
<value>hadoop02</value>
</property>
<!-- 环境变量的继承 -->
<property>
<name>yarn.nodemanager.env-whitelist</name>
<value>JAVA_HOME,HADOOP_COMMON_HOME,HADOOP_HDFS_HOME,HADOOP_CONF_DIR,CLASSPATH_PREPEND_DISTCACHE,HADOOP_YARN_HOME,HADOOP_MAPRED_HOME</value>
</property>
<!-- yarn容器允许分配的最大最小内存 -->
<property>
<name>yarn.scheduler.minimum-allocation-mb</name>
<value>512</value>
</property>
<property>
<name>yarn.scheduler.maximum-allocation-mb</name>
<value>014</value>
</property>
<!-- yarn容器允许管理的物理内存大小 -->
<property>
<name>yarn.nodemanager.resource.memory-mb</name>
<value>2048</value>
</property>
<!-- 关闭yarn对虚拟内存的限制检查 -->
<property>
<name>yarn.nodemanager.vmem-check-enabled</name>
<value>false</value>
</property>
</configuration>
(4) 修改配置文件mapred-site.xml
编辑mapred-site.xml
[root@hadoop01 hadoop]$ vim mapred-site.xml
内容为:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<configuration>
<!-- 指定MapReduce程序运行在Yarn上 -->
<property>
<name>mapreduce.framework.name</name>
<value>yarn</value>
</property>
</configuration>
(5) 修改配置文件workers
编辑workers
[root@hadoop01 hadoop]$ vim workers
内容如下
hadoop01
hadoop02
hadoop03
注意:该文件中添加的内容结尾不允许有空格,文件中不允许有空行。
(6) 配置历史服务器 编辑mapred-site.xml
[root@hadoop01 hadoop]$ vim mapred-site.xml
增加以下内容:
<!-- 历史服务器端地址 -->
<property>
<name>mapreduce.jobhistory.address</name>
<value>hadoop01:10020</value>
</property>
<!-- 历史服务器web端地址 -->
<property>
<name>mapreduce.jobhistory.webapp.address</name>
<value>hadoop01:19888</value>
</property>
(7) 配置日志聚集 编辑yarn-site.xml
日志聚集概念:应用运行完成以后,将程序运行日志信息上传到HDFS系统上。
日志聚集功能好处:可以方便的查看到程序运行详情,方便开发调试。
注意:开启日志聚集功能,需要重新启动NodeManager 、ResourceManager和HistoryManager。
配置步骤如下:
编辑yarn-site.xml
[root@hadoop01 hadoop]$ vim yarn-site.xml
增加如下内容:
<!-- 开启日志聚集功能 -->
<property>
<name>yarn.log-aggregation-enable</name>
<value>true</value>
</property>
<!-- 设置日志聚集服务器地址 -->
<property>
<name>yarn.log.server.url</name>
<value>http://hadoop01:19888/jobhistory/logs</value>
</property>
<!-- 设置日志保留时间为7天 (7*24*60*60)-->
<property>
<name>yarn.log-aggregation.retain-seconds</name>
<value>604800</value>
</property>
分发Hadoop
[root@hadoop01 hadoop]$ scp -r /opt/service/hadoop-3.1.3 hadoop02:/opt/service
[root@hadoop01 hadoop]$ scp -r /opt/service/hadoop-3.1.3 hadoop03:/opt/service
分发配置文件my_env.sh
[root@hadoop01 hadoop]$ scp -r /etc/profile.d/my_env.sh hadoop02:/etc/profile.d
[root@hadoop01 hadoop]$ scp -r /etc/profile.d/my_env.sh hadoop03:/etc/profile.d
hadoop02、hadoop03上刷新配置文件
[root@hadoop02 hadoop]$ source /etc/profile.d/my_env.sh
[root@hadoop03 hadoop]$ source /etc/profile.d/my_env.sh
相关shell脚本
分发脚本
功能:循环复制文件到所有节点的相同目录下
在bin
目录下新建xsync
[root@hadoop01 bin]# cd /bin
[root@hadoop01 bin]# vim xsync
#!/bin/bash
#1. 判断参数个数
if [ $# -lt 1 ]
then
echo Not Enough Arguement!
exit;
fi
#2. 遍历集群所有机器
for host in hadoop01 hadoop02 hadoop03
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
done
[root@hadoop01 bin]# chmod +x xsync
测试:
[root@hadoop01 ~]# xsync /bin/xsync
集群启停脚本
[root@hadoop01 ~]# cd /bin/
[root@hadoop01 bin]# vim hadoopall
#!/bin/bash
if [ $# -lt 1 ]
then
echo "No Args Input..."
exit ;
fi
case $1 in
"start")
echo " =================== 启动 hadoop 集群 ==================="
echo " --------------- 启动 hdfs ---------------"
ssh hadoop01 "$HADOOP_HOME/sbin/start-dfs.sh"
echo " --------------- 启动 yarn ---------------"
ssh hadoop02 "$HADOOP_HOME/sbin/start-yarn.sh"
echo " --------------- 启动 historyserver ---------------"
ssh hadoop01 "$HADOOP_HOME/bin/mapred --daemon start historyserver"
;;
"stop")
echo " =================== 关闭 hadoop 集群 ==================="
echo " --------------- 关闭 historyserver ---------------"
ssh hadoop01 "$HADOOP_HOME/bin/mapred --daemon stop historyserver"
echo " --------------- 关闭 yarn ---------------"
ssh hadoop02 "$HADOOP_HOME/sbin/stop-yarn.sh"
echo " --------------- 关闭 hdfs ---------------"
ssh hadoop01 "$HADOOP_HOME/sbin/stop-dfs.sh"
;;
*)
echo "Input Args Error..."
;;
esac
添加执行权限
[root@hadoop01 bin]# chmod +x hadoopall
查看三台服务器Java进程脚本
[root@hadoop01 bin]# vim jpsall
#!/bin/bash
for host in hadoop01 hadoop02 hadoop03
do
echo =============== $host ===============
ssh $host jps
done
添加执行权限
[root@hadoop01 bin]# chmod +x jpsall