etcd集群搭建

2017-08-24  本文已影响0人  liuzg0734

本文摘自:http://blog.csdn.net/tzm1921995/article/details/52212168

对于通过apt或yum方式安装etcd的,可以修改etcd.conf配置文件来配置各个参数。


Etcd集群简介

etcd组件作为一个高可用强一致性的服务发现存储仓库。我们此次在三个主机上搭建了一个包含三个Etcd节点的集群,实现了集群的动态扩展和收缩,并测试和验证了Etcd集群键——值存储的一致性和高

一、环境准备

OS: Ubuntu 14.04

user: root

ipaddress: 

etcd01: 192.168.200.24

etcd02: 192.168.200.25

etcd03: 192.168.200.26

下载etcd 源码包:https://github.com/coreos/etcd/releases/

这里用的是etcd-v2.3.7-Linux-amd64.tar.gz

二、安装配置etcd

etcd01上安装

tar xf etcd-v2.3.7-linux-amd64.tar.gz

cd etcd-v2.3.7-linux-amd64

cp etcd* /usr/local/bin/

创建etcd01脚本,可方便配置etcd启动参数

#cat etcd01

/usr/local/bin/etcd  -name etcd01 \

-data-dir /data/etcd01 \

-advertise-client-urls http://192.168.200.24:2379,http://192.168.200.24:4001  \

-listen-client-urls  http://0.0.0.0:2379,http://192.168.200.24:4001 \

-initial-advertise-peer-urls http://192.168.200.24:2380 \

-listen-peer-urls http://0.0.0.0:2380 \

-initial-cluster-token etcd-cluster-1 \

-initial-cluster etcd01=http://192.168.200.24:2380,etcd02=http://192.168.200.25:2380,etcd03=http://192.168.200.26:2380 \

-initial-cluster-state new

参数说明:-name 指定名字

-data-dir 指定数据保存目录,默认是当前目录

-initial-cluster-state 集群状态 new为新创建集群 existing为已存在(可不指定)

在etcd02 etcd03上分别做相似操作

脚本中-advertise-client-urls 和 -initial-advertis-peer-urls 参数修改一下即可

然后分别运行脚本:nohup ./etcd01 &

三、测试

在任一台主机上执行etcdctl member list

#etcdctl member list

6a223770249e927d: name=etcd02 peerURLs=http://192.168.200.25:2380 clientURLs=http://192.168.200.25:2379,http://192.168.200.25:4001 isLeader=false

7e0ce16121dfea24: name=etcd01 peerURLs=http://192.168.200.24:2380 clientURLs=http://192.168.200.24:2379,http://192.168.200.24:4001 isLeader=true

bfc28be8765b503e: name=etcd03 peerURLs=http://192.168.200.26:2380 clientURLs=http://192.168.200.26:2379,http://192.168.200.26:4001 isLeader=false

可以看到集群的节点情况,并能看出哪个是leader节点

我们在etcd01上设置一个key/value

root@etcd1:~# etcdctl set api_server http://192.168.5.44:8080

http://192.168.5.44:8080

这时就可以在任意一台主机上获取这个key/value

root@etcd2:~# etcdctl get api_server

http://192.168.5.44:8080

root@etcd3:~# etcdctl get api_server

http://192.168.5.44:8080

在member list上看到etcd01是leader ,这时把etcd01停掉(kill)

用etcdctl cluster-health查看

root@etcd2:~# etcdctl cluster-health

member 6a223770249e927d is healthy: got healthy result from http://192.168.200.25:2379

failed to check the health of member 7e0ce16121dfea24 on http://192.168.200.24:2379: Get http://192.168.200.24:2379/health: dial tcp 192.168.200.24:2379: getsockopt: connection refused

failed to check the health of member 7e0ce16121dfea24 on http://192.168.200.24:4001: Get http://192.168.200.24:4001/health: dial tcp 192.168.200.24:4001: getsockopt: connection refused

member 7e0ce16121dfea24 is unreachable: [http://192.168.200.24:2379 http://192.168.200.24:4001] are all unreachable

member bfc28be8765b503e is healthy: got healthy result from http://192.168.200.26:2379

cluster is healthy

并且集群leader进行了重新选举

root@etcd2:~# etcdctl member list

6a223770249e927d: name=etcd02 peerURLs=http://192.168.200.25:2380 clientURLs=http://192.168.200.25:2379,http://192.168.200.25:4001 isLeader=true

7e0ce16121dfea24: name=etcd01 peerURLs=http://192.168.200.24:2380 clientURLs=http://192.168.200.24:2379,http://192.168.200.24:4001 isLeader=false

bfc28be8765b503e: name=etcd03 peerURLs=http://192.168.200.26:2380 clientURLs=http://192.168.200.26:2379,http://192.168.200.26:4001 isLeader=false

现在etcd02是leader了,这时我们在群集中设置两个key/value

root@etcd2:~# etcdctl set test01 123456

123456

root@etcd2:~# etcdctl set test02 abcdefg

abcdefg

重新启动etcd01

root@etcd1:~# etcdctl cluster-health

member 6a223770249e927d is healthy: got healthy result from http://192.168.200.25:2379

member 7e0ce16121dfea24 is healthy: got healthy result from http://192.168.200.24:2379

member bfc28be8765b503e is healthy: got healthy result from http://192.168.200.26:2379

cluster is healthy

root@etcd1:~# etcdctl get test01

123456

root@etcd1:~# etcdctl get test02

abcdefg

但这时在etcd01重新加入集群,并保持了key/value的全局一致性,由此可见 etcd 搭建的集群是可以实现高可用的。

Etcd集群的扩展与收缩

etcd集群如果收缩很简单,直接在命令行输入

etcdctl member remove {$memberID}

$memberID是你即将要删除节点的etcd的ID,etcd的扩展有一些地方需要注意一下,我在这里操作的时候遇到了不少坑。从上文写到现在,有一个文件夹很重要,几乎每个坑都与它有关,那就是-data-dir所声明的文件夹,注意要扩展一个etcd集群时,首先在集群内的任一台机器上输入

etcdctl member add $etcd_name $peer_url

$etcd_name:新加入的etcd节点的名字

$peer_url:一般为新加入的节点 IP:2380

下面为通过yum方式安装后,/etc/etcd/etcd.conf配置文件的参考实例:


# 节点名称

ETCD_NAME=etcd0

# # 数据存放位置

ETCD_DATA_DIR="/var/lib/etcd/default.etcd"

# # 监听其他 Etcd 实例的地址

ETCD_LISTEN_PEER_URLS="http://0.0.0.0:2380"

# # 监听客户端地址

ETCD_LISTEN_CLIENT_URLS="http://0.0.0.0:2379,http://0.0.0.0:4001"

# # 通知其他 Etcd 实例地址

ETCD_INITIAL_ADVERTISE_PEER_URLS="http://192.168.212.140:2380"

# # 初始化集群内节点地址

ETCD_INITIAL_CLUSTER="etcd0=http://192.168.212.140:2380,etcd1=http://192.168.212.139:2380,etcd2=http://192.168.212.138:2380"

# # 初始化集群状态,new 表示新建

ETCD_INITIAL_CLUSTER_STATE="new"

# # 初始化集群 token

ETCD_INITIAL_CLUSTER_TOKEN="mritd-etcd-cluster"

# # 通知 客户端地址

ETCD_ADVERTISE_CLIENT_URLS="http://192.168.212.140:2379,http://192.168.212.140:4001"

上一篇 下一篇

猜你喜欢

热点阅读