一、compose简介

   编排功能是复杂系统是否具有灵活可操作性的关键。特别在docker应用场景中,编排意味着用户可以灵活的对各种容器资源实现定义和管理。compose作为docker官方编排工具,它可以让用户通过编写一个简单的模板文件,快速的创建和管理基于docker容器的应用集群。

    compose项目是docker官方的开源项目,负责实现对基于docker容器的多应用服务的快速编排。其代码目前在https://github.com/docker/compose上开源。

    compose允许用户通过一个单独的docker-compose.yml模板文件来定义一组相关联的应用容器为一个服务栈。

    compose几个概念

1. 任务:一个容器被称为一个任务。任务拥有独一无二的ID,在同一个服务中的多个任务序号依次递增。

2. 服务:某个相同应用镜像的容器副本集合,一个服务可以横向扩展为多个容器实例。

3. 服务栈:由多个服务组成,相互配合完成特定业务,如web应用服务、数据库服务共同构成web服务栈,一般由一个docker-compose.yml文件定义。

    compose的默认管理对象是服务栈,通过子命令对栈中的多个服务进行便捷的生命周期管理。compose项目由python编写,实现上调用了docker服务提供的API来对容器进行管理。因此,只要所操作的平台支持docker API,就可以在其上利用compose来进行编排管理。

二、安装docker-compose

1. 安装compose之前,要先安装docker引擎。compose可以通过Python的pip工具进行安装。

[root@web01 ~]# 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   4337      0 --:--:-- --:--:-- --:--:--  4368[root@web01 ~]# yum install -y python-pip
[root@web01 ~]# pip install --upgrade pip
[root@web01 ~]# pip -V
pip 20.2.3 from /usr/lib/python2.7/site-packages/pip (python 2.7)[root@web01 ~]# pip install  -U  docker-compose

如果执行过程中,报错1:

ERROR: Cannot uninstall 'requests'. It is a distutils installed project and thus we cannot accurately determine which files belong to it which would lead to only a partial uninstall.

解决办法:

[root@web01 ~]# pip install --ignore-installed requests

报:2:

ERROR: After October 2020 you may experience errors when installing or updating packages. This is because pip will change the way that it resolves dependency conflicts.
We recommend you use --use-feature=2020-resolver to test your packages with the new resolver before it becomes the default.
jsonschema 3.2.0 requires six>=1.11.0, but you'll have six 1.9.0 which is incompatible.

解决办法:

[root@web02 ~]# pip install six --user -U 

2. 检查是否安装成功

[root@web01 ~]# docker-compose -v
/usr/lib/python2.7/site-packages/paramiko/transport.py:33: CryptographyDeprecationWarning: Python 2 is no longer supported by the Python core team. Support for it is now deprecated in cryptography, and will be removed in a future release.
  from cryptography.hazmat.backends import default_backend
docker-compose version 1.26.2, build unknown

三、compose模板文件

模板文件是使用compose的核心,涉及的指令关键字比较多。默认的模板文件名称为docker-compose.yml,格式为yaml格式,目前最新的版本为v3.

[root@web01 ~]# mkdir /docker-compose
[root@web01 ~]# cd /docker-compose/
[root@web01 docker-compose]# vi docker-compose.yml

docker 列表 容器 docker容器编排_python


注:提前pull下mysql和wordpress两款镜像

[root@web01 docker-compose]#  docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
wordpress           latest              420b971d0f8b        9 days ago          546MB
mysql               5.7                 ef08065b0a30        10 days ago         448MB

四、启动docker-compose

[root@web01 docker-compose]# docker-compose up

1. 查看相关信息

[root@web01 ~]# docker ps -a

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                   NAMES
c7fcbdbe8f07        wordpress:latest    "docker-entrypoint.s…"   39 minutes ago      Up 3 minutes        0.0.0.0:32770->80/tcp   docker-compose_wordpress_1
d9bc706dc5a7        mysql:5.7           "docker-entrypoint.s…"   39 minutes ago      Up 6 minutes        3306/tcp, 33060/tcp     docker-compose_db_1[root@web01 ~]# docker volume ls
DRIVER              VOLUME NAME
local               docker-compose_db_data
local               docker-compose_web_data[root@web01 ~]# docker network ls
NETWORK ID          NAME                     DRIVER              SCOPE
406aa541b3b3        bridge                   bridge              local
181f52e786c7        docker-compose_default   bridge              local
1f756a7d8624        host                     host                local
a358bdb73e1b        none                     null                local

