LOADING...
LOADING...
LOADING...
当前位置: 玩币族首页 > 区块链资产 > 搭建etcd集群、添加和删除节点

搭建etcd集群、添加和删除节点

2019-10-22 Online 来源:区块链网络
搭建etcd集群、添加和删除节点 1.首先在终端执行起来一个节点

./etcd --name htcd0 --initial-advertise-peer-urls?http://127.0.0.1:2380?--listen-peer-urls?http://127.0.0.1:2380?--listen-client-urls tp://127.0.0.1:2379 --advertise-client-urls?http://127.0.0.1:2379?--initial-cluster-token etcd-cluster-1 --initial-cluster cd0=http://127.0.0.1:2380,cd1=http://127.0.0.1:2480,cd2=http://127.0.0.1:2580?--initial-cluster-state new上边的127.0.0.1:2380是终端执行的节点url而127.0.0.1:2379的节点是2380端口节点要监听的节点。

为上边的执行命令做个解析:--nameetcd集群中的节点名,这里可以随意,可区分且不重复就行--listen-peer-urls监听的用于节点之间通信的url,可监听多个,集群内部将通过这些url进行数据交互(如选举,数据同步等)--initial-advertise-peer-urls建议用于节点之间通信的url,节点间将以该值进行通信。--listen-client-urls监听的用于客户端通信的url,同样可以监听多个。--advertise-client-urls建议使用的客户端通信url,该值用于etcd代理或etcd成员与etcd节点通信。--initial-cluster-token etcd-cluster-1节点的token值,设置该值后集群将生成唯一id,并为每个节点也生成唯一id,当使用相同配置文件再启动一个集群时,只要该token值不一样,etcd集群就不会相互影响。--initial-cluster也就是集群中所有的initial-advertise-peer-urls 的合集--initial-cluster-state new新建集群的标志,初始化状态使用 new,建立之后改此值为 existing

2.再在终端开启2个节点,组成一个集群

./etcd --name cd1 --initial-advertise-peer-urls?http://127.0.0.1:2480?--listen-peer-urls?http://127.0.0.1:2480?--listen-client-urls?http://127.0.0.1:2479?--advertise-client-urls?http://127.0.0.1:2479?--initial-cluster-token etcd-cluster-1 --initial-cluster cd0=http://127.0.0.1:2380,cd1=http://127.0.0.1:2480,cd2=http://127.0.0.1:2580?--initial-cluster-state new这段命令是添加第二个节点,也可以通过go编程添加节点,只不过要用2479这个url通讯./etcd --name cd2 --initial-advertise-peer-urls?http://127.0.0.1:2580?--listen-peer-urls?http://127.0.0.1:2580?--listen-client-urls?http://127.0.0.1:2579?--advertise-client-urls?http://127.0.0.1:2579?--initial-cluster-token etcd-cluster-1 --initial-cluster cd0=http://127.0.0.1:2380,cd1=http://127.0.0.1:2480,cd2=http://127.0.0.1:2580?--initial-cluster-state new执行第三个节点,用2579通讯

3.查询 member 列表

export?ETCDCTL_API=3ENDPOINTS=127.0.0.1:2379,127.0.0.1:2479,127.0.0.1:2579./etcdctl?--endpoints=$ENDPOINTS?member?list

运行结果:

98f0c6bf64240842,?started,?cd2,?http://127.0.0.1:2580,?http://127.0.0.1:2579 bf9071f4639c75cc,?started,?cd0,?http://127.0.0.1:2380,?http://127.0.0.1:2379 e3ba87c3b4858ef1,?started,?cd1,?http://127.0.0.1:2480,?http://127.0.0.1:2479

4.添加 member 节点

member add?添加节点

./etcdctl?--endpoints=$ENDPOINTS?member?add?cd3?--peer-urls=http://127.0.0.1:2180Member?b9057cfdc8ff17ce?added?to?cluster?9da8cd75487bd6dc

运行结果:

ETCD_NAME="cd3"ETCD_INITIAL_CLUSTER="cd2=http://127.0.0.1:2580,cd3=http://127.0.0.1:2180,cd0=http://127.0.0.1:2380,cd1=http://127.0.0.1:2480"ETCD_INITIAL_ADVERTISE_PEER_URLS="http://127.0.0.1:2180"ETCD_INITIAL_CLUSTER_STATE="existing"

查询 member 节点列表信息

