etcd部署和使用

本文介绍了etcd 3.4.14作为一款高可用的分布式键值数据库,重点讲解了其raft算法实现一致性,以及在Kubernetes中作为元数据存储的角色。包括安装步骤、常用命令和实际应用场景,如服务注册发现、分布式锁和选主等。
摘要由CSDN通过智能技术生成

介绍

高可用的分布式键值(key-value)数据库,raft共识算法实现分布式一致性,在k8s中做元数据存储。类似zookeeper。

官网
https://etcd.io/

github
https://github.com/etcd-io/etcd

部署:
1.github的release上下载最新安装包
etcd-v3.4.14-linux-amd64.tar.gz

2.解压

tar -zxvf etcd-v3.4.14-linux-amd64.tar.gz

[root@localhost etcd-v3.4.14-linux-amd64]# ls
default.etcd  Documentation  etcd  etcdctl  README-etcdctl.md  README.md  READMEv2-etcdctl.md

说明:
etcd 服务端
etcdctl 客户端工具

3.启动

cd etcd-v3.4.14-linux-amd64
nohup ./etcd --listen-peer-urls 'http://0.0.0.0:2380' --listen-client-urls 'http://0.0.0.0:2379' --advertise-client-urls 'http://0.0.0.0:2379' > /dev/null 2>&1 &

# 说明
默认2379端口为客户端通信端口
默认2380位服务之间内部通信端口
0.0.0.0 为配置局域网通过ip访问

常用命令(来自官网)

# put 塞值
./etcdctl put foo "hello world"
# get 获取
./etcdctl get foo

# put 塞值
./etcdctl --endpoints=localhost:2379 put web1 value1
./etcdctl --endpoints=localhost:2379 put web2 value2
./etcdctl --endpoints=localhost:2379 put web3 value3
# 前缀模糊获取
./etcdctl --endpoints=localhost:2379 get web --prefix

# 事务操作
./etcdctl --endpoints=localhost:2379 put user1 bad
./etcdctl --endpoints=localhost:2379 txn --interactive

compares:
value("user1") = "bad"

success requests (get, put, delete):
del user1

failure requests (get, put, delete):
put user1 good

# del 删除操作
./etcdctl --endpoints=localhost:2379 put key myvalue
./etcdctl --endpoints=localhost:2379 del key
# 前缀模糊删除
./etcdctl --endpoints=localhost:2379 put k1 value1
./etcdctl --endpoints=localhost:2379 put k2 value2
./etcdctl --endpoints=localhost:2379 del k --prefix

# watch监听数据变化
./etcdctl --endpoints=localhost:2379 watch stock1
./etcdctl --endpoints=localhost:2379 put stock1 1000
# 前缀模糊监听
./etcdctl --endpoints=localhost:2379 watch stock --prefix
./etcdctl --endpoints=localhost:2379 put stock1 10
./etcdctl --endpoints=localhost:2379 put stock2 20

# lease租约
./etcdctl --endpoints=localhost:2379 lease grant 30
# lease 694d779a4a223c8e granted with TTL(30s)

# put值指定租约
./etcdctl --endpoints=localhost:2379 put sample value --lease=694d779a4a223c8e
./etcdctl --endpoints=localhost:2379 get sample

# 续约,租约的1/3时间,就会发生一次续约,比如租约是30s,每10s续约一次
./etcdctl --endpoints=localhost:2379 lease keep-alive 694d7799dbeb0580
# 取消租约,执行之后值也清除
./etcdctl --endpoints=localhost:2379 lease revoke 694d779a4a223c8e

# 分布式锁, 获取一个锁,公平锁,释放之后另外一个客户端获得
./etcdctl --endpoints=localhost:2379 lock mutex1

# 选举
./etcdctl --endpoints=localhost:2379 elect one p1

# another client with the same name blocks
./etcdctl --endpoints=localhost:2379 elect one p2

# 查看集群状态
./etcdctl --write-out=table --endpoints=localhost:2379 endpoint status

etcd常用场景:

1.元数据存储
    强一致性的分布式kv存储系统特点,可实现可靠的分部署数据存储;put和get指令实现;
2.服务注册发现
    利用lease租约和watch监听模糊前缀方式可实现;lease、put、实现方式:
    1.客户端watch前缀模糊监听,当客户端上(put操作)/下线(客户端断开后ttl超时触发DELETE操作) watch
    ./etcdctl --endpoints=localhost:2379 watch host_ --prefix
    2.客户端创建租约 lease
    ./etcdctl --endpoints=localhost:2379 lease grant 30
    3.客户端put值指定租约 put
    ./etcdctl --endpoints=localhost:2379 put host_192.168.1.101 value101 --lease=694d779a4a223c8e
    4.客户端续约,lease keep-alive (租约的1/3时间,就会发生一次续约,比如租约是30s,每10s续约一次)
    ./etcdctl --endpoints=localhost:2379 lease keep-alive 694d779a4a223c8e
    5.watch收到变化之后,通过get前缀模糊获取服务list get
    ./etcdctl --endpoints=localhost:2379 get host_ --prefix
3.分布式锁;lock指令实现
    ./etcdctl --endpoints=localhost:2379 lock mutex1
4.选主/故障转移 elect选举指令实现
	基于elect指令可实现选主,故障转移,当一个主节点故障之后,可通过选举得到主节点;

 

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
当你想要在Docker中部署etcd时,可以按照以下步骤进行操作: 1. 首先,确保你已经安装了Docker,并且Docker守护进程正在运行。 2. 下载etcd的Docker镜像。你可以使用以下命令从Docker Hub上获取官方的etcd镜像: ``` docker pull quay.io/coreos/etcd ``` 3. 创建一个etcd容器。使用以下命令创建一个新的etcd容器: ``` docker run -d --name my-etcd -p 2379:2379 -p 2380:2380 \ --volume=/path/to/data:/etcd-data \ quay.io/coreos/etcd:latest \ /usr/local/bin/etcd \ --name my-etcd \ --data-dir /etcd-data \ --advertise-client-urls http://0.0.0.0:2379 \ --listen-client-urls http://0.0.0.0:2379 \ --initial-advertise-peer-urls http://0.0.0.0:2380 \ --listen-peer-urls http://0.0.0.0:2380 \ --initial-cluster my-etcd=http://0.0.0.0:2380 \ --initial-cluster-token my-etcd-token \ --initial-cluster-state new ``` 这个命令会创建一个名为my-etcd的容器,并将容器的2379端口映射到主机的2379端口,以及将容器的2380端口映射到主机的2380端口。你可以根据需要修改这些端口映射。 4. 现在,你的etcd容器已经在Docker中运行起来了。你可以使用etcd客户端工具连接到容器并进行操作。例如,你可以使用以下命令连接到etcd容器: ``` docker exec -it my-etcd /bin/sh ``` 这将在容器内部启动一个shell会话,你可以在其中运行etcd客户端命令。 以上是在Docker中部署etcd的基本步骤。你可以根据自己的需求进行进一步的配置和操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小小绿豆

你的鼓励是我创作最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值