etcd部署和使用
介绍高可用的分布式键值(key-value)数据库,raft共识算法实现分布式一致性,在k8s中做元数据存储。类似zookeeper。官网https://etcd.io/githubhttps://github.com/etcd-io/etcd部署:1.github的release上下载最新安装包etcd-v3.4.14-linux-amd64.tar.gz2.解压tar -zxvf etcd-v
·
介绍
高可用的分布式键值(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指令可实现选主,故障转移,当一个主节点故障之后,可通过选举得到主节点;
更多推荐
已为社区贡献5条内容
所有评论(0)