docker consul 集群

搭建

3个server节点:node1、node2、node3
1个client节点:node4

拉镜像

docker pull consul

启动节点

docker run -d \
    -e 'CONSUL_LOCAL_CONFIG={"skip_leave_on_interrupt": true}' \
    --name=node1 \
    consul agent -server \
    -bind=172.17.0.3 \
    -bootstrap-expect=3 \
    -node=node1


docker run -d \
    -e 'CONSUL_LOCAL_CONFIG={"skip_leave_on_interrupt": true}' \
    --name=node2 \
    consul agent -server \
    -bind=172.17.0.4 \
    -join=172.17.0.3 \
    -node=node2

docker run -d \
    -e 'CONSUL_LOCAL_CONFIG={"skip_leave_on_interrupt": true}' \
    --name=node3 \
    consul agent -server \
    -bind=172.17.0.5 \
    -join=172.17.0.3  \
    -node=node3 \
    -client=172.17.0.5


docker run -d \
    -e 'CONSUL_LOCAL_CONFIG={"leave_on_terminate": true}' \
    --name=node4 \
    consul agent \
    -bind=172.17.0.6 \
    -retry-join=172.17.0.3 \
    -node=node4

查看集群节点状态

docker exec -t node1 consul members

模拟节点异常

docker stop node1

image.png

查看leader节点/节点状态
需要通过 -client 指定ip ?

curl http://172.17.0.5:8500/v1/status/leader
curl http://172.17.0.5:8500/v1/catalog/nodes

使用

注册服务
服务名:userService
Address:127.100.0.1:8888,服务发现返回的地址

curl http://172.17.0.5:8500/v1/agent/service/register -X PUT -i -H "Content-Type:application/json" -d '{
 "ID": "userServiceId",  
 "Name": "userService",
 "Tags": [
   "primary",
   "v1"
 ],
 "Address": "127.100.0.1",
 "Port": 8888,
 "EnableTagOverride": false,
 "Check": {
   "DeregisterCriticalServiceAfter": "90m",
   "HTTP": "http://www.baidu.com",
   "Interval": "1s"
 }
}'

发现服务

curl http://172.17.0.5:8500/v1/catalog/service/userService

KV存储

docker exec -t node1 consul kv put /user/info 1
docker exec -t node4 consul kv get user/info
docker exec -t node1 consul kv get --detailed user/info

参考

你可能感兴趣的:(consul)