当前位置: 代码网 > 服务器>服务器>云虚拟主机 > Docker容器服务编排利器详解

Docker容器服务编排利器详解

2024年05月23日 云虚拟主机 我要评论
一、使用docker compose必要性及定义用容器运行一个服务,需要使用docker run命令。但如果我要运行多个服务呢?假设我要运行一个web服务,还要运行一个db服务,那么是用一个容器运行,

一、使用docker compose必要性及定义

用容器运行一个服务,需要使用docker run命令。但如果我要运行多个服务呢?

假设我要运行一个web服务,还要运行一个db服务,那么是用一个容器运行,还是用多个容器运行呢?

一个容器运行多个服务会造成镜像的复杂度提高,docker倾向于一个容器运行一个应用

那么复杂的架构就会需要很多的容器,并且需要它们之间有关联(容器之间的依赖和连接)就更复杂了。

这个复杂的问题需要解决,这就涉及到了**容器编排**的问题了。

  • compose
  • 编排
    • 是对多个容器进行启动和管理的方法
    • 例如:lnmt,先启动mysql,再启动tomcat,最后启动nginx
  • 服务架构的演进
  • 单体服务架构
  • 分布式服务架构
  • 微服务架构
  • 超微服务架构
  • 容器编排工具
  • docker machine
  • 在虚拟机中部署docker容器引擎的工具
  • docker compose
  • 是一个用于定义和运行多容器docker的应用程序工具
  • docker swarm
  • 是docker host主机批量管理及资源调度管理工具
  • mesos+marathon
  • mesos 对计算机计算资源进行管理和调度
  • marathon 服务发现及负载均衡的功能
  • kubernetes
  • google开源的容器编排工具

二、docker compose应用参考资料

网址 https://docs.docker.com/compose/

  • yaml格式

https://yaml.org/

三、docker compose应用最佳实践步骤

3.1 概念

  • 工程(project)
  • 服务 (service)
  • 容器 (container)

3.2 步骤

1.定义应用的dockerfile文件,为了anywhere进行构建。

2.使用docker-compose.yaml定义一套服务,这套服务可以一起在一个隔离环境中运行。

3.使用docker-compose up就可以启动整套服务。

四、docker compose安装

# wget https://github.com/docker/compose/releases/download/v2.2.3/docker-compose-linux-x86_64
# mv docker-compose-linux-x86_64 /usr/bin/docker-compose
# chmod +x /usr/bin/docker-compose
# docker-compose version
docker compose version v2.2.3

五、docker compose应用案例

运行python语言开发的网站

5.1 网站文件准备

# mkdir flaskproject
[root@localhost ~]# cd flaskproject/
[root@localhost flaskproject]#
[root@localhost flaskproject]# vim app.py
[root@localhost flaskproject]# cat app.py
import time

import redis
from flask import flask

app = flask(__name__)
cache = redis.redis(host='redis', port=6379)

def get_hit_count():
    retries = 5
    while true:
        try:
            return cache.incr('hits')
        except redis.exceptions.connectionerror as exc:
            if retries == 0:
                raise exc
            retries -= 1
            time.sleep(0.5)

@app.route('/')
def hello():
    count = get_hit_count()
    return 'hello world! i have been seen {} times.\n'.format(count)
[root@localhost flaskproject]# vim requirements.txt
[root@localhost flaskproject]# cat requirements.txt
flask
redis

5.2 dockerfile文件准备

[root@localhost flaskproject]# vim dockerfile
[root@localhost flaskproject]# cat dockerfile
from python:3.7-alpine
workdir /code
env flask_app app.py
env flask_run_host 0.0.0.0
run apk add --no-cache gcc musl-dev linux-headers
copy requirements.txt requirements.txt
run pip install -r requirements.txt
copy . .
cmd ["flask", "run"]

5.3 compose文件准备

[root@localhost flaskproject]# vim docker-compose.yaml
[root@localhost flaskproject]# cat docker-compose.yaml
version: '3'
services:
  web:
    build: .
    ports:
      - "5000:5000"
  redis:
    image: "redis:alpine"

5.4 使用docker-compose up启动容器

