天天看点

docker服务发现——etcd集群

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

etcd安装非常简单,可以用go自己编译,etcd也提供了可以直接使用的二进制包(64位)。

具体的安装提示页面在github上,

直接按照上面的描述下载即可。为了方便,把里面的etcd相关的二进制文件(etcd, etcdctl等)

复制到了/usr/local/bin中,方便后续使用。

首先尝试单机版启动,参照手册先直接启动,etcd默认监听的是localhost,既只监听了lo设备,

这样会导致启动后集群中的其他机器无法访问,因此在启动的时候将默认的localhost改成0.0.0.0,

确保etcd监听了所有网卡。

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

输出结果为:

{“releaseversion”:”2.0.0″,”internalversion”:”2″}

简单写入和读取:

{“action”:”set”,”node”:{“key”:”/message”,”value”:”hello world”,”modifiedindex”:3,”createdindex”:3}}

</blockquote>

{“action”:”get”,”node”:{“key”:”/message”,”value”:”hello world”,”modifiedindex”:3,”createdindex”:3}}

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

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

{“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″]}]}

向集群中添加一台机器:

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,该命令会提示在启动新的节点时所需要配置的环境变量。

启动新的节点:

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

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台机器集群:

最终集群为:

76610041ace6c4f8: name=node3 peerurls=http://10.211.55.13:2380 clienturls=http://0.0.0.0:4001

在node1节点机器上运行:

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

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

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

继续阅读