文章目录
-
- 环境准备
- 安装docker
- 单机
-
- 创建目录
- 启动
- 集群
-
- 创建目录
- 启动server
- 启动client
- 检查集群状态
环境准备
节点名称 | ip地址 |
---|---|
consul-s1 | 192.168.130.20 |
consul-s2 | 192.168.130.19 |
consul-s3 | 192.168.130.21 |
consul-c1 | 192.168.130.21 |
3个server 1个client
安装docker
略
单机
创建目录
mkdir -p /root/consul_sg/conf
mkdir -p /root/consul_sg/data
启动
docker run -d --net=host --name consul-s1 \
-v /root/consul_sg/data:/consul/data \
-v /root/consul_sg/conf:/consul/config \
docker.io/consul:1.5.0 consul agent -server \
-bind=192.168.130.20 \
-client 0.0.0.0 \
-data-dir /consul/data \
-config-dir /consul/config \
-ui
集群
创建目录
创建配置目录和数据目录(consul-s1\consul-s2\consul-s3节点都执行)
mkdir -p /root/consul/conf
mkdir -p /root/consul/data
启动server
consul-s1
docker run -d --net=host --name consul-s1 \
-v /root/consul/data:/consul/data \
-v /root/consul/conf:/consul/config \
docker.io/consul:1.5.0 consul agent -server \
-bind=192.168.130.20 \
-client 0.0.0.0 \
-bootstrap-expect=2 \
-data-dir /consul/data \
-config-dir /consul/config \
-ui
consul-s2
docker run -d --net=host --name consul-s2 \
-v /root/consul/data:/consul/data \
-v /root/consul/conf:/consul/config \
docker.io/consul:1.5.0 consul agent -server \
-bind=192.168.130.19 \
-join=192.168.130.20 \
-client 0.0.0.0 \
-bootstrap-expect=2 \
-data-dir /consul/data \
-config-dir /consul/config \
-ui
consul-s3
docker run -d --net=host --name consul-s3 \
-v /root/consul/data:/consul/data \
-v /root/consul/conf:/consul/config \
docker.io/consul:1.5.0 consul agent -server \
-bind=192.168.130.21 \
-join=192.168.130.20 \
-client 0.0.0.0 \
-bootstrap-expect=2 \
-data-dir /consul/data \
-config-dir /consul/config \
-ui
启动client
进入到consul-c1
创建数据和配置目录
mkdir -p /root/consul_client/data
mkdir -p /root/consul_client/conf
由于和server公用一台机器,为了端口不冲突,修改一下默认监听
cd /root/consul_client/conf
vi basic.json
内容如下
{
"ports": {
"http":18501,
"dns":18601,
"serf_lan":18301,
"serf_wan":18302,
"server":18300
}
}
启动client1
docker rm -f consul-c1
docker run -d --net=host --name consul-c1 \
-v /root/consul_client/data:/consul/data \
-v /root/consul_client/conf:/consul/config \
docker.io/consul:1.5.0 consul agent \
-node kont004ecs \
-bind=192.168.130.21 \
-data-dir /consul/data \
-config-dir /consul/config \
-join 192.168.130.20:8301
检查集群状态
进入server节点,查看集群状态
docker exec consul-s1 consul members
Node Address Status Type Build Protocol DC Segment
kont001ecs 192.168.130.20:8301 alive server 1.5.0 2 dc1 <all>
kont002ecs 192.168.130.19:8301 alive server 1.5.0 2 dc1 <all>
kont003ecs 192.168.130.21:8301 alive server 1.5.0 2 dc1 <all>
kont004ecs 192.168.130.21:18301 alive client 1.5.0 2 dc1 <default>