etcd 是什么?

中文:etcd是一个分布式一致性键值存储,其主要用于分布式系统的共享配置和服务发现
英文:A distributed, reliable key-value store for the most critical data of a distributed system

1、学习文档

英文:https://coreos.com/etcd/docs/latest/learning/data_model.html
辅助文档:https://legacy.gitbook.com/book/skyao/learning-etcd3/details

2、简单搭建单例测试

主要参考:https://coreos.com/etcd/docs/latest/dl_build.html

简单测试:https://github.com/coreos/etcd/releases
下载最新的版本

./etcd
ETCDCTL_API=3 etcdctl put michael 111
ETCDCTL_API=3 etcdctl get michael

为了简单起见:配置环境变量 /etc/profile
指定客户端工具etcdctl使用api v3和服务器程序etcd进行通信
```
export PATH=$PATH:/home/etcd/etcd335
export ETCDCTL_API=3
```
source /etc/profile

3、本地快速搭建集群测试

主要参考:https://coreos.com/etcd/docs/latest/demo.html


首先安装goreman  go get -v  github.com/mattn/goreman
然后下载 https://github.com/coreos/etcd/blob/master/Procfile
goreman -f Procfile start 即可快速开启一个本地集群
# Use goreman to run `go get github.com/mattn/goreman`
etcd1: etcd --name infra1 --listen-client-urls http://127.0.0.1:2379 --advertise-client-urls http://127.0.0.1:2379 --listen-peer-urls http://127.0.0.1:12380 --initial-advertise-peer-urls http://127.0.0.1:12380 --initial-cluster-token etcd-cluster-1 --initial-cluster 'infra1=http://127.0.0.1:12380,infra2=http://127.0.0.1:22380,infra3=http://127.0.0.1:32380' --initial-cluster-state new --enable-pprof
etcd2: etcd --name infra2 --listen-client-urls http://127.0.0.1:22379 --advertise-client-urls http://127.0.0.1:22379 --listen-peer-urls http://127.0.0.1:22380 --initial-advertise-peer-urls http://127.0.0.1:22380 --initial-cluster-token etcd-cluster-1 --initial-cluster 'infra1=http://127.0.0.1:12380,infra2=http://127.0.0.1:22380,infra3=http://127.0.0.1:32380' --initial-cluster-state new --enable-pprof
etcd3: etcd --name infra3 --listen-client-urls http://127.0.0.1:32379 --advertise-client-urls http://127.0.0.1:32379 --listen-peer-urls http://127.0.0.1:32380 --initial-advertise-peer-urls http://127.0.0.1:32380 --initial-cluster-token etcd-cluster-1 --initial-cluster 'infra1=http://127.0.0.1:12380,infra2=http://127.0.0.1:22380,infra3=http://127.0.0.1:32380' --initial-cluster-state new --enable-pprof
# in future, use proxy to listen on 2379
#proxy: bin/etcd --name infra-proxy1 --proxy=on --listen-client-urls http://127.0.0.1:2378 --initial-cluster 'infra1=http://127.0.0.1:12380,infra2=http://127.0.0.1:22380,infra3=http://127.0.0.1:32380' --enable-pprof
etcdctl --endpoints=localhost:22379 put michael 222 
etcdctl --endpoints=localhost:22379 get michael 

测试其他操作:1、Set up a cluster 2、Transactional write 3、Watch 4、Lease 5、Distributed locks 6、Elections 7、Auth

其他参数解释:
https://github.com/coreos/etcd/blob/master/Documentation/op-guide/configuration.md
or
https://blog.csdn.net/varyall/article/details/79128181
or
https://legacy.gitbook.com/book/skyao/learning-etcd3/details
or
https://alexstocks.github.io/html/etcd.html

4、3台机器搭建的最小集群

	name       |  内网ip          | 外网ip
	---------- | ---------------- | --------
	mt1		   |  10.105.79.74    | ***
	mt2        |  10.105.90.27    | ***
	mt2        |  10.154.6.123    | ***
```	
#### mt1 上执行	

nohup etcd –name infra0 –initial-advertise-peer-urls http://10.105.79.74:2380
–listen-peer-urls http://10.105.79.74:2380
–listen-client-urls http://10.105.79.74:2379,http://127.0.0.1:2379
–advertise-client-urls http://10.105.79.74:2379
–initial-cluster-token etcd-cluster-1
–initial-cluster infra0=http://10.105.79.74:2380,infra1=http://10.105.90.27:2380,infra2=http://10.154.6.123:2380
–initial-cluster-state new
–data-dir=/data/etcdir/ > /var/log/etcd.log 2>&1 &

#### mt2 上执行

nohup etcd –name infra1 –initial-advertise-peer-urls http://10.105.90.27:2380
–listen-peer-urls http://10.105.90.27:2380
–listen-client-urls http://10.105.90.27:2379,http://127.0.0.1:2379
–advertise-client-urls http://10.105.90.27:2379
–initial-cluster-token etcd-cluster-1
–initial-cluster infra0=http://10.105.79.74:2380,infra1=http://10.105.90.27:2380,infra2=http://10.154.6.123:2380
–initial-cluster-state new
–data-dir=/data/etcdir/ > /var/log/etcd.log 2>&1 &


#### mt3 上执行

nohup etcd –name infra2 –initial-advertise-peer-urls http://10.154.6.123:2380
–listen-peer-urls http://10.154.6.123:2380
–listen-client-urls http://10.154.6.123:2379,http://127.0.0.1:2379
–advertise-client-urls http://10.154.6.123:2379
–initial-cluster-token etcd-cluster-1
–initial-cluster infra0=http://10.105.79.74:2380,infra1=http://10.105.90.27:2380,infra2=http://10.154.6.123:2380
–initial-cluster-state new
–data-dir=/data/etcdir/ > /var/log/etcd.log 2>&1 & ```

MarkDown表格表示
http://xianbai.me/learn-md/article/extension/table.html

5、etcd 角色控制

http://www.huweihuang.com/article/etcd/etcd-auth&security/

6、etcd 客户端介绍

参考:https://yuerblog.cc/2017/12/12/etcd-v3-sdk-usage/

7、使用etcd作为服务注册的例子

参考:
https://ralphbupt.github.io/2017/05/04/etcd-%E6%9C%8D%E5%8A%A1%E6%B3%A8%E5%86%8C%E4%B8%8E%E5%8F%91%E7%8E%B0/
https://daizuozhuo.github.io/etcd-service-discovery/

8、使用etcd作为gRPC服务发现&负载均衡的例子

https://segmentfault.com/a/1190000008672912#articleHeader3

9、etcd:从应用场景到实现原理的全方位解读

http://www.infoq.com/cn/articles/etcd-interpretation-application-scenario-implement-principle