zhouzhaoxin
zhouzhaoxin 主攻python,喜欢读书,喜欢摄影,芒格的忠实追随者。关注各个领域发展,取长补短,擅长结合多学科技术解决问题

ETCD 安装

ETCD 安装

应用场景

https://tonydeng.github.io/2015/10/19/etcd-application-scenarios/

  1. 集群监控与LEADER竞选
  2. 分布式队列
  3. 分布式锁
  4. 分布式通知与协调
  5. 消息发布与订阅
  6. 服务发现

本地使用

  1. 负载均衡
  2. 健康检查
  3. nginx配置

模拟安装运行

https://yeasy.gitbooks.io/docker_practice/etcd/

直接安装

1
2
3
4
5
6
7
$ mkdir etcd
$ cd etcd
$ curl -L https://github.com/etcd-io/etcd/releases/download/v3.3.13/etcd-v3.3.13-linux-amd64.tar.gz -o 
$ ./etcd-v3.3.13-linux-amd64.tar.gz
$ tar xzvf etcd-v3.3.13-linux-amd64.tar.gz --strip-components=1
$ ./etcd -version
$ ./etcdctl -version

本地独立

使用 API 版本 3

1
2
3
4
5
$ export ETCDCTL_API=3
$ ./etcdctl put foo bar
OK
$ ./etcdctl get foo
bar

本地多成员集群

模拟多机器集群

1. 安装go

2. 安装goreman

1
$ go get github.com/mattn/goreman

3. 查看gopath

1
2
$ go env
GOPATH="/home/apple/go"

所以goreman运行路径为home/apple/go/bin/goreman

4. 编写goroman配置文件

1
$ vim Procfile

编辑内容如下

1
2
3
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:12    380 --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

5. 运行

1
$ /home/apple/go/bin/goreman -f  Procfile start

6.查看集群列表

1
2
3
4
5
$ export ETCDCTL_API=3
$ ./etcdctl member list
8211f1d0f64f3269: name=infra1 peerURLs=http://127.0.0.1:12380 clientURLs=http://127.0.0.1:2379 isLeader=false
91bc3c398fb3c146: name=infra2 peerURLs=http://127.0.0.1:22380 clientURLs=http://127.0.0.1:22379 isLeader=true
fd422379fda50e48: name=infra3 peerURLs=http://127.0.0.1:32380 clientURLs=http://127.0.0.1:32379 isLeader=false