Etcd 项目

什么是 etcd

etcdCoreOS 团队于 2013 年 6 月发起的开源项目,它的目标是构建一个高可用的分布式键值(key-value)数据库,基于 Go 语言实现。我们知道,在分布式系统中,各种服务的配置信息的管理分享,服务的发现是一个很基本同时也是很重要的问题。CoreOS 项目就希望基于 etcd 来解决这一问题。

etcd 目前在 github.com/coreos/etcd 进行维护。

受到 Apache ZooKeeper 项目和 doozer 项目的启发,etcd 在设计的时候重点考虑了下面四个要素:

  • 简单:具有定义良好、面向用户的 API (gRPC)

  • 安全:支持 HTTPS 方式的访问

  • 快速:支持并发 10 k/s 的写操作

  • 可靠:支持分布式结构,基于 Raft 的一致性算法

Apache ZooKeeper 是一套知名的分布式系统中进行同步和一致性管理的工具。

doozer 是一个一致性分布式数据库。

Raft 是一套通过选举主节点来实现分布式系统一致性的算法,相比于大名鼎鼎的 Paxos 算法,它的过程更容易被人理解,由 Stanford 大学的 Diego Ongaro 和 John Ousterhout 提出。更多细节可以参考 raftconsensus.github.io

一般情况下,用户使用 etcd 可以在多个节点上启动多个实例,并添加它们为一个集群。同一个集群中的 etcd 实例将会保持彼此信息的一致性。

安装

etcd 基于 Go 语言实现,因此,用户可以从 项目主页 下载源代码自行编译,也可以下载编译好的二进制文件,甚至直接使用制作好的 Docker 镜像文件来体验。

注意:本章节内容基于 etcd 3.x 版本

二进制文件方式下载

编译好的二进制文件都在 github.com/coreos/etcd/releases 页面,用户可以选择需要的版本,或通过下载工具下载。

例如,使用 curl 工具下载压缩包,并解压。

  1. $ curl -L https://github.com/coreos/etcd/releases/download/v3.2.10/etcd-v3.2.10-linux-amd64.tar.gz -o etcd-v3.2.10-linux-amd64.tar.gz
  2. $ tar xzvf etcd-v3.2.10-linux-amd64.tar.gz
  3. $ cd etcd-v3.2.10-linux-amd64

解压后,可以看到文件包括

  1. $ ls
  2. Documentation README-etcdctl.md README.md READMEv2-etcdctl.md etcd etcdctl

其中 etcd 是服务主文件,etcdctl 是提供给用户的命令客户端,其他文件是支持文档。

下面将 etcd etcdctl 文件放到系统可执行目录(例如 /usr/local/bin/)。

  1. $ sudo cp etcd* /usr/local/bin/

默认 2379 端口处理客户端的请求,2380 端口用于集群各成员间的通信。启动 etcd 显示类似如下的信息:

  1. $ etcd
  2. 2017-12-03 11:18:34.406082 I | etcdmain: etcd Version: 3.2.10
  3. 2017-12-03 11:18:34.406226 I | etcdmain: Git SHA: GitNotFound
  4. 2017-12-03 11:18:34.406235 I | etcdmain: Go Version: go1.9.2
  5. 2017-12-03 11:18:34.406242 I | etcdmain: Go OS/Arch: darwin/amd64
  6. 2017-12-03 11:18:34.406250 I | etcdmain: setting maximum number of CPUs to 4, total number of available CPUs is 4
  7. 2017-12-03 11:18:34.406265 N | etcdmain: failed to detect default host (default host not supported on darwin_amd64)
  8. 2017-12-03 11:18:34.406279 W | etcdmain: no data-dir provided, using default data-dir ./default.etcd
  9. 2017-12-03 11:18:34.406457 N | etcdmain: the server is already initialized as member before, starting as etcd member...
  10. 2017-12-03 11:18:34.411579 I | embed: listening for peers on http://localhost:2380
  11. 2017-12-03 11:18:34.411938 I | embed: listening for client requests on localhost:2379

