ETCD本地多成员集群部署
创始人
2024-05-26 08:02:36
0

目录

  • 安装部署
    • etcdctl 操作etcd
    • 使用http请求操作etcd
  • 本地多成员集群搭建
    • python获取成员信息
  • 参考

安装部署

按照官网文档,安装release版本

https://etcd.io/docs/v3.4/install/

[root@VM-33-162-centos /usr/local/bin]# etcd --version
etcd Version: 3.4.16
Git SHA: d19fbe541
Go Version: go1.12.17
Go OS/Arch: linux/amd64

直接使用etcdctl不行,需要添加到path,或者

/tmp/etcd-download-test/etcdctl

etcd_svr默认开放端口2379

后台启动etcd

 etcd &

etcdctl 操作etcd

put / get

/tmp/etcd-download-test/etcdctl put greeting "hello etcd"
OK
[root@VM-33-162-centos /usr/local/bin]# /tmp/etcd-download-test/etcdctl get greeting 
greeting
hello etcd

使用http请求操作etcd

首先安装etcd3,注意可能会出现问题

关于protobuf报错:If this call came from a _pb2.py file, 
your generated code is out of date and must be regenerated with protoc >= 3.19.0.

所以我们安装的时候需要:

pip install etcd3
pip3 install --upgrade protobuf==3.20.1

接下来我们对本地的etcd进行访问

import etcd3etcd = etcd3.client(host='127.0.0.1', port=2379)print(etcd.put('bar', 'doot'))
print(etcd.get('bar'))
print(etcd.delete('bar'))
print(etcd.get('bar'))
(env) [hanhandi@VM-33-162-centos ~/hanhan_PythonScripts/etcdTest]$ python demo.py 
header {cluster_id: 14841639068965178418member_id: 10276657743932975437revision: 5raft_term: 4
}(b'doot', )
True
(None, None)

本地多成员集群搭建

 go get github.com/mattn/goremangoreman -f Procfile start

在执行第二句时:

goreman: open Procfile: no such file or directory

我们从下面这个网址拷贝出Profile

https://github.com/etcd-io/etcd/blob/main/Procfile

然后在/usr/local/bin/目录下创建文件,粘贴进去,并将bin/去除

# Use goreman to run `go install github.com/mattn/goreman@latest`
# Change the path of bin/etcd if etcd is located elsewhereetcd1: etcd --name infra1 --listen-client-urls http://127.0.0.1:2379 --advertise-client-urls http://127.0.0.1:2379 --listen-peer-urls http://127.0.0.1:12380 --initial-advertise-peer-urls http://127.0.0.1:12380 --initial-cluster-token etcd-cluster-1 --initial-cluster 'infra1=http://127.0.0.1:12380,infra2=http://127.0.0.1:22380,infra3=http://127.0.0.1:32380' --initial-cluster-state new --enable-pprof --logger=zap --log-outputs=stderr
etcd2: etcd --name infra2 --listen-client-urls http://127.0.0.1:22379 --advertise-client-urls http://127.0.0.1:22379 --listen-peer-urls http://127.0.0.1:22380 --initial-advertise-peer-urls http://127.0.0.1:22380 --initial-cluster-token etcd-cluster-1 --initial-cluster 'infra1=http://127.0.0.1:12380,infra2=http://127.0.0.1:22380,infra3=http://127.0.0.1:32380' --initial-cluster-state new --enable-pprof --logger=zap --log-outputs=stderr
etcd3: etcd --name infra3 --listen-client-urls http://127.0.0.1:32379 --advertise-client-urls http://127.0.0.1:32379 --listen-peer-urls http://127.0.0.1:32380 --initial-advertise-peer-urls http://127.0.0.1:32380 --initial-cluster-token etcd-cluster-1 --initial-cluster 'infra1=http://127.0.0.1:12380,infra2=http://127.0.0.1:22380,infra3=http://127.0.0.1:32380' --initial-cluster-state new --enable-pprof --logger=zap --log-outputs=stderr
#proxy: bin/etcd grpc-proxy start --endpoints=127.0.0.1:2379,127.0.0.1:22379,127.0.0.1:32379 --listen-addr=127.0.0.1:23790 --advertise-client-url=127.0.0.1:23790 --enable-pprof# A learner node can be started using Procfile.learner

然后在该目录下执行

 goreman -f ./Procfile start &

然后就可以看到有三个etcd节点在运行了

在这里插入图片描述

查看一下集群运行状态:

(env) [hanhandi@VM-33-162-centos ~/hanhan_PythonScripts]$ export ETCDCTL_API=3
(env) [hanhandi@VM-33-162-centos ~/hanhan_PythonScripts]$ /tmp/etcd-download-test/etcdctl --write-out=table --endpoints=localhost:2379 member list
+------------------+---------+--------+------------------------+------------------------+------------+
|        ID        | STATUS  |  NAME  |       PEER ADDRS       |      CLIENT ADDRS      | IS LEARNER |
+------------------+---------+--------+------------------------+------------------------+------------+
| 8211f1d0f64f3269 | started | infra1 | http://127.0.0.1:12380 |  http://127.0.0.1:2379 |      false |
| 91bc3c398fb3c146 | started | infra2 | http://127.0.0.1:22380 | http://127.0.0.1:22379 |      false |
| fd422379fda50e48 | started | infra3 | http://127.0.0.1:32380 | http://127.0.0.1:32379 |      false |
+------------------+---------+--------+------------------------+------------------------+------------+

为了模拟容灾情况,我们手动kill一个etcd节点

$ ps -ef | grep etcd | grep 127.0.0.1:22379
root     29622 29613  0 14:45 pts/4    00:00:00 etcd --name infra2 --listen-client-urls http://127.0.0.1:22379 --advertise-client-urls http://127.0.0.1:22379 --listen-peer-urls http://127.0.0.1:22380 --initial-advertise-peer-urls http://127.0.0.1:22380 --initial-cluster-token etcd-cluster-1 --initial-cluster infra1=http://127.0.0.1:12380,infra2=http://127.0.0.1:22380,infra3=http://127.0.0.1:32380 --initial-cluster-state new --enable-pprof --logger=zap --log-outputs=stderr
$ kill -9 29622

然后向其他节点插入key value

# /tmp/etcd-download-test/etcdctl --endpoints=localhost:2379 put key hello
OK
# /tmp/etcd-download-test/etcdctl --endpoints=localhost:2379 get key
key
hello

然后尝试从已删除节点进行获取信息

# /tmp/etcd-download-test/etcdctl --endpoints=localhost:22379 get key
{"level":"warn","ts":"2023-02-18T14:49:58.251+0800","caller":"clientv3/retry_interceptor.go:62","msg":"retrying of unary invoker failed","target":"endpoint://client-6cf6d681-cf64-4762-bb9d-c2a2d1332190/localhost:22379","attempt":0,"error":"rpc error: code = DeadlineExceeded desc = latest balancer error: all SubConns are in TransientFailure, latest connection error: connection error: desc = \"transport: Error while dialing dial tcp [::1]:22379: connect: connection refused\""}
Error: context deadline exceeded

我们再重启该节点,并尝试获取

# goreman run restart etcd2
# /tmp/etcd-download-test/etcdctl --endpoints=localhost:22379 get key
key
hello

获取成功

python获取成员信息

import etcd3
import json
etcd = etcd3.client(host='127.0.0.1', port=2379)
members = etcd.members
for mem in members:print(mem.__str__())print(mem.id)print(mem.name)print(mem.peer_urls)print(mem.client_urls)
(env) [root@VM-33-162-centos /data/home/hanhandi/hanhan_PythonScripts/etcdTest]# python demo.py 
Member 9372538179322589801: peer urls: ['http://127.0.0.1:12380'], client urls: ['http://127.0.0.1:2379']
9372538179322589801
infra1
['http://127.0.0.1:12380']
['http://127.0.0.1:2379']
Member 10501334649042878790: peer urls: ['http://127.0.0.1:22380'], client urls: ['http://127.0.0.1:22379']
10501334649042878790
infra2
['http://127.0.0.1:22380']
['http://127.0.0.1:22379']
Member 18249187646912138824: peer urls: ['http://127.0.0.1:32380'], client urls: ['http://127.0.0.1:32379']
18249187646912138824
infra3
['http://127.0.0.1:32380']
['http://127.0.0.1:32379']

参考

etcd基本操作:

https://python-etcd3.readthedocs.io/en/latest/readme.html

https://cizixs.com/2016/08/02/intro-to-etcd/

https://python-etcd3.readthedocs.io/en/latest/

中文文档:https://www.zhaowenyu.com/etcd-doc/ops/etcd-install-shell.html

集群部署文档:

https://blog.51cto.com/xiaoyaoyou10/5468746

相关内容

热门资讯

喜欢穿一身黑的男生性格(喜欢穿... 今天百科达人给各位分享喜欢穿一身黑的男生性格的知识,其中也会对喜欢穿一身黑衣服的男人人好相处吗进行解...
发春是什么意思(思春和发春是什... 本篇文章极速百科给大家谈谈发春是什么意思,以及思春和发春是什么意思对应的知识点,希望对各位有所帮助,...
网络用语zl是什么意思(zl是... 今天给各位分享网络用语zl是什么意思的知识,其中也会对zl是啥意思是什么网络用语进行解释,如果能碰巧...
为什么酷狗音乐自己唱的歌不能下... 本篇文章极速百科小编给大家谈谈为什么酷狗音乐自己唱的歌不能下载到本地?,以及为什么酷狗下载的歌曲不是...
华为下载未安装的文件去哪找(华... 今天百科达人给各位分享华为下载未安装的文件去哪找的知识,其中也会对华为下载未安装的文件去哪找到进行解...
怎么往应用助手里添加应用(应用... 今天百科达人给各位分享怎么往应用助手里添加应用的知识,其中也会对应用助手怎么添加微信进行解释,如果能...
家里可以做假山养金鱼吗(假山能... 今天百科达人给各位分享家里可以做假山养金鱼吗的知识,其中也会对假山能放鱼缸里吗进行解释,如果能碰巧解...
四分五裂是什么生肖什么动物(四... 本篇文章极速百科小编给大家谈谈四分五裂是什么生肖什么动物,以及四分五裂打一生肖是什么对应的知识点,希...
一帆风顺二龙腾飞三阳开泰祝福语... 本篇文章极速百科给大家谈谈一帆风顺二龙腾飞三阳开泰祝福语,以及一帆风顺二龙腾飞三阳开泰祝福语结婚对应...
美团联名卡审核成功待激活(美团... 今天百科达人给各位分享美团联名卡审核成功待激活的知识,其中也会对美团联名卡审核未通过进行解释,如果能...