Docker-compose容器编排的使用Web项目架构(前台web数据和后台数据库以及负载均衡容器统一部署在一个容器中)

Posted 抛物线.

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Docker-compose容器编排的使用Web项目架构(前台web数据和后台数据库以及负载均衡容器统一部署在一个容器中)相关的知识,希望对你有一定的参考价值。

Docker-compose容器编排的使用

首先贴上官方手册https://docs.docker.com/compose/install/ 学东西尽可能读官方手册

得有个问题引入:哈哈

** 随着容器使用得越发频繁,应用服务和容器间的关系更是复杂,
面对这种情况,研发部希望能使用更好的方法管理这些服务和对应的容器。工程师小w经过分析之后,决定使用集群来统一管理应用服务。
最近公司有个项目,需要将前台Web服务器和后台数据库,再加上负载均衡容器统一部署 在容器中,
研发部把这个项目交给了工程师小w面对这个任务,
小w决定使用Docker-compose容器编排服务,通过一-个单独的docker-compose.yml模板文件( YAML格式)为一个项目来定义一组相关联的应用容器。
**

1. Compose 简介
Compose的定位是“定义和运行多个Docker 容器的应用( Defining and runingnulti-container Docker Applications )",
其前身是开源项目Fig,目前仍然兼容Fig格式的模
板文件。
通过之前的介绍可以知道,使用一-
一个Dockerfile模板文件可以让用户很方便地定义一个单独的应用容器。
然而,在日常工作中,经常会碰到需要多个容器相互配合来完成某项任务的情况。
例如要实现一个Web项目,除了Web服务容器本身外,往往还需要加上后端的数据库服务容器,甚至还包括负载均衡容器等。
下图是Web项目架构示意图。
Compose恰好满足了这样的需求。
它允许用户通过一个单独的docker- compose.yml模板文件( YAML格式)来定义一组相关联的应 用容器为一个项目。
2. Compose中的service 和projectCompose中有两个重要的概念。

①服务( service ):一个应用的容器,实际上可以包括若干运行相同镜像的容器实例。
②项目(project): 由一组关联的应用容器组成的一一个完整业务单元,在docker- compose.yml文件中定义。
Coppose的默认管理对象是项目,可通过子命令对项目中的一组容器进行便捷的生命周期管理。



WEB项目架构示意图

Compose项目由Python编写,在实现上调用了Docker服务提供的API来对容器进行管理。
因此,只要所操作的平台支持Docker API,就可以在其上利用Compose来进行编排管理。
任务实施
1. 安装Compose
Compose目前支持Linux 和Mac OS平台,两者的安装过程大同小异。

安装Compose之前,要先安装Docker (需要Docker Engine 1.7.1+ ),可参考上一个博客介绍,https://blog.csdn.net/qq_28513801/article/details/93381492此外不再赘述。

Compose可以通过Python的pip工具进行安装,也可以直接下载编译好的二进制文牛进行使用,甚至可以直接运行在Docker容器中。
前两种方式是传统方式,适合在本地环境下安装使用;最后一种方式则不破坏系统不境,更适合云计算场景。
①安装Docker Compose (选择1.8.1 版本)
[root@devops ~]#  yum install -y epel-release
[root@devops ~]# yum install -y python-pip
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: centos.ustc.edu.cn
 * epel: ftp.riken.jp
 * extras: ap.stykers.moe
 * updates: mirrors.163.com
Resolving Dependencies
--> Running transaction check
---> Package python2-pip.noarch 0:8.1.2-8.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=======================================================================================================================================
 Package                            Arch                          Version                            Repository                   Size
=======================================================================================================================================
Installing:
 python2-pip                        noarch                        8.1.2-8.el7                        epel                        1.7 M

Transaction Summary
=======================================================================================================================================
Install  1 Package

Total download size: 1.7 M
Installed size: 7.2 M
Downloading packages:
warning: /var/cache/yum/x86_64/7/epel/packages/python2-pip-8.1.2-8.el7.noarch.rpm: Header V3 RSA/SHA256 Signature, key ID 352c64e5: NOKEY
Public key for python2-pip-8.1.2-8.el7.noarch.rpm is not installed
python2-pip-8.1.2-8.el7.noarch.rpm                                                                              | 1.7 MB  00:00:14     
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
Importing GPG key 0x352C64E5:
 Userid     : "Fedora EPEL (7) <epel@fedoraproject.org>"
 Fingerprint: 91e9 7d7c 4a5e 96f1 7f3e 888f 6a2f aea2 352c 64e5
 Package    : epel-release-7-11.noarch (@extras)
 From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : python2-pip-8.1.2-8.el7.noarch                                                                                      1/1 
  Verifying  : python2-pip-8.1.2-8.el7.noarch                                                                                      1/1 

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

