Etcd快速入门

1. etcd安装

etcd在生产环境中一般推荐集群方式部署。本文定位为入门,主要讲讲单节点安装和基本使用。

etcd目前默认使用2379端口提供HTTP API服务,2380端口和peer通信(这两个端口已经被IANA官方预留给etcd);在之前的版本中可能会分别使用4001和7001,在使用的过程中需要注意这个区别。

因为etcd是go语言编写的,安装只需要下载对应的二进制文件,并放到合适的路径就行。

1.1 mac安装

brew search etcd
brew install etcd

1.2 linux安装

1.2.1 脚本方式(推荐)

ETCD_VER=v3.4.17

# choose either URL
GOOGLE_URL=https://storage.googleapis.com/etcd
GITHUB_URL=https://github.com/etcd-io/etcd/releases/download
DOWNLOAD_URL=${GOOGLE_URL}

rm -f /tmp/etcd-${ETCD_VER}-linux-amd64.tar.gz
rm -rf /tmp/etcd-download-test && mkdir -p /tmp/etcd-download-test

curl -L ${DOWNLOAD_URL}/${ETCD_VER}/etcd-${ETCD_VER}-linux-amd64.tar.gz -o /tmp/etcd-${ETCD_VER}-linux-amd64.tar.gz
tar xzvf /tmp/etcd-${ETCD_VER}-linux-amd64.tar.gz -C /tmp/etcd-download-test --strip-components=1
rm -f /tmp/etcd-${ETCD_VER}-linux-amd64.tar.gz

/tmp/etcd-download-test/etcd --version
/tmp/etcd-download-test/etcdctl version

运行测试

# start a local etcd server
/tmp/etcd-download-test/etcd

# write,read to etcd
/tmp/etcd-download-test/etcdctl --endpoints=localhost:2379 put foo bar
/tmp/etcd-download-test/etcdctl --endpoints=localhost:2379 get foo

1.2.2 源码安装

wget https://github.com/etcd-io/etcd/releases/download/v3.4.17/etcd-v3.4.17-linux-amd64.tar.gz
tar xzvf etcd-v3.4.17-linux-amd64.tar.gz
mv etcd-v3.4.17-linux-amd64 /opt/etcd

build_etcd.sh

#!/usr/bin/env bash

wget -c https://github.com/coreos/etcd/releases/download/v3.2.0/etcd-v3.2.0-linux-amd64.tar.gz
tar xzf etcd-v3.2.0-linux-amd64.tar.gz
cd etcd-v3.2.0-linux-amd64

./etcd &> /dev/null &

解压后是一些文档和两个二进制文件etcd和etcdctl。etcd是server端,etcdctl是客户端。

ls
Documentation  etcd  etcdctl  README-etcdctl.md  README.md  READMEv2-etcdctl.md

创建systemd服务

  • 建立相关目录
mkdir -p /var/lib/etcd/
mkdir -p /opt/etcd/config/
  • 创建etcd配置文件
cat <<EOF | sudo tee /opt/etcd/config/etcd.conf
#节点名称
ETCD_NAME=$(hostname -s)
#数据存放位置
ETCD_DATA_DIR=/var/lib/etcd
EOF
  • 创建systemd配置文件
cat <<EOF | sudo tee /etc/systemd/system/etcd.service

[Unit]
Description=Etcd Server
Documentation=https://github.com/coreos/etcd
After=network.target

[Service]
User=root
Type=notify
EnvironmentFile=-/opt/etcd/config/etcd.conf
ExecStart=/opt/etcd/etcd
Restart=on-failure
RestartSec=10s
LimitNOFILE=40000

[Install]
WantedBy=multi-user.target
EOF
  • 启动etcd
systemctl daemon-reload && systemctl enable etcd && systemctl start etcd

1.3 运行(mac模式)

# 启动某个应用,这里用 etcd 做演示
brew services start etcd

# 停止某个应用
brew services stop etcd

