ETCD的使用

使用ETCD的目的是想用它来监控服务进程的状态。

显示服务列表:

etcdctl --write-out=table --endpoints=localhost:2379 member list

写入一个键值对:

etcdctl put ${key} ${value}

根据键获取值:

etcdctl get ${key}

只打印值:

etcdctl get ${key} --print-value-only

从指定的服务器获取值:

etcdctl --endpoints=localhost:2379 get ${key}

打印该键和它后面所有的键值:

etcdctl get --from-key ${key}

 删除一个键值对:

etcdctl del ${key}

用以下CURL的方式去设置键值对,需要把键值先转成base64:

curl -L http://localhost:2379/v3beta/kv/put   -X POST -d '{"key": "c2VyMjE4", "value": "MTM5LjIxNy4xMTIuMjE4OjUwMDU1"}'

查看存储的内容:

etcdctl get --prefix ser
ser150
192.168.88.150:50055
ser218
139.217.112.218:50055

转base64的网址:

https://www.base64encode.org/

 

网上获取的ETCD命令:

ETCD 命令 存储: curl http://127.0.0.1:4001/v2/keys/testkey -XPUT -d value='testvalue' curl -s http://127.0.0.1:4001/v2/keys/message2 -XPUT -d value='hello etcd' -d ttl=5 获取: curl http://127.0.0.1:4001/v2/keys/testkey 查看版本: curl http://127.0.0.1:4001/version 删除: curl -s http://127.0.0.1:4001/v2/keys/testkey -XDELETE 监视: 窗口1:curl -s http://127.0.0.1:4001/v2/keys/message2 -XPUT -d value='hello etcd 1' curl -s http://127.0.0.1:4001/v2/keys/message2?wait=true 窗口2: curl -s http://127.0.0.1:4001/v2/keys/message2 -XPUT -d value='hello etcd 2' 自动创建key: curl -s http://127.0.0.1:4001/v2/keys/message3 -XPOST -d value='hello etcd 1' curl -s 'http://127.0.0.1:4001/v2/keys/message3?recursive=true&sorted=true' 创建目录: curl -s http://127.0.0.1:4001/v2/keys/message8 -XPUT -d dir=true 删除目录: curl -s 'http://127.0.0.1:4001/v2/keys/message7?dir=true' -XDELETE curl -s 'http://127.0.0.1:4001/v2/keys/message7?recursive=true' -XDELETE 查看所有key: curl -s http://127.0.0.1:4001/v2/keys/?recursive=true 存储数据: curl -s http://127.0.0.1:4001/v2/keys/file -XPUT --data-urlencode value@upfile 使用etcdctl客户端: 存储: etcdctl set /liuyiling/testkey "610" --ttl '100' --swap-with-value value 获取: etcdctl get /liuyiling/testkey 更新: etcdctl update /liuyiling/testkey "world" --ttl '100' 删除: etcdctl rm /liuyiling/testkey 使用ca获取: etcdctl --cert-file=/etc/etcd/ssl/etcd.pem --key-file=/etc/etcd/ssl/etcd-key.pem --ca-file=/etc/etcd/ssl/ca.pem get /message 目录管理: etcdctl mk /liuyiling/testkey "hello" 类似set,但是如果key已经存在,报错 etcdctl mkdir /liuyiling etcdctl setdir /liuyiling etcdctl updatedir /liuyiling etcdctl rmdir /liuyiling 查看: etcdctl ls --recursive 监视: etcdctl watch mykey --forever + etcdctl update mykey "hehe" #监视目录下所有节点的改变 etcdctl exec-watch --recursive /foo -- sh -c "echo hi" etcdctl exec-watch mykey -- sh -c 'ls -al' + etcdctl update mykey "hehe" etcdctl member list 集群启动步骤 1.启动一个etcd,任意机器,如192.168.1.1:2379 2.curl -X PUT http://192.168.1.1:2379/v2/keys/discovery/6c007a14875d53d9bf0ef5a6fc0257c817f0f222/_config/size -d value=3 3.etcd -name machine1 -initial-advertise-peer-urls http://127.0.0.1:2380 -listen-peer-urls http://127.0.0.1:2380 -discovery http://192.168.1.1:2379/v2/keys/discovery/6c007a14875d53d9bf0ef5a6fc0257c817f0f222 4.如果是在三台不同的服务器上,则重复上面的命令3次,否则重复上面的命令1次+下面的命令2次 etcd -name machine2 -discovery http://192.168.1.1:2379/v2/keys/discovery/6c007a14875d53d9bf0ef5a6fc0257c817f0f222 -addr 127.0.0.1:2389 -bind-addr 127.0.0.1:2389 -peer-addr 127.0.0.1:2390 -peer-bind-addr 127.0.0.1:2390 etcd -name machine3 -discovery http://192.168.1.1:2379/v2/keys/discovery/6c007a14875d53d9bf0ef5a6fc0257c817f0f222 -addr 127.0.0.1:2409 -bind-addr 127.0.0.1:2409 -peer-addr 127.0.0.1:2490 -peer-bind-addr 127.0.0.1:2490 5.curl -L http://localhost:2379/v2/members | python -m json.tool

 

posted on 2019-08-14 16:25  lialin  阅读(1086)  评论(0编辑  收藏  举报

导航