目录

安装部署

按照官网文档,安装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 &

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

关于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.

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

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

import etcd3

etcd = 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: 14841639068965178418
  member_id: 10276657743932975437
  revision: 5
  raft_term: 4
}

(b'doot', <etcd3.client.KVMetadata object at 0x7f07150efa30>)
True
(None, None)

本地多成员集群搭建

go get github.com/mattn/goreman
goreman -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/去除

然后在该目录下执行

goreman -f Procfile start &

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

然后向其他节点插入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

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

获取成功

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']

参考

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

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

集群部署文档:

etcd实现选举