由于docker越来越火,给运维和开发者都带来了很大的方便。在很多场景下我们都习惯使用docker,docker做集群,分布式,同时启动几十个docker已经不算是什么较大的集群了,像京东6.18和天猫的双11,为了能保证持续的高并发和访问速度,基本都采用了10w+的docker。可想而知,docker的使用在大型公司也基本普及和大批量的使用。

那么10w+的docker,我们是如何去启动呢?难道启动一个docker就要执行一条命令吗?这时候就会有人想到了采用shell脚本,创建一个init.sh在主机上运行,启动所有的docker容器,不错这也算是个办法,但是如果有1000台主机,那又需要将这个脚本分别复制到1000太宿主机运行,这又是件麻烦的事。说到这里有没有想到ansible呢,通过写剧本那安装部署各种软件,一个剧本可以搞定所有主机的操作。其实docker它也有自己的管理工具docker-compose,我们来体验一下吧!
#安装docker-compose并检验是否安装成功。(需要安装epel源,再安装pip,通过pip安装docker-compose)

[root@docker01 ~]# curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   664  100   664    0     0    118      0  0:00:05  0:00:05 --:--:--   162
[root@docker01 ~]# yum install python2-pip -y
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
Resolving Dependencies
   .
   .
   .
 省略部分
   .
   .

Installed:
  python2-pip.noarch 0:8.1.2-8.el7                                                                                                     

Complete!
[root@docker01 ~]# pip install docker-compose  #可能会报错或等待时间较长
Collecting docker-compose
  Retrying (Retry(total=4, connect=None, read=None, redirect=None)) after connection broken by 'NewConnectionError('<pip._vendor.requests.packages.urllib3.connection.VerifiedHTTPSConnection object at 0x2555890>: Failed to establish a new connection: [Errno 101] Network is unreachable',)': /simple/docker-compose/
  Downloading https://files.pythonhosted.org/packages/dd/e6/1521d1dfd9c0da1d1863b18e592d91c3df222e55f258b9876fa1e59bc4b5/docker_compose-1.24.1-py2.py3-none-any.whl (134kB)
    68% |██████████████████████          | 92kB 4.4kB/s eta 0:00:10Exception:
Traceback (most recent call last):
  File "/usr/lib/python2.7/site-packages/pip/basecommand.py", line 215, in main
    status = self.run(options, args)
   .
   .
   .
 省略部分
   .
   .
    flush_decoder = True
  File "/usr/lib64/python2.7/contextlib.py", line 35, in __exit__
    self.gen.throw(type, value, traceback)
  File "/usr/lib/python2.7/site-packages/pip/_vendor/requests/packages/urllib3/response.py", line 242, in _error_catcher
    raise ReadTimeoutError(self._pool, None, 'Read timed out.')
ReadTimeoutError: HTTPSConnectionPool(host='files.pythonhosted.org', port=443): Read timed out.
You are using pip version 8.1.2, however version 19.2.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
[root@docker01 ~]# 

在通过pip install docker-compose安装的时候可能会等待时间较长甚至报错,这里去清华源找到可以采用加速的方式:
https://mirrors.tuna.tsinghua.edu.cn/
https://mirrors.tuna.tsinghua.edu.cn/help/pypi/

[root@docker01 ~]# pip install -i https://pypi.tuna.tsinghua.edu.cn/simple docker-compose
Collecting docker-compose
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/dd/e6/1521d1dfd9c0da1d1863b18e592d91c3df222e55f258b9876fa1e59bc4b5/docker_compose-1.24.1-py2.py3-none-any.whl (134kB)
    100% |████████████████████████████████| 143kB 1.2MB/s 
Collecting websocket-client<1.0,>=0.32.0 (from docker-compose)
  Downloading 
   .
   .
   .
省略部分
   .
   .
   .
  Running setup.py install for dockerpty ... done
  Running setup.py install for docopt ... done
  Running setup.py install for texttable ... done
Successfully installed asn1crypto-0.24.0 bcrypt-3.1.7 cached-property-1.5.1 cryptography-2.7 docker-3.7.3 docker-compose-1.24.1 docker-pycreds-0.4.0 dockerpty-0.4.1 docopt-0.6.2 paramiko-2.6.0 pynacl-1.3.0 texttable-0.9.1 websocket-client-0.56.0
You are using pip version 8.1.2, however version 19.2.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
[root@docker01 ~]# docker-compose -v  #检查docker-compose
docker-compose version 1.24.1, build 4667896
[root@docker01 ~]# 

-i:表示指定镜像源地址,默认从国外地址,这里从清华源下载。
 

接下来我们以安装wordpress为例,先来看看docker-compose.yml

[root@docker01 wordpress]# vim docker-compose.yml 

version: '3'     #指定版本
services:        #每启动一个类型的容器定义为一个服务
   db:           #服务名
     image: mysql:5.7           #镜像名
     volumes:                   #数据卷
       - db_data:/var/lib/mysql
     restart: always            #容器随docker服务启动
     environment:               #环境变量
       MYSQL_ROOT_PASSWORD: somewordpress
       MYSQL_DATABASE: wordpress
       MYSQL_USER: wordpress
       MYSQL_PASSWORD: wordpress
   wordpress:
     depends_on:      #wordpress安装依赖db
       - db
     image: wordpress:latest    #镜像
     volumes:         #数据卷
       - web_data:/var/www/html
     ports:           #开放端口
       - "80"
     restart: always   #容器随docker服务启动
     environment:      #环境变量
       WORDPRESS_DB_HOST: db:3306
       WORDPRESS_DB_USER: wordpress
       WORDPRESS_DB_PASSWORD: wordpress
volumes:
    db_data:
    web_data:

