consul集群部署

简介: consul集群部署

Docker 部署Consul

docker run -d --name consul -p 8500:8500 consul

端口说明:https://www.consul.io/docs/install/ports.html

  • 挂载说明
  • /consul/data:持久化数据存储
  • /consul/config:配置文件

Consul 配置:https://www.consul.io/docs/agent/options.html

浏览器打开 http://ip:8500

Docker部署集群

启动4个Consul Agent,3个Server(会选举出一个leader),1个Client

#启动第1个Server节点,集群要求要有3个Server,将容器8500端口映射到主机8900端口,同时开启管理界面
docker run -d --name=consul1 -p 8900:8500 -e CONSUL_BIND_INTERFACE=eth0 consul agent --server=true --bootstrap-expect=3 --client=0.0.0.0 -ui
#启动第2个Server节点,并加入集群
docker run -d --name=consul2 -e CONSUL_BIND_INTERFACE=eth0 consul agent --server=true --client=0.0.0.0 --join 172.17.0.2
#启动第3个Server节点,并加入集群
docker run -d --name=consul3 -e CONSUL_BIND_INTERFACE=eth0 consul agent --server=true --client=0.0.0.0 --join 172.17.0.2
#启动第4个Client节点,并加入集群
docker run -d --name=consul4 -e CONSUL_BIND_INTERFACE=eth0 consul agent --server=false --client=0.0.0.0 --join 172.17.0.2

第1个启动容器的IP一般是172.17.0.2,后边启动的几个容器IP会排着来:172.17.0.3、172.17.0.4、172.17.0.5。

这些Consul节点在Docker的容器内是互通的,他们通过桥接的模式通信。但是如果主机要访问容器内的网络,需要做端口映射。在启动第一个容器时,将Consul的8500端口映射到了主机的8900端口,这样就可以方便的通过主机的浏览器查看集群信息。

docker-compose 方式部署 consul 集群

cat > /data0/consul/docker-compose.yaml << \EOF
version: '2'
networks:
  byfn:
services:
  consul1:
    image: consul
    container_name: node1
    volumes: 
      - /data0/consul/conf_with_acl:/consul/config
    command: agent -server -bootstrap-expect=3 -node=node1 -bind=0.0.0.0 -client=0.0.0.0 -config-dir=/consul/config
    networks:
      - byfn
  consul2:
    image: consul
    container_name: node2
    volumes:
      - /data0/consul/conf_with_acl:/consul/config
    command: agent -server -retry-join=node1 -node=node2 -bind=0.0.0.0 -client=0.0.0.0 -config-dir=/consul/config
    ports:
       - 8500:8500
    depends_on:
        - consul1
    networks:
      - byfn
  consul3:
    image: consul
    volumes:
      - /data0/consul/conf_with_acl:/consul/config
    container_name: node3
    command: agent -server -retry-join=node1 -node=node3 -bind=0.0.0.0 -client=0.0.0.0 -config-dir=/consul/config
    depends_on:
        - consul1
    networks:
      - byfn
  consul4:
    image: consul
    container_name: node4
    volumes:
      - /data0/consul/conf_with_acl:/consul/config
    command: agent -retry-join=node1 -node=ndoe4 -bind=0.0.0.0 -client=0.0.0.0 -ui -config-dir=/consul/config
    ports:
      - 8501:8500
    depends_on:
        - consul2
        - consul3
    networks:
      - byfn
  consul5:
    image: consul
    container_name: node5
    volumes:
      - /data0/consul/conf_without_acl:/consul/config
    command: agent -retry-join=node1 -node=ndoe5 -bind=0.0.0.0 -client=0.0.0.0 -ui -config-dir=/consul/config
    ports:
      - 8502:8500
    depends_on:
        - consul2
        - consul3
    networks:
      - byfn
EOF

单机启动脚本

创建同级目录config和data分别作为配置文件以及数据文件的目录

config目录中增加acl.json, 其中token作为登录的认证,相当于密码

{
    "acl_datacenter": "dc1",
    "acl_master_token": "p2BE1AtpwPbrxZdC6k+eXA==",
    "acl_default_policy": "deny",
    "acl_down_policy": "extend-cache"
}
./consul agent -config-dir=./config -server -data-dir=./data -bootstrap -ui

加上 -client=0.0.0.0 表示对外提供服务


集群启动脚本

在consul.exe目录下新建三个目录node1、node2、node3,分别新建配置文件basic.json

node1目录下basic.json