./etcdctl?--endpoints=$ENDPOINTS?member?list

运行结果:

98f0c6bf64240842,?started,?cd2,?http://127.0.0.1:2580,?http://127.0.0.1:2579 b9057cfdc8ff17ce,?unstarted,?,?http://127.0.0.1:2180,bf9071f4639c75cc,?started,?cd0,?http://127.0.0.1:2380,?http://127.0.0.1:2379 e3ba87c3b4858ef1,?started,?cd1,?http://127.0.0.1:2480,?http://127.0.0.1:2479

通过查询结果可以发现:http://127.0.0.1:2180?显示状态为:unstarted

启动新节点

./etcd?--name?cd3?--listen-client-urls?http://127.0.0.1:2179?--advertise-client-urls?http://127.0.0.1:2179?--listen-peer-urls?http://127.0.0.1:2180?--initial-advertise-peer-urls?http://127.0.0.1:2180?--initial-cluster-state?existing?--initial-cluster?cd2=http://127.0.0.1:2580,cd0=http://127.0.0.1:2380,cd3=http://127.0.0.1:2180,cd1=http://127.0.0.1:2480?--initial-cluster-token?etcd-cluster-1

查询 member 节点列表信息

./etcdctl?--endpoints=$ENDPOINTS?member?list

运行结果:

98f0c6bf64240842,?started,?cd2,?http://127.0.0.1:2580,?http://127.0.0.1:2579 b9057cfdc8ff17ce,?started,?cd3,?http://127.0.0.1:2180,?http://127.0.0.1:2179 bf9071f4639c75cc,?started,?cd0,?http://127.0.0.1:2380,?http://127.0.0.1:2379 e3ba87c3b4858ef1,?started,?cd1,?http://127.0.0.1:2480,?http://127.0.0.1:2479

5.删除 member

./etcdctl?--endpoints=$ENDPOINTS?member?remove?21a304d74d267837

运行结果:

Member?b9057cfdc8ff17ce?removed?from?cluster?9da8cd75487bd6dc

查询 member 节点列表信息

./etcdctl?--endpoints=$ENDPOINTS?member?list

运行结果:

98f0c6bf64240842,?started,?cd2,?http://127.0.0.1:2580,?http://127.0.0.1:2579 bf9071f4639c75cc,?started,?cd0,?http://127.0.0.1:2380,?http://127.0.0.1:2379 e3ba87c3b4858ef1,?started,?cd1,?http://127.0.0.1:2480,?http://127.0.0.1:2479

6. 添加 member 节点

member add?添加节点

./etcdctl?--endpoints=$ENDPOINTS?member?add?cd3?--peer-urls=http://127.0.0.1:2180Member?b9057cfdc8ff17ce?added?to?cluster?9da8cd75487bd6dc

运行结果:

ETCD_NAME="cd3"ETCD_INITIAL_CLUSTER="cd2=http://127.0.0.1:2580,cd3=http://127.0.0.1:2180,cd0=http://127.0.0.1:2380,cd1=http://127.0.0.1:2480"ETCD_INITIAL_ADVERTISE_PEER_URLS="http://127.0.0.1:2180"ETCD_INITIAL_CLUSTER_STATE="existing"

查询 member 节点列表信息

./etcdctl?--endpoints=$ENDPOINTS?member?list

运行结果:

98f0c6bf64240842,?started,?cd2,?http://127.0.0.1:2580,?http://127.0.0.1:2579 b9057cfdc8ff17ce,?unstarted,?,?http://127.0.0.1:2180,bf9071f4639c75cc,?started,?cd0,?http://127.0.0.1:2380,?http://127.0.0.1:2379 e3ba87c3b4858ef1,?started,?cd1,?http://127.0.0.1:2480,?http://127.0.0.1:2479

通过查询结果可以发现:http://127.0.0.1:2180?显示状态为:unstarted

启动新节点

./etcd?--name?cd3?--listen-client-urls?http://127.0.0.1:2179?--advertise-client-urls?http://127.0.0.1:2179?--listen-peer-urls?http://127.0.0.1:2180?--initial-advertise-peer-urls?http://127.0.0.1:2180?--initial-cluster-state?existing?--initial-cluster?cd2=http://127.0.0.1:2580,cd0=http://127.0.0.1:2380,cd3=http://127.0.0.1:2180,cd1=http://127.0.0.1:2480?--initial-cluster-token?etcd-cluster-1

