[Hadoop]在CentOS7上使用Docker部署Hadoop

[Hadoop]在CentOS7上使用Docker部署Hadoop

  • 〇、先决条件
    • 1、Linux操作系统
    • 2、阅读本文的注意事项
    • 3、本文相关连接
    • 4、本文说明
    • 5、用户设置
      • Command 1:添加用户到组
    • 6、图解安装步骤
  • 一、安装Docker
    • 1、查看内核
    • 2、更新yum源和yum包
    • 3、卸载旧版本
    • 4、安装所需的软件包
    • 5、查看仓库中的Docker版本
    • 6、安装docker
    • 7、启动并加入开机启动
      • Command 2:Docker常用命令
      • Error 1:Docker服务启动错误(防火墙未关闭)
  • 二、运行Docker
    • 1、试运行Docker
      • Command 3:Docker镜像和容器常用命令
      • Error 2:进入Docker镜像启动错误(Selinux)
    • 2、正式运行Docker
      • Error 3:删除Docker镜像报错
      • Command 4:Docker拉取镜像(安装)
      • Branch 1:使用阿里云的镜像

请支持原创,认准DannisTang([email protected]

时间 更新日志
20190425 1、创建文章:以后慢慢填坑(虽然留了很多坑,但是正在慢慢解决中,不明白的地方请发邮件给我)
20190427 1、添加内容:运行Docker;2、修改文章的部分问题;3、I love you three thousand times.
20190428 1、添加内容:Docker网络和子网容器设置;2、添加内容:Docker镜像和容器安装;3、添加内容:Docker镜像删除报错.
20190430 1、添加内容:我也忘了添加了什么了
20190504 1、添加内容:docker的镜像查找;阿里云镜像的使用;修改yum的更新步骤
20190508 1、添加内容:安装完阿里云的docker之后,与宿主机通信的方式;2、新增资源:找到了一个比较不错docker教程网站贴出来
20190624 1、添加内容:诈尸级别的更新,我把阿里云的docker的完全分布式搞定了
20190702 1、添加内容:上次更新的时候,突然被其他事情打断了,没有更新完成,这次好好更新一下
20190707 1、添加内容:继续填坑

〇、先决条件

如果你没有使用过docker来部署hadoop,那么这篇文章从零开始教你使用Docker部署Hadoop。
网上的资料众多,我本人也是踩了不少的坑,顺表贴一个比较好的docker教程,算是比较不错的入门的网站的,docker教程

1、Linux操作系统

  1. 系统要求:
    我们这篇文章是讲Cent OS7上面的安装,所以先安装好的一个Cent OS7以上的系统吧
  2. 设置静态ip:
    在设置静态IP的时候,容易踩坑,容易出现设置完(即修改完network-scripts里面的网络配置文件后无法ping通外网,如百度,显示:未知网址的错误)无法联网的问题。所以要先设置好静态ip,而且能够连网的。之前我在设置静态ip的时候踩了一个大坑,设置完不能连网,可以看我的另一篇文章[LINUX]CentOS7设置静态IP,设置静态ip
  3. 主机设置:
    因为搭建集群,所以要四台以上的机器,我这里就贴出了我自己搭建好的四台机器信息,IP一定要静态IP,不然容易出现已配置好的集群找不到机器的错误
OS 内核 主机名Hostname IP地址 备注
CentOS 7.6.18 3.10.0 hadoopmaster 192.168.137.25 Hadoop架构主节点
CentOS 7.6.18 3.10.0 hadoopslave1 192.168.137.26 Hadoop架构从节点
CentOS 7.6.18 3.10.0 hadoopslave2 192.168.137.27 Hadoop架构从节点
CentOS 7.6.18 3.10.0 hadoopslave3 192.168.137.28 Hadoop架构从节点
CentOS 7.6.18 3.10.0 hadoopslave4 192.168.137.29 Hadoop架构从节点

2、阅读本文的注意事项

1. 在贴出的代码中,如果没有标注,均为ssh会话
2. 使用sh脚本的注释号#号来表示说明,如以下的示例中,三个井号后面:###就是我自己的注释讲解

###这里先查看内核版本
[root@localhost ~]# uname -r 

3. 因为文章篇幅问题,部分的代码(通常是ssh会话出现的代码)就省略了
4. 在文中会列举一些出现错误的情况:如果出现了,请按照提示进行解决;如果没出现,那就不要管了,跳过就行;如果未能解决,请发邮件给我或是问问神奇的百度

3、本文相关连接

本文所有的外部连接都会在此处贴出,省去重复查找的麻烦

  1. 设置静态IP[LINUX]CentOS7设置静态IP
  2. 关闭防火墙linux常用命令和关闭防火墙(其他博主的文章)
  3. 使用阿里云的镜像在 Centos7 下使用 Docker 快速搭建 Hadoop 集群(其他博主的文章)
  4. 使用无密的ssh机制ssh-keygen的使用ssh-keygen的使用(其他博主的文章)
  5. 比较不错的docker教程docker教程

4、本文说明

  1. Error开头的为可能遇到的问题举例示意,并且贴上解决方法
  2. Command开头的为一些常用命令补充

5、用户设置

用户设置这一步应该是在安装之前的步骤,所以先贴在这里

[root@localhost ~]# groupadd docker
###创建用户组docker组
[root@localhost ~]# usermod -aG docker hadoopmaster
###将hadoopslave1用户添加到docker用户组中,你可以自己创建其他名字的docker用户来完成这一步
[root@localhost ~]# su - hadoopmaster
###然后切换到hadoopslave1这个用户开始进行会话
###注意在后面会话中的根据会话前面的用户名来切换用户
[hadoopmaster@localhost ~]$ 

还要赋予hadoopmaster用户root权限

[root@localhost ~]# vim /etc/sudoers
###找到一处有如下所示的地方

## Allows people in group wheel to run all commands
%wheel  ALL=(ALL)       ALL

###你的原文可能是这样: #%wheel  ALL=(ALL)       ALL    这里要把这个注释号去掉,变成上面这样,意思是让这条语句生效
###再找到还有下面这样一处的地方

## Allow root to run any commands anywhere 
root    ALL=(ALL)       ALL
hadoopmaster ALL=(ALL)   ALL
###hadoopmaster这一行是新加的,不一定是hadoopmaster,也可以使用你自己命名的用户
[root@localhost ~]# usermod -a -G root hadoopmaster
###添加hadoopmaster用户到root组中
[root@localhost ~]# su - hadoopmaster
###切换到hadoopmaster用户
[hadoopmaster@localhost ~]$ who mom likes
root     pts/0        2019-04-27 05:06 (###这个括号会显示出计算机域组名,这里为了脱敏就不贴出来了)
[hadoopmaster@localhost ~]$ 

在添加这些完成之后,一些root用户的权限的命令也能用的hadoopmaster执行,只不过需要加sudo,可以自行尝试一下,比如root用户权限的yum update可以使用hadoopmaster用户的sudo yum update来执行。

Command 1:添加用户到组

用户添加到多个组的用户

usermod -G groupname username 
###这种会把用户从其他组中去掉,只属于该组)、
###例如:usermod -G hadoop hadoopmaster(hadoopmaster只属于hadoop组)

usermod -a -G groupname username 
###也可以写成usermod -aG groupname username 
###把用户添加到这个组,之前所属组不影响
###例如:usermod -a -G spark hadoopmaster(hadoopmaster属于之前hadoop组,也属于spark 组)

6、图解安装步骤

一、安装Docker

很多人先卡死在Docker上,所以还是先安装Docker比较好。

1、查看内核

Docker 对于 CentOS 系统的内核版本要求是高于或等于 3.10 ,在安装之前,先验证你的CentOS 版本是否支持 Docker 。可以通过 uname -r 命令查看你当前的内核版本

[root@localhost ~]$ uname -r
3.10.0-327.el7.x86_64
[root@localhost ~]$ 

如果显示的数字大于或等于3.10即可,如果低于3.10版本的话,需要更新一下。

2、更新yum源和yum包

使用 root 权限登录 Centos。更换成阿里云的yum源,确保 yum 包更新到最新。但是你也可以用其他的yum源。
此处更新完毕后,版本系统会更新,属于正常现象,但是内核不会更新,要更新内核的话要用其他的方法

[root@localhost ~]# cat /etc/redhat-release 
###更新之前先看一下版本号
CentOS Linux release 7.2.0940 (Core) 
[root@localhost ~]# yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
###这里设置的是阿里云的yum源
Loaded plugins: fastestmirror, langpacks
adding repo from: https://download.docker.com/linux/centos/docker-ce.repo
grabbing file https://download.docker.com/linux/centos/docker-ce.repo to /etc/yum.repos.d/docker-ce.repo
repo saved to /etc/yum.repos.d/docker-ce.repo
[root@localhost ~]# yum update
###这里省略中间的代码,主要是显示更新安装包的信息,内容巨多,然后会开始让你确认是否下载安装,直接输入y即可
[root@localhost ~]# yum makecache fast
###更新完之后,更新一下yum缓存即可,你电脑上显示的可能跟我的不一样,属于正常
Loaded plugins: fastestmirror, langpacks
Determining fastest mirrors
 * base: ftp.sjtu.edu.cn
 * extras: mirrors.tuna.tsinghua.edu.cn
 * updates: ftp.sjtu.edu.cn
base                                                                                                                                                                                           | 3.6 kB  00:00:00     
docker-ce-stable                                                                                                                                                                               | 3.5 kB  00:00:00     
extras                                                                                                                                                                                         | 3.4 kB  00:00:00     
updates                                                                                                                                                                                        | 3.4 kB  00:00:00     
(1/2): extras/7/x86_64/primary_db                                                                                                                                                              | 201 kB  00:00:00     
(2/2): updates/7/x86_64/primary_db                                                                                                                                                             | 4.2 MB  00:00:01     
Metadata Cache Created
[root@localhost ~]# reboot

更新完之后最好重启一下

[root@localhost ~]# cat /etc/redhat-release 
###再看版本号,发现一家被更新到了7.6
CentOS Linux release 7.6.1810 (Core) 

3、卸载旧版本

卸载旧版本(如果安装过旧版本的话,如果之前没有安装过,这一步可以忽略)

[root@localhost ~]# yum remove docker  docker-common docker-selinux docker-engine
Loaded plugins: fastestmirror, langpacks
No Match for argument: docker
No Match for argument: docker-common
No Match for argument: docker-selinux
No Match for argument: docker-engine
No Packages marked for removal
###以上是没有安装过旧版本显示出来的会话
[root@localhost ~]# 

4、安装所需的软件包

安装需要的软件包, yum-util提供yum-config-manager功能,另外两个是devicemapper驱动依赖的,不过部分系统在更新完之后执行这一个步骤,一般会显示“nothing to do”,是正常现象,请放行使用

[root@localhost ~]# yum install -y yum-utils device-mapper-persistent-data lvm2
###在执行这一条命令的过程中,也是需要输入y进行确认的,直接输入即可
###如果之前更新过yum包,那这里会不需要安装的,如下所示
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
base                                                                                                                                                                                           | 3.6 kB  00:00:00     
docker-ce-stable                                                                                                                                                                               | 3.5 kB  00:00:00     
extras                                                                                                                                                                                         | 3.4 kB  00:00:00     
updates                                                                                                                                                                                        | 3.4 kB  00:00:00     
Package yum-utils-1.1.31-50.el7.noarch already installed and latest version
Package device-mapper-persistent-data-0.7.3-3.el7.x86_64 already installed and latest version
Package 7:lvm2-2.02.180-10.el7_6.3.x86_64 already installed and latest version
Nothing to do
[root@localhost ~]# 

5、查看仓库中的Docker版本

可以查看所有仓库中所有docker版本,并选择特定版本安装

[root@localhost ~]# yum list docker-ce --showduplicates | sort -r
 * updates: mirrors.aliyun.com
Loading mirror speeds from cached hostfile
Loaded plugins: fastestmirror, langpacks
Installed Packages
 * extras: mirrors.aliyun.com
docker-ce.x86_64            3:18.09.5-3.el7                    docker-ce-stable 
docker-ce.x86_64            3:18.09.5-3.el7                    @docker-ce-stable
docker-ce.x86_64            3:18.09.4-3.el7                    docker-ce-stable 
docker-ce.x86_64            3:18.09.3-3.el7                    docker-ce-stable 
docker-ce.x86_64            3:18.09.2-3.el7                    docker-ce-stable 
docker-ce.x86_64            3:18.09.1-3.el7                    docker-ce-stable 
docker-ce.x86_64            3:18.09.0-3.el7                    docker-ce-stable 
docker-ce.x86_64            18.06.3.ce-3.el7                   docker-ce-stable 
docker-ce.x86_64            18.06.2.ce-3.el7                   docker-ce-stable 
docker-ce.x86_64            18.06.1.ce-3.el7                   docker-ce-stable 
docker-ce.x86_64            18.06.0.ce-3.el7                   docker-ce-stable 
docker-ce.x86_64            18.03.1.ce-1.el7.centos            docker-ce-stable 
docker-ce.x86_64            18.03.0.ce-1.el7.centos            docker-ce-stable 
docker-ce.x86_64            17.12.1.ce-1.el7.centos            docker-ce-stable 
docker-ce.x86_64            17.12.0.ce-1.el7.centos            docker-ce-stable 
docker-ce.x86_64            17.09.1.ce-1.el7.centos            docker-ce-stable 
docker-ce.x86_64            17.09.0.ce-1.el7.centos            docker-ce-stable 
docker-ce.x86_64            17.06.2.ce-1.el7.centos            docker-ce-stable 
docker-ce.x86_64            17.06.1.ce-1.el7.centos            docker-ce-stable 
docker-ce.x86_64            17.06.0.ce-1.el7.centos            docker-ce-stable 
docker-ce.x86_64            17.03.3.ce-1.el7                   docker-ce-stable 
docker-ce.x86_64            17.03.2.ce-1.el7.centos            docker-ce-stable 
docker-ce.x86_64            17.03.1.ce-1.el7.centos            docker-ce-stable 
docker-ce.x86_64            17.03.0.ce-1.el7.centos            docker-ce-stable 
 * base: mirrors.aliyun.com
Available Packages
[root@localhost ~]# 

6、安装docker

[root@localhost ~]# yum install docker-ce 
###由于repo中默认只开启stable仓库,故这里安装的是最新稳定版17.12.0


[root@localhost ~]# yum install XXXX  
###如果不想安装做默认的最新的稳定版,那就使用这条命令来安装你想要的版本
###	例如:sudo yum install docker-ce-17.12.0.ce。但是如果执行了上一条命令,这一命令就nothing to do

安装完成之后,运行查看docker版本的命令,如果出现如下的情况,则为安装成功了(有client和service两部分表示docker安装启动都成功了)

[root@localhost ~]# docker version
Client:
 Version:           18.09.5
 API version:       1.39
 Go version:        go1.10.8
 Git commit:        e8ff056
 Built:             Thu Apr 11 04:43:34 2019
 OS/Arch:           linux/amd64
 Experimental:      false

Server: Docker Engine - Community
 Engine:
  Version:          18.09.5
  API version:      1.39 (minimum version 1.12)
  Go version:       go1.10.8
  Git commit:       e8ff056
  Built:            Thu Apr 11 04:13:40 2019
  OS/Arch:          linux/amd64
  Experimental:     false
[root@localhost ~]# 

7、启动并加入开机启动

这里就直接输入命令开始启动,如果没有错误的话,应该是下面这样

[root@localhost ~]# systemctl start docker
###启动docker
[root@localhost ~]# systemctl enable docker
###加入开机启动
###这两条命令没有任何显示即为成功,如果有问题会直接报错。如果没有任何显示的话,这时候可以查看一下docker的状态
[root@localhost ~]# systemctl status docker
● docker.service - Docker Application Container Engine
   Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled)
   Active: active (running) since Thu 2019-04-25 02:30:55 PDT; 4h 57min ago
     Docs: https://docs.docker.com
 Main PID: 6762 (dockerd)
    Tasks: 11
   Memory: 65.9M
   CGroup: /system.slice/docker.service
           └─6762 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock

Apr 25 02:31:20 localhost.localdomain dockerd[6762]: time="2019-04-25T02:31:20.590666763-07:00" level=warning msg="overlay2: the backing xfs filesystem is formatted without d_type support, which leads to incorrec...
Apr 25 02:31:20 localhost.localdomain dockerd[6762]: time="2019-04-25T02:31:20.594310216-07:00" level=info msg="[graphdriver] using prior storage driver: overlay2"
Apr 25 02:31:20 localhost.localdomain dockerd[6762]: time="2019-04-25T02:31:20.763955599-07:00" level=info msg="Graph migration to content-addressability took 0.00 seconds"
Apr 25 02:31:20 localhost.localdomain dockerd[6762]: time="2019-04-25T02:31:20.773216850-07:00" level=info msg="Loading containers: start."
Apr 25 02:30:54 localhost.localdomain dockerd[6762]: time="2019-04-25T02:30:54.542179572-07:00" level=info msg="Default bridge (docker0) is assigned with an IP address 172.17.0.0/16. Daemon option -...ed IP address"
Apr 25 02:30:55 localhost.localdomain dockerd[6762]: time="2019-04-25T02:30:55.198779391-07:00" level=info msg="Loading containers: done."
Apr 25 02:30:55 localhost.localdomain dockerd[6762]: time="2019-04-25T02:30:55.714092284-07:00" level=info msg="Docker daemon" commit=e8ff056 graphdriver(s)=overlay2 version=18.09.5
Apr 25 02:30:55 localhost.localdomain dockerd[6762]: time="2019-04-25T02:30:55.728857958-07:00" level=info msg="Daemon has completed initialization"
Apr 25 02:30:55 localhost.localdomain dockerd[6762]: time="2019-04-25T02:30:55.762959230-07:00" level=info msg="API listen on /var/run/docker.sock"
Apr 25 02:30:55 localhost.localdomain systemd[1]: Started Docker Application Container Engine.
Hint: Some lines were ellipsized, use -l to show in full.
###可以看到,已经成功运行了
[root@localhost ~]# 

Command 2:Docker常用命令

一些docker的常用命令请先记好:

systemctl start docker         ###启动docker
systemctl stop docker          ###停止docker
systemctl restart docker       ###重启docker 
systemctl status docker        ###查看docker状态
systemctl enable docker        ###开机启动
docker info                    ###查看docker概要信息
docker --help                  ###查看docker帮助文档

如果启动有错误,请看错误示例

Error 1:Docker服务启动错误(防火墙未关闭)

先运行一下docker info看一下,查看是否有警告信息,因为可能会有警告,导致无法正常启动

[root@localhost ~]# docker info
Containers: 4
 Running: 0
 Paused: 0
 Stopped: 4
Images: 2
Server Version: 18.09.5
Storage Driver: overlay2
 Backing Filesystem: xfs
 Supports d_type: false
 Native Overlay Diff: true
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins:
 Volume: local
 Network: bridge host macvlan null overlay
 Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
Swarm: inactive
Runtimes: runc
Default Runtime: runc
Init Binary: docker-init
containerd version: bb71b10fd8f58240ca47fbb579b9d1028eea7c84
runc version: 2b18fe1d885ee5083ef9f0838fee39b62d653e30
init version: fec3683
Security Options:
 seccomp
  Profile: default
Kernel Version: 3.10.0-957.10.1.el7.x86_64
Operating System: CentOS Linux 7 (Core)
OSType: linux
Architecture: x86_64
CPUs: 1
Total Memory: 972.6MiB
Name: localhost.localdomain
ID: WXOC:K7N5:U4EE:B3DH:ZSFK:PJDI:X342:VYQD:5IZ4:3I7B:N57A:YXO5
Docker Root Dir: /var/lib/docker
Debug Mode (client): false
Debug Mode (server): false
Registry: https://index.docker.io/v1/
Labels:
Experimental: false
Insecure Registries:
 127.0.0.0/8
Live Restore Enabled: false
Product License: Community Engine

WARNING: bridge-nf-call-iptables is disabled        ###这个是防火墙的警告
WARNING: bridge-nf-call-ip6tables is disabled       ###同上
WARNING: overlay2: the backing xfs filesystem is formatted without d_type support, which leads to incorrect behavior.
         Reformat the filesystem with ftype=1 to enable d_type support.
         Running without d_type support will not be supported in future releases.
[root@localhost ~]# 

这里有三条预警,需要关闭防火墙,并且重启系统

[root@localhost ~]# systemctl stop firewalld       ###关闭防火墙
systemctl disable firewalld    
[root@localhost ~]# systemctl disable firewalld    ###禁用防火墙
[root@localhost ~]# reboot                         ###然后重启一下

然后再试试重新启动就可以了

二、运行Docker

1、试运行Docker

在启动了Docker的服务之后,就要试一下Docker运行会不会报错,我们试运行一下hello-world

[root@localhost ~]# docker run hello-world

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

[root@localhost ~]#

此处说明docker的服务是正常运行的,已经生成了hello-world镜像,我们查看一下所有的镜像

[root@localhost ~]# docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
hello-world         latest              fce289e99eb9        3 months ago        1.84kB
[root@localhost ~]# 

可以看到Docker的镜像列表中已经被创建了hello-world的镜像

这里解释一下镜像(Image)和容器(Container)的关系,就像是面向对象编程中的 “类” 和 “实例” 一样,镜像是静态的,容器是镜像运行时的实体。镜像是图纸上的画着的高达模型,容器是按照图纸真正做出来的高达模型。同时容器可以创建、运行、停止、删除、暂停等。

我们找一下有哪些可用的镜像

[root@localhost ~]# docker search centos
NAME                               DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
centos                             The official build of CentOS.                   5332                [OK]                
ansible/centos7-ansible            Ansible on Centos7                              121                                     [OK]
jdeathe/centos-ssh                 CentOS-6 6.10 x86_64 / CentOS-7 7.5.1804 x86…   108                                     [OK]
consol/centos-xfce-vnc             Centos container with "headless" VNC session…   88                                      [OK]
imagine10255/centos6-lnmp-php56    centos6-lnmp-php56                              55                                      [OK]
centos/mysql-57-centos7            MySQL 5.7 SQL database server                   51                                      
tutum/centos                       Simple CentOS docker image with SSH access      44                                      
openshift/base-centos7             A Centos7 derived base image for Source-To-I…   43                                      
gluster/gluster-centos             Official GlusterFS Image [ CentOS-7 +  Glust…   41                                      [OK]
centos/postgresql-96-centos7       PostgreSQL is an advanced Object-Relational …   37                                      
centos/python-35-centos7           Platform for building and running Python 3.5…   35                                      
kinogmt/centos-ssh                 CentOS with SSH                                 26                                      [OK]
centos/php-56-centos7              Platform for building and running PHP 5.6 ap…   20                                      
openshift/jenkins-2-centos7        A Centos7 based Jenkins v2.x image for use w…   20                                      
pivotaldata/centos-gpdb-dev        CentOS image for GPDB development. Tag names…   10                                      
openshift/wildfly-101-centos7      A Centos7 based WildFly v10.1 image for use …   6                                       
openshift/jenkins-1-centos7        DEPRECATED: A Centos7 based Jenkins v1.x ima…   4                                       
darksheer/centos                   Base Centos Image -- Updated hourly             3                                       [OK]
pivotaldata/centos                 Base centos, freshened up a little with a Do…   3                                       
pivotaldata/centos-mingw           Using the mingw toolchain to cross-compile t…   2                                       
pivotaldata/centos-gcc-toolchain   CentOS with a toolchain, but unaffiliated wi…   1                                       
blacklabelops/centos               CentOS Base Image! Built and Updates Daily!     1                                       [OK]
smartentry/centos                  centos with smartentry                          0                                       [OK]
pivotaldata/gpdb6-centos6-build    CentosOS 6 image for GPDB 6 compilation         0                                       
jameseckersall/sonarr-centos       Sonarr on CentOS 7                              0                                       [OK]
[root@localhost ~]# 

可以看到centos是官方的镜像,我们等下就使用这个镜像,还有其他的镜像可选,这里就暂时不做赘述

Command 3:Docker镜像和容器常用命令

这里补充一下Docker的镜像和容器的常用命令

docker ps          ###查看活动的容器
docker ps -a       ###查看所有的容器(包括终止状态的)
docker image ls    ###查看镜像
docker run         ###基于镜像新建一个容器
docker container start 容器id         ###启动已经终止的容器,但不会进入容器,容器在后台执行
docker container stop 容器id          ###终止容器
docker container rm -f 容器id         ###强制删除终止状态的容器
docker run 镜像name                   ###创建镜像
docker image rm 镜像id                ###删除镜像
docker image rm -f 镜像id             ###强制删除镜像

###一般进入docker容器的命令有两个:attach和exec
docker attach 容器id            ###进入容器,使用exit退出时,容器会终止
docker exec -it 容器id  bash    ###进入容器,使用exit退出时,容器不会终止(推荐)

Error 2:进入Docker镜像启动错误(Selinux)

如果入Docker镜像启动错误的时候出现下面这种错误的话,试试关闭Selinux

[root@localhost ~]# docker run -it centos
permission denied
Error response from daemon:Cannot start container 54bnm34sdf34qw878zxb989df:[8] System error:permission denied
###中间这串字符为docker的镜像id,不一定会和本文一样

有两种方法解决:

  1. 执行setenforce命令
[root@localhost ~]# setenforce 0        ###会立即立刻生效,不需要重启
  1. 修改配置文件

修改/etc/selinux/config文件中的SELINUX=disabled ,然后重启系统生效。
这里关闭防火墙的方法可以参考其他博主的方法linux常用命令和关闭防火墙

个人建议两个步骤都执行,这样可以确保系统重启之后selinux也是关闭状态,就不用重复执行setenforce命令了

2、正式运行Docker

在上面试运行Docker之后,我们开始运行,在运行之前,先把刚刚创建的hello-world的镜像先删除,但是发现出现错误,无法删除,报错情况如下情况所示

Error 3:删除Docker镜像报错

删除的时候报错,显示无法删除,是因为镜像中的有容器存在,要先删除镜像中的容器,才能删除镜像

[root@localhost ~]# docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
hello-world         latest              fce289e99eb9        3 months ago        1.84kB
[root@localhost ~]# docker image rm fce289e99eb9
Error response from daemon: conflict: unable to delete fce289e99eb9 (must be forced) - image is being used by stopped container 46ef9aa565d7
[root@localhost ~]# docker ps -a
###查看所有容器,不管其容器状态如何
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                    PORTS               NAMES
99d674bc5384        hello-world         "/hello"            2 days ago          Exited (0) 2 days ago                         gracious_cartwright
46ef9aa565d7        hello-world         "/hello"            7 days ago          Exited (0) 7 days ago                         nostalgic_allen
[root@localhost ~]# 
[root@localhost ~]# 
[root@localhost ~]# docker container rm 99d674bc5384
###删除这些容器
99d674bc5384
[root@localhost ~]# docker container rm  46ef9aa565d7
46ef9aa565d7
[root@localhost ~]# docker ps -a
###查看是否还有这些容器,没有了就再次进行删除
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                    PORTS               NAMES
[root@localhost ~]# docker image rm fce289e99eb9
Untagged: hello-world:latest
Untagged: hello-world@sha256:92695bc579f31df7a63da6922075d0666e565ceccad16b59c3374d2cf4e8e50e
Deleted: sha256:fce289e99eb9bca977dae136fbe2a82b6b7d4c372474c9235adc1741675f587e
Deleted: sha256:af0b15c8625bb1938f1d7b17081031f649fd14e6b233688eea3c5483994a66a3
###以上就删除成功
[root@localhost ~]# 

以上问题处理结束。

回到文章上来,我们开始正式安装镜像,我们先要安装一个镜像

Command 4:Docker拉取镜像(安装)

我们开始使用docker拉取镜像,你可以理解为安装镜像,但是根据中文理解为拉取,属于在线拉取,可以更换其他的Docker源来拉取,如以下命令
(一般不需要修改,我们前面更换过yum的镜像源)

curl -sSL http://acs-public-mirror.oss-cn-hangzhou.aliyuncs.com/docker-engine/internet| sh -
###安装阿里云最新版本的Docker Engine
curl -sSL http://acs-public-mirror.oss-cn-hangzhou.aliyuncs.com/docker-engine/internet| sh /dev/stdin 1.9.1
###安装1.9.1版本的Docker Engine
curl -sSL https://get.daocloud.io/docker | sh 
###安装DaoCloud的Docker(国内做Docker一家公司)

直接拉取Docker进行安装的命令如下

docker pull [选项] [Docker Registry 地址[:端口号]/]仓库名[:标签]

docker pull registry.aliyuncs.com/kaibb/hadoop
###这个是阿里云的docker镜像,但是我们这里没有用上,因为十分的快捷方便,甚至不用自己去安装hadoop,所以我们没有采用

回到正文上来,我们这里就直接用的docker pull centos:7这条命令安装镜像

[root@localhost ~]# docker pull centos:7
###直接创建centos7这个镜像
7: Pulling from library/centos
8ba884070f61: Pull complete 
Digest: sha256:8d487d68857f5bc9595793279b33d082b03713341ddec91054382641d14db861
Status: Downloaded newer image for centos:7
[root@localhost ~]# docker image ls
###查看是否安装成功
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              7                   9f38484d220f        6 weeks ago         202MB
[root@localhost ~]# docker network create --subnet=192.168.137.0/16 netgroup
c8df49a0406f7c9eb622bc368523026d9796f69410bb91c91504dce6697a285a
###创建完成后,我们开始构建Docker的网络,这一步是创建子网
[root@localhost ~]# docker run -d --privileged -ti -v /home/docker/build:/root/build --name hadoop -h hadoopmaster --net netgroup --ip 192.168.137.25 centos:7 /usr/sbin/init
f7c97011a1a86e3c7ed5d7badcdc7c4e282defa0ef3c8af5e7276c11c26c471f
[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS               NAMES
f7c97011a1a8        centos:7            "/usr/sbin/init"    9 seconds ago       Up 8 seconds                                   hadoop

Branch 1:使用阿里云的镜像

我为什么不用阿里云的镜像,因为一旦用了,就直接终结这篇文章了。
这个方法我们引用的是另外一篇博主的文章在 Centos7 下使用 Docker 快速搭建 Hadoop 集群
还参考的另一个博主的文章使用Docker搭建hadoop集群

但是,我创建了一个分支,给满足好奇心的你

上面说的这个直接拉取阿里云的镜像,我们执行一下试一下(前提条件是你装好了这个docker并且能够成功运行)

[root@localhost ~]# docker pull registry.aliyuncs.com/kaibb/hadoop
###安装阿里云镜像
Using default tag: latest
latest: Pulling from kaibb/hadoop
9502adfba7f1: Pull complete 
4332ffb06e4b: Pull complete 
2f937cc07b5f: Pull complete 
a3ed95caeb02: Pull complete 
3d79f27b1e40: Pull complete 
77a36faf2c9f: Pull complete 
96aedd27b519: Pull complete 
296414404601: Pull complete 
c72f0e485bc3: Pull complete 
5e7788487957: Pull complete 
1255c865c6b8: Pull complete 
Digest: sha256:aea029939a52d589596fa96c20db44169f54b257ef22f2153cd52d8a11e6925b
Status: Downloaded newer image for registry.aliyuncs.com/kaibb/hadoop:latest
[root@localhost ~]# docker image ls 
###查看阿里云的镜像是否拉取成功
REPOSITORY                           TAG                 IMAGE ID            CREATED             SIZE
centos                               7                   9f38484d220f        6 weeks ago         202MB
registry.aliyuncs.com/kaibb/hadoop   latest              44c31aee79de        3 years ago         927MB
[root@localhost ~]# docker ps -a
###查看生成的容器,可以发现没有刚刚的阿里云镜像生成的容器
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
f7c97011a1a8        centos:7            "/usr/sbin/init"    4 minutes ago       Up 4 minutes                            hadoop
[root@localhost ~]# docker run -d --privileged -ti -v /home/docker/ali-build:/root/build --name ali-hadoop -h hadoopmaster --net netgroup --ip 192.168.137.30 registry.aliyuncs.com/kaibb/hadoop
###使用刚刚的阿里云镜像来创建容器
###我使用了刚刚创建的网络netgroup,如果没有创建的话,可能会报错以下错误
###docker: Error response from daemon: network netgroup not found.
###这里如果没有创建netgroup的子网,那就使用以下这条命令
###docker run -d --privileged -ti -v /home/docker/ali-build:/root/build --name ali-hadoop -h hadoopmaster registry.aliyuncs.com/kaibb/hadoop
609c7f4981dec7c418e18a75643c091931b40a3ba8f02195ca2ceaed2bf980f1
[root@localhost ~]# docker ps -a
###查看容器是否创建成功
CONTAINER ID        IMAGE                                COMMAND               CREATED             STATUS              PORTS                                                                                                                                        NAMES
609c7f4981de        registry.aliyuncs.com/kaibb/hadoop   "/usr/sbin/sshd -D"   24 seconds ago      Up 19 seconds       22/tcp, 2122/tcp, 8030-8033/tcp, 8040/tcp, 8042/tcp, 8088/tcp, 19888/tcp, 49707/tcp, 50010/tcp, 50020/tcp, 50070/tcp, 50075/tcp, 50090/tcp   ali-hadoop
[root@localhost ~]# docker container start 609c7f4981de
###运行刚刚的容器
609c7f4981de
[root@localhost ~]# docker exec -it 609c7f4981de bash
###进入容器
root@hadoopmaster:/# hadoop version
###进入容器后可以发现前面的会话开端变了,@后面你命名的容器名
###该镜像本身就安装好了hadoop和java,可以用查看版本和环境变量的命令看一下是否有存在hadoop和java
Hadoop 2.7.2
Subversion https://git-wip-us.apache.org/repos/asf/hadoop.git -r b165c4fe8a74265c792ce23f546c64604acf0e41
Compiled by jenkins on 2016-01-26T00:08Z
Compiled with protoc 2.5.0
From source with checksum d0fda26633fa762bff87ec759ebe689c
This command was run using /opt/tools/hadoop-2.7.2/share/hadoop/common/hadoop-common-2.7.2.jar
root@hadoopmaster:/# java -version
java version "1.8.0_77"
Java(TM) SE Runtime Environment (build 1.8.0_77-b03)
Java HotSpot(TM) 64-Bit Server VM (build 25.77-b03, mixed mode)
root@hadoopmaster:/# echo ${PATH}
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/opt/tools/jdk1.8.0_77/bin:/opt/tools/hadoop/bin:/opt/tools/hadoop/sbin
root@hadoopmaster:/# exit
###使用exit命令来退出容器
exit
[root@localhost ~]# 

可以看到已经安装好了hadoop和java环境,

这里需要注意一下,我们刚刚是使用这个阿里云的镜像创建了一个主节点的容器而已,我们还要依次创建几个从节点的容器,使用以下的语句来创建的其他的几个节点

###注意:这里一定是切回宿主机再进行创建,刚刚我们还在容器中,现在要切回宿主机创建从节点的容器了
[root@localhost ~]# docker run -d --privileged -ti -v /home/docker/ali-build:/root/build --name ali-hadoop-s1 -h hadoopslave1 registry.aliyuncs.com/kaibb/hadoop
8c1d48cfe1e81dcd01c8257c55793c167ce4bf318e2af7378978f1c15037e3a9
[root@localhost ~]# docker run -d --privileged -ti -v /home/docker/ali-build:/root/build --name ali-hadoop-s2 -h hadoopslave2 registry.aliyuncs.com/kaibb/hadoop
d74e3c70cf3ad8f3430e9cabb7263ed004da7a689b5a03d1e580301b850617f4
###创建完成之后,我们看一下是否创建成功,这里的话,应该是有三个容器在了
[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE                                COMMAND               CREATED             STATUS              PORTS                                                                                                                                        NAMES
d74e3c70cf3a        registry.aliyuncs.com/kaibb/hadoop   "/usr/sbin/sshd -D"   6 minutes ago       Up 6 minutes        22/tcp, 2122/tcp, 8030-8033/tcp, 8040/tcp, 8042/tcp, 8088/tcp, 19888/tcp, 49707/tcp, 50010/tcp, 50020/tcp, 50070/tcp, 50075/tcp, 50090/tcp   ali-hadoop-s2
8c1d48cfe1e8        registry.aliyuncs.com/kaibb/hadoop   "/usr/sbin/sshd -D"   7 minutes ago       Up 7 minutes        22/tcp, 2122/tcp, 8030-8033/tcp, 8040/tcp, 8042/tcp, 8088/tcp, 19888/tcp, 49707/tcp, 50010/tcp, 50020/tcp, 50070/tcp, 50075/tcp, 50090/tcp   ali-hadoop-s1
609c7f4981de        registry.aliyuncs.com/kaibb/hadoop   "/usr/sbin/sshd -D"   8 minutes ago       Up 8 minutes        22/tcp, 2122/tcp, 8030-8033/tcp, 8040/tcp, 8042/tcp, 8088/tcp, 19888/tcp, 49707/tcp, 50010/tcp, 50020/tcp, 50070/tcp, 50075/tcp, 50090/tcp   ali-hadoop

创建完成之后,为了让这三个容器能相互通讯的免密登陆(本身是处于同一网络之下),我们来配置一下无密的ssh的机制,就是为了让两台机器之间ssh不需要密码,这里可以看其他博主的文章关于SSH-keygen

我们先进入到主节点环境中,先把主节点运行起来

[root@localhost ~]# docker container start 609c7f4981de
609c7f4981de
[root@localhost ~]# docker exec -it 609c7f4981de bash

然后,我们开始配置ssh,为了让三台容器都能相互免密ssh到

root@hadoopmaster:/# /etc/init.d/ssh start
###启用ssh会话
 * Starting OpenBSD Secure Shell server sshd    
root@hadoopmaster:/# ssh-keygen -t rsa
###开启免密
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): /root/.ssh/id_rsa   
###这里输入相同的路径就可以 
/root/.ssh/id_rsa already exists.
Overwrite (y/n)? y
###确定重写
Enter passphrase (empty for no passphrase): 
###这里两遍都不需要输入
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
9e:17:e0:61:d3:a2:72:b6:c7:c4:31:f7:5f:ed:0f:b9 root@hadoopmaster
The key's randomart image is:
+---[RSA 2048]----+
|                 |
|         .       |
|        O o      |
|       = O .    .|
|    . + S . .   o|
|     + = . . . + |
|      . = .   + .|
|       . .     o.|
|              E .|
+-----------------+
root@hadoopmaster:/# cat /root/.ssh/id_rsa.pub > /root/.ssh/authorized_keys
root@hadoopmaster:/# cat  /root/.ssh/authorized_keys
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDABdZ/9AA5P4XfOwH4EPxxVWoIBB5zZvXBCAIcnaEr9zdU8i9QHsbkBtTGSUvKd7ZszE847/Hup2TaIMhiuFkeJlgcdO2lSsBkB1X7kowvt2G3+H0TqbOWkenoTL0dyd5adVs4Qg1aWliRrkX4uUbdhJwFtzKMeUyFPoceAfUYnIQwfXr8kiC1mZ4bh2Ib6t2TJh/9aM4/nC0JtIlZiRxWA4BLdEP8V3lqojv8Cjtlxc8GlsCeToqaUY3bIvmoEVL7K7a45jUOviDIHyLuD2tGGVHFgOJIrxbViGGL/76akdS2e08FsdYl75+5JwEXsVEEKrZRq0kDJSav0FDQUhgh root@hadoopmaster
###这一串要记下来
root@hadoopmaster:~/.ssh#

在其他的两台从节点中,要做相同的事,以下就开始简写一下
同样,也是先启动容器

在这里插入代码片

然后要配置一下java环境

root@ali-hadoop:/opt/tools/jdk1.8.0_77/bin# whereis java
java: /opt/tools/jdk1.8.0_77/bin/java
root@ali-hadoop:/opt/tools/jdk1.8.0_77/bin# vim /etc/profile
root@ali-hadoop:/opt/tools/jdk1.8.0_77/bin# cat /etc/profile
# /etc/profile: system-wide .profile file for the Bourne shell (sh(1))
# and Bourne compatible shells (bash(1), ksh(1), ash(1), ...).

if [ "$PS1" ]; then
  if [ "$BASH" ] && [ "$BASH" != "/bin/sh" ]; then
    # The file bash.bashrc already sets the default PS1.
    # PS1='\h:\w\$ '
    if [ -f /etc/bash.bashrc ]; then
      . /etc/bash.bashrc
    fi
  else
    if [ "`id -u`" -eq 0 ]; then
      PS1='# '
    else
      PS1='$ '
    fi
  fi
fi

# The default umask is now handled by pam_umask.
# See pam_umask(8) and /etc/login.defs.

if [ -d /etc/profile.d ]; then
  for i in /etc/profile.d/*.sh; do
    if [ -r $i ]; then
      . $i
    fi
  done
  unset i
fi

export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL
export JAVA_HOME=/opt/tools/jdk1.8.0_77
export PATH=$JAVA_HOME/bin:$PATH
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
root@ali-hadoop:/opt/tools/jdk1.8.0_77/bin# source /etc/profile
root@ali-hadoop:/opt/tools/jdk1.8.0_77/bin# 

结束分支,这个可以自行体验一下


回到正文上来

微信赞赏码

你可能感兴趣的:(HADOOP)