此时,可以使用 etcdctl 命令进行测试,设置和获取键值 testkey: "hello world",检查 etcd 服务是否启动成功:

  1. $ ETCDCTL_API=3 etcdctl member list
  2. 8e9e05c52164694d, started, default, http://localhost:2380, http://localhost:2379
  3. $ ETCDCTL_API=3 etcdctl put testkey "hello world"
  4. OK
  5. $ etcdctl get testkey
  6. testkey
  7. hello world

说明 etcd 服务已经成功启动了。

Docker 镜像方式运行

镜像名称为 quay.io/coreos/etcd,可以通过下面的命令启动 etcd 服务监听到 23792380 端口。

  1. $ export NODE1=192.168.1.21
  2. $ docker run --name etcd \
  3. -p 2379:2379 \
  4. -p 2380:2380 \
  5. --volume=etcd-data:/etcd-data \
  6. quay.io/coreos/etcd:latest \
  7. /usr/local/bin/etcd \
  8. --data-dir=/etcd-data --name node1 \
  9. --initial-advertise-peer-urls http://${NODE1}:2380 --listen-peer-urls http://0.0.0.0:2380 \
  10. --advertise-client-urls http://${NODE1}:2379 --listen-client-urls http://0.0.0.0:2379 \
  11. --initial-cluster node1=http://${NODE1}:2380

注意:etcd 官方标注 quay.io/coreos/etcd 即将废弃,启用新的 gcr.io/etcd-development/etcd 镜像,但后者由于网络原因,国内不能下载到该镜像,这里仍然使用前者作为演示。

打开新的终端按照上一步的方法测试 etcd 是否成功启动。

macOS 中运行

  1. $ brew install etcd
  2. $ etcd
  3. $ etcdctl member list

etcd 集群

下面我们使用 Docker Compose 模拟启动一个 3 节点的 etcd 集群。

编辑 docker-compose.yml 文件

  1. version: "3.6"
  2. services:
  3. node1:
  4. image: quay.io/coreos/etcd
  5. volumes:
  6. - node1-data:/etcd-data
  7. expose:
  8. - 2379
  9. - 2380
  10. networks:
  11. cluster_net:
  12. ipv4_address: 172.16.238.100
  13. environment:
  14. - ETCDCTL_API=3
  15. command:
  16. - /usr/local/bin/etcd
  17. - --data-dir=/etcd-data
  18. - --name
  19. - node1
  20. - --initial-advertise-peer-urls
  21. - http://172.16.238.100:2380
  22. - --listen-peer-urls
  23. - http://0.0.0.0:2380
  24. - --advertise-client-urls
  25. - http://172.16.238.100:2379
  26. - --listen-client-urls
  27. - http://0.0.0.0:2379
  28. - --initial-cluster
  29. - node1=http://172.16.238.100:2380,node2=http://172.16.238.101:2380,node3=http://172.16.238.102:2380
  30. - --initial-cluster-state
  31. - new
  32. - --initial-cluster-token
  33. - docker-etcd
  34. node2:
  35. image: quay.io/coreos/etcd
  36. volumes:
  37. - node2-data:/etcd-data
  38. networks:
  39. cluster_net:
  40. ipv4_address: 172.16.238.101
  41. environment:
  42. - ETCDCTL_API=3
  43. expose:
  44. - 2379
  45. - 2380
  46. command:
  47. - /usr/local/bin/etcd
  48. - --data-dir=/etcd-data
  49. - --name
  50. - node2
  51. - --initial-advertise-peer-urls
  52. - http://172.16.238.101:2380
  53. - --listen-peer-urls
  54. - http://0.0.0.0:2380
  55. - --advertise-client-urls
  56. - http://172.16.238.101:2379
  57. - --listen-client-urls
  58. - http://0.0.0.0:2379
  59. - --initial-cluster
  60. - node1=http://172.16.238.100:2380,node2=http://172.16.238.101:2380,node3=http://172.16.238.102:2380
  61. - --initial-cluster-state
  62. - new
  63. - --initial-cluster-token
  64. - docker-etcd
  65. node3:
  66. image: quay.io/coreos/etcd
  67. volumes:
  68. - node3-data:/etcd-data
  69. networks:
  70. cluster_net:
  71. ipv4_address: 172.16.238.102
  72. environment:
  73. - ETCDCTL_API=3
  74. expose:
  75. - 2379
  76. - 2380
  77. command:
  78. - /usr/local/bin/etcd
  79. - --data-dir=/etcd-data
  80. - --name
  81. - node3
  82. - --initial-advertise-peer-urls
  83. - http://172.16.238.102:2380
  84. - --listen-peer-urls
  85. - http://0.0.0.0:2380
  86. - --advertise-client-urls
  87. - http://172.16.238.102:2379
  88. - --listen-client-urls
  89. - http://0.0.0.0:2379
  90. - --initial-cluster
  91. - node1=http://172.16.238.100:2380,node2=http://172.16.238.101:2380,node3=http://172.16.238.102:2380
  92. - --initial-cluster-state
  93. - new
  94. - --initial-cluster-token
  95. - docker-etcd
  96. volumes:
  97. node1-data:
  98. node2-data:
  99. node3-data:
  100. networks:
  101. cluster_net:
  102. driver: bridge
  103. ipam:
  104. driver: default
  105. config:
  106. -
  107. subnet: 172.16.238.0/24