在/opt/wordpress/下创建docker-compose.yml并粘贴以上代码,该代码可在https://hub.docker.com/_/wordpress上找到。

1.建议先将mysql5.7容器和wordpress容器下载到本地,否则会等待好久(为了能顺利从dockerhub上能顺利下载wordpress容器,还不得不添加docker加速器,原因你懂的,参考连接:http://dpskys.com/c/Docker%E5%8A%A0%E9%80%9F%E5%99%A8)。

[root@docker01 wordpress]# docker pull mysql:5.7
5.7: Pulling from library/mysql
0a4690c5d889: Pull complete 
98aa2fc6cbeb: Pull complete 
0777e6eb0e6f: Pull complete 
2464189c041c: Pull complete 
b45df9dc827d: Pull complete 
b42b00086160: Pull complete 
bb93567627c7: Pull complete 
419b68a254a1: Pull complete 
8c4bc5b87c07: Pull complete 
59234e88c262: Pull complete 
2406ac6e266f: Pull complete 
Digest: sha256:20fe2b36deb937bf17544a2ef2c0603f10af9232e1f1d203000fb77140d45450
Status: Downloaded newer image for mysql:5.7
[root@docker01 wordpress]# 

[root@docker01 opt]# docker pull wordpress
Using default tag: latest
latest: Pulling from library/wordpress
f5d23c7fed46: Already exists 
4f36b8588ea0: Already exists 
6f4f95ddefa8: Already exists 
187af28c9b1d: Already exists 
3c4135e0a3e9: Pull complete 
764143bdfc93: Pull complete 
3a141d427998: Pull complete 
273ba44de0c6: Pull complete 
a508367b4ba3: Pull complete 
0196a98ca6a5: Pull complete 
0accc27cff60: Pull complete 
d9745b3647e0: Pull complete 
92b4636120c9: Pull complete 
73aa9840a7e0: Pull complete 
0e67f686caa1: Pull complete 
5bc0b6b84a5d: Pull complete 
58053bc92680: Pull complete 
454775204071: Pull complete 
0ebd9bf48850: Pull complete 
844b9b12245b: Pull complete 
Digest: sha256:eea16a6af138150d71e090b279839b3e4854d56903c3698110961f9b61a7b635
Status: Downloaded newer image for wordpress:latest
[root@docker01 opt]#

2.运行docker-compose 
#docker-compose up -d   后台运行
#docker-compose up       前台运行

[root@docker01 wordpress]# docker-compose up -d
Starting wordpress_db_1 ... done
Starting wordpress_wordpress_1 ... done
[root@docker01 wordpress]#

3.查看启动的容器和数据卷,以及网卡
#以wordpress开头的都是新创建的容器,数据卷和网卡

[root@docker01 wordpress]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                   NAMES
7bc9c63a52e7        wordpress:latest    "docker-entrypoint.s…"   17 minutes ago      Up 15 seconds       0.0.0.0:32773->80/tcp   wordpress_wordpress_1
a1095601261d        mysql:5.7           "docker-entrypoint.s…"   17 minutes ago      Up 16 seconds       3306/tcp, 33060/tcp     wordpress_db_1
[root@docker01 wordpress]# docker volume ls
DRIVER              VOLUME NAME
local               61e207f0a53d5621b1c9c8bd75ac5a60760c87e94b0f1a167ecb748558506919
local               wordpress_db_data
local               wordpress_web_data
[root@docker01 wordpress]# docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
ee3200a1482b        bridge              bridge              local
1a2a87ff3711        host                host                local
3a7f8b6f580d        none                null                local
1a7c4ed89c32        wordpress_default   bridge              local
[root@docker01 wordpress]#

4.可以通过docker-compose scale wordpress=3调度,同时启动多个容器

[root@docker01 wordpress]# docker-compose scale wordpress = 3
WARNING: The scale command is deprecated. Use the up command with the --scale flag instead.
ERROR: Arguments to scale should be in the form service=num
[root@docker01 wordpress]# docker-compose scale wordpress=3
WARNING: The scale command is deprecated. Use the up command with the --scale flag instead.
Starting wordpress_wordpress_1 ... done
Creating wordpress_wordpress_2 ... done
Creating wordpress_wordpress_3 ... done
[root@docker01 wordpress]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                   NAMES
22cdd3db47ac        wordpress:latest    "docker-entrypoint.s…"   5 seconds ago       Up 4 seconds        0.0.0.0:32775->80/tcp   wordpress_wordpress_2
eb496876ae79        wordpress:latest    "docker-entrypoint.s…"   5 seconds ago       Up 4 seconds        0.0.0.0:32774->80/tcp   wordpress_wordpress_3
7bc9c63a52e7        wordpress:latest    "docker-entrypoint.s…"   19 minutes ago      Up 2 minutes        0.0.0.0:32773->80/tcp   wordpress_wordpress_1
a1095601261d        mysql:5.7           "docker-entrypoint.s…"   19 minutes ago      Up 2 minutes        3306/tcp, 33060/tcp     wordpress_db_1
[root@docker01 wordpress]# 

docker-compose会自动检测,已经存在一个启动的容器,再启动2两个即可。这时启动了3个wordpress容器和一个mysql5.7容器,分别映射宿主机的端口:32775,32774,32773,3306,这是我们就可以在宿主机安装nginx,做反向代理,完成wordpress集群,nginx反向代理会在另一个章节讲到,这里就不再赘述。这里只用对应的端口进行访问:
http://10.0.0.11:32775/wp-admin/install.php

http://10.0.0.11:32774/wp-admin/install.php

http://10.0.0.11:32773/wp-admin/install.php

最后修改于 2019-07-29 14:06:42
如果觉得我的文章对你有用,请随意赞赏
扫一扫支付
上一篇