容器云系列之Docker管理工具Docker-compose

Docker-compose和Docker machine是Docker容器管理的工具,本文简单介绍了Docker-compose的使用,并结合案例Haproxy实现负载均衡加深对Docker管理的理解。


1、Docker-compose编排工具

Docker-compose是用于定义和运行多容器Docker应用程序的工具。通过Compose,可以使用YML文件来配置应用程序需要的所有服务。然后,使用一个命令,就可以从YML文件配置中创建并启动所有服务。

Docker-Compose将所管理的容器分为三层,分别是工程(project),服务(service)以及容器(container)。Docker-Compose运行目录下的所有文件(docker-compose.yml,extends文件或环境变量文件等)组成一个工程,若无特殊指定工程名即为当前目录名。一个工程当中可包含多个服务,每个服务中定义了容器运行的镜像,参数,依赖。一个服务当中可包括多个容器实例,Docker-Compose并没有解决负载均衡的问题,因此需要借助其它工具实现服务发现及负载均衡。

1.1 安装docker-compose

1)安装docker-compose

[root@tango-01 /]# curl -L https://github.com/docker/compose/releases/download/1.27.0/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   651  100   651    0     0    230      0  0:00:02  0:00:02 --:--:--   230
100 11.6M  100 11.6M    0     0  61742      0  0:03:17  0:03:17 --:--:-- 73847
[root@tango-01 /]# chmod +x /usr/local/bin/docker-compose
[root@tango-01 /]# 

2)查看安装版本

[root@tango-01 /]# docker-compose --version
docker-compose version 1.27.0, build 980ec85b
1.2 Docker-compose常用命令
#1. Docker-compose命令格式
docker-compose [-f <arg>...] [options] [COMMAND] [ARGS...]

#2. docker-compose up运行容器
docker-compose up [options] [--scale SERVICE=NUM...] [SERVICE...]

#3. docker-compose ps:列出项目中所有的容器
docker-compose  ps [options] [SERVICE...]

#4. docker-compose stop:停止正在运行的容器
docker-compose stop [options] [SERVICE...]

#5. docker-compose down:停用移除所有容器以及网络相关
docker-compose down [options]

#6. docker-compose logs:查看服务容器的输出
docker-compose logs [options] [SERVICE...]

#7. docker-compose bulid:构建(重新构建)项目中的服务容器
docker-compose build [options] [--build-arg key=val...] [SERVICE...]

#8. docker-compose pull:拉取服务依赖的镜像
docker-compose pull [options] [SERVICE...]

#9. docker-compose restart:重启项目中的服务
docker-compose restart [options] [SERVICE...]

#10. docker-compose rm:删除所有(停止状态的)服务容器
docker-compose rm [options] [SERVICE...]

#11. docker-compose start:启动已经存在的服务容器
docker-compose start [SERVICE...]

#12. docker-compose run:在指定服务上执行一个命令
docker-compose run [options] [-v VOLUME...] [-p PORT...] [-e KEY=VAL...] SERVICE [COMMAND] [ARGS...]

#13. docker-compose scale:设置指定服务运行的容器个数
docker-compose scale web=3 db=2

#14. docker-compose pause:暂停一个服务容器
docker-compose pause [SERVICE...]

#15. docker-compose kill:通过发送SIGKILL信号来强制停止服务容器
docker-compose kill [options] [SERVICE...]

#16. docker-compose config:验证并查看compose文件配置
docker-compose config [options]

#17. docker-compose create:为服务创建容器
docker-compose create [options] [SERVICE...]

#18. docker-compose exec:
docker-compose exec [options] SERVICE COMMAND [ARGS...]

#19. docker-compose port:显示某个容器端口所映射的公共端口
docker-compose port [options] SERVICE PRIVATE_PORT

#20. docker-compose push:推送服务依的镜像
docker-compose push [options] [SERVICE...]

#21. docker-compose stop:停止运行的容器
docker-compose stop [options] [SERVICE...]