使用 docker-compose up 启动集群之后使用 docker exec 命令登录到任一节点测试 etcd 集群。

  1. / # etcdctl member list
  2. daf3fd52e3583ff, started, node3, http://172.16.238.102:2380, http://172.16.238.102:2379
  3. 422a74f03b622fef, started, node1, http://172.16.238.100:2380, http://172.16.238.100:2379
  4. ed635d2a2dbef43d, started, node2, http://172.16.238.101:2380, http://172.16.238.101:2379

使用 etcdctl

etcdctl 是一个命令行客户端,它能提供一些简洁的命令,供用户直接跟 etcd 服务打交道,而无需基于 HTTP API 方式。这在某些情况下将很方便,例如用户对服务进行测试或者手动修改数据库内容。我们也推荐在刚接触 etcd 时通过 etcdctl 命令来熟悉相关的操作,这些操作跟 HTTP API 实际上是对应的。

etcd 项目二进制发行包中已经包含了 etcdctl 工具,没有的话,可以从 github.com/coreos/etcd/releases 下载。

etcdctl 支持如下的命令,大体上分为数据库操作和非数据库操作两类,后面将分别进行解释。

  1. NAME:
  2. etcdctl - A simple command line client for etcd3.
  3. USAGE:
  4. etcdctl
  5. VERSION:
  6. 3.2.10
  7. API VERSION:
  8. 3.2
  9. COMMANDS:
  10. get Gets the key or a range of keys
  11. put Puts the given key into the store
  12. del Removes the specified key or range of keys [key, range_end)
  13. txn Txn processes all the requests in one transaction
  14. compaction Compacts the event history in etcd
  15. alarm disarm Disarms all alarms
  16. alarm list Lists all alarms
  17. defrag Defragments the storage of the etcd members with given endpoints
  18. endpoint health Checks the healthiness of endpoints specified in `--endpoints` flag
  19. endpoint status Prints out the status of endpoints specified in `--endpoints` flag
  20. watch Watches events stream on keys or prefixes
  21. version Prints the version of etcdctl
  22. lease grant Creates leases
  23. lease revoke Revokes leases
  24. lease timetolive Get lease information
  25. lease keep-alive Keeps leases alive (renew)
  26. member add Adds a member into the cluster
  27. member remove Removes a member from the cluster
  28. member update Updates a member in the cluster
  29. member list Lists all members in the cluster
  30. snapshot save Stores an etcd node backend snapshot to a given file
  31. snapshot restore Restores an etcd member snapshot to an etcd directory
  32. snapshot status Gets backend snapshot status of a given file
  33. make-mirror Makes a mirror at the destination etcd cluster
  34. migrate Migrates keys in a v2 store to a mvcc store
  35. lock Acquires a named lock
  36. elect Observes and participates in leader election
  37. auth enable Enables authentication
  38. auth disable Disables authentication
  39. user add Adds a new user
  40. user delete Deletes a user
  41. user get Gets detailed information of a user
  42. user list Lists all users
  43. user passwd Changes password of user
  44. user grant-role Grants a role to a user
  45. user revoke-role Revokes a role from a user
  46. role add Adds a new role
  47. role delete Deletes a role
  48. role get Gets detailed information of a role
  49. role list Lists all roles
  50. role grant-permission Grants a key to a role
  51. role revoke-permission Revokes a key from a role
  52. check perf Check the performance of the etcd cluster
  53. help Help about any command
  54. OPTIONS:
  55. --cacert="" verify certificates of TLS-enabled secure servers using this CA bundle
  56. --cert="" identify secure client using this TLS certificate file
  57. --command-timeout=5s timeout for short running command (excluding dial timeout)
  58. --debug[=false] enable client-side debug logging
  59. --dial-timeout=2s dial timeout for client connections
  60. --endpoints=[127.0.0.1:2379] gRPC endpoints
  61. --hex[=false] print byte strings as hex encoded strings
  62. --insecure-skip-tls-verify[=false] skip server certificate verification
  63. --insecure-transport[=true] disable transport security for client connections
  64. --key="" identify secure client using this TLS key file
  65. --user="" username[:password] for authentication (prompt if password is not supplied)
  66. -w, --write-out="simple" set the output format (fields, json, protobuf, simple, table)

