0%

consul【docker-compose部署】

前章回顾

上一章使用了docker命令部署consul集群,但是每次都敲命令太麻烦了,可以使用docker-compose简化。

docker-compose简介

本章内容参考官网:https://docs.docker.com/compose/

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

docker-compose安装

进入官网,https://docs.docker.com/compose/install/,选择linux,可以找到安装命令

#运行此命令以下载Docker Compose的当前稳定版本:
sudo curl -L "https://github.com/docker/compose/releases/download/1.24.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
 
#对二进制文件应用可执行权限:
sudo chmod +x /usr/local/bin/docker-compose
 
#可以移到到你需要的目录下,也可以不移动,如果移动,例如
mv /usr/local/bin/docker-compose /docker/docker-compose
 
#可以再创建一个链接就行
sudo ln -s /docker/docker-compose /usr/bin/docker-compose
 
#测试安装
$ docker-compose --version
docker-compose version 1.24.1, build 1110ad01

编写docker-compose.yml

version: '3'
services:
  #服务名
  consul_server_1:
    #容器名
    container_name: consul_server_1
    #使用的镜像
    image: consul:1.6.0
    #映射的端口
    ports:
      - "8500:8500"
    #使用的网络
    networks: 
      - consul
    #映射的目录
    volumes:
      - /docker/consul/server1/config:/consul/config
      - /docker/consul/server1/data:/consul/data
    #需要导入的环境变量
    environment:
      - CONSUL_BIND_INTERFACE=eth0
      - TZ=Asia/Shanghai
    #是否伴随docker重启
    restart: always
    #执行的命令
    command: [
      'agent'
    ]
 
#使用自定义网络,这样每次启动就不会创建新的网络了,ip相对固定
networks:
  consul:

docker-compose启动

#启动/停止都需要进入到docker-compose.yml目录下
#启动命令-前台运行
docker-compose up
 
#启动命令-后台运行
docker-compose up -d
 
#停止命令
docker-compose stop
 
#更多命令进入官网学习:https://docs.docker.com/compose/

注意网络问题

docker-compose使用的是docker网络

#查看现有docker网络
docker network ls
 
#删除一个网络
docker network rm +name
 
#查看一个网络的详情
docker network inspect +name
 
#docker-compose网络设置
#自定义网络
networks:
  frontend:
  backend:  
 
#使用默认网络
networks:
  default:
    # Use a custom driver
    driver: custom-driver-1
 
#使用现有网络
networks:
  default:
    external:
      name: my-pre-existing-network
#更多参考官网:https://docs.docker.com/compose/networking/

修改consul另外节点的join IP

获取consul_server_1ip,可以根据启动日志,或者运行完之后执行docker exec consul_server_1 consul members查看IP
consul

consul

修改其余consul配置的join ip
consul

docker-compose.yml中增加其他节点服务,完整配置如下:

version: '3'
services:
  consul_server_1:
    container_name: consul_server_1
    image: consul:1.6.0
    ports:
      - "8500:8500"
    networks: 
      - consul
    volumes:
      - /docker/consul/server1/config:/consul/config
      - /docker/consul/server1/data:/consul/data
    environment:
      - CONSUL_BIND_INTERFACE=eth0
      - TZ=Asia/Shanghai
    restart: always
    command: [
      'agent'
    ]
 
  consul_server_2:
    container_name: consul_server_2
    image: consul:1.6.0
    networks: 
      - consul
    volumes:
      - /docker/consul/server2/config:/consul/config
      - /docker/consul/server2/data:/consul/data
    environment:
      - CONSUL_BIND_INTERFACE=eth0
      - TZ=Asia/Shanghai
    restart: always
    command: [
      'agent'
    ]
 
  consul_server_3:
    container_name: consul_server_3
    image: consul:1.6.0
    networks: 
      - consul
    volumes:
      - /docker/consul/server3/config:/consul/config
      - /docker/consul/server3/data:/consul/data
    environment:
      - CONSUL_BIND_INTERFACE=eth0
      - TZ=Asia/Shanghai
    restart: always
    command: [
      'agent'
    ]
 
  consul_client_1:
    container_name: consul_client_1
    image: consul:1.6.0
    networks: 
      - consul
    volumes:
      - /docker/consul/client1/config:/consul/config
      - /docker/consul/client1/data:/consul/data
    environment:
      - CONSUL_BIND_INTERFACE=eth0
      - TZ=Asia/Shanghai
    restart: always
    command: [
      'agent'
    ]
 
networks:
  consul:

启动,查看

consul
consul
consul

服务均正常,以后启动只需要docker-compose up -ddocker-compose stop即可,大大方便!

增加固定IP部署配置

version: '3'
services:
  consul_server_1:
    container_name: consul_server_1
    image: consul:1.6.0
    ports:
      - "8500:8500"
    networks: 
      consul:
        ipv4_address: 172.20.0.2
    volumes:
      - /docker/consul/server1/config:/consul/config
      - /docker/consul/server1/data:/consul/data
    environment:
      - CONSUL_BIND_INTERFACE=eth0
      - TZ=Asia/Shanghai
    restart: always
    command: [
      'agent'
    ]
 
  consul_server_2:
    container_name: consul_server_2
    image: consul:1.6.0
    networks: 
      consul:
        ipv4_address: 172.20.0.3
    volumes:
      - /docker/consul/server2/config:/consul/config
      - /docker/consul/server2/data:/consul/data
    environment:
      - CONSUL_BIND_INTERFACE=eth0
      - TZ=Asia/Shanghai
    restart: always
    command: [
      'agent'
    ]
 
  consul_server_3:
    container_name: consul_server_3
    image: consul:1.6.0
    networks: 
      consul:
        ipv4_address: 172.20.0.4
    volumes:
      - /docker/consul/server3/config:/consul/config
      - /docker/consul/server3/data:/consul/data
    environment:
      - CONSUL_BIND_INTERFACE=eth0
      - TZ=Asia/Shanghai
    restart: always
    command: [
      'agent'
    ]
 
  consul_client_1:
    container_name: consul_client_1
    image: consul:1.6.0
    networks: 
      consul:
        ipv4_address: 172.20.0.5
    volumes:
      - /docker/consul/client1/config:/consul/config
      - /docker/consul/client1/data:/consul/data
    environment:
      - CONSUL_BIND_INTERFACE=eth0
      - TZ=Asia/Shanghai
    restart: always
    command: [
      'agent'
    ]
 
networks:
  consul:
    ipam:
      config:
        - subnet: 172.20.0.0/16