查询 member 节点列表信息

./etcdctl?--endpoints=$ENDPOINTS?member?list

运行结果:

98f0c6bf64240842,?started,?cd2,?http://127.0.0.1:2580,?http://127.0.0.1:2579 b9057cfdc8ff17ce,?started,?cd3,?http://127.0.0.1:2180,?http://127.0.0.1:2179 bf9071f4639c75cc,?started,?cd0,?http://127.0.0.1:2380,?http://127.0.0.1:2379 e3ba87c3b4858ef1,?started,?cd1,?http://127.0.0.1:2480,?http://127.0.0.1:2479

删除 member

./etcdctl?--endpoints=$ENDPOINTS?member?remove?21a304d74d267837

运行结果:

Member?b9057cfdc8ff17ce?removed?from?cluster?9da8cd75487bd6dc

查询 member 节点列表信息

./etcdctl?--endpoints=$ENDPOINTS?member?list

运行结果:

98f0c6bf64240842,?started,?cd2,?http://127.0.0.1:2580,?http://127.0.0.1:2579 bf9071f4639c75cc,?started,?cd0,?http://127.0.0.1:2380,?http://127.0.0.1:2379 e3ba87c3b4858ef1,?started,?cd1,?http://127.0.0.1:2480,?http://127.0.0.1:2479

7.执行go程序代码,增加或者删除节点

package main

import ("github.com/coreos/etcd/clientv3""context""fmt""time")

//通过代码向网络添加和删除节点,配置这台电脑的url和超时保护var (dialTimeout=5time.SecondrequestTimeout=2time.SecondendPoints=[]string{"127.0.0.1:2379"})

//添加节点func addMember(cli *clientv3.Client,url string) {peerURLs:=[]string {url}//向系统中插入节点_,err:=cli.MemberAdd(context.Background(),peerURLs)if err!=nil{fmt.Println("memberadd",err)}//显示插入的节点resp,err:=cli.MemberList(context.Background())if err!=nil{fmt.Println("memberlist",err)}fmt.Println("添加后的members为",resp.Members)

}func main() {//运行之前终端先开启一个节点,再执行下边的代码,来链接终端那侧的节点,链接后这个cli其实就是个中介的角色,下边再进行给创建及删除节点,//新建的节点与终端执行的节点组成集群。cli,err:=clientv3.New(clientv3.Config{Endpoints:endPoints,DialTimeout:dialTimeout,})if err!=nil{fmt.Println("clientv3 new",err)}

fmt.Println("cli.Cluster:",cli)defer?cli.Close()//添加新节点addMember(cli,"127.0.0.1:2180")addMember(cli,"127.0.0.1:2181")addMember(cli,"127.0.0.1:2182")//删除节点//delMember(cli,0xe32088547caa8215)

}//删除节点func delMember(cli *clientv3.Client,memberId uint64) {_,err:=cli.MemberRemove(context.Background(),memberId)if err!=nil{fmt.Println("delMember",err)}else {fmt.Printf("删除节点%s成功了\n",memberId)}}

执行go编程创建的节点连接到集群,但是处于unstarted的状态执行完 添加节点,需要打开终端启动节点服务器./etcd --name cd3 --listen-client-urls http://127.0.0.1:2179 --advertise-client-urls http://127.0.0.1:2179 --listen-peer-urls http://127.0.0.1:2180 --initial-advertise-peer-urls http://127.0.0.1:2180 --initial-cluster-state existing --initial-cluster cd2=http://127.0.0.1:2580,cd0=http://127.0.0.1:2380,cd3=http://127.0.0.1:2180,cd1=http://127.0.0.1:2480 --initial-cluster-token etcd-cluster-1

6aS7Wnhx86vpAdjkpgRggvxzkeLdjE87ZTPVzIyJ.png

作者:张默默

区块链高级工程师、go语言工程师、IPFS早期研究者。网络著书有《IPFS一问一答》、《IPFS操作教程》、《区块链密码学》、《区块链共识算法》;跟踪并翻译报道《IPFS Weekly》,研究并翻译IPFS、Filecoin白皮书,业界知名专家学者——张默默老师!

—-

编译者/作者:Online

玩币族申明:玩币族作为开放的资讯翻译/分享平台,所提供的所有资讯仅代表作者个人观点,与玩币族平台立场无关,且不构成任何投资理财建议。文章版权归原作者所有。

LOADING...
LOADING...