数据库操作

数据库操作围绕对键值和目录的 CRUD (符合 REST 风格的一套操作:Create)完整生命周期的管理。

etcd 在键的组织上采用了层次化的空间结构(类似于文件系统中目录的概念),用户指定的键可以为单独的名字,如 testkey,此时实际上放在根目录 / 下面,也可以为指定目录结构,如 cluster1/node2/testkey,则将创建相应的目录结构。

注:CRUD 即 Create, Read, Update, Delete,是符合 REST 风格的一套 API 操作。

put

  1. $ etcdctl put /testdir/testkey "Hello world"
  2. OK

get

获取指定键的值。例如

  1. $ etcdctl put testkey hello
  2. OK
  3. $ etcdctl get testkey
  4. testkey
  5. hello

支持的选项为

--sort 对结果进行排序

--consistent 将请求发给主节点,保证获取内容的一致性

del

删除某个键值。例如

  1. $ etcdctl del testkey
  2. 1

非数据库操作

watch

监测一个键值的变化,一旦键值发生更新,就会输出最新的值。

例如,用户更新 testkey 键值为 Hello world

  1. $ etcdctl watch testkey
  2. PUT
  3. testkey
  4. 2

member

通过 listaddupdateremove 命令列出、添加、更新、删除 etcd 实例到 etcd 集群中。

例如本地启动一个 etcd 服务实例后,可以用如下命令进行查看。

  1. $ etcdctl member list
  2. 422a74f03b622fef, started, node1, http://172.16.238.100:2380, http://172.16.238.100:23

写于 2019年05月08日Docker 1652

如非特别注明,文章皆为原创。

转载请注明出处: https://www.liayal.com/article/5cd24210c0ab13505eeefabf

记小栈小程序上线啦~搜索【记小栈】【点击扫码】体验

你不想说点啥么?
😀😃😄😁😆😅😂🤣☺️😊😇🙂🙃😉😌😍😘😗😙😚😋😜😝😛🤑🤗🤓😎🤡🤠😏😒😞😔😟😕🙁☹️😣😖😫😩😤😠😡😶😐😑😯😦😧😮😲😵😳😱😨😰😢😥🤤😭😓😪😴🙄🤔🤥😬🤐🤢🤧😷🤒🤕😈👿👹👺💩👻💀☠️👽👾🤖🎃😺😸😹😻😼😽🙀😿😾👐👐🏻👐🏼👐🏽👐🏾👐🏿🙌🙌🏻🙌🏼🙌🏽🙌🏾🙌🏿👏👏🏻👏🏼👏🏽👏🏾👏🏿🙏🙏🏻🙏🏼🙏🏽🙏🏾🙏🏿🤝👍👍🏻👍🏼👍🏽👍🏾👍🏿👎👎🏻👎🏼👎🏽👎🏾👎🏿👊👊🏻👊🏼👊🏽👊🏾👊🏿✊🏻✊🏼✊🏽✊🏾✊🏿

评论

~ 评论还没有,沙发可以有 O(∩_∩)O~