[root@localhost flaskproject]# ls
app.py  docker-compose.yaml  dockerfile  requirements.txt
[root@localhost flaskproject]# docker-compose up
输出:
[+] running 7/7
 ⠿ redis pulled                                                                         15.8s
   ⠿ 59bf1c3509f3 pull complete                                                          2.9s
   ⠿ 719adce26c52 pull complete                                                          3.0s
   ⠿ b8f35e378c31 pull complete                                                          5.8s
   ⠿ d034517f789c pull complete                                                          6.5s
   ⠿ 3772d4d76753 pull complete                                                          6.6s
   ⠿ 211a7f52febb pull complete                                                          6.8s
sending build context to docker daemon     714b
step 1/9 : from python:3.7-alpine
3.7-alpine: pulling from library/python
59bf1c3509f3: already exists
07a400e93df3: already exists
bdabb07397e1: already exists
cd0af01c7b70: already exists
d0f18e022200: already exists
digest: sha256:5a776e3b5336827faf7a1c3a191b73b5b2eef4cdcfe8b94f59b79cb749a2b5d8
status: downloaded newer image for python:3.7-alpine
 ---> e72b511ad78e
step 2/9 : workdir /code
 ---> running in 2b9d07bef719
removing intermediate container 2b9d07bef719
 ---> 7d39e96fadf1
step 3/9 : env flask_app app.py
 ---> running in 9bcb28bd632a
removing intermediate container 9bcb28bd632a
 ---> 79f656a616d5
step 4/9 : env flask_run_host 0.0.0.0
 ---> running in 8470c2dbd6c2
removing intermediate container 8470c2dbd6c2
 ---> e212ba688fcd
step 5/9 : run apk add --no-cache gcc musl-dev linux-headers
 ---> running in 6e9ca0766bc8
fetch https://dl-cdn.alpinelinux.org/alpine/v3.15/main/x86_64/apkindex.tar.gz
fetch https://dl-cdn.alpinelinux.org/alpine/v3.15/community/x86_64/apkindex.tar.gz
(1/13) installing libgcc (10.3.1_git20211027-r0)
(2/13) installing libstdc++ (10.3.1_git20211027-r0)
(3/13) installing binutils (2.37-r3)
(4/13) installing libgomp (10.3.1_git20211027-r0)
(5/13) installing libatomic (10.3.1_git20211027-r0)
(6/13) installing libgphobos (10.3.1_git20211027-r0)
(7/13) installing gmp (6.2.1-r1)
(8/13) installing isl22 (0.22-r0)
(9/13) installing mpfr4 (4.1.0-r0)
(10/13) installing mpc1 (1.2.1-r0)
(11/13) installing gcc (10.3.1_git20211027-r0)
(12/13) installing linux-headers (5.10.41-r0)
(13/13) installing musl-dev (1.2.2-r7)
executing busybox-1.34.1-r3.trigger
ok: 143 mib in 49 packages
removing intermediate container 6e9ca0766bc8
 ---> 273d4f04dfbc
step 6/9 : copy requirements.txt requirements.txt
 ---> daf51c54e8ba
step 7/9 : run pip install -r requirements.txt
 ---> running in 2aa2d30c5311
collecting flask
  downloading flask-2.0.3-py3-none-any.whl (95 kb)
collecting redis
  downloading redis-4.1.3-py3-none-any.whl (173 kb)
collecting jinja2>=3.0
  downloading jinja2-3.0.3-py3-none-any.whl (133 kb)
collecting itsdangerous>=2.0
  downloading itsdangerous-2.0.1-py3-none-any.whl (18 kb)
collecting click>=7.1.2
  downloading click-8.0.3-py3-none-any.whl (97 kb)
collecting werkzeug>=2.0
  downloading werkzeug-2.0.3-py3-none-any.whl (289 kb)
collecting deprecated>=1.2.3
  downloading deprecated-1.2.13-py2.py3-none-any.whl (9.6 kb)
collecting packaging>=20.4
  downloading packaging-21.3-py3-none-any.whl (40 kb)
collecting importlib-metadata>=1.0
  downloading importlib_metadata-4.11.1-py3-none-any.whl (17 kb)
