etcd 实战

安装etcd

安装包下载路径

https://download.csdn.net/download/wankwan/10902359

安装单节点etcd

vim conf.yml

name: etcd-1
data-dir: /opt/etcd-v3.3.10-linux-amd64/data
listen-client-urls: http://192.168.13.130:2379,http://127.0.0.1:2379
advertise-client-urls: http://192.168.13.130:2379,http://127.0.0.1:2379
listen-peer-urls: http://192.168.13.130:2380
initial-advertise-peer-urls: http://192.168.13.130:2380
initial-cluster: etcd-1=http://192.168.13.130:2380
initial-cluster-token: etcd-cluster-token
initial-cluster-state: new

启动etcd

root@ubuntu-130:/opt/etcd-v3.3.10-linux-amd64# ./etcd --config-file=conf.yml
2019-10-20 14:03:24.239088 I | etcdmain: Loading server configuration from "conf.yml"
2019-10-20 14:03:24.241164 I | etcdmain: etcd Version: 3.3.10
2019-10-20 14:03:24.241988 I | etcdmain: Git SHA: 27fc7e2
2019-10-20 14:03:24.242546 I | etcdmain: Go Version: go1.10.4
2019-10-20 14:03:24.243099 I | etcdmain: Go OS/Arch: linux/amd64
2019-10-20 14:03:24.243668 I | etcdmain: setting maximum number of CPUs to 1, total number of available CPUs is                                                        1
2019-10-20 14:03:24.244295 N | etcdmain: the server is already initialized as member before, starting as etcd me                                                       mber...
2019-10-20 14:03:24.244873 I | embed: listening for peers on http://192.168.13.130:2380
2019-10-20 14:03:24.245519 I | embed: listening for client requests on 127.0.0.1:2379
2019-10-20 14:03:24.246034 I | embed: listening for client requests on 192.168.13.130:2379
2019-10-20 14:03:24.247124 I | etcdserver: name = etcd-1
2019-10-20 14:03:24.247591 I | etcdserver: data dir = /opt/etcd-v3.3.10-linux-amd64/data
2019-10-20 14:03:24.248013 I | etcdserver: member dir = /opt/etcd-v3.3.10-linux-amd64/data/member
2019-10-20 14:03:24.248388 I | etcdserver: heartbeat = 100ms
2019-10-20 14:03:24.248762 I | etcdserver: election = 1000ms
2019-10-20 14:03:24.249162 I | etcdserver: snapshot count = 100000
2019-10-20 14:03:24.249377 I | etcdserver: advertise client URLs = http://127.0.0.1:2379,http://192.168.13.130:2                                                       379
2019-10-20 14:03:24.249704 I | etcdserver: restarting member e320d37d47ffd2b9 in cluster 5f9f57aff3a7b560 at com                                                       mit index 6
2019-10-20 14:03:24.250029 I | raft: e320d37d47ffd2b9 became follower at term 3
2019-10-20 14:03:24.250263 I | raft: newRaft e320d37d47ffd2b9 [peers: [], term: 3, commit: 6, applied: 0, lastin                                                       dex: 6, lastterm: 3]
2019-10-20 14:03:24.255862 W | auth: simple token is not cryptographically signed
2019-10-20 14:03:24.260080 I | etcdserver: starting server... [version: 3.3.10, cluster version: to_be_decided]
2019-10-20 14:03:24.264671 I | etcdserver/membership: added member e320d37d47ffd2b9 [http://192.168.13.130:2380]                                                        to cluster 5f9f57aff3a7b560
2019-10-20 14:03:24.267207 N | etcdserver/membership: set the initial cluster version to 3.3
2019-10-20 14:03:24.267878 I | etcdserver/api: enabled capabilities for version 3.3
2019-10-20 14:03:25.551322 I | raft: e320d37d47ffd2b9 is starting a new election at term 3
2019-10-20 14:03:25.551387 I | raft: e320d37d47ffd2b9 became candidate at term 4
2019-10-20 14:03:25.551469 I | raft: e320d37d47ffd2b9 received MsgVoteResp from e320d37d47ffd2b9 at term 4
2019-10-20 14:03:25.551507 I | raft: e320d37d47ffd2b9 became leader at term 4
2019-10-20 14:03:25.551524 I | raft: raft.node: e320d37d47ffd2b9 elected leader e320d37d47ffd2b9 at term 4
2019-10-20 14:03:25.554579 I | etcdserver: published {Name:etcd-1 ClientURLs:[http://127.0.0.1:2379 http://192.1                                                       68.13.130:2379]} to cluster 5f9f57aff3a7b560
2019-10-20 14:03:25.556753 E | etcdmain: forgot to set Type=notify in systemd service file?
2019-10-20 14:03:25.557305 I | embed: ready to serve client requests
2019-10-20 14:03:25.561823 N | embed: serving insecure client requests on 127.0.0.1:2379, this is strongly disco                                                       uraged!
2019-10-20 14:03:25.564202 I | embed: ready to serve client requests
2019-10-20 14:03:25.568558 N | embed: serving insecure client requests on 192.168.13.130:2379, this is strongly                                                        discouraged!
 

查看节点信息

查看节点成员

root@ubuntu-130:/opt/etcd-v3.3.10-linux-amd64# etcdctl member list
e320d37d47ffd2b9, started, etcd-1, http://192.168.13.130:2380, http://127.0.0.1:2379,http://192.168.13.130:2379

查看集群状态(leader节点)

root@ubuntu-130:/opt/etcd-v3.3.10-linux-amd64# etcdctl --write-out=table endpoint status
+----------------+------------------+---------+---------+-----------+-----------+------------+
|    ENDPOINT    |        ID        | VERSION | DB SIZE | IS LEADER | RAFT TERM | RAFT INDEX |
+----------------+------------------+---------+---------+-----------+-----------+------------+
| 127.0.0.1:2379 | e320d37d47ffd2b9 |  3.3.10 |   20 kB |      true |         4 |         13 |
+----------------+------------------+---------+---------+-----------+-----------+------------+

集群信息

root@ubuntu-130:/opt/etcd-v3.3.10-linux-amd64# etcdctl member list
e320d37d47ffd2b9, started, etcd-1, http://192.168.13.130:2380, http://127.0.0.1:2379,http://192.168.13.130:2379
root@ubuntu-130:/opt/etcd-v3.3.10-linux-amd64# curl http://127.0.0.1:2379/v2/stats/leader

查看自己状态

root@ubuntu-130:/opt/etcd-v3.3.10-linux-amd64# curl http://127.0.0.1:2379/v2/stats/self
{"name":"etcd-1","id":"e320d37d47ffd2b9","state":"StateLeader","startTime":"2019-10-20T14:03:24.250394612+08:00","leaderInfo":{"leader":"e320d37d47ffd2b9","uptime":"7m45.794728901s","startTime":"2019-10-20T14:03:25.55155864+08:00"},"recvAppendRequestCnt":0,"sendAppendRequestCnt":0}root@ubuntu-130:/opt/etcd-v3.3.10-linux-amd64#

键值对写入读取

root@ubuntu-130:/opt/etcd-v3.3.10-linux-amd64# etcdctl put  /aaa 111
OK
root@ubuntu-130:/opt/etcd-v3.3.10-linux-amd64# etcdctl get   /aaa
/aaa
111
root@ubuntu-130:/opt/etcd-v3.3.10-linux-amd64# etcdctl put    bbb 222
OK
root@ubuntu-130:/opt/etcd-v3.3.10-linux-amd64# etcdctl get   bbb
bbb
222
root@ubuntu-130:/opt/etcd-v3.3.10-linux-amd64# etcdctl del  bbb
1
root@ubuntu-130:/opt/etcd-v3.3.10-linux-amd64# etcdctl get   bbb
root@ubuntu-130:/opt/etcd-v3.3.10-linux-amd64#

查看全部key

过期时间(租约)

#生成租约定时器

root@ubuntu-130:/opt/etcd-v3.3.10-linux-amd64# etcdctl lease grant 50
lease 52b96de7d61a270f granted with TTL(50s)

#关联租约到key

root@ubuntu-130:/opt/etcd-v3.3.10-linux-amd64# etcdctl put test_lease 50 --lease=52b96de7d61a270f
OK

#查看key
root@ubuntu-130:/opt/etcd-v3.3.10-linux-amd64# etcdctl get      test_lease
test_lease
50

扫描二维码关注公众号,回复: 9404702 查看本文章

过期消失
root@ubuntu-130:/opt/etcd-v3.3.10-linux-amd64# etcdctl get      test_lease
root@ubuntu-130:/opt/etcd-v3.3.10-linux-amd64#
 

观察者

在第一个终端:

root@ubuntu-130:/opt/etcd-v3.3.10-linux-amd64# curl http://127.0.0.1:2379/v2/keys/foo?wait=true

然后在第二个终端去改变它的值

root@ubuntu-130:~# curl  http://127.0.0.1:2379/v2/keys/foo -XPUT -d value=bar
{"action":"set","node":{"key":"/foo","value":"bar","modifiedIndex":7,"createdIndex":7}}

然后等待第一个终端的变化

root@ubuntu-130:/opt/etcd-v3.3.10-linux-amd64# curl http://127.0.0.1:2379/v2/keys/foo?wait=true
{"action":"set","node":{"key":"/foo","value":"bar","modifiedIndex":7,"createdIndex":7}}

分布式锁

在第一个终端

root@ubuntu-130:/opt/etcd-v3.3.10-linux-amd64# etcdctl lock my_mutex
my_mutex/52b96de7d61a271f

在第二个终端:

会等待进入,直到第一个终端的锁被释放

root@ubuntu-130:~# etcdctl lock my_mutex

javak客户端操作请参考github

https://github.com/heheliu321/etcdDemo

发布了135 篇原创文章 · 获赞 16 · 访问量 9万+

猜你喜欢

转载自blog.csdn.net/nmjhehe/article/details/102648516
今日推荐