摘要:創建的集群下使用部署基礎環境關鍵詞集群架構集群包含臺虛擬主機,采用創建容器的方式創建,無需創建多臺虛擬機,簡單方便。
Docker創建的集群下使用ansible部署hadoop 基礎環境
MBP, Palallels Desktop, Centos7
關鍵詞docker, ansible, hadoop
集群架構集群包含4臺“虛擬主機”,采用Docker創建容器的方式創建,無需創建多臺虛擬機,簡單方便。
OS | hostname | IP |
---|---|---|
Centos7 | cluster-master | 172.18.0.2 |
Centos7 | cluster-slave1 | 172.18.0.3 |
Centos7 | cluster-slave1 | 172.18.0.4 |
Centos7 | cluster-slave1 | 172.18.0.5 |
登錄到Centos 7寄主機中安裝docker
[root@centos-linux ~]# yum -y install docker.x86_64啟動docker服務
[root@centos-linux ~]# systemctl start dockerCentos鏡像拉取
國內使用docker.io拉取鏡像的時候非常慢,所以找了鏡像倉庫進行拉取。
本次用到的鏡像倉庫是http://hub.daocloud.io, 在搜索欄中輸入centos并搜索
在檢索到的鏡像詳情右側給出了pull的命令,直接運行即可。
[root@centos-linux ~]# docker pull daocloud.io/library/centos:latest
拉取完成之后可以看到
[root@centos-linux ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE daocloud.io/library/centos latest 328edcd84f1b 3 weeks ago 192.5 MB創建容器
按照集群的架構,創建容器時需要設置固定IP,所以先要在docker下創建固定IP的網絡組
[root@centos-linux ~]# docker network create --subnet=172.18.0.0/16 netgroup
docker的網絡組創建完成之后就可以創建固定IP的容器了
[root@centos-linux ~]# docker run -d --privileged -ti -v /sys/fs/cgroup:/sys/fs/cgroup --name cluster-master -h cluster-master --net netgroup --ip 172.18.0.2 daocloud.io/library/centos /usr/sbin/init [root@centos-linux ~]# docker run -d --privileged -ti -v /sys/fs/cgroup:/sys/fs/cgroup --name cluster-slave1 -h cluster-slave1 --net netgroup --ip 172.18.0.3 daocloud.io/library/centos /usr/sbin/init [root@centos-linux ~]# docker run -d --privileged -ti -v /sys/fs/cgroup:/sys/fs/cgroup --name cluster-slave2 -h cluster-slave2 --net netgroup --ip 172.18.0.4 daocloud.io/library/centos /usr/sbin/init [root@centos-linux ~]# docker run -d --privileged -ti -v /sys/fs/cgroup:/sys/fs/cgroup --name cluster-slave3 -h cluster-slave3 --net netgroup --ip 172.18.0.5 daocloud.io/library/centos /usr/sbin/init
在Centos7下使用簡單方式創建容器后遇到sshd啟動失敗的問題,所以需要添加參數--privileged和-v /sys/fs/cgroup:/sys/fs/cgroup,并在啟動的時候運行/usr/sbin/init。
openssh創建的centos容器中并沒有預裝ssh服務,需要登錄到所有容器中手動安裝。
登錄容器[root@centos-linux ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 328913df0d52 daocloud.io/library/centos "/usr/sbin/init" 1 hours ago Up 1 hours cluster-slave3 83361d4bf079 daocloud.io/library/centos "/usr/sbin/init" 1 hours ago Up 1 hours cluster-slave2 35d1e5732340 daocloud.io/library/centos "/usr/sbin/init" 1 hours ago Up 1 hours cluster-slave1 37fbcc24b0e3 daocloud.io/library/centos "/usr/sbin/init" 1 hours ago Up 1 hours cluster-master
[root@centos-linux ~]# docker exec -it 37fbcc24b0e3 /bin/bash安裝openssh
[root@cluster-master /]# yum -y install openssh openssh-server openssh-clients啟動ssh服務
[root@cluster-master /]# systemctl start sshdssh自動接受新的公鑰
master設置ssh登錄自動添加kown_hosts vi編輯/etc/ssh/ssh_config配置文件 設置StrictHostKeyChecking為no配置cluster-master的hosts
/etc/hosts文件在容器啟動時被重寫,直接修改內容在容器重啟后不能保留,為了讓容器在重啟之后獲取集群hosts,使用了一種啟動容器后重寫hosts的方法。
需要在~/.bashrc中追加以下指令
:>/etc/hosts cat >>/etc/hosts<運行source ~/.bashrc使之生效
這時可以看到/etc/hosts文件已經被改為需要的內容[root@cluster-master /]# cat /etc/hosts 127.0.0.1 localhost 172.18.0.2 cluster-master 172.18.0.3 cluster-slave1 172.18.0.4 cluster-slave2 172.18.0.5 cluster-slave3cluster-master公鑰分發在master機上執行ssh-keygen -t rsa并一路回車,完成之后會生成~/.ssh目錄,目錄下有id_rsa(私鑰文件)和id_rsa.pub(公鑰文件),再將id_rsa.pub重定向到文件authorized_keys
[root@cluster-master /]# cd ~/.ssh [root@cluster-master /]# cat id_rsa.pub>authorized_keys文件生成之后用scp將公鑰文件分發到集群slave主機
[root@cluster-master /]# ssh root@cluster-slave1 "mkdir ~/.ssh" [root@cluster-master /]# scp ~/.ssh/authorized_keys root@cluster-slave1:~/.ssh [root@cluster-master /]# ssh root@cluster-slave2 "mkdir ~/.ssh" [root@cluster-master /]# scp ~/.ssh/authorized_keys root@cluster-slave2:~/.ssh [root@cluster-master /]# ssh root@cluster-slave3 "mkdir ~/.ssh" [root@cluster-master /]# scp ~/.ssh/authorized_keys root@cluster-slave3:~/.ssh分發完成之后測試是否已經可以免輸入密碼登錄。另外本次實驗使用到了root用戶,如果在其他用戶下使用免密碼登錄,需要確保用戶對~/.ssh/authorized_keys文件有可操作權限。
Ansible 源碼安裝ansible 使用git將文件安裝在cluster-master的/opt目錄下,因為無需agent,所以安裝在一臺主控機上即可管理集群。容器中的centos沒有預裝git,在安裝之前需要事先安裝git:[root@cluster-master /]# yum -y install git在/opt下執行[root@cluster-master opt]# git clone git://github.com/ansible/ansible.git --recursive在執行ansible的env-setup之前需要安裝一些容器中沒有預裝的軟件包[root@cluster-master opt]# yum -y install python-setuptools [root@cluster-master opt]# easy_install pip [root@cluster-master opt]# pip install paramiko PyYAML Jinja2 httplib2 six進入ansible目錄,執行安裝腳本[root@cluster-master opt]# cd ./ansible [root@cluster-master ansible]# source ./hacking/env-setup配置ansible hosts創建/etc/ansible/hosts文件,并將cluster內的主機按組的方式寫入該文件
[cluster] cluster-master cluster-slave1 cluster-slave2 cluster-slave3 [master] cluster-master [slaves] cluster-slave1 cluster-slave2 cluster-slave3Hadoop 在集群中安裝openjdk使用ansible在在集群中安裝openjdk
[root@cluster-master ansible]# ansible cluster -m yum -a "name=java-1.8.0-openjdk,java-1.8.0-openjdk-devel state=latest"在cluster-master上安裝hadoop將hadoop安裝包下載至/opt目錄下
[root@cluster-master opt]# wget http://mirrors.tuna.tsinghua.edu.cn/apache/hadoop/common/hadoop-2.7.4/hadoop-2.7.4.tar.gz下載完成之后解壓安裝包,并創建鏈接文件
[root@cluster-master opt]# tar -xzvf hadoop-2.7.4.tar.gz [root@cluster-master opt]# ln -s hadoop-2.7.4 hadoop設置java和hadoop環境變量(.bashrc)# java export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.141-1.b16.el7_3.x86_64 export PATH=$HADOOP_HOME/bin:$PATH # hadoop export HADOOP_HOME=/opt/hadoop export PATH=$HADOOP_HOME/bin:$HADOOP_HOME/sbin:$PATH修改slaves文件[root@cluster-master opt]# vi /opt/hadoop/etc/hadoop/slaves cluster-slave1 cluster-slave2 cluster-slave3修改hadoop運行所需配置文件core-site.xml
hadoop.tmp.dir /home/hadoop/tmp A base for other temporary directories. fs.default.name hdfs://cluster-master:9000 hdfs-site.xml
dfs.replication 3 mapred-site.xml
mapred.job.tracker http://cluster-master:9001 yarn-site.xml
打包hadoop文件yarn.nodemanager.aux-services mapreduce_shuffle yarn.resourcemanager.address cluster-master:18040 將hadoop鏈接文件和hadoop-2.7.4打包成一個文件方便ansible分發到slave主機
[root@cluster-master opt]# tar -cvf hadoop-dis.tar hadoop hadoop-2.7.4使用ansible-playbook分發.bashrc和hadoop-dis.tar至slave主機--- - hosts: cluster tasks: - name: copy .bashrc to slaves copy: src=~/.bashrc dest=~/ notify: - exec source - name: mkdir /home/hadoop/tmp shell: mkdir -p /home/hadoop/tmp - name: copy hadoop-dis.tar to slaves unarchive: src=/opt/hadoop-dis.tar dest=/opt handlers: - name: exec source shell: source ~/.bashrc將以上yaml保存為hadoop-dis.yaml,并執行
[root@cluster-master opt]# ansible-playbook hadoop-dis.yamlhadoop-dis.tar會自動解壓到slave主機的/opt目錄下。
格式化namenode[root@cluster-master opt]# hadoop namenode -format啟動hadoop集群到這一步已經可以開始hadoop之旅了,啟動比較簡單,在$HADOOP_HOME/sbin下有幾個啟動和停止的腳本如下:
[root@cluster-master opt]# cd $HADOOP_HOME/sbin [root@cluster-master sbin]# ls -l total 120 -rwxr-xr-x. 1 20415 101 2752 Aug 1 00:35 distribute-exclude.sh -rwxr-xr-x. 1 20415 101 6452 Aug 1 00:35 hadoop-daemon.sh -rwxr-xr-x. 1 20415 101 1360 Aug 1 00:35 hadoop-daemons.sh -rwxr-xr-x. 1 20415 101 1640 Aug 1 00:35 hdfs-config.cmd -rwxr-xr-x. 1 20415 101 1427 Aug 1 00:35 hdfs-config.sh -rwxr-xr-x. 1 20415 101 2291 Aug 1 00:35 httpfs.sh -rwxr-xr-x. 1 20415 101 3128 Aug 1 00:35 kms.sh -rwxr-xr-x. 1 20415 101 4080 Aug 1 00:35 mr-jobhistory-daemon.sh -rwxr-xr-x. 1 20415 101 1648 Aug 1 00:35 refresh-namenodes.sh -rwxr-xr-x. 1 20415 101 2145 Aug 1 00:35 slaves.sh -rwxr-xr-x. 1 20415 101 1779 Aug 1 00:35 start-all.cmd -rwxr-xr-x. 1 20415 101 1471 Aug 1 00:35 start-all.sh -rwxr-xr-x. 1 20415 101 1128 Aug 1 00:35 start-balancer.sh -rwxr-xr-x. 1 20415 101 1401 Aug 1 00:35 start-dfs.cmd -rwxr-xr-x. 1 20415 101 3734 Aug 1 00:35 start-dfs.sh -rwxr-xr-x. 1 20415 101 1357 Aug 1 00:35 start-secure-dns.sh -rwxr-xr-x. 1 20415 101 1571 Aug 1 00:35 start-yarn.cmd -rwxr-xr-x. 1 20415 101 1347 Aug 1 00:35 start-yarn.sh -rwxr-xr-x. 1 20415 101 1770 Aug 1 00:35 stop-all.cmd -rwxr-xr-x. 1 20415 101 1462 Aug 1 00:35 stop-all.sh -rwxr-xr-x. 1 20415 101 1179 Aug 1 00:35 stop-balancer.sh -rwxr-xr-x. 1 20415 101 1455 Aug 1 00:35 stop-dfs.cmd -rwxr-xr-x. 1 20415 101 3206 Aug 1 00:35 stop-dfs.sh -rwxr-xr-x. 1 20415 101 1340 Aug 1 00:35 stop-secure-dns.sh -rwxr-xr-x. 1 20415 101 1642 Aug 1 00:35 stop-yarn.cmd -rwxr-xr-x. 1 20415 101 1340 Aug 1 00:35 stop-yarn.sh -rwxr-xr-x. 1 20415 101 4295 Aug 1 00:35 yarn-daemon.sh -rwxr-xr-x. 1 20415 101 1353 Aug 1 00:35 yarn-daemons.sh主要使用HDFS,啟動start-dfs.sh即可
[root@cluster-master sbin]# ./start-dfs.sh Starting namenodes on [cluster-master] cluster-master: starting namenode, logging to /opt/hadoop-2.7.4/logs/hadoop-root-namenode-cluster-master.out cluster-slave1: starting datanode, logging to /opt/hadoop-2.7.4/logs/hadoop-root-datanode-cluster-slave1.out cluster-slave3: starting datanode, logging to /opt/hadoop-2.7.4/logs/hadoop-root-datanode-cluster-slave3.out cluster-slave2: starting datanode, logging to /opt/hadoop-2.7.4/logs/hadoop-root-datanode-cluster-slave2.out Starting secondary namenodes [0.0.0.0] 0.0.0.0: starting secondarynamenode, logging to /opt/hadoop-2.7.4/logs/hadoop-root-secondarynamenode-cluster-master.out啟動完成之后會在master和slave看到以下進程
cluster-master
[root@cluster-master sbin]# jps 2484 SecondaryNameNode 2648 Jps 2315 NameNodecluster-slave
[root@cluster-slave1 logs]# jps 27502 DataNode 27583 Jps體驗hadoop在master端上傳文件試試
[root@cluster-master sbin]# hadoop dfs -put start-dfs.sh / DEPRECATED: Use of this script to execute hdfs command is deprecated. Instead use the hdfs command for it. [root@cluster-master sbin]# hadoop dfs -ls / DEPRECATED: Use of this script to execute hdfs command is deprecated. Instead use the hdfs command for it. Found 1 items -rw-r--r-- 3 root supergroup 3734 2017-08-26 10:57 /start-dfs.sh總結docker下部署hadoop確實比較奇葩,但是對于個人學習來說確是十分方便,不用在虛擬機里復制多套環境,也簡化了網絡配置的步驟,加上ansible的使用使得集群的管理更加高效。
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://m.specialneedsforspecialkids.com/yun/27016.html
摘要:基于安裝好的和集群部署創建的集群下使用部署創建的集群下使用部署在上制作安裝包下載創建目錄,并將軟件包現在到這個目錄,依然使用國內鏡像下載。部署使用執行完成的部署工作。 基于安裝好的hadoop和zookeeper集群部署hbase Docker創建的集群下使用ansible部署hadoop Docker創建的集群下使用ansible部署zookeeper OS hostname...
摘要:使用文章創建的集群下使用部署中創建的集群進行的安裝在上制作安裝包下載官方源下載顯得十分緩慢,所以還是選擇國內的鏡像源,將下載到創建鏈接下載完成后將解壓并創建鏈接,方便管理修改配置文件中已經提供了配置模板,復制一份 使用文章Docker創建的集群下使用ansible部署hadoop中創建的集群進行zookeeper的安裝 OS hostname IP Centos7 clust...
閱讀 3462·2023-04-26 00:39
閱讀 4066·2021-09-22 10:02
閱讀 2549·2021-08-09 13:46
閱讀 1106·2019-08-29 18:40
閱讀 1452·2019-08-29 18:33
閱讀 779·2019-08-29 17:14
閱讀 1520·2019-08-29 12:40
閱讀 2981·2019-08-28 18:07