摘要:創(chuàng)建的集群下使用部署基礎(chǔ)環(huán)境關(guān)鍵詞集群架構(gòu)集群包含臺(tái)虛擬主機(jī),采用創(chuàng)建容器的方式創(chuàng)建,無需創(chuàng)建多臺(tái)虛擬機(jī),簡(jiǎn)單方便。
Docker創(chuàng)建的集群下使用ansible部署hadoop 基礎(chǔ)環(huán)境
MBP, Palallels Desktop, Centos7
關(guān)鍵詞docker, ansible, hadoop
集群架構(gòu)集群包含4臺(tái)“虛擬主機(jī)”,采用Docker創(chuàng)建容器的方式創(chuàng)建,無需創(chuàng)建多臺(tái)虛擬機(jī),簡(jiǎn)單方便。
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寄主機(jī)中安裝docker
[root@centos-linux ~]# yum -y install docker.x86_64啟動(dòng)docker服務(wù)
[root@centos-linux ~]# systemctl start dockerCentos鏡像拉取
國內(nèi)使用docker.io拉取鏡像的時(shí)候非常慢,所以找了鏡像倉庫進(jìn)行拉取。
本次用到的鏡像倉庫是http://hub.daocloud.io, 在搜索欄中輸入centos并搜索
在檢索到的鏡像詳情右側(cè)給出了pull的命令,直接運(yùn)行即可。
[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創(chuàng)建容器
按照集群的架構(gòu),創(chuàng)建容器時(shí)需要設(shè)置固定IP,所以先要在docker下創(chuàng)建固定IP的網(wǎng)絡(luò)組
[root@centos-linux ~]# docker network create --subnet=172.18.0.0/16 netgroup
docker的網(wǎng)絡(luò)組創(chuàng)建完成之后就可以創(chuàng)建固定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下使用簡(jiǎn)單方式創(chuàng)建容器后遇到sshd啟動(dòng)失敗的問題,所以需要添加參數(shù)--privileged和-v /sys/fs/cgroup:/sys/fs/cgroup,并在啟動(dòng)的時(shí)候運(yùn)行/usr/sbin/init。
openssh創(chuàng)建的centos容器中并沒有預(yù)裝ssh服務(wù),需要登錄到所有容器中手動(dòng)安裝。
登錄容器[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啟動(dòng)ssh服務(wù)
[root@cluster-master /]# systemctl start sshdssh自動(dòng)接受新的公鑰
master設(shè)置ssh登錄自動(dòng)添加kown_hosts vi編輯/etc/ssh/ssh_config配置文件 設(shè)置StrictHostKeyChecking為no配置cluster-master的hosts
/etc/hosts文件在容器啟動(dòng)時(shí)被重寫,直接修改內(nèi)容在容器重啟后不能保留,為了讓容器在重啟之后獲取集群hosts,使用了一種啟動(dòng)容器后重寫hosts的方法。
需要在~/.bashrc中追加以下指令
:>/etc/hosts cat >>/etc/hosts<運(yùn)行source ~/.bashrc使之生效
這時(shí)可以看到/etc/hosts文件已經(jīng)被改為需要的內(nèi)容[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公鑰分發(fā)在master機(jī)上執(zhí)行ssh-keygen -t rsa并一路回車,完成之后會(huì)生成~/.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將公鑰文件分發(fā)到集群slave主機(jī)
[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分發(fā)完成之后測(cè)試是否已經(jīng)可以免輸入密碼登錄。另外本次實(shí)驗(yàn)使用到了root用戶,如果在其他用戶下使用免密碼登錄,需要確保用戶對(duì)~/.ssh/authorized_keys文件有可操作權(quán)限。
Ansible 源碼安裝ansible 使用git將文件安裝在cluster-master的/opt目錄下,因?yàn)闊o需agent,所以安裝在一臺(tái)主控機(jī)上即可管理集群。容器中的centos沒有預(yù)裝git,在安裝之前需要事先安裝git:[root@cluster-master /]# yum -y install git在/opt下執(zhí)行[root@cluster-master opt]# git clone git://github.com/ansible/ansible.git --recursive在執(zhí)行ansible的env-setup之前需要安裝一些容器中沒有預(yù)裝的軟件包[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進(jìn)入ansible目錄,執(zhí)行安裝腳本[root@cluster-master opt]# cd ./ansible [root@cluster-master ansible]# source ./hacking/env-setup配置ansible hosts創(chuàng)建/etc/ansible/hosts文件,并將cluster內(nèi)的主機(jī)按組的方式寫入該文件
[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下載完成之后解壓安裝包,并創(chuàng)建鏈接文件
[root@cluster-master opt]# tar -xzvf hadoop-2.7.4.tar.gz [root@cluster-master opt]# ln -s hadoop-2.7.4 hadoop設(shè)置java和hadoop環(huán)境變量(.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運(yùn)行所需配置文件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打包成一個(gè)文件方便ansible分發(fā)到slave主機(jī)
[root@cluster-master opt]# tar -cvf hadoop-dis.tar hadoop hadoop-2.7.4使用ansible-playbook分發(fā).bashrc和hadoop-dis.tar至slave主機(jī)--- - 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,并執(zhí)行
[root@cluster-master opt]# ansible-playbook hadoop-dis.yamlhadoop-dis.tar會(huì)自動(dòng)解壓到slave主機(jī)的/opt目錄下。
格式化namenode[root@cluster-master opt]# hadoop namenode -format啟動(dòng)hadoop集群到這一步已經(jīng)可以開始hadoop之旅了,啟動(dòng)比較簡(jiǎn)單,在$HADOOP_HOME/sbin下有幾個(gè)啟動(dòng)和停止的腳本如下:
[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,啟動(dòng)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啟動(dòng)完成之后會(huì)在master和slave看到以下進(jìn)程
cluster-master
[root@cluster-master sbin]# jps 2484 SecondaryNameNode 2648 Jps 2315 NameNodecluster-slave
[root@cluster-slave1 logs]# jps 27502 DataNode 27583 Jps體驗(yàn)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總結(jié)docker下部署hadoop確實(shí)比較奇葩,但是對(duì)于個(gè)人學(xué)習(xí)來說確是十分方便,不用在虛擬機(jī)里復(fù)制多套環(huán)境,也簡(jiǎn)化了網(wǎng)絡(luò)配置的步驟,加上ansible的使用使得集群的管理更加高效。
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/27016.html
摘要:基于安裝好的和集群部署創(chuàng)建的集群下使用部署創(chuàng)建的集群下使用部署在上制作安裝包下載創(chuàng)建目錄,并將軟件包現(xiàn)在到這個(gè)目錄,依然使用國內(nèi)鏡像下載。部署使用執(zhí)行完成的部署工作。 基于安裝好的hadoop和zookeeper集群部署hbase Docker創(chuàng)建的集群下使用ansible部署hadoop Docker創(chuàng)建的集群下使用ansible部署zookeeper OS hostname...
摘要:使用文章創(chuàng)建的集群下使用部署中創(chuàng)建的集群進(jìn)行的安裝在上制作安裝包下載官方源下載顯得十分緩慢,所以還是選擇國內(nèi)的鏡像源,將下載到創(chuàng)建鏈接下載完成后將解壓并創(chuàng)建鏈接,方便管理修改配置文件中已經(jīng)提供了配置模板,復(fù)制一份 使用文章Docker創(chuàng)建的集群下使用ansible部署hadoop中創(chuàng)建的集群進(jìn)行zookeeper的安裝 OS hostname IP Centos7 clust...
閱讀 3466·2023-04-26 00:39
閱讀 4073·2021-09-22 10:02
閱讀 2555·2021-08-09 13:46
閱讀 1108·2019-08-29 18:40
閱讀 1455·2019-08-29 18:33
閱讀 781·2019-08-29 17:14
閱讀 1523·2019-08-29 12:40
閱讀 2983·2019-08-28 18:07