RedHat/Centos

关注公众号 jb51net

关闭
操作系统 > RedHat/Centos >

CentOS6.x上部署Docker容器环境的全流程攻略

seanlook

Docker.io 是轻量级的“容器引擎+映像仓库”,在LXC(linux轻量级容器)的基础上构建,可以运行任何应用程序。
docker.io的核心层由以下几个部分组成:
1、可执行程序,“/usr/bin/docker”
2、docker.io网站上有一系列创建好的操作系统和应用程序映像
3、AUFS(另一个统一文件系统)来实现文件系统的快照,文件变化,控制只读或读写的需求。
4、LXC(Linux轻量级容器)
5、Cgroups(各种资源和命名空间的管理)
在本文写作的时候,最新版本是0.5.3,但目前开发很活跃,一些功能和特性可能在新版本中会发生变化。
需要注意的是CentOS 6.5与7.0的安装是有一点点不同的,CentOS-6上docker的安装包叫docker-io,并且来源于Fedora epel库,这个仓库维护了大量的没有包含在发行版中的软件,所以先要安装EPEL,而CentOS-7的docker直接包含在官方镜像源的Extras仓库(CentOS-Base.repo下的[extras]节enable=1启用)。前提是都需要联网,具体安装过程如下。

1. 禁用selinux
[/code]
# getenforce
enforcing
# setenforce 0
permissive
# vi /etc/selinux/config
SELINUX=disabled
...
[/code]
2. 安装 Fedora EPEL
epel-release-6-8.noarch.rpm包在发行版的介质里面已经自带了,可以从rpm安装。


# yum install epel-release-6-8.noarch.rpm
//或
yum -y install http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm

如果出现GPG key retrieval failed: [Errno 14] Could not open/read file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6问题,请在线安装epel,下载RPM-GPG-KEY-EPEL-6文件。
这一步执行之后,会在/etc/yum.repos.d/下生成epel.repo、epel-testing.repo两个文件,用于从Fedora官网下载rpm包。

3. 检查内核版本


# uname -r
2.6.32-431.el6.x86_64
# cat /etc/redhat-release
CentOS release 6.5 (Final)

看到这个最低的内核版本,事实运行起来是没太大问题的,你也可以升级到3.10.x版本。
另外你也可以运行脚本check-config.sh,来检查内核模块符不符合(下面有些missing的,我的docker还是可以正常启动):


[root@sean ~]# ./check-config
warning: /proc/config.gz does not exist, searching other paths for kernel config...
info: reading kernel config from /boot/config-2.6.32-431.el6.x86_64 ...
Generally Necessary:
- cgroup hierarchy: properly mounted [/cgroup]
- CONFIG_NAMESPACES: enabled
- CONFIG_NET_NS: enabled
- CONFIG_PID_NS: enabled
- CONFIG_IPC_NS: enabled
- CONFIG_UTS_NS: enabled
- CONFIG_DEVPTS_MULTIPLE_INSTANCES: enabled
- CONFIG_CGROUPS: enabled
- CONFIG_CGROUP_CPUACCT: enabled
- CONFIG_CGROUP_DEVICE: enabled
- CONFIG_CGROUP_FREEZER: enabled
- CONFIG_CGROUP_SCHED: enabled
- CONFIG_MACVLAN: enabled
- CONFIG_VETH: enabled
- CONFIG_BRIDGE: enabled
- CONFIG_NF_NAT_IPV4: missing
- CONFIG_IP_NF_TARGET_MASQUERADE: enabled
- CONFIG_NETFILTER_XT_MATCH_ADDRTYPE: missing
- CONFIG_NETFILTER_XT_MATCH_CONNTRACK: enabled
- CONFIG_NF_NAT: enabled
- CONFIG_NF_NAT_NEEDED: enabled
Optional Features:
- CONFIG_MEMCG_SWAP: missing
- CONFIG_RESOURCE_COUNTERS: enabled
- CONFIG_CGROUP_PERF: enabled
- Storage Drivers:
- "aufs":
- CONFIG_AUFS_FS: missing
- CONFIG_EXT4_FS_POSIX_ACL: enabled
- CONFIG_EXT4_FS_SECURITY: enabled
- "btrfs":
- CONFIG_BTRFS_FS: enabled
- "devicemapper":
- CONFIG_BLK_DEV_DM: enabled
- CONFIG_DM_THIN_PROVISIONING: enabled
- CONFIG_EXT4_FS: enabled
- CONFIG_EXT4_FS_POSIX_ACL: enabled
- CONFIG_EXT4_FS_SECURITY: enabled
  
假如你是自己编译内核,请特别留意几个绝对不能缺少的:DM_THIN_PROVISIONING、IP_NF_TARGET_MASQUERADE、NF_NAT。(AUFS_FS没有对应选项,还不清楚怎么回事,但不是必须)

4. 安装 docker-io


# yum install docker-io
Dependencies Resolved
===========================================================================================
Package Arch Version Repository Size
===========================================================================================
Installing:
docker-io x86_64 1.1.2-1.el6 epel 4.5 M
Installing for dependencies:
lua-alt-getopt noarch 0.7.0-1.el6 epel 6.9 k
lua-filesystem x86_64 1.4.2-1.el6 epel 24 k
lua-lxc x86_64 1.0.6-1.el6 epel 15 k
lxc x86_64 1.0.6-1.el6 epel 120 k
lxc-libs x86_64 1.0.6-1.el6 epel 248 k
Transaction Summary
===========================================================================================
Install 6 Package(s)

许多文档介绍到这里,下一步为挂载/cgroup文件系统,我的docker版本为1.1.2,没有修改/etc/fstab的步骤。

5. 启动试运行


# service docker start
//或
# docker -d

6. 以守护模式运行docker.io(在一个新的终端里)


[root@localhost ~]# docker -d
2013/08/21 07:47:07 WARNING: Your kernel does not support cgroup swap limit.
2013/08/21 07:47:07 Listening for HTTP on /var/run/docker.sock (unix)

7. 在centos6.4容器里输出hello world


[root@localhost ~]# docker run centos:6.4 echo "hello world"
2013/08/21 07:48:41 POST /v1.4/containers/create
2013/08/21 07:48:41 POST /v1.4/containers/c6bc9e80097e/start
2013/08/21 07:48:41 POST /v1.4/containers/c6bc9e80097e/attach?logs=1&stderr=1&stdout=1&stream=1
hello world

8. 从容器里测试ping


[root@localhost ~]# docker -dns '8.8.8.8' run centos:6.4 ping -c 3 yahoo.com
2013/08/21 08:02:15 POST /v1.4/containers/create
2013/08/21 08:02:15 POST /v1.4/containers/c40a1244f9bc/start
2013/08/21 08:02:15 POST /v1.4/containers/c40a1244f9bc/attach?logs=1&stderr=1&stdout=1&stream=1
PING yahoo.com (98.138.253.109) 56(84) bytes of data.
64 bytes from ir1.fp.vip.ne1.yahoo.com (98.138.253.109): icmp_seq=1 ttl=48 time=323 ms
64 bytes from ir1.fp.vip.ne1.yahoo.com (98.138.253.109): icmp_seq=2 ttl=48 time=329 ms
64 bytes from ir1.fp.vip.ne1.yahoo.com (98.138.253.109): icmp_seq=3 ttl=49 time=302 ms
--- yahoo.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2304ms
rtt min/avg/max/mdev = 302.032/318.318/329.656/11.807 ms

9. 异常
在我的一次安装过程中,很不幸遇到下面的问题:
docker -d启动,或tail -f /var/log/docker查看日志


[f32e7d9f] +job initserver()
[f32e7d9f.initserver()] Creating server
[f32e7d9f] +job serveapi(unix:///var/run/docker.sock)
2014/10/22 13:02:45 Listening for HTTP on unix (/var/run/docker.sock)
Error running DeviceCreate (createPool) dm_task_run failed
[f32e7d9f] -job initserver() = ERR (1)
2014/10/22 13:02:45 Error running DeviceCreate (createPool) dm_task_run failed
\nWed Oct 22 14:35:54 CST 2014\n

再或者是service docker restart


Stopping docker: [ OK ]
Starting cgconfig service: Error: cannot mount cpuset to /cgroup/cpuset: Device or resource busy
/sbin/cgconfigparser; error loading /etc/cgconfig.conf: Cgroup mounting failed
Failed to parse /etc/cgconfig.conf [FAILED]
Starting docker: [ OK ]

全选复制放进笔记Unable to enable network bridge NAT: iptables failed: iptables -I POSTROUTING -t nat -s 172.17.42.1/16 ! -d 172.17.42.1/16 -j MASQUERADE: iptables v1.4.7: can't initialize iptables table `nat': Table does not exist (do you need to insmod?)
Perhaps iptables or your kernel needs to be upgraded.