etcd笔记
很早就写的一篇笔记,放在这里充数。
运行一个最简单的etcd
docker run -p 4001:4001 --rm -ti microbox/etcd --name etcd0
API
- 获取版本
curl -L http://127.0.0.1:2379/version
- 获取键值
curl http://127.0.0.1:2379/v2/keys/message
- 设置键值
curl http://127.0.0.1:2379/v2/keys/message -XPUT -d value="Hello world"
curl -X PUT http://127.0.0.1:2379/v2/keys/message?value="Hello world"
curl -X PUT http://127.0.0.1:2379/v2/keys/message?value="Hello etcd"
- 删除键
curl -X DELETE http://127.0.0.1:2379/v2/keys/message
- 使用TTL
curl http://127.0.0.1:2379/v2/keys/foo -XPUT -d value=bar -d ttl=5
curl http://127.0.0.1:2379/v2/keys/foo?value=bar&ttl=5 -XPUT
- 取消TTL
curl http://127.0.0.1:2379/v2/keys/foo?value=bar&ttl= -XPUT
- 创建目录
curl -X PUT http://127.0.0.1:2379/v2/keys/dir1?dir=true
curl -X PUT http://127.0.0.1:2379/v2/keys/dir1 -d dir=true
- 删除目录
curl 'http://127.0.0.1:2379/v2/keys/foo_dir?dir=true' -XDELETE
- 判断条件
curl http://127.0.0.1:2379/v2/keys/foo -XPUT -d value=one
curl http://127.0.0.1:2379/v2/keys/foo?prevExist=false -XPUT -d value=three
- get error:
{
"cause": "/foo",
"errorCode": 105,
"index": 39776,
"message": "Key already exists"
}
prevValue - 判断之前的值
prevIndex - 判断上次的index
prevExist - 判断键是否存在
- 递归查询
curl http://127.0.0.1:2379/v2/keys/?recursive=true
- 创建一个隐藏的键
curl http://127.0.0.1:2379/v2/keys/_message -XPUT -d value="Hello hidden world"
- 从文件中创建
echo "Hello\nWorld" > afile.txt
curl http://127.0.0.1:2379/v2/keys/afile -XPUT --data-urlencode value@afile.txt
统计信息:
- 查看leader状态
curl http://127.0.0.1:2379/v2/stats/leader
- 查看自身状态
curl http://127.0.0.1:32873/v2/stats/self
- 查看存储统计
curl http://127.0.0.1:2379/v2/stats/store
- 列出members
curl http://10.0.0.10:2379/v2/members
- 检查集群状态
ETCDCTL_API=3 etcdctl --endpoints=http://node1:2379,http://node2:2379,http://node3:2379 endpoint health
etcd集群的 docker-compose.yml:
version: "2"
networks:
etcd-network:
services:
node1:
image: quay.io/coreos/etcd
networks:
- etcd-network
ports:
- 2379
command: etcd -name=node1 -advertise-client-urls=http://node1:2379 -initial-advertise-peer-urls=http://node1:2380 -listen-client-urls=http://0.0.0.0:2379 -listen-peer-urls=http://node1:2380 -initial-cluster=node1=http://node1:2380,node2=http://node2:2380,node3=http://node3:2380
node2:
image: quay.io/coreos/etcd
networks:
- etcd-network
ports:
- 2379
command: etcd -name=node2 -advertise-client-urls=http://node2:2379 -initial-advertise-peer-urls=http://node2:2380 -listen-client-urls=http://0.0.0.0:2379 -listen-peer-urls=http://node2:2380 -initial-cluster=node1=http://node1:2380,node2=http://node2:2380,node3=http://node3:2380
node3:
image: quay.io/coreos/etcd
networks:
- etcd-network
ports:
- 2379
command: etcd -name=node3 -advertise-client-urls=http://node3:2379 -initial-advertise-peer-urls=http://node3:2380 -listen-client-urls=http://0.0.0.0:2379 -listen-peer-urls=http://node3:2380 -initial-cluster=node1=http://node1:2380,node2=http://node2:2380,node3=http://node3:2380