8、ubuntu docker-compose 安装consul集群

1、创建需要映射的目录

mkdir -p /docker/consul/{server1,server2,server3}/{config,data}

mkdir -p /docker/consul/client1/{config,data}

2、创建网络环境

docker network create --driver=bridge --subnet=172.20.0.0/16 wfw

3、给文件夹授权

chmod -R 777 /docker/consul/

4、编写docker-compose.yml

cat > /docker/consul/docker-compose.yml <

version: '3'

services:

  consulnode1:

    image: consul

    container_name: consulnode1

    command: agent -server -bootstrap-expect=3 -config-dir=/consul/config -node=consulnode1 -bind=0.0.0.0 -client=0.0.0.0 -datacenter=dc1

    volumes:

      - /docker/consul/server1/data:/consul/data

      - /docker/consul/server1/config:/consul/config

    networks:

      - wfw

  consulnode2:

    image: consul

    container_name: consulnode2

    command: agent -server -config-dir=/consul/config -retry-join=consulnode1 -node=consulnode2 -bind=0.0.0.0 -client=0.0.0.0 -datacenter=dc1

    volumes:

      - /docker/consul/server2/data:/consul/data

      - /docker/consul/server2/config:/consul/config

    depends_on:

        - consulnode1

    networks:

      - wfw

  consulnode3:

    image: consul

    container_name: consulnode3

    command: agent -server -config-dir=/consul/config -retry-join=consulnode1 -node=consulnode3 -bind=0.0.0.0 -client=0.0.0.0 -datacenter=dc1

    volumes:

      - /docker/consul/server3/data:/consul/data

      - /docker/consul/server3/config:/consul/config

    depends_on:

        - consulnode1

    networks:

      - wfw

  consulnode4:

    image: consul

    container_name: consulnode4

    command: agent -config-dir=/consul/config -retry-join=consulnode1 -node=consulnode4 -bind=0.0.0.0 -client=0.0.0.0 -datacenter=dc1 -ui

    volumes:

      - /docker/consul/client1/data:/consul/data

      - /docker/consul/client1/config:/consul/config

    ports:

      - 8500:8500

    depends_on:

        - consulnode2

        - consulnode3

    networks:

      - wfw

networks:

  wfw:

    external: true

END

5、查看集群和leader

进入目录

cd /docker/consul/

#启动

docker-compose up -d 

#查看列表

docker exec -t consulnode1 consul members

#查看谁是领导者

docker exec -t consulnode1 consul operator raft list-peers

6、打开localhost:8500查看node

7、使用命令进行集群测试

docker exec -t consulnode1 consul kv put hello world   # hello 是key,world 是value 意思就是put了一个字典项

#显示后面内容为正常:Success! Data written to: hello

#从其他consul获取值

docker exec -t consulnode2 consul kv get hello     

#显示world,表示集群数据同步了。

8、停止docker中consolnode1,查看leader重新选举

docker stop consulnode1   #停止容器

docker exec -t consulnode1 consul operator raft list-peers  #查看当前列表,可以看到leader发生变化

#重复第7步可查看集群消息同步

你可能感兴趣的:(8、ubuntu docker-compose 安装consul集群)