Complete!
[root@devops ~]# 

如果提示Pip版本低的话就省级下
[root@devops ~]# pip install -U docker-compuse
Collecting docker-compuse
  Could not find a version that satisfies the requirement docker-compuse (from versions: )
No matching distribution found for docker-compuse
You are using pip version 8.1.2, however version 19.1.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
[root@devops ~]# pip install --upgrade pip
Collecting pip
  Downloading https://files.pythonhosted.org/packages/5c/e0/be401c003291b56efc55aeba6a80ab790d3d4cece2778288d65323009420/pip-19.1.1-py2.py3-none-any.whl (1.4MB)
    100% |████████████████████████████████| 1.4MB 176kB/s 
Installing collected packages: pip
  Found existing installation: pip 8.1.2
    Uninstalling pip-8.1.2:
      Successfully uninstalled pip-8.1.2
Successfully installed pip-19.1.1
[root@devops ~]# 

我这里使用的是secureCRT 如果出现乱码 则我们设置一下字符编码

[root@devops ~]# pip install -U docker-compuse
DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won't be maintained after that date. A future version of pip will drop support for Python 2.7.
Collecting docker-compuse
  ERROR: Could not find a version that satisfies the requirement docker-compuse (from versions: none)
ERROR: No matching distribution found for docker-compuse
[root@devops ~]# pip install -U docker-compose
DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won't be maintained after that date. A future version of pip will drop support for Python 2.7.
Collecting docker-compose
  Downloading https://files.pythonhosted.org/packages/51/56/5745e66b33846e92a8814466c163f165a26fadad8b33afe381e8b6c3f652/docker_compose-1.24.0-py2.py3-none-any.whl (134kB)
     |████████████████████████████████| 143kB 146kB/s 
Collecting docker[ssh]<4.0,>=3.7.0 (from docker-compose)
  Downloading https://files.pythonhosted.org/packages/09/da/7cc7ecdcd01145e9924a8ccbe9c1baf3a362fc75d4cb150676eb5231ea60/docker-3.7.3-py2.py3-none-any.whl (134kB)
     |████████████████████████████████| 143kB 219kB/s 
Requirement already satisfied, skipping upgrade: backports.ssl-match-hostname>=3.5; python_version < "3.5" in /usr/lib/python2.7/site-packages (from docker-compose) (3.5.0.1)
Collecting requests!=2.11.0,!=2.12.2,!=2.18.0,<2.21,>=2.6.1 (from docker-compose)
  Downloading https://files.pythonhosted.org/packages/ff/17/5cbb026005115301a8fb2f9b0e3e8d32313142fe8b617070e7baad20554f/requests-2.20.1-py2.py3-none-any.whl (57kB)
     |████████████████████████████████| 61kB 3.0MB/s 
Requirement already satisfied, skipping upgrade: six<2,>=1.3.0 in /usr/lib/python2.7/site-packages (from docker-compose) (1.9.0)
Requirement already satisfied, skipping upgrade: PyYAML<4.3,>=3.10 in /usr/lib64/python2.7/site-packages (from docker-compose) (3.10)
Collecting texttable<0.10,>=0.9.0 (from docker-compose)
  Downloading https://files.pythonhosted.org/packages/02/e1/2565e6b842de7945af0555167d33acfc8a615584ef7abd30d1eae00a4d80/texttable-0.9.1.tar.gz
Collecting dockerpty<0.5,>=0.4.1 (from docker-compose)
  Downloading https://files.pythonhosted.org/packages/8d/ee/e9ecce4c32204a6738e0a5d5883d3413794d7498fe8b06f44becc028d3ba/dockerpty-0.4.1.tar.gz
Requirement already satisfied, skipping upgrade: ipaddress>=1.0.16; python_version < "3.3" in /usr/lib/python2.7/site-packages (from docker-compose) (1.0.16)
Collecting websocket-client<1.0,>=0.32.0 (from docker-compose)
  Downloading https://files.pythonhosted.org/packages/29/19/44753eab1fdb50770ac69605527e8859468f3c0fd7dc5a76dd9c4dbd7906/websocket_client-0.56.0-py2.py3-none-any.whl (200kB)
     |████████████████████████████████| 204kB 224kB/s 
Requirement already satisfied, skipping upgrade: enum34<2,>=1.0.4; python_version < "3.4" in /usr/lib/python2.7/site-packages (from docker-compose) (1.0.4)
Collecting docopt<0.7,>=0.6.1 (from docker-compose)
  Downloading https://files.pythonhosted.org/packages/a2/55/8f8cab2afd404cf578136ef2cc5dfb50baa1761b68c9da1fb1e4eed343c9/docopt-0.6.2.tar.gz