# 查看当前应用列表
brew services list
etcd
{"level":"info","ts":"2021-10-12T16:31:30.685+0800","caller":"etcdmain/etcd.go:72","msg":"Running: ","args":["etcd"]}
{"level":"info","ts":"2021-10-12T16:31:30.686+0800","caller":"etcdmain/etcd.go:99","msg":"failed to detect default host","error":"default host not supported on darwin_amd64"}
{"level":"warn","ts":"2021-10-12T16:31:30.686+0800","caller":"etcdmain/etcd.go:104","msg":"'data-dir' was empty; using default","data-dir":"default.etcd"}
{"level":"info","ts":"2021-10-12T16:31:30.686+0800","caller":"embed/etcd.go:131","msg":"configuring peer listeners","listen-peer-urls":["http://localhost:2380"]}
{"level":"info","ts":"2021-10-12T16:31:30.686+0800","caller":"embed/etcd.go:367","msg":"closing etcd server","name":"default","data-dir":"default.etcd","advertise-peer-urls":["http://localhost:2380"],"advertise-client-urls":["http://localhost:2379"]}
{"level":"info","ts":"2021-10-12T16:31:30.686+0800","caller":"embed/etcd.go:369","msg":"closed etcd server","name":"default","data-dir":"default.etcd","advertise-peer-urls":["http://localhost:2380"],"advertise-client-urls":["http://localhost:2379"]}
{"level":"warn","ts":"2021-10-12T16:31:30.686+0800","caller":"etcdmain/etcd.go:145","msg":"failed to start etcd","error":"listen tcp 127.0.0.1:2380: bind: address already in use"}
{"level":"fatal","ts":"2021-10-12T16:31:30.686+0800","caller":"etcdmain/etcd.go:203","msg":"discovery failed","error":"listen tcp 127.0.0.1:2380: bind: address already in use","stacktrace":"go.etcd.io/etcd/server/v3/etcdmain.startEtcdOrProxyV2\n\t/private/tmp/etcd-20210616-70326-6ud3hj/server/etcdmain/etcd.go:203\ngo.etcd.io/etcd/server/v3/etcdmain.Main\n\t/private/tmp/etcd-20210616-70326-6ud3hj/server/etcdmain/main.go:40\nmain.main\n\t/private/tmp/etcd-20210616-70326-6ud3hj/server/main.go:32\nruntime.main\n\t/usr/local/Cellar/go/1.16.5/libexec/src/runtime/proc.go:225"}

从执行结果中可以看出:

  • etcdserver: name = default, name表示节点名称,默认为default。
  • etcdserver: data dir = default.etcd,data-dir保存日志和快照的目录,默认为当前工作目录“./default.etcd/”。
  • etcdserver: initial advertise peer URLs = http://localhost:2380,通过http://localhost:2380,和集群中其他节点通信。
  • etcdserver: advertise client URLs = http://localhost:2379,通过http://localhost:2379,对外提供HTTP API服务,供客户端交互。如果配置webui,就使用这个地址。
  • etcdserver: heartbeat = 100ms leader发送心跳到followers的间隔时间。
  • etcdserver: election = 1000ms 重新投票的超时时间,如果follow在该时间间隔没有收到心跳包,会触发重新投票,默认为1000ms
  • 集群和每个节点都会生成一个 uuid。
  • 启动的时候,会运行 raft协议,选举出 leader。
~|⇒ etcdctl -h
NAME:
    etcdctl - A simple command line client for etcd3.

USAGE:
    etcdctl [flags]

VERSION:
    3.5.0

API VERSION:
    3.5


COMMANDS:
    alarm disarm        Disarms all alarms
    alarm list      Lists all alarms
    auth disable        Disables authentication
    auth enable     Enables authentication
    auth status     Returns authentication status
    check datascale     Check the memory usage of holding data for different workloads on a given server endpoint.
    check perf      Check the performance of the etcd cluster
    compaction      Compacts the event history in etcd
    defrag          Defragments the storage of the etcd members with given endpoints
    del         Removes the specified key or range of keys [key, range_end)
    elect           Observes and participates in leader election
    endpoint hashkv     Prints the KV history hash for each endpoint in --endpoints
    endpoint health     Checks the healthiness of endpoints specified in `--endpoints` flag
    endpoint status     Prints out the status of endpoints specified in `--endpoints` flag
    get         Gets the key or a range of keys
    help            Help about any command
    lease grant     Creates leases
    lease keep-alive    Keeps leases alive (renew)
    lease list      List all active leases
    lease revoke        Revokes leases
    lease timetolive    Get lease information
    lock            Acquires a named lock
    make-mirror     Makes a mirror at the destination etcd cluster
    member add      Adds a member into the cluster
    member list     Lists all members in the cluster
    member promote      Promotes a non-voting member in the cluster
    member remove       Removes a member from the cluster
    member update       Updates a member in the cluster
    move-leader     Transfers leadership to another etcd cluster member.
    put         Puts the given key into the store
    role add        Adds a new role
    role delete     Deletes a role
    role get        Gets detailed information of a role
    role grant-permission   Grants a key to a role
    role list       Lists all roles
    role revoke-permission  Revokes a key from a role
    snapshot restore    Restores an etcd member snapshot to an etcd directory
    snapshot save       Stores an etcd node backend snapshot to a given file
    snapshot status     [deprecated] Gets backend snapshot status of a given file
    txn         Txn processes all the requests in one transaction
    user add        Adds a new user
    user delete     Deletes a user
    user get        Gets detailed information of a user
    user grant-role     Grants a role to a user
    user list       Lists all users
    user passwd     Changes password of user
    user revoke-role    Revokes a role from a user
    version         Prints the version of etcdctl
    watch           Watches events stream on keys or prefixes

