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

Posted 上海-悠悠

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了docker学习6-docker-compose容器集群编排相关的知识,希望对你有一定的参考价值。

前言

实际工作中我们部署一个应用,一般不仅仅只有一个容器,可能会涉及到多个,比如用到数据库,中间件MQ,web前端和后端服务,等多个容器。
我们如果一个个去启动应用,当项目非常多时,就很难记住了,所有需要一个配置文件,负责实现对Docker容器集群的快速编排。

docker-compose

简介

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

Docker-Compose的工程配置文件默认为docker-compose.yml,可通过环境变量COMPOSE_FILE或-f参数自定义配置文件,其定义了多个有依赖关系的服务及每个服务运行的容器。

安装

docker-compose使用pip安装即可

pip install docker-compose

使用docker-compose version查看版本

[root@yoyo ~]# docker-compose version
/usr/lib/python2.7/site-packages/requests/__init__.py:80: RequestsDependencyWarning: urllib3 (1.22) or chardet (2.2.1) doesn\'t match a supported version!
  RequestsDependencyWarning)
docker-compose version 1.24.1, build 4667896
docker-py version: 3.7.3
CPython version: 2.7.5
OpenSSL version: OpenSSL 1.0.1e-fips 11 Feb 2013
[root@yoyo ~]# 

实例

docker-compose.yml文件

docker-compose已经安装成功,接下来去https://hub.docker.com上找个项目部署练练手,项目地址:https://hub.docker.com/r/easymock/easymock

docker-compose 文件内容

version: \'3\'

services:
  mongodb:
    image: mongo:3.4
    volumes:
      # ./data/db 数据库文件存放地址,根据需要修改为本地地址
      - \'./data/db:/data/db\'
    networks:
      - easy-mock
    restart: always

  redis:
    image: redis:4.0.6
    command: redis-server --appendonly yes
    volumes:
      # ./data/redis redis 数据文件存放地址,根据需要修改为本地地址
      - \'./data/redis:/data\'
    networks:
      - easy-mock
    restart: always

  web:
    image: easymock/easymock:1.6.0
    command: /bin/bash -c "npm start"
    ports:
      - 7300:7300
    volumes:
      # 日志地址,根据需要修改为本地地址
      - \'./logs:/home/easy-mock/easy-mock/logs\'
      # 配置地址,请使用本地配置地址替换
      # - \'./production.json:/home/easy-mock/easy-mock/config/production.json\'
    networks:
      - easy-mock
    restart: always

networks:
  easy-mock:

在本地新建一个文件夹/root/easymock,接着新建文件docker-compose.yml,把上面的内容写入到这个文件里,如下步骤

[root@yoyo ~]# mkdir /root/easymock
[root@yoyo ~]# cd /root/easymock/
[root@yoyo easymock]# touch docker-compose.yml
[root@yoyo easymock]# vi docker-compose.yml 
[root@yoyo easymock]# cat docker-compose.yml 
version: \'3\'

services:
  mongodb:
    image: mongo:3.4
    volumes:
      # ./data/db 数据库文件存放地址,根据需要修改为本地地址
      - \'./data/db:/data/db\'
    networks:
      - easy-mock
    restart: always

  redis:
    image: redis:4.0.6
    command: redis-server --appendonly yes
    volumes:
      # ./data/redis redis 数据文件存放地址,根据需要修改为本地地址
      - \'./data/redis:/data\'
    networks:
      - easy-mock
    restart: always

  web:
    image: easymock/easymock:1.6.0
    command: /bin/bash -c "npm start"
    ports:
      - 7300:7300
    volumes:
      # 日志地址,根据需要修改为本地地址
      - \'./logs:/home/easy-mock/easy-mock/logs\'
      # 配置地址,请使用本地配置地址替换
      # - \'./production.json:/home/easy-mock/easy-mock/config/production.json\'
    networks:
      - easy-mock
    restart: always

networks:
  easy-mock:

[root@yoyo easymock]# 

docker-compose up启动

使用docker-compose up 指令启动服务,可以看到依次启动三个容器

  • Creating easymock_redis_1 ... done
  • Creating easymock_mongodb_1 ... done
  • Creating easymock_web_1 ... done
[root@yoyo easymock]# docker-compose up
/usr/lib/python2.7/site-packages/requests/__init__.py:80: RequestsDependencyWarning: urllib3 (1.22) or chardet (2.2.1) doesn\'t match a supported version!
  RequestsDependencyWarning)
