etcd-集群节点的增加、删除操作

下载安装

$ mkdir -p $GOPATH/src/github.com/coreos
$ cd !$
$ git clone https://github.com/coreos/etcd.git
$ cd etcd
//编译
$ ./build 

$ cd /bin

集群搭建

下面只用同一台服务器进行三个成员节点的开启,以下命令在 ` /bin ` 目录下操作

节点1

./etcd --name cd0 --initial-advertise-peer-urls http://127.0.0.1:2380 --listen-peer-urls http://127.0.0.1:2380 --listen-client-urls http://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

节点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

节点3

./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

注意:每个一个新节点需要开启一个新的命令端。再以上相同的路径下操作

查询 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

添加 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 b9057cfdc8ff17ce

运行结果:

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

代码实现 member 的管理

添加节点

func addMember(cli *clientv3.Client)  {
    peerURLs := []string{"http://127.0.0.1:2180"}

    mresp, err := cli.MemberAdd(context.Background(), peerURLs)
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println("added member.PeerURLs:", mresp.Member.PeerURLs)
    resp, err := cli.MemberList(context.Background())
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println("添加后 members:", resp.Members)
}

执行完 添加节点,需要打开终端启动节点服务器

./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

如果启动失败,需要删除 cd3 的信息

删除节点

// 删除节点
func delMember (cli *clientv3.Client, memberId uint64) {

	resp, err := cli.MemberList(context.Background())
	if err != nil {
		log.Fatal(err)
	}

	_, err = cli.MemberRemove(context.Background(), memberId)
	if err != nil {
		log.Fatal(err)
	}

	resp, err = cli.MemberList(context.Background())
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println("删除后 members:", resp.Members)
}

全部代码

package main

import (
	"github.com/coreos/etcd/clientv3"
	"log"
	"fmt"
	"context"
	"time"
)
var (
	dialTimeout    = 5 * time.Second
	requestTimeout = 2 * time.Second
	endpoints      = []string{"127.0.0.1:2379"}
)
func main()  {

	cli, err := clientv3.New(clientv3.Config{
		Endpoints:   endpoints,
		DialTimeout: dialTimeout,
	})
	if err != nil {
		log.Fatal(err)
	}
	defer cli.Close()

	resp, err := cli.MemberList(context.Background())
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println("members:", resp.Members)


	//添加member
	//addMember(cli)


	// 删除节点
	//delMember(cli,uint64(7438291228984697304))

}

func addMember(cli *clientv3.Client)  {
	peerURLs := []string{"http://127.0.0.1:2180"}

	mresp, err := cli.MemberAdd(context.Background(), peerURLs)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println("added member.PeerURLs:", mresp.Member.PeerURLs)
	resp, err := cli.MemberList(context.Background())
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println("添加后 members:", resp.Members)
}

// 删除节点
func delMember (cli *clientv3.Client, memberId uint64) {

	resp, err := cli.MemberList(context.Background())
	if err != nil {
		log.Fatal(err)
	}

	_, err = cli.MemberRemove(context.Background(), memberId)
	if err != nil {
		log.Fatal(err)
	}

	resp, err = cli.MemberList(context.Background())
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println("删除后 members:", resp.Members)
}

总结

在工业生产中,节点的增加删除操作一般使用代码实现

如有错误描述,请评论纠正,谢谢大家!🐳🐳🐳

打赏一个呗

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