您好,登錄后才能下訂單哦!
今天小編給大家分享一下ECS如何部署Docker服務的相關知識點,內容詳細,邏輯清晰,相信大部分人都還太了解這方面的知識,所以分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后有所收獲,下面我們一起來了解一下吧。
京東2016年618大促,全部應用系統和大部分的DB服務都跑在Docker上。據官方估計,本次大促活動中,京東線上將會啟動近15萬個Docker容器,從數量上來看,京東是全球范圍內Docker的應用大戶之一。
官方文檔要求Linux kernel至少3.8以上,且docker只能運行在64位的系統中。由于RHEL6和CentOS6的內核版本為2.6,因此必須要先升級內核。
首先,查看阿里云ECS Linux服務器系統內核版本:
$uname -r 2.6.32-220.23.2.ali878.el6.x86_64
由于當前內核版本較低,我們需要把系統內核升級到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
[dddd@v069208183.sqa.zmf /home/admin/] $sudo setenforce 0 setenforce: SELinux is disabled [dddd@v069208183.sqa.zmf /home/admin/] $sudo sed -i '/^SELINUX=/c\SELINUX=disabled' /etc/selinux/config
[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
[dddd@v069208183.sqa.zmf /] $sudo yum -y install docker-io
安裝成功:
啟動的過程中有點小插曲,第一次啟動失敗。
[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 \nSat Jun 25 18:54:55 CST 2016\n 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 \ninsmod /lib/modules/3.10.5-3.el6.x86_64/kernel/net/802/stp.ko \ninsmod /lib/modules/3.10.5-3.el6.x86_64/kernel/net/ipv6/ipv6.ko disable=1\ninsmod /lib/modules/3.10.5-3.el6.x86_64/kernel/net/bridge/bridge.ko \ninsmod /lib/modules/3.10.5-3.el6.x86_64/kernel/net/netfilter/nf_conntrack.ko \ninsmod /lib/modules/3.10.5-3.el6.x86_64/kernel/net/netfilter/nf_nat.ko \n, 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”,重啟即可。注意不要用192.168.0.1/24,這段地址也被占用了。
方法二:啟動docker服務在指定的網段。sudo docker –bip 192.168.100.1/24 -d &
上述兩種方法均能過正常啟動,本地均已測試通過,不過我還是覺得方法一好,直接把網段寫入配置中,省的每次啟動都要帶上網段。
針對方法一進行如下說明,在/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
利用命令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
sudo chkconfig docker on
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版本等。
1、創建一個新容器并登入:docker run -i -t IMAGE /bin/bash
使用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
以上就是“ECS如何部署Docker服務”這篇文章的所有內容,感謝各位的閱讀!相信大家閱讀完這篇文章都有很大的收獲,小編每天都會為大家更新不同的知識,如果還想學習更多的知識,請關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。