Creating network "easymock_easy-mock" with the default driver
Pulling mongodb (mongo:3.4)...
3.4: Pulling from library/mongo
f7277927d38a: Pull complete
8d3eac894db4: Pull complete
edf72af6d627: Pull complete
3e4f86211d23: Pull complete
5747135f14d2: Pull complete
f56f2c3793f6: Pull complete
f8b941527f3a: Pull complete
4000e5ef59f4: Pull complete
b2229b3c9065: Pull complete
63e3700b27e5: Pull complete
a77f0e7057fd: Pull complete
4ea8e2e4c04b: Pull complete
3214e739257b: Pull complete
d05fd2e6729f: Pull complete
Digest: sha256:0264d096073b0a862586cfe9a4a8d1a41e206b447d5f1043f241177d9cc70b7d
Status: Downloaded newer image for mongo:3.4
Pulling redis (redis:4.0.6)...
4.0.6: Pulling from library/redis
c4bb02b17bb4: Pull complete
58638acf67c5: Pull complete
f98d108cc38b: Pull complete
83be14fccb07: Pull complete
5d5f41793421: Pull complete
ed89ff0d9eb2: Pull complete
Digest: sha256:0e773022cd6572a5153e5013afced0f7191652d3cdf9b1c6785eb13f6b2974b1
Status: Downloaded newer image for redis:4.0.6
Pulling web (easymock/easymock:1.6.0)...
1.6.0: Pulling from easymock/easymock
7e6591854262: Pull complete
089d60cb4e0a: Pull complete
9c461696bc09: Pull complete
45085432511a: Pull complete
70a7567bd8cf: Pull complete
0f7e44bf28f5: Pull complete
523067e58a08: Pull complete
35e77c0527fd: Pull complete
0df0b0c81b8b: Pull complete
cd5e5a709bf4: Pull complete
d50e2d78de99: Pull complete
Digest: sha256:37d50204c694eb4e50a7ba66406736bc1ae631e0e230abf77573d5ed154fb5a2
Status: Downloaded newer image for easymock/easymock:1.6.0
Creating easymock_redis_1   ... done
Creating easymock_mongodb_1 ... done
Creating easymock_web_1     ... done

如果看到以下报错,permission denied, open \'logs/2019-08-18-info.log\',给log文件夹加个权限即可

chmod 777 /root/easymock/logs/

web_1      | WARNING: NODE_ENV value of \'production\' did not match any deployment config file names.
web_1      | WARNING: See https://github.com/lorenwest/node-config/wiki/Strict-Mode
web_1      | server started at http://0.0.0.0:7300
web_1      | events.js:182
web_1      |       throw er; // Unhandled \'error\' event
web_1      |       ^
web_1      | 
web_1      | Error: EACCES: permission denied, open \'logs/2019-08-18-info.log\'
web_1      | npm ERR! code ELIFECYCLE
web_1      | npm ERR! errno 1
web_1      | npm ERR! easy-mock@1.6.0 start: `cross-env NODE_ENV=production node app`
web_1      | npm ERR! Exit status 1
web_1      | npm ERR! 
web_1      | npm ERR! Failed at the easy-mock@1.6.0 start script.
web_1      | npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
web_1      | 
web_1      | npm ERR! A complete log of this run can be found in:
web_1      | npm ERR!     /home/easy-mock/.npm/_logs/2019-08-18T15_02_51_366Z-debug.log

接着浏览器输入http://ip:7300/即可访问了

docker-compose挂后台运行

挂后台运行需加-d参数

docker-compose up -d

查看正在启动的docker容器使用docker ps

[root@yoyo easymock]# docker-compose up -d
/usr/lib/python2.7/site-packages/requests/__init__.py:80: RequestsDependencyWarning: urllib3 (1.22) or chardet (2.2.1) doesn\'t match a supported version!
  RequestsDependencyWarning)
Starting easymock_mongodb_1 ... done
Starting easymock_web_1     ... done
Starting easymock_redis_1   ... done
[root@yoyo easymock]# docker ps
CONTAINER ID        IMAGE                     COMMAND                  CREATED             STATUS              PORTS                    NAMES
026f41a372cf        easymock/easymock:1.6.0   "/bin/bash -c \'npm s…"   11 minutes ago      Up 7 seconds        0.0.0.0:7300->7300/tcp   easymock_web_1
5dbbb2086b1c        redis:4.0.6               "docker-entrypoint.s…"   11 minutes ago      Up 7 seconds        6379/tcp                 easymock_redis_1
f4772582dd03        mongo:3.4                 "docker-entrypoint.s…"   11 minutes ago      Up 7 seconds        27017/tcp                easymock_mongodb_1
1d07763a6eaa        tomcat                    "catalina.sh run"        7 weeks ago         Up 5 weeks          0.0.0.0:8090->8080/tcp   yoyotomcat
37a3b50d151c        mysql:5.6                 "docker-entrypoint.s…"   7 weeks ago         Up 5 weeks          0.0.0.0:3308->3306/tcp   mymysql
[root@yoyo easymock]# 

docker-compose停止运行

停掉服务使用docker-compose down

docker-compose down

[root@yoyo easymock]# docker-compose down
/usr/lib/python2.7/site-packages/requests/__init__.py:80: RequestsDependencyWarning: urllib3 (1.22) or chardet (2.2.1) doesn\'t match a supported version!
  RequestsDependencyWarning)
Stopping easymock_web_1     ... done
Stopping easymock_redis_1   ... done
Stopping easymock_mongodb_1 ... done
Removing easymock_web_1     ... done
Removing easymock_redis_1   ... done
Removing easymock_mongodb_1 ... done
Removing network easymock_easy-mock
[root@yoyo easymock]# 

以上是关于docker学习6-docker-compose容器集群编排的主要内容,如果未能解决你的问题,请参考以下文章

Docker学习重点~docker 部署环境

Docker学习笔记 —— 镜像制作(Dockerfile)

Docker学习笔记 —— 镜像制作(Dockerfile)

Docker学习Docker系列结束-新的开始K8S

Docker SwarmScale 扩(缩)容服务

基于fabric配置文件的Docker-compose学习