安装部署
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
/tmp/etcd-download-test/etcdctl
etcd &
etcdctl 操作etcd
/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.
pip install etcd3
pip3 install --upgrade protobuf==3.20.1
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
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 elsewhere
etcd1: 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 &
(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 |
+------------------+---------+--------+------------------------+------------------------+------------+
$ 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
# /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
原文地址:https://blog.csdn.net/qq_42604176/article/details/129113803
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_19623.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。