#22. docker-compose uppause:恢复处于暂停状态中的服务
docker-compose unpause [SERVICE...]
1.3 使用Docker-compose编排容器
1.3.1 Docker-compose模板文件

Compose允许用户通过一个docker-compose.yml模板文件(YAML格式)来定义一组相关联的应用容器为一个项目(project),YAML文件中会定义服务、网络和卷等配置信息。Docker-Compose标准模板文件应该包含version、services、networks 三大部分,docker-compose.yml模板如下:

version: "3.8"
services:
  web:
    build: .
    ports:
      - "5000:5000"
    volumes:
      - .:/code
      - logvolume01:/var/log
networks:
  front-tier:
    driver: bridge
  back-tier:
    driver: bridge

YAML中的关键标签如下:
在这里插入图片描述

1.3.2 Docker-compose示例

接下去以Django/PostgreSQL为例介绍Docker-compose的使用

1)创建文件目录

[root@tango-01 docker]# mkdir /usr/local/docker/app/my_django

2)在目录中创建新的Dockerfile

[root@tango-01 my_django]# vi Dockerfile 
FROM python:3
ENV PYTHONUNBUFFERED=1
RUN mkdir /code
WORKDIR /code
COPY requirements.txt /code/
RUN pip install -r requirements.txt
COPY . /code/

3)创建requirements.txt

[root@tango-01 my_django]# vi requirements.txt 
Django>=3.0,<4.0
psycopg2-binary>=2.8

4)创建docker-compose.yml

[root@tango-01 my_django]# vi docker-compose.yml 
version: "3.8"

services:
  db:
    image: postgres
    environment:
      - POSTGRES_DB=postgres
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
  django:
    build: .
    command: python manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/code
    ports:
      - "8000:8000"
    depends_on:
      - db

5)创建Django Project

[root@tango-01 my_django]# docker-compose run web django-admin startproject composeexample .

6)命令结束后查看目录文件

[root@tango-01 my_django]# ls -l
total 16
drwxr-xr-x 2 root root  89 Oct 24 08:08 composeexample
-rw-r--r-- 1 root root 327 Oct 24 07:49 docker-compose.yml
-rw-r--r-- 1 root root 146 Oct 24 07:48 Dockerfile
-rwxr-xr-x 1 root root 670 Oct 24 08:08 manage.py
-rw-r--r-- 1 root root  38 Oct 24 07:48 requirements.txt

7)修改Django配置,连接到Database

[root@tango-01 composeexample]# vi settings.py
ALLOWED_HOSTS = ['*']

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'postgres',
        'USER': 'postgres',
        'PASSWORD': 'postgres',
        'HOST': 'db',
        'PORT': 5432,
    }
}

注意修改ALLOWED_HOSTS = [’*’],否则提示
在这里插入图片描述
8)启动容器

[root@tango-01 my_django]# docker-compose up
Starting my_django_db_1 ... done
Starting my_django_web_1 ... done
Attaching to my_django_db_1, my_django_web_1
db_1   | 
db_1   | PostgreSQL Database directory appears to contain a database; Skipping initialization
db_1   | 
db_1   | 2020-10-24 00:17:29.464 UTC [1] LOG:  starting PostgreSQL 13.0 (Debian 13.0-1.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit
db_1   | 2020-10-24 00:17:29.464 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
db_1   | 2020-10-24 00:17:29.464 UTC [1] LOG:  listening on IPv6 address "::", port 5432
db_1   | 2020-10-24 00:17:29.476 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
db_1   | 2020-10-24 00:17:29.481 UTC [23] LOG:  database system was shut down at 2020-10-24 00:15:49 UTC
db_1   | 2020-10-24 00:17:29.487 UTC [1] LOG:  database system is ready to accept connections
web_1  | Watching for file changes with StatReloader
web_1  | Performing system checks...
web_1  | 
web_1  | System check identified no issues (0 silenced).
web_1  | 
web_1  | You have 18 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
web_1  | Run 'python manage.py migrate' to apply them.
web_1  | October 24, 2020 - 00:17:31
web_1  | Django version 3.1.2, using settings 'composeexample.settings'
web_1  | Starting development server at http://0.0.0.0:8000/
web_1  | Quit the server with CONTROL-C.

9)连接到应用
在这里插入图片描述

1.3.3 haproxy实现负载均衡

1)修改编排脚本

[root@tango-01 my_django]# vi docker-compose.yml 
version: "3.8"

services:
  db:
    image: postgres
    environment:
      - POSTGRES_DB=postgres
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
  web1:
    build: .
    command: python manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/code
    ports:
      - "8000"
    depends_on:
      - db
  web2:
    build: .
    command: python manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/code
    ports:
      - "8000"
    depends_on:
      - db
  haproxy:
         image: haproxy:latest
         volumes:
                 - ./haproxy:/usr/local/etc/haproxy
         links:
                 - web1
                 - web2
         ports:
                 - "8000:8000"
                 - "8088:8088"
         expose:
                 - 8000
                 - 8088

2)修改haproxy配置文件

[root@tango-01 my_django]# vi haproxy/haproxy.cfg 
global
    log         127.0.0.1 local2
    maxconn     4000
    daemon

defaults
    mode                    http
    log                     global
    option                  httplog
    option                  dontlognull
    option http-server-close
    option forwardfor       except 127.0.0.0/8
    option                  redispatch
    retries                 3
    timeout http-request    10s
    timeout queue           1m
    timeout connect         10s
    timeout client          1m
    timeout server          1m
    timeout http-keep-alive 10s
    timeout check           10s
    maxconn                 3000

listen stats
    mode http
    log global
    bind 0.0.0.0:8088
    stats enable
    stats refresh 30s
    stats uri     /haproxy-stats 
    stats auth    admin:123456

frontend balancer
    mode http
    bind 0.0.0.0:8000
    option httplog
    log global
    default_backend      backend_web

backend backend_web
    balance     roundrobin
    server server1 web1:8000 check inter 2000 rise 30 fall 15
    server server2 web2:8000 check inter 2000 rise 30 fall 15

列出文件

[root@tango-01 my_django]# ls -lR
.:
total 20
drwxr-xr-x 3 root root 136 Oct 24 08:59 composeexample
-rw-r--r-- 1 root root 322 Oct 25 15:31 docker-compose-bk201025.yml
-rw-r--r-- 1 root root 760 Oct 25 15:33 docker-compose.yml
-rw-r--r-- 1 root root 146 Oct 24 07:48 Dockerfile
drwxr-xr-x 2 root root  25 Oct 25 15:36 haproxy
-rwxr-xr-x 1 root root 670 Oct 24 08:08 manage.py
-rw-r--r-- 1 root root  38 Oct 24 07:48 requirements.txt

./composeexample:
total 20
-rw-r--r-- 1 root root  405 Oct 24 08:08 asgi.py
-rw-r--r-- 1 root root    0 Oct 24 08:08 __init__.py
drwxr-xr-x 2 root root  122 Oct 24 08:17 __pycache__
-rw-r--r-- 1 root root 3086 Oct 24 08:10 settings-bk201024.py
-rw-r--r-- 1 root root 3182 Oct 24 08:17 settings.py
-rw-r--r-- 1 root root  756 Oct 24 08:08 urls.py
-rw-r--r-- 1 root root  405 Oct 24 08:08 wsgi.py

./composeexample/__pycache__:
total 16
-rw-r--r-- 1 root root  123 Oct 24 08:11 __init__.cpython-39.pyc
-rw-r--r-- 1 root root 2269 Oct 24 08:17 settings.cpython-39.pyc
-rw-r--r-- 1 root root  907 Oct 24 08:11 urls.cpython-39.pyc
-rw-r--r-- 1 root root  540 Oct 24 08:11 wsgi.cpython-39.pyc

./haproxy:
total 4
-rw-r--r-- 1 root root 3375 Oct 25 15:36 haproxy.cfg

3)启动haproxy

[root@tango-01 my_django]# docker-compose build
db uses an image, skipping
haproxy uses an image, skipping
Building web1
Step 1/7 : FROM python:3
 ---> 5336a27a9b1f
Step 2/7 : ENV PYTHONUNBUFFERED=1
 ---> Using cache
 ---> abb90a7e7d6a
Step 3/7 : RUN mkdir /code
 ---> Using cache
 ---> 7b1d83046ccb
Step 4/7 : WORKDIR /code
 ---> Using cache
 ---> 8762b6c51840
Step 5/7 : COPY requirements.txt /code/
 ---> Using cache
 ---> 9568943f3862
Step 6/7 : RUN pip install -r requirements.txt
 ---> Using cache
 ---> fc255cdfc761
Step 7/7 : COPY . /code/
 ---> Using cache
 ---> d4f9e243da50

Successfully built d4f9e243da50
Successfully tagged my_django_web1:latest
Building web2
Step 1/7 : FROM python:3
 ---> 5336a27a9b1f
Step 2/7 : ENV PYTHONUNBUFFERED=1
 ---> Using cache
 ---> abb90a7e7d6a
Step 3/7 : RUN mkdir /code
 ---> Using cache
 ---> 7b1d83046ccb
Step 4/7 : WORKDIR /code
 ---> Using cache
 ---> 8762b6c51840
Step 5/7 : COPY requirements.txt /code/
 ---> Using cache
 ---> 9568943f3862
Step 6/7 : RUN pip install -r requirements.txt
 ---> Using cache
 ---> fc255cdfc761
Step 7/7 : COPY . /code/
 ---> Using cache
 ---> d4f9e243da50

Successfully built d4f9e243da50
Successfully tagged my_django_web2:latest
[root@tango-01 my_django]# docker-compose up
WARNING: Found orphan containers (my_django_django_1, my_django_web_2, my_django_django_2, my_django_web_1) for this project. If you removed or renamed this service in your compose file, you can run this command with the --remove-orphans flag to clean it up.
Starting my_django_db_1 ... done
Starting my_django_web1_1 ... done
Starting my_django_web2_1 ... done
Starting my_django_haproxy_1 ... done
Attaching to my_django_db_1, my_django_web1_1, my_django_web2_1, my_django_haproxy_1
db_1       | 
db_1       | PostgreSQL Database directory appears to contain a database; Skipping initialization
db_1       | 
db_1       | 2020-10-25 12:13:13.381 UTC [1] LOG:  starting PostgreSQL 13.0 (Debian 13.0-1.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit
db_1       | 2020-10-25 12:13:13.381 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
db_1       | 2020-10-25 12:13:13.381 UTC [1] LOG:  listening on IPv6 address "::", port 5432
db_1       | 2020-10-25 12:13:13.384 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
db_1       | 2020-10-25 12:13:13.390 UTC [23] LOG:  database system was shut down at 2020-10-25 12:12:16 UTC
db_1       | 2020-10-25 12:13:13.395 UTC [1] LOG:  database system is ready to accept connections
haproxy_1  | [NOTICE] 298/121314 (1) : New worker #1 (7) forked
haproxy_1  | [WARNING] 298/121314 (7) : Server backend_web/server1 is DOWN, reason: Layer4 connection problem, info: "Connection refused", check duration: 0ms. 1 active and 0 backup servers left. 0 sessions active, 0 requeued, 0 remaining in queue.
haproxy_1  | [WARNING] 298/121315 (7) : Server backend_web/server2 is DOWN, reason: Layer4 connection problem, info: "Connection refused", check duration: 0ms. 0 active and 0 backup servers left. 0 sessions active, 0 requeued, 0 remaining in queue.
haproxy_1  | [NOTICE] 298/121315 (7) : haproxy version is 2.2.4-de45672
haproxy_1  | [NOTICE] 298/121315 (7) : path to executable is /usr/local/sbin/haproxy
haproxy_1  | [ALERT] 298/121315 (7) : backend 'backend_web' has no server available!
web1_1     | Watching for file changes with StatReloader
web1_1     | Performing system checks...
web1_1     | 
web2_1     | Watching for file changes with StatReloader
web2_1     | Performing system checks...
web2_1     | 
web1_1     | System check identified no issues (0 silenced).
web2_1     | System check identified no issues (0 silenced).
web1_1     | 
web1_1     | You have 18 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
web1_1     | Run 'python manage.py migrate' to apply them.
web1_1     | October 25, 2020 - 12:13:16
web1_1     | Django version 3.1.2, using settings 'composeexample.settings'
web1_1     | Starting development server at http://0.0.0.0:8000/
web1_1     | Quit the server with CONTROL-C.
web2_1     | 
web2_1     | You have 18 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
web2_1     | Run 'python manage.py migrate' to apply them.
web2_1     | October 25, 2020 - 12:13:16
web2_1     | Django version 3.1.2, using settings 'composeexample.settings'
web2_1     | Starting development server at http://0.0.0.0:8000/
web2_1     | Quit the server with CONTROL-C. 
[root@tango-01 /]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                                            NAMES
3b42a68d193d        haproxy:latest      "/docker-entrypoint.…"   23 seconds ago      Up 21 seconds       0.0.0.0:8000->8000/tcp, 0.0.0.0:8088->8088/tcp   my_django_haproxy_1
6d9c71d42fc7        my_django_web1      "python manage.py ru…"   24 seconds ago      Up 22 seconds       0.0.0.0:32787->8000/tcp                          my_django_web1_1
3f68aebeb03d        my_django_web2      "python manage.py ru…"   24 seconds ago      Up 22 seconds       0.0.0.0:32786->8000/tcp                          my_django_web2_1
4cb012feb0d9        postgres            "docker-entrypoint.s…"   36 hours ago        Up 24 seconds       5432/tcp

4)使用浏览器访问hapeoxy监听的8000端口可以看到负载的情况

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-KHbd7ijJ-1637370995491)(https://note.youdao.com/favicon.ico)]

查看log日志,看到访问已经在web1和web2切换

haproxy_1  | [WARNING] 298/121415 (7) : Server backend_web/server1 is UP, reason: Layer4 check passed, check duration: 0ms. 1 active and 0 backup servers online. 0 sessions requeued, 0 total in queue.
haproxy_1  | [WARNING] 298/121416 (7) : Server backend_web/server2 is UP, reason: Layer4 check passed, check duration: 0ms. 2 active and 0 backup servers online. 0 sessions requeued, 0 total in queue.
web1_1     | [25/Oct/2020 12:14:17] "GET / HTTP/1.1" 200 16351
web2_1     | [25/Oct/2020 12:14:17] "GET /static/admin/css/fonts.css HTTP/1.1" 304 0
web2_1     | [25/Oct/2020 12:14:18] "GET /static/admin/fonts/Roboto-Bold-webfont.woff HTTP/1.1" 304 0
web1_1     | [25/Oct/2020 12:14:18] "GET /static/admin/fonts/Roboto-Regular-webfont.woff HTTP/1.1" 304 0
web1_1     | [25/Oct/2020 12:14:18] "GET /static/admin/fonts/Roboto-Light-webfont.woff HTTP/1.1" 304 0
web2_1     | [25/Oct/2020 12:15:14] "GET / HTTP/1.1" 200 16351
web1_1     | [25/Oct/2020 12:15:19] "GET / HTTP/1.1" 200 16351

5)访问 http://192.168.112.10:8088/haproxy-stats,可以看到后端节点的监控状况
在这里插入图片描述


参考资料:

  1. https://docs.docker.com/compose/

转载请注明原文地址:https://blog.csdn.net/solihawk/article/details/121434982
文章会同步在公众号“牧羊人的方向”更新,感兴趣的可以关注公众号,谢谢!
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值