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


    ---恢复内容开始---

    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查看版本

    我的centos中有python3,所以直接查看提示docker-compose命令不存在

    默认的Python版本是2.7,我是使用3.6的pip安装的,所以去、/usr/local/python3/bin目录下找到docker-compose创建软连接

    [root@bogon bin]# ln -s /usr/local/python3/bin/docker-compose /usr/bin/docker-compose

    [root@bogon /]# docker-compose version
    docker-compose version 1.24.1, build 4667896
    docker-py version: 3.7.3
    CPython version: 3.6.2
    OpenSSL version: OpenSSL 1.0.2k-fips 26 Jan 2017
    [root@bogon /]#

    实例

    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:

    使用方式

    1. 安装 docker-compose
    2. 新建文件 docker-compose.yml 并将上面 docker-compose 文件内容复制进入 docker-compose.yml,然后将内容中注释位置替换为自己需要的本地地址即可。主要有三个需要替换的地方,数据库文件存储位置,日志文件存储位置,自定义配置文件本地地址。
    3. 启动:docker-compose up -d

    自定义配置参考 easymock readme 中的配置小节。

    注意

    • 使用容器方式运行不需要指定 db 和 redis 参数
    • production.json 配置中注意以下问题
    {
      "port": 7300,
      "host": "0.0.0.0",
      "pageSize": 30,
      "proxy": false,
      "db": "mongodb://mongodb/easy-mock" # host 请务必替换为mongodb, 而非 localhost
      "unsplashClientId": "",
      "redis": {
        "keyPrefix": "[Easy Mock]",
        "port": 6379,
        "host": "redis", // 请勿使用 localhost,换 "redis"
        "password": "",
        "db": 0
      },
      ......
      ......
    }

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

    [root@bogon easymock]# ls
    docker-aompose.yal
    [root@bogon easymock]# less docker-aompose.yal
    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:

    docker-compose up启动

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

    • Creating easymock_redis_1 ... done
    • Creating easymock_mongodb_1 ... done
    • Creating easymock_web_1 ... done

    web_1 |
    web_1 | > easy-mock@1.6.0 start /home/easy-mock/easy-mock
    web_1 | > cross-env NODE_ENV=production node app
    web_1 |
    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
    mongodb_1 | 2019-09-04T07:14:47.881+0000 I NETWORK [thread1] connection accepted from 172.18.0.2:43855 #2 (1 connection now open)
    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-09-04-info.log'
    mongodb_1 | 2019-09-04T07:14:47.907+0000 I - [conn2] end connection 172.18.0.2:43855 (1 connection now open)
    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`

    如果看到以下报错,给log文件夹加个权限即可

    chmod 777 logs

    ey: { project: 1, create_at: -1 }, name: "project_1_create_at_-1", ns: "easy-mock.mocks", background: true }
    mongodb_1 | 2019-09-04T07:17:34.582+0000 I INDEX [conn2] build index on: easy-mock.groups properties: { v: 2, unique: true, key: { name: 1 }, name: "name_1", ns: "easy-mock.groups", background: true }
    mongodb_1 | 2019-09-04T07:17:34.589+0000 I INDEX [conn3] build index on: easy-mock.projects properties: { v: 2, key: { user: 1, members: 1, create_at: -1 }, name: "user_1_members_1_create_at_-1", ns: "easy-mock.projects", background: true }
    mongodb_1 | 2019-09-04T07:17:34.596+0000 I INDEX [conn5] build index on: easy-mock.mock_counts properties: { v: 2, key: { mock: 1, create_at: -1 }, name: "mock_1_create_at_-1", ns: "easy-mock.mock_counts", background: true }
    mongodb_1 | 2019-09-04T07:17:34.603+0000 I INDEX [conn7] build index on: easy-mock.user_project properties: { v: 2, unique: true, key: { project: 1, user: 1 }, name: "project_1_user_1", ns: "easy-mock.user_project", background: true }
    mongodb_1 | 2019-09-04T07:17:34.609+0000 I INDEX [conn6] build index on: easy-mock.user_group properties: { v: 2, unique: true, key: { group: 1, user: 1 }, name: "group_1_user_1", ns: "easy-mock.user_group", background: true }
    mongodb_1 | 2019-09-04T07:17:34.609+0000 I INDEX [conn4] build index done. scanned 0 total records. 0 secs
    mongodb_1 | 2019-09-04T07:17:34.610+0000 I INDEX [conn1] build index done. scanned 0 total records. 0 secs
    mongodb_1 | 2019-09-04T07:17:34.610+0000 I INDEX [conn2] build index done. scanned 0 total records. 0 secs
    mongodb_1 | 2019-09-04T07:17:34.611+0000 I INDEX [conn3] build index done. scanned 0 total records. 0 secs
    mongodb_1 | 2019-09-04T07:17:34.611+0000 I INDEX [conn5] build index done. scanned 0 total records. 0 secs
    mongodb_1 | 2019-09-04T07:17:34.612+0000 I INDEX [conn7] build index done. scanned 0 total records. 0 secs
    mongodb_1 | 2019-09-04T07:17:34.622+0000 I INDEX [conn3] build index on: easy-mock.groups properties: { v: 2, key: { user: 1, create_at: -1 }, name: "user_1_create_at_-1", ns: "easy-mock.groups", background: true }
    mongodb_1 | 2019-09-04T07:17:34.623+0000 I INDEX [conn3] build index done. scanned 0 total records. 0 secs
    mongodb_1 | 2019-09-04T07:17:34.626+0000 I INDEX [conn6] build index done. scanned 0 total records. 0 secs

    就正常了

    docker-compose挂后台运行

    挂后台运行需加-d参数

    docker-compose up -d

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

    [root@bogon easymock]# docker ps
    CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
    188db7406653 redis:4.0.6 "docker-entrypoint.s…" 32 seconds ago Up 28 seconds 6379/tcp easymock_redis_1
    0d74514cecb4 mongo:3.4 "docker-entrypoint.s…" 32 seconds ago Up 29 seconds 27017/tcp easymock_mongodb_1
    70d37e69706e easymock/easymock:1.6.0 "/bin/bash -c 'npm s…" 32 seconds ago Up 29 seconds 0.0.0.0:7300->7300/tcp easymock_web_1
    2c83aab72e3e tomcat "catalina.sh run" 21 hours ago Up 4 hours 0.0.0.0:8089->8080/tcp leslietomcat
    [root@bogon easymock]#

    docker-compose停止运行

    停掉服务使用docker-compose down

    docker-compose down

    [root@bogon easymock]# docker-compose down
    Stopping easymock_redis_1 ... done
    Stopping easymock_mongodb_1 ... done
    Stopping easymock_web_1 ... done
    Removing easymock_redis_1 ... done
    Removing easymock_mongodb_1 ... done
    Removing easymock_web_1 ... done
    Removing network easymock_easy-mock
    [root@bogon easymock]#

  • 相关阅读:
    Git push 常见用法
    Git commit 常见用法
    Git add 常见用法
    Git-仓库
    Git clone 常见用法
    Git-简介
    ZOJ Problem Set
    ZOJ Problem Set
    ZOJ Problem Set
    ZOJ Problem Set
  • 原文地址:https://www.cnblogs.com/leslie003/p/11459452.html
Copyright © 2020-2023  润新知