OPTIONS:
      --cacert=""               verify certificates of TLS-enabled secure servers using this CA bundle
      --cert=""                 identify secure client using this TLS certificate file
      --command-timeout=5s          timeout for short running command (excluding dial timeout)
      --debug[=false]               enable client-side debug logging
      --dial-timeout=2s             dial timeout for client connections
  -d, --discovery-srv=""            domain name to query for SRV records describing cluster endpoints
      --discovery-srv-name=""           service name to query when using DNS discovery
      --endpoints=[127.0.0.1:2379]      gRPC endpoints
  -h, --help[=false]                help for etcdctl
      --hex[=false]             print byte strings as hex encoded strings
      --insecure-discovery[=true]       accept insecure SRV records describing cluster endpoints
      --insecure-skip-tls-verify[=false]    skip server certificate verification (CAUTION: this option should be enabled only for testing purposes)
      --insecure-transport[=true]       disable transport security for client connections
      --keepalive-time=2s           keepalive time for client connections
      --keepalive-timeout=6s            keepalive timeout for client connections
      --key=""                  identify secure client using this TLS key file
      --password=""             password for authentication (if this option is used, --user option shouldn't include password)
      --user=""                 username[:password] for authentication (prompt if password is not supplied)
  -w, --write-out="simple"          set the output format (fields, json, protobuf, simple, table

好了, etcd 已经启动了,现在验证下,是否正确的启动:

etcdctl endpoint health
~|⇒ etcdctl endpoint health
127.0.0.1:2379 is healthy: successfully committed proposal: took = 8.816294ms

至此,etcd 已经安装完毕。

2. 简单操作

最常见的就是put、get和del命令。

# 放入一个 键值对
~  etcdctl put "name" "zyq1"
OK
#取出一个 键值对
~  etcdctl get  "name"
name
zyq1
# 删除一个 键值对
~  etcdctl del  "name"
1

watch
watch命令用来监测key的变化,会建立长连接,一直监听。

 ~  etcdctl watch "name"
PUT
name
zyq1
DELETE
name

3. 租约

租约是一段时间,可以为etcd的key授予租约。当key被附加到租约时,它的生存时间被绑定到租约的生存时间,一旦租约的TTL到期,租约就过期并且所有附带的key都将被删除。

一个租约可以绑定不止一个key。

# 创建一个20s的租约
$ ./etcdctl lease grant 20
lease 694d673115905e37 granted with TTL(20s)

# 使用租约的 id 进行 put 操作
$ ./etcdctl put --lease=694d673115905e37 "name" "zyq"

# 20s后get发现 key被删除了
$ ./etcdctl get "name"
# 空应答

租约可以被删除

# 创建一个20s的租约
$ ./etcdctl lease grant 1000
lease 694d673115905e49 granted with TTL(1000s)

# 使用租约的 id 进行 put 操作
$ ./etcdctl put --lease=694d673115905e49 "name" "zyq"
OK
# 删除租约
$ ./etcdctl lease revoke 694d673115905e49
lease 694d673115905e49 revoked
# 发现key也被删除了
$ ./etcdctl get "name"
# 空应答

租约可以自动续租

# 创建一个20s的租约
$ ./etcdctl lease grant 20
lease 694d673115905e4f granted with TTL(20s)
# 自动续租
$ ./etcdctl lease keep-alive 694d673115905e4f
lease 694d673115905e4f keepalived with TTL(20)
lease 694d673115905e4f keepalived with TTL(20)


参考资料

mac etcd

Etcd 使用入门

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 214,172评论 6 493
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 91,346评论 3 389
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 159,788评论 0 349
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,299评论 1 288
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,409评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,467评论 1 292
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,476评论 3 412
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,262评论 0 269
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,699评论 1 307
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,994评论 2 328
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,167评论 1 343
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,827评论 4 337
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,499评论 3 322
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,149评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,387评论 1 267
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,028评论 2 365
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,055评论 2 352

推荐阅读更多精彩内容

  • 官方链接: etcd命令行 快速入门单机启动etcd本地集群启动使用goreman启动本地三节点Procfileg...
    捞月亮的阿汤哥阅读 1,845评论 0 2
  • 一、使用 brew 安装 1、 确定 brew 是否有 etcd 包: 当然肯定有这个包,这样做的好处是养成一个好...
    Darker_坤阅读 9,231评论 0 5
  • 转载: Etcd 使用入门 etcd简介 etcd是CoreOS团队于2013年6月发起的开源项目,它的目标是构建...
    Austin_Brant阅读 253,640评论 6 53
  • Etcd主要功能 键值写入与读取。 过期时间。 观察者。 租约。 集群管理相关操作。 维护操作。 用户及权限管理。...
    不喜欢夜雨天阅读 2,105评论 0 0
  • 因为工作需求,公司需要使用ETCD来做gRPC服务的负载均衡,以及集群管理,所以对etcd做了一些研究,希望能给大...
    Jay_Guo阅读 46,581评论 8 47