{
  "datacenter": "dc1",
  "data_dir": "./node1/data/",
  "log_level": "INFO",
  "server": true,
  "node_name": "node1",
  "ui": true,
  "bind_addr": "192.168.1.108",
  "client_addr": "192.168.1.108",
  "advertise_addr": "192.168.1.108",
  "bootstrap_expect": 3,
  "ports":{
    "http": 8500,
    "dns": 8600,
    "server": 8300,
    "serf_lan": 8301,
    "serf_wan": 8302
    }
}

node2目录下basic.json

{
  "datacenter": "dc1",
  "data_dir": "./node2/data/",
  "log_level": "INFO",
  "server": true,
  "node_name": "node2",
  "ui": true,
  "bind_addr": "192.168.1.108",
  "client_addr": "192.168.1.108",
  "advertise_addr": "192.168.1.108",
  "bootstrap_expect": 3,
  "ports":{
    "http": 8510,
    "dns": 8610,
    "server": 8310,
    "serf_lan": 8311,
    "serf_wan": 8312
    }
}

node3目录下basic.json

{
  "datacenter": "dc1",
  "data_dir": "./node3/data/",
  "log_level": "INFO",
  "server": true,
  "node_name": "node3",
  "ui": true,
  "bind_addr": "192.168.1.108",
  "client_addr": "192.168.1.108",
  "advertise_addr": "192.168.1.108",
  "bootstrap_expect": 3,
  "ports":{
    "http": 8520,
    "dns": 8620,
    "server": 8320,
    "serf_lan": 8321,
    "serf_wan": 8322
    }
}

在consul同目录下新建.bat文件,用于启动consul并加入集群, -retry-join指定要加入的集群节点

node1.bat

consul agent -server -client 0.0.0.0 -ui -config-file=./node1/basic.json -retry-join=192.168.1.108:8301
pause

node2.bat

consul agent -server -client 0.0.0.0 -ui -config-file=./node2/basic.json -retry-join=192.168.1.108:8301
pause

node3.bat

consul agent -server -client 0.0.0.0 -ui -config-file=./node3/basic.json -retry-join=192.168.1.108:8301
pause

启动三个consul节点后,访问localhost:8500即可看到自带的管理页面


相关文章
|
30天前
|
存储 JSON 网络协议
微服务Consul集群搭建
Consul是HashiCorp的开源工具,用于服务发现、配置管理和分布式一致性。它提供服务注册与发现、健康检查、KV存储、多数据中心支持,并基于Raft协议保证一致性。Consul还具有DNS接口和Web UI。要安装,可从HashiCorp或阿里云下载,使用`yum`在Linux上安装。启动单机模式用`consul agent -dev`,集群部署涉及配置文件如`/etc/consul.d/consul.hcl`。常用命令包括启动、加入集群、查看成员及服务管理等。
微服务Consul集群搭建
|
Windows
『Consul』Consul数据持久化配置并且注册为Windows服务
📣读完这篇文章里你能收获到 - Consul数据持久化配置并且注册为Windows服务
823 0
『Consul』Consul数据持久化配置并且注册为Windows服务
|
存储 Kubernetes 数据安全/隐私保护
在kubernetes上部署consul集群
本教程将帮助你在kubernetes上部署一个拥有3个节点的consul集群 备注:consul教程见Consul; 预览 consul 集群的三个节点部署方式使用 StatefulSet consul集群成员之间使用TLS进行安全通信 TLS and encryption keys 预备知识 本教程利用了Kubernetes 1.
4185 0
|
6月前
|
Linux
06SpringCloud - Consul高可用及搭建步骤
06SpringCloud - Consul高可用及搭建步骤
25 0
|
6月前
|
Prometheus 监控 Cloud Native
Prometheus基于consul服务发现
Prometheus基于consul服务发现
|
8月前
|
存储 Java 数据中心
|
8月前
Eureka、Zk、Consul对比
Eureka、Zk、Consul对比
|
11月前
|
存储 负载均衡 Kubernetes
配置管理和服务发现之Confd和Consul
配置管理和服务发现之Confd和Consul
158 0
|
存储 分布式计算 监控
服务发现:Zookeeper vs etcd vs Consul
服务发现:Zookeeper vs etcd vs Consul
292 0
|
存储 负载均衡 网络协议
C#使用Consul集群进行服务注册与发现
我个人觉得,中间件的部署与使用是非常难记忆的;也就是说,如果两次使用中间件的时间间隔比较长,那基本上等于要重新学习使用。 所以,我觉得学习中间件的文章,越详细越好;因为,这对作者而言也是一份珍贵的备忘资料。
C#使用Consul集群进行服务注册与发现

热门文章

最新文章

相关实验场景

更多