etcd 使用入门

etcd 使用入门

前言

介绍 etcd 和 raft协议

etcd 安装

etcd 的安装有两种方式

“1. 直接从官网下载 release 版本的二进制文件

  1. 下载源码手动编译安装

个人倾向于使用 release 版本安装,下面开始下载安装

“1. 找到下载的压缩包,解压

1
unzip etcd-v3.3.8-darwin-amd64.zip

  1. 将解压的文件夹的移动到安装目录中
    1
    sudo mv /path/to/etcd /usr/local/etcd

此时我们可以先看看解压后有什么?

➜  etcd-v3.3.8-darwin-amd64 ll
total 113064
drwxr-xr-x  22 ?  staff   748B  6 16 00:55 Documentation
-rw-r--r--   1 ?  staff    38K  6 16 00:55 README-etcdctl.md
-rw-r--r--   1 ?  staff   7.1K  6 16 00:55 README.md
-rw-r--r--   1 ?  staff   7.7K  6 16 00:55 READMEv2-etcdctl.md
drwx------   3 ?  staff   102B  7 16 23:45 default.etcd
-rwxr-xr-x   1 ?  staff    30M  6 16 00:55 etcd
-rwxr-xr-x   1 ?  staff    25M  6 16 00:55 etcdctl

可以看到这里有两个可执行程序: etcdetcdctl

etcd: etcd 服务端程序
etcdctl: etcd 客户端程序

  1. 启动程序

3.1 使用默认配置启动程序

1
./etcd

3.2 来一些启动配置,启动一个集群

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
nohup ./etcd --name test1 --initial-advertise-peer-urls http://localhost:2380 \
--listen-peer-urls http://localhost:2380 \
--listen-client-urls http://localhost:2379,http://127.0.0.1:2379 \
--advertise-client-urls http://localhost:2379 \
--initial-cluster-token test-cluster \
--initial-cluster test1=http://localhost:2380,test2=http://localhost:2390,test3=http://localhost:2400 \
--initial-cluster-state new \
--data-dir /app/etcd/data1 &

nohup ./etcd --name test2 --initial-advertise-peer-urls http://localhost:2390 \
--listen-peer-urls http://localhost:2390 \
--listen-client-urls http://localhost:2389,http://127.0.0.1:2389 \
--advertise-client-urls http://localhost:2389 \
--initial-cluster-token test-cluster \
--initial-cluster test1=http://localhost:2380,test2=http://localhost:2390,test3=http://localhost:2400 \
--initial-cluster-state new \
--data-dir /app/etcd/data2 &

nohup ./etcd --name test3 --initial-advertise-peer-urls http://localhost:2400 \
--listen-peer-urls http://localhost:2400 \
--listen-client-urls http://localhost:2399,http://127.0.0.1:2399 \
--advertise-client-urls http://localhost:2399 \
--initial-cluster-token test-cluster \
--initial-cluster test1=http://localhost:2380,test2=http://localhost:2390,test3=http://localhost:2400 \
--initial-cluster-state new \
--data-dir /app/etcd/data3 &

ETCD 简单指令操作

“1. ETCD 数据插入

1
./etcdctl put key value
  1. 插入文件数据到 ETCD
1
cat file | ./etcdctl put key
  1. 数据查看
1
./etcdctl get key
  1. 集群健康度查看
1
./etcdctl --endpoints=[endpoint1, endpoint2, endpoint3] endpoint health

得到结果

1
2
3
endpoint1 is healthy: successfully committed proposal: took = 932.637µs
endpoint2 is healthy: successfully committed proposal: took = 1.058401ms
endpoint3 is healthy: successfully committed proposal: took = 1.127266ms
  1. 集群节点状态查询
1
./etcdctl --endpoints=[endpoint1, endpoint2, endpoint3] endpoint status

得到结果

1
2
3
endpoint1, 180821f2462664c9, 3.2.12, 555 MB, true, 169, 12167260
endpoint2, b2b4375ce5b9bb02, 3.2.12, 555 MB, false, 169, 12167260
endpoint3, e4927ddc8eb44d9e, 3.2.12, 555 MB, false, 169, 12167260
注意:`ETCD` 的API 分为 `V2` 和 `V3` 两个版本,两者之间差距很大,上述 `etcdctl` 客户端的使用都是 `V3` API,在执行之前,请执行命令 `export ETCDCTL_API=3`