安装部署

按照官网文档安装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 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/去除

# 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 &amp;

然后可以看到有三个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

原文地址:https://blog.csdn.net/qq_42604176/article/details/129113803

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_19623.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注