2. 动态扩展wordpress节点数

[root@web01 docker-compose]# docker-compose scale wordpress=3
/usr/lib/python2.7/site-packages/paramiko/transport.py:33: CryptographyDeprecationWarning: Python 2 is no longer supported by the Python core team. Support for it is now deprecated in cryptography, and will be removed in a future release.
  from cryptography.hazmat.backends import default_backend
WARNING: The scale command is deprecated. Use the up command with the --scale flag instead.
Starting docker-compose_wordpress_1 ... done
Creating docker-compose_wordpress_2 ... done
Creating docker-compose_wordpress_3 ... done[root@web01 docker-compose]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                   NAMES
d1d8755ab2f0        wordpress:latest    "docker-entrypoint.s…"   5 seconds ago       Up 4 seconds        0.0.0.0:32771->80/tcp   docker-compose_wordpress_3
629fdb7cae28        wordpress:latest    "docker-entrypoint.s…"   5 seconds ago       Up 4 seconds        0.0.0.0:32772->80/tcp   docker-compose_wordpress_2
c7fcbdbe8f07        wordpress:latest    "docker-entrypoint.s…"   43 minutes ago      Up 7 minutes        0.0.0.0:32770->80/tcp   docker-compose_wordpress_1
d9bc706dc5a7        mysql:5.7           "docker-entrypoint.s…"   43 minutes ago      Up 10 minutes       3306/tcp, 33060/tcp     docker-compose_db_1

3. 配置反向代理,访问wordpress

[root@web02 ~]# yum install nginx -y
[root@web02 ~]# vim /etc/nginx/nginx.conf
[root@web02 ~]# cat /etc/nginx/nginx.conf
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
upstream wordpress{
     server 192.168.0.208:32770;
     server 192.168.0.208:32771;
     server 192.168.0.208:32772;
}
    server {
        listen       80;
        server_name  localhost;

        location / {
                proxy_pass            http://wordpress;
                proxy_set_header      Host $host;
                proxy_set_header      X-Real-IP $remote_addr;
                proxy_set_header      X-Forwarded-For   $proxy_add_x_forwarded_for;
        }    }
}
[root@web02 ~]# systemctl start nginx[root@web02 ~]# netstat -lntup
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1210/master         
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      11939/nginx: master 
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1342/sshd           
tcp6       0      0 ::1:25                  :::*                    LISTEN      1210/master         
tcp6       0      0 :::22                   :::*                    LISTEN      1342/sshd           
udp        0      0 0.0.0.0:68              0.0.0.0:*                           700/dhclient        
udp        0      0 127.0.0.1:323           0.0.0.0:*                           642/chronyd         
udp6       0      0 ::1:323                 :::*                                642/chronyd

4. 访问

[root@web02 ~]# curl -v 192.168.0.184
* About to connect() to 192.168.0.184 port 80 (#0)
*   Trying 192.168.0.184...
* Connected to 192.168.0.184 (192.168.0.184) port 80 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.29.0
> Host: 192.168.0.184
> Accept: */*
> 
< HTTP/1.1 302 Found
< Server: nginx/1.16.1
< Date: Sun, 20 Sep 2020 10:15:59 GMT
< Content-Type: text/html; charset=UTF-8
< Content-Length: 0
< Connection: keep-alive
< X-Powered-By: PHP/7.4.10
< Expires: Wed, 11 Jan 1984 05:00:00 GMT
< Cache-Control: no-cache, must-revalidate, max-age=0
< X-Redirect-By: WordPress
< Location: http://192.168.0.184/wp-admin/install.php
< 
* Connection #0 to host 192.168.0.184 left intact

docker 列表 容器 docker容器编排_python_02

docker 列表 容器 docker容器编排_docker 列表 容器_03

docker 列表 容器 docker容器编排_python_04

docker 列表 容器 docker容器编排_docker_05

docker 列表 容器 docker容器编排_docker 列表 容器_06