collecting wrapt<2,>=1.10
  downloading wrapt-1.13.3-cp37-cp37m-musllinux_1_1_x86_64.whl (78 kb)
collecting typing-extensions>=3.6.4
  downloading typing_extensions-4.1.1-py3-none-any.whl (26 kb)
collecting zipp>=0.5
  downloading zipp-3.7.0-py3-none-any.whl (5.3 kb)
collecting markupsafe>=2.0
  downloading markupsafe-2.0.1-cp37-cp37m-musllinux_1_1_x86_64.whl (30 kb)
collecting pyparsing!=3.0.5,>=2.0.2
  downloading pyparsing-3.0.7-py3-none-any.whl (98 kb)
installing collected packages: zipp, typing-extensions, wrapt, pyparsing, markupsafe, importlib-metadata, werkzeug, packaging, jinja2, itsdangerous, deprecated, click, redis, flask
successfully installed jinja2-3.0.3 markupsafe-2.0.1 werkzeug-2.0.3 click-8.0.3 deprecated-1.2.13 flask-2.0.3 importlib-metadata-4.11.1 itsdangerous-2.0.1 packaging-21.3 pyparsing-3.0.7 redis-4.1.3 typing-extensions-4.1.1 wrapt-1.13.3 zipp-3.7.0
warning: running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. it is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv
warning: you are using pip version 21.2.4; however, version 22.0.3 is available.
you should consider upgrading via the '/usr/local/bin/python -m pip install --upgrade pip' command.
removing intermediate container 2aa2d30c5311
 ---> dd8f52b132f8
step 8/9 : copy . .
 ---> b36938a26cf5
step 9/9 : cmd ["flask", "run"]
 ---> running in 260cbfa02959
removing intermediate container 260cbfa02959
 ---> fa04dfec6ff2
successfully built fa04dfec6ff2
successfully tagged flaskproject_web:latest

use 'docker scan' to run snyk tests against images to find vulnerabilities and learn how to fix them
[+] running 3/3
 ⠿ network flaskproject_default    created                                               0.1s
 ⠿ container flaskproject-redis-1  created                                               0.1s
 ⠿ container flaskproject-web-1    created                                               0.1s
attaching to flaskproject-redis-1, flaskproject-web-1
flaskproject-redis-1  | 1:c 15 feb 2022 14:14:21.696 # oo0ooo0ooo0oo redis is starting oo0ooo0ooo0oo
flaskproject-redis-1  | 1:c 15 feb 2022 14:14:21.696 # redis version=6.2.6, bits=64, commit=00000000, modified=0, pid=1, just started
flaskproject-redis-1  | 1:c 15 feb 2022 14:14:21.696 # warning: no config file specified, using the default config. in order to specify a config file use redis-server /path/to/redis.conf
flaskproject-redis-1  | 1:m 15 feb 2022 14:14:21.697 * monotonic clock: posix clock_gettime
flaskproject-redis-1  | 1:m 15 feb 2022 14:14:21.698 * running mode=standalone, port=6379.
flaskproject-redis-1  | 1:m 15 feb 2022 14:14:21.698 # warning: the tcp backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
flaskproject-redis-1  | 1:m 15 feb 2022 14:14:21.698 # server initialized
flaskproject-redis-1  | 1:m 15 feb 2022 14:14:21.698 # warning overcommit_memory is set to 0! background save may fail under low memory condition. to fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
flaskproject-redis-1  | 1:m 15 feb 2022 14:14:21.698 * ready to accept connections
flaskproject-web-1    |  * serving flask app 'app.py' (lazy loading)
flaskproject-web-1    |  * environment: production
flaskproject-web-1    |    warning: this is a development server. do not use it in a production deployment.
flaskproject-web-1    |    use a production wsgi server instead.
flaskproject-web-1    |  * debug mode: off
flaskproject-web-1    |  * running on all addresses.
flaskproject-web-1    |    warning: this is a development server. do not use it in a production deployment.
flaskproject-web-1    |  * running on http://172.18.0.2:5000/ (press ctrl+c to quit)

5.5 访问

到此这篇关于docker容器服务编排利器的文章就介绍到这了,更多相关docker容器编排内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com