Collecting jsonschema<3,>=2.5.1 (from docker-compose)
  Downloading https://files.pythonhosted.org/packages/77/de/47e35a97b2b05c2fadbec67d44cfcdcd09b8086951b331d82de90d2912da/jsonschema-2.6.0-py2.py3-none-any.whl
Collecting cached-property<2,>=1.2.0 (from docker-compose)
  Downloading https://files.pythonhosted.org/packages/3b/86/85c1be2e8db9e13ef9a350aecd6dea292bd612fa288c2f40d035bb750ded/cached_property-1.5.1-py2.py3-none-any.whl
Collecting docker-pycreds>=0.4.0 (from docker[ssh]<4.0,>=3.7.0->docker-compose)
  Downloading https://files.pythonhosted.org/packages/f5/e8/f6bd1eee09314e7e6dee49cbe2c5e22314ccdb38db16c9fc72d2fa80d054/docker_pycreds-0.4.0-py2.py3-none-any.whl
Collecting paramiko>=2.4.2; extra == "ssh" (from docker[ssh]<4.0,>=3.7.0->docker-compose)
  Downloading https://files.pythonhosted.org/packages/17/9f/7430d1ed509e195d5a5bb1a2bda6353a4aa64eb95491f198a17c44e2075c/paramiko-2.5.0-py2.py3-none-any.whl (198kB)
     |████████████████████████████████| 204kB 89kB/s 
Collecting idna<2.8,>=2.5 (from requests!=2.11.0,!=2.12.2,!=2.18.0,<2.21,>=2.6.1->docker-compose)
  Downloading https://files.pythonhosted.org/packages/4b/2a/0276479a4b3caeb8a8c1af2f8e4355746a97fab05a372e4a2c6a6b876165/idna-2.7-py2.py3-none-any.whl (58kB)
     |████████████████████████████████| 61kB 33kB/s 
Collecting chardet<3.1.0,>=3.0.2 (from requests!=2.11.0,!=2.12.2,!=2.18.0,<2.21,>=2.6.1->docker-compose)
  Downloading https://files.pythonhosted.org/packages/bc/a9/01ffebfb562e4274b6487b4bb1ddec7ca55ec7510b22e4c51f14098443b8/chardet-3.0.4-py2.py3-none-any.whl (133kB)
     |████████████████████████████████| 143kB 33kB/s 
Collecting urllib3<1.25,>=1.21.1 (from requests!=2.11.0,!=2.12.2,!=2.18.0,<2.21,>=2.6.1->docker-compose)
  Downloading https://files.pythonhosted.org/packages/01/11/525b02e4acc0c747de8b6ccdab376331597c569c42ea66ab0a1dbd36eca2/urllib3-1.24.3-py2.py3-none-any.whl (118kB)
     |████████████████████████████████| 122kB 38kB/s 
Collecting certifi>=2017.4.17 (from requests!=2.11.0,!=2.12.2,!=2.18.0,<2.21,>=2.6.1->docker-compose)
  Downloading https://files.pythonhosted.org/packages/69/1b/b853c7a9d4f6a6d00749e94eb6f3a041e342a885b87340b79c1ef73e3a78/certifi-2019.6.16-py2.py3-none-any.whl (157kB)
     |████████████████████████████████| 163kB 46kB/s 
Collecting functools32; python_version == "2.7" (from jsonschema<3,>=2.5.1->docker-compose)
  Downloading https://files.pythonhosted.org/packages/c5/60/6ac26ad05857c601308d8fb9e87fa36d0ebf889423f47c3502ef034365db/functools32-3.2.3-2.tar.gz
Collecting cryptography>=2.5 (from paramiko>=2.4.2; extra == "ssh"->docker[ssh]<4.0,>=3.7.0->docker-compose)
  Downloading https://files.pythonhosted.org/packages/e6/68/50698ce24c61db7d44d93a5043c621a0ca7839d4ef9dff913e6ab465fc92/cryptography-2.7-cp27-cp27mu-manylinux1_x86_64.whl (2.3MB)
     |████████████████████████████████| 2.3MB 45kB/s 
Collecting pynacl>=1.0.1 (from paramiko>=2.4docker-compose 编排lnmp容器

docker学习6-docker-compose容器集群编排

docker-compose编排最佳实战(多服务)

docker三剑客之docker-compose和搭建wordpress的博客

使用Docker-compose编排容器

docker-compose概述与编排部署