分布式终端与go编程添加和删除节点
Online
2019-10-22
热度6212

etcd搭建集群

分布式终端与go编程添加和删除节点

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端口节点要监听的节点。

为上边的执行命令做个解析:
--name
etcd集群中的节点名,这里可以随意,可区分且不重复就行
--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=3
ENDPOINTS=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:2180
Member 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:2180
Member 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.Second
requestTimeout=2time.Second
endPoints=[]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

作者:张默默

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

本内容旨在传递行业动态,不构成投资建议或承诺。
为你推荐

商务合作:TG:@Lottie96