阿里云ECS部署Docker服务的实现步骤
目录
1、前言
京东2016年618大促,全部应用系统和大部分的DB服务都跑在Docker上。据官方估计,本次大促活动中,京东线上将会启动近15万个Docker容器,从数量上来看,京东是全球范围内Docker的应用大户之一。
为什么docker被京东如此看重,这种微服务化治理到底有何魅力?
决定开始对docker进行下学习并尝试应用到日常业务中来。今天在阿里云ECS的centos上搭建了一个Docker,顺便将一些常用操作记录下,操作系统版本为CentOS6.5。
本文的主要内容介绍阿里云ECS如何安装docker,及docker基础命令详解,通过这篇文章,可以让docker新手开始熟悉和操作docker,慢慢入门。
2、内核支持
官方文档要求Linux kernel至少3.8以上,且docker只能运行在64位的系统中。由于RHEL6和CentOS6的内核版本为2.6,因此必须要先升级内核。
首先,查看阿里云ECS Linux服务器系统内核版本:
$uname -r
2.6.32-220.23.2.ali878.el6.x86_64
2.1、升级内核
由于当前内核版本较低,我们需要把系统内核升级到3.10版本,接下来为具体操作步骤:
(1). yum安装带aufs模块的3.10内核
[dddd@v069208183.sqa.zmf /home/dddd]
$cd /etc/yum.repos.d
$sudo wget http://www.hop5.in/yum/el6/hop5.repo
$sudo yum install kernel-ml-aufs kernel-ml-aufs-devel
(2). 下载内核
(3). 安装内核
(4). 安装成功
(5). 修改内核配置
修改grub的主配置文件/etc/grub.conf,设置default=0,表示第一个title下的内容为默认启动的kernel(一般新安装的内核在第一个位置)
[dddd@v069208183.sqa.zmf /etc/yum.repos.d]
$sudo vi /etc/grub.conf
(6). 重启系统
[dddd@v069208183.sqa.zmf /etc/yum.repos.d]
$sudo reboot
Broadcast message from dddd@v069208183.sqa.zmf
(/dev/pts/1) at 18:32 ...
The system is going down for reboot NOW!
这时候你的内核就成功升级了
[dddd@v069208183.sqa.zmf /home/admin/
$uname -r
3.10.5-3.el6.x86_64
查看内核是否支持aufs:
[dddd@v069208183.sqa.zmf /home/admin/]
$grep aufs /proc/filesystems
nodev aufs
3、Docker安装
3.1、关闭selinux
[dddd@v069208183.sqa.zmf /home/admin/]
$sudo setenforce 0
setenforce: SELinux is disabled
[dddd@v069208183.sqa.zmf /home/admin/]
$sudo sed -i '/^SELINUX=/cSELINUX=disabled' /etc/selinux/config
3.2、安装epel
[dddd@v069208183.sqa.zmf /]
$sudo rpm -ivh http://mirrors.sohu.com/fedora-epel/6/x86_64/epel-release-6-8.noarch.rpm
Retrieving http://mirrors.sohu.com/fedora-epel/6/x86_64/epel-release-6-8.noarch.rpm
warning: /var/tmp/rpm-tmp.NNZYOY: Header V3 RSA/SHA256 Signature, key ID 0608b895: NOKEY
Preparing... ########################################### [100%]
1:epel-release ########################################### [100%]
[dddd@v069208183.sqa.zmf /]
$sudo sed -i 's/^mirrorlist=https/mirrorlist=http/' /etc/yum.repos.d/epel.repo
3.3、yum安装docker-io
[dddd@v069208183.sqa.zmf /]
$sudo yum -y install docker-io
安装成功:
3.4、启动docker
启动的过程中有点小插曲,第一次启动失败。
[dddd@v069208183.sqa.zmf /]
$sudo service docker start
Starting cgconfig service: [ OK ]
Starting docker: [ OK ]
[dddd@v069208183.sqa.zmf /]
$docker version
Client version: 1.7.1
Client API version: 1.19
Go version (client): go1.4.2
Git commit (client): 786b29d/1.7.1
OS/Arch (client): linux/amd64
Get http:///var/run/docker.sock/v1.19/version: dial unix /var/run/docker.sock: permission denied. Are you trying to connect to a TLS-enabled daemon without TLS?
上面看似启动成功,其实是失败了,查看docker日志即可,docker日志地址为/var/log/docker:
$cat /var/log/docker
Sat Jun 25 18:54:55 CST 2016
time="2016-06-25T18:54:56.013084200+08:00" level=info msg="Listening for HTTP on unix (/var/run/docker.sock)"
time="2016-06-25T18:54:56.206980482+08:00" level=warning msg="Running modprobe bridge nf_nat failed with message: insmod /lib/modules/3.10.5-3.el6.x86_64/kernel/net/llc/llc.ko
insmod /lib/modules/3.10.5-3.el6.x86_64/kernel/net/802/stp.ko
insmod /lib/modules/3.10.5-3.el6.x86_64/kernel/net/ipv6/ipv6.ko disable=1
insmod /lib/modules/3.10.5-3.el6.x86_64/kernel/net/bridge/bridge.ko
insmod /lib/modules/3.10.5-3.el6.x86_64/kernel/net/netfilter/nf_conntrack.ko
insmod /lib/modules/3.10.5-3.el6.x86_64/kernel/net/netfilter/nf_nat.ko
, error: exit status 1"
time="2016-06-25T18:54:56.380986865+08:00" level=fatal msg="Error starting daemon: Error initializing network controller: Error creating default "bridge" network: can't find an address range for interface "docker0""
这个问题的来源就是因为docker弱智地为docker0接口选择了一个172.17.42.1的IP,而恰好ECS占用了172.16.0.0/255.240.0.0。
解决方法有两种:
上述两种方法均能过正常启动,本地均已测试通过,不过我还是觉得方法一好,直接把网段写入配置中,省的每次启动都要带上网段。
针对方法一进行如下说明,在/etc/default/docker文件中新增记录DOCKER_OPTS=”–bip=192.168.17.1/24”
[xiaolong.xiao@v069208183.sqa.zmf /var/log]
$sudo vi /etc/default/docker
Error detected while processing /root/.vimrc:
line 16:
E518: Unknown option: foldlevel=100
Press ENTER or type command to continue
有关docker0网桥的详细介绍参考:http://wiki.jikexueyuan.com/project/docker-technology-and-combat/docker0.html
3.5、查看docker版本
利用命令docker version
[dddd@v069208183.sqa.zmf /var/log]
$sudo docker version
Client version: 1.7.1
Client API version: 1.19
Go version (client): go1.4.2
Git commit (client): 786b29d/1.7.1
OS/Arch (client): linux/amd64
Server version: 1.7.1
Server API version: 1.19
Go version (server): go1.4.2
Git commit (server): 786b29d/1.7.1
OS/Arch (server): linux/amd64
3.6、开启启动docker
sudo chkconfig docker on
4、docker命令的使用
1、直接输入docker命令来查看所有的Options和Commands。
查看某一个command的详细使用方法:docker COMMAND –help
[dddd@v069208183.sqa.zmf /home/dddd]
$sudo docker pull -help
Warning: '-help' is deprecated, it will be replaced by '--help' soon. See usage.
Usage: docker pull [OPTIONS] NAME[:TAG|@DIGEST]
Pull an image or a repository from the registry
-a, --all-tags=false Download all tagged images in the repository
--help=false Print usage
2、搜索可用的docker镜像:docker search NAME
[dddd@v069208183.sqa.zmf /]
$sudo docker search centos
INFO[0104] GET /v1.19/images/search?term=centos
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
centos The official build of CentOS. 2373 [OK]
ansible/centos7-ansible Ansible on Centos7 77 [OK]
jdeathe/centos-ssh CentOS-6 6.7 x86_64 / CentOS-7 7.2.1511 x8... 25 [OK]
nimmis/java-centos This is docker images of CentOS 7 with dif... 12 [OK]
million12/centos-supervisor Base CentOS-7 with supervisord launcher, h... 11 [OK]
gluster/gluster-centos Official GlusterFS Image [ CentOS7 + Glus... 9 [OK]
torusware/speedus-centos Always updated official CentOS docker imag... 8 [OK]
nickistre/centos-lamp LAMP on centos setup 4 [OK]
centos/mariadb55-centos7 3 [OK]
nathonfowlie/centos-jre Latest CentOS image with the JRE pre-insta... 3 [OK]
consol/sakuli-centos-xfce Sakuli end-2-end testing and monitoring co... 2 [OK]
darksheer/centos Base Centos Image -- Updated hourly 1 [OK]
blacklabelops/centos CentOS Base Image! Built and Updates Daily! 1 [OK]
timhughes/centos Centos with systemd installed and running 1 [OK]
grossws/centos CentOS 6 and 7 base images with gosu and l... 0 [OK]
kz8s/centos Official CentOS plus epel-release 0 [OK]
harisekhon/centos-scala Scala + CentOS (OpenJDK tags 2.10-jre7 - 2... 0 [OK]
jsmigel/centos-epel Docker base image of CentOS w/ EPEL installed 0 [OK]
labengine/centos Centos image base 0 [OK]
ustclug/centos USTC centos 0 [OK]
januswel/centos yum update-ed CentOS image 0 [OK]
ericuni/centos centos dev 0 [OK]
grayzone/centos auto build for centos. 0 [OK]
dmglab/centos CentOS with some extras - This is for the ... 0 [OK]
repositoryjp/centos Docker Image for CentOS. 0 [OK]
3、下载镜像:docker pull NAME[:TAG]
比如获取最新的centos镜像:docker pull centos:latest
注意:这里要写用docker search搜索到的完整的镜像名。
同时当前下载的是官方的镜像,下载速度会有点慢,centos镜像大约200M,下载耗时半小时。后面有时间将会实践如何搭建私有仓库。
[dddd@v069208183.sqa.zmf /home/dddd]
$sudo docker pull centos:latest
Pulling repository centos
2a332da70fd1: Download complete
3690474eb5b4: Download complete
b48a20c39b28: Download complete
c79ebe41b35a: Download complete
Status: Downloaded newer image for centos:latest
4、查看安装的镜像:docker images [NAME]
$sudo docker images centos
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
centos latest 2a332da70fd1 3 weeks ago 196.8 MB
5、在docker容器中运行命令:docker run IMAGE [COMMAND] [ARG…]
docker run命令有两个参数,一个是镜像名,一个是要在镜像中运行的命令。
注意:IMAGE=REPOSITORY[:TAG],如果IMAGE参数不指定镜像的TAG,默认TAG为latest。
在刚刚下载的镜像中输出”loongshawn”:docker run centos echo ‘loongshawn’
[dddd@v069208183.sqa.zmf /home/dddd]
$sudo docker run centos echo "loongshawn"
loongshawn
6、列出容器:docker ps -a
查看最近生成的容器:docker ps -l
查看正在运行的容器:docker ps
7、显示容器的标准输出:docker logs CONTAINERID
无需拷贝完整的id,一般写最开始的三至四个字符即可。
8、在容器中安装新程序,比如安装ifconfig命令(centos7默认没有ifconfig)
运行镜像,执行ifconfig,找不到此命令。此时进入镜像执行yum install net-tools。
[dddd@v069208183.sqa.zmf /home/dddd]
$sudo docker run -i -t centos /bin/bash
[root@bed98ed5934a /]# ifconfig
bash: ifconfig: command not found
[root@bed98ed5934a /]#
给容器安装网络服务:
执行ifconfig命令:
9、保存对容器的修改并生成新的镜像:docker commit CONTAINERID [REPOSITORY[:TAG]]
REPOSITORY参数可以是新的镜像名字,也可以是旧的镜像名;如果和旧的镜像名和TAG都相同,会覆盖掉旧的镜像。
10、停止正在运行的容器:docker stop CONTAINERID
默认等待10秒钟再杀死指定容器。可以使用-t参数来设置等待时间。
11、查看容器或镜像的详细信息:docker inspect CONTAINERID|IMAGE
参数可以是容器的ID或者是镜像名(NAME:TAG)。
12、删除容器:docker rm CONTAINERID
查看所有容器ID:docker ps -a -q
删除所有的容器:docker rm $(docker ps -a -q)
13、删除镜像:docker rmi IMAGE
14.查看docker的信息,包括Containers和Images数目、kernel版本等。
5、创建容器并登入
1、创建一个新容器并登入:
使用image创建container并进入交互模式,login shell是/bin/bash,现在可以自由的对容器进行操作了。最后使用exit退出容器。
注意:如果IMAGE参数不指定TAG,默认TAG为latest。
$ sudo docker run -i -t centos /bin/bash
2、启动一个退出的容器:docker start CONTAINERID
[dddd@v069208183.sqa.zmf /home/dddd]
$sudo docker start 340943d115b6
340943d115b6
[dddd@v069208183.sqa.zmf /home/dddd]
$sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
340943d115b6 centos "sleep 100" 11 minutes ago Up 6 seconds cocky_pare
3、attach到运行中的容器:docker attach CONTAINERID
6、参考资料
Docker官网教程
CentOS 安装 Docker
Docker入门教程
到此这篇关于阿里云ECS部署Docker服务的实现步骤的文章就介绍到这了,更多相关阿里云ECS部署Docker 内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
您可能感兴趣的文章: