本文共 2964 字,大约阅读时间需要 9 分钟。
作为CoreOS生态系统的核心组件,Etcd提供了一套分布式键-值存储解决方案,可通过多个节点构建集群并实现全局数据访问。本文将深入探讨Etcd的集群发现模式、操作命令以及相关配置方法。
Etcd通过云发现服务(Cloud discovery service)实现多节点设备的自动发现。CoreOS在引导节点时会检查cloud-config文件中的发现地址,获取集群成员的信息。例如,以下是示例发现地址:
https://discovery.etcd.io/dcadc5d4d42328488ecdcd7afae5f57c
此地址返回JSON数据,描述已知的设备信息。集群引导完成后,设备会通过此地址获取其他节点的信息,并将自身信息提交至集群。以下是初始和集群完成后的示例数据:
{"action":"get","node":{"key":"/_etcd/registry/dcadc5d4d42328488ecdcd7afae5f57c","dir":true,"modifiedIndex":102511104,"createdIndex":102511104}} {"action":"get","node":{"key":"/_etcd/registry/1edee33e6b03e75d9428eacf0ff94fda","dir":true,"nodes":[{"key":"/_etcd/registry/1edee33e6b03e75d9428eacf0ff94fda/2ddbdb7c872b4bc59dd1969ac166501e","value":"http://10.132.252.38:7001","expiration":"2014-09-19T13:41:26.912303668Z","ttl":598881,"modifiedIndex":102453704,"createdIndex":102453704},{"key":"/_etcd/registry/1edee33e6b03e75d9428eacf0ff94fda/921a7241c31a499a97d43f785108b17c","value":"http://10.132.248.118:7001","expiration":"2014-09-19T13:41:29.602508981Z","ttl":598884,"modifiedIndex":102453736,"createdIndex":102453736},{"key":"/_etcd/registry/1edee33e6b03e75d9428eacf0ff94fda/27987f5eaac243f88ca6823b47012c5b","value":"http://10.132.248.121:7001","expiration":"2014-09-19T13:41:41.817958205Z","ttl":598896,"modifiedIndex":102453860,"createdIndex":102453860}],"modifiedIndex":101632353,"createdIndex":101632353}} Etcdctl是Etcd的命令行工具,支持创建、读取、更新和删除操作。以下是常用命令示例:
etcdctl ls /coreos.com
etcdctl get /coreos.com/coreos.com: is a directory
etcdctl ls / --recursive/coreos.com/coreos.com/updateengine/coreos.com/updateengine/rebootlock/coreos.com/updateengine/rebootlock/semaphore
etcdctl get /coreos.com/updateengine/rebootlock/semaphore{"semaphore":1,"max":1,"holders":null} etcdctl mkdir /example
etcdctl mk /example/key datadata
etcdctl get /example/keydata
etcdctl update /example/key turtlesturtles
etcdctl mkdir /here/you/go --ttl 120
etcdctl updatedir /here/you/go --ttl 500
etcdctl set /example/key newnew
etcdctl set /a/b/c herehere
etcdctl setdir /x/y/z
etcdctl rm /a/b/c
etcdctl rm /a --recursive
etcdctl rmdir /x/y/z
etcdctl watch /example/hello
etcdctl watch --recursive /example
whiletrue; do etcdctl watch --recursive /example; done
etcdctl exec-watch --recursive /example -- echo"hello"
通过HTTP/JSON API可以对Etcd进行远程控制。以下是一些常用操作示例:
curl -L http://127.0.0.1:4001/v2/keys/
curl -L http://127.0.0.1:4001/v2/keys/?recursive=true
curl -L http://127.0.0.1:4001/v2/stats/leader
curl -L http://127.0.0.1:4001/v2/stats/store
Etcd的配置可以通过多种方式实现,包括:
coreos: etcd: discovery: https://discovery.etcd.io/dcadc5d4d42328488ecdcd7afae5f57c addr: $private_ipv4:4001 peer-addr: $private_ipv4:7001
curl -L http://127.0.0.1:7001/v2/admin/config
curl -L http://127.0.0.1:7001/v2/admin/machines
通过以上方法,我们可以利用Etcd实现集群节点的自动发现、键值存储的管理以及集群状态的监控。Etcd的设计理念使其成为分布式系统的理想选择,其灵活性和可扩展性为服务部署提供了强有力的支持。
转载地址:http://wbdt.baihongyu.com/