且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

docker服务发现——etcd集群

更新时间:2022-03-12 13:24:15

etcd

etcd用于服务发现的基础注册和通知,功能类似于zk,通过注册和监听,实现基础的服务发现。

安装

etcd安装非常简单,可以用go自己编译,etcd也提供了可以直接使用的二进制包(64位)。
具体的安装提示页面在github上,
直接按照上面的描述下载即可。为了方便,把里面的etcd相关的二进制文件(etcd, etcdctl等)
复制到了/usr/local/bin中,方便后续使用。

运行

首先尝试单机版启动,参照手册先直接启动,etcd默认监听的是localhost,既只监听了lo设备,
这样会导致启动后集群中的其他机器无法访问,因此在启动的时候将默认的localhost改成0.0.0.0,
确保etcd监听了所有网卡。


etcd -listen-client-urls "http://0.0.0.0:4001" -listen-peer-urls="http://0.0.0.0:7001"

启动之后可通过rest接口或者etcdctl执行命令:


curl -L http://127.0.0.1:4001/version

输出结果为:

{“releaseVersion”:”2.0.0″,”internalVersion”:”2″}

简单写入和读取:


curl -L http://127.0.0.1:4001/v2/keys/message -XPUT -d value="Hello world"

{“action”:”set”,”node”:{“key”:”/message”,”value”:”Hello world”,”modifiedIndex”:3,”createdIndex”:3}}


curl -L http://127.0.0.1:4001/v2/keys/message

{“action”:”get”,”node”:{“key”:”/message”,”value”:”Hello world”,”modifiedIndex”:3,”createdIndex”:3}}

集群启动

之前的启动方式为单机启动,集群创建官方给了很多种方式,这里尝试动态添加机器的方式。

etcd -name "node1" -initial-cluster "node1=http://10.211.55.11:2380" 
    -initial-advertise-peer-urls "http://10.211.55.11:2380" 
    -listen-client-urls "http://0.0.0.0:4001" 
    -listen-peer-urls="http://0.0.0.0:2380"

启动之后,通过member api,可以查看到当前集群:


curl -L http://10.211.55.11:4001/v2/members

{“members”:[{“id”:”f0b31008acf03099″,”name”:”node1″,”peerURLs”:[“http://10.211.55.11:2380″,”http://10.211.55.11:7001″],”clientURLs”:[“http://localhost:2379″,”http://localhost:4001″]}]}

向集群中添加一台机器:


etcdctl member add node2 http://10.211.55.12:2380

Added member named node2 with ID 6d345c68496f80fc to cluster

ETCD_NAME=”node2″
ETCD_INITIAL_CLUSTER=”node2=http://10.211.55.12:2380,node1=http://10.211.55.11:2380,node1=http://10.211.55.11:7001″
ETCD_INITIAL_CLUSTER_STATE=”existing”

这里为了方便,没有只用rest接口,直接使用了etcdctl,该命令会提示在启动新的节点时所需要配置的环境变量。

启动新的节点:


export ETCD_NAME="node2"
export ETCD_INITIAL_CLUSTER="node1=http://10.211.55.11:2380,node2=http://10.211.55.12:2380"
export ETCD_INITIAL_CLUSTER_STATE="existing"
etcd -listen-client-urls http://0.0.0.0:4001 
    -advertise-client-urls http://0.0.0.0:4001 
    -listen-peer-urls http://10.211.55.12:2380 
    -initial-advertise-peer-urls http://10.211.55.12:2380

启动之后,通过member接口查看当前集群机器:


etcdctl member list

293ea5ba1d70f5f1: name=node2 peerURLs=http://10.211.55.12:2380 clientURLs=http://0.0.0.0:4001

bd93686a68a54c2d: name=node1 peerURLs=http://10.211.55.11:2380 clientURLs=http://localhost:2379,http://localhost:4001

同样的方式再添加一台,成为基础的3台机器集群:


export ETCD_NAME="node3"
export ETCD_INITIAL_CLUSTER="node2=http://10.211.55.12:2380,node3=http://10.211.55.13:2380,node1=http://10.211.55.11:2380"
export ETCD_INITIAL_CLUSTER_STATE="existing"
etcd -listen-client-urls http://0.0.0.0:4001 
    -advertise-client-urls http://0.0.0.0:4001 
    -listen-peer-urls http://10.211.55.13:2380 
    -initial-advertise-peer-urls http://10.211.55.13:2380

最终集群为:

293ea5ba1d70f5f1: name=node2 peerURLs=http://10.211.55.12:2380 clientURLs=http://0.0.0.0:4001

76610041ace6c4f8: name=node3 peerURLs=http://10.211.55.13:2380 clientURLs=http://0.0.0.0:4001

bd93686a68a54c2d: name=node1 peerURLs=http://10.211.55.11:2380 clientURLs=http://localhost:2379,http://localhost:4001

在node1节点机器上运行:


etcdctl set /message hello

之后,在三台机器中执行:


etcdctl get /message

都能够正确的获取这个key的值:hello。


转载自:https://coolex.info/blog/481.html