linux下安装使用etcd - Go语言中文社区

linux下安装使用etcd


一、简介

 etcd是一个分布式一致性键值存储系统,用于共享配置和服务发现,专注于:

简单:良好定义的,面向用户的API (gRPC)

安全: 带有可选客户端证书认证的自动TLS

快速:测试验证,每秒10000写入

可靠:使用Raft适当分布

etcdGo编写,并使用Raft一致性算法来管理高可用复制日志:

 

二、安装

1.方法1

$curl -L https://github.com/coreos/etcd/releases/download/v3.3.2/etcd-v3.3.2-linux-amd64.tar.gz -o etcd-v3.3.2-linux-amd64.tar.gz

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

$tar zxf etcd-v3.3.2-linux-amd64.tar.gz

#将解压后的etcdetcdctl移动到$GOPATH/bin目录下,可以直接使用etcdetcdctl命令

$mv etcd-v3.3.2-linux-amd64/etcd* /$GOPATH/bin

 



2.方法2

$ mkdir -p $GOPATH/src/github.com/coreos

$ cd !$

$ git clone https://github.com/coreos/etcd.git

$ cd etcd

$ ./build

$ ./bin/etcd



 

三、启动

$ ./etcd (或直接使用etcd



启动的 etcd 成员在 localhost:2379 监听客户端请求。

通过使用 etcdctl 来和已经启动的集群交互:

# 使用 API 版本 3

$ export ETCDCTL_API=3

$ ./etcdctl put foo bar

OK

 

$ ./etcdctl get foo

bar


 

四、本地多成员集群

1. 安装goreman

关于goreman可参考https://segmentfault.com/a/1190000003778084

$ go get github.com/mattn/goreman

goreman help   //检验gorenman是否安装成功

当然,记得先把GOPATH、GOROOT环境变量配置好,并记得把$GOPATH/bin添加到$PATH

 

2. 启动

Procfile下载地址 https://github.com/coreos/etcd/blob/master/Procfile

$ goreman -f Procfile start

 

1: 必须先安装 go,请见章节 Go语言安装

2: 这里所说的 Procfile 文件是来自 etcd gitub 项目的根目录下的 Procfile 文件,但

是需要修改一下,将里面的 bin/etcd 修改为 etcd

 Profile内容如下:

# 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

 

 

3. 交互

$ export ETCDCTL_API=3

$ etcdctl --write-out=table --endpoints=localhost:2379 member list

 

 

4. 使用

$ etcdctl --endpoints=localhost:2379 put foo bar

 

 

可以从其他节点获取:

$etcdctl --endpoints=localhost:22379 get foo

 

 

   删除key

  $etcdctl --endpoints=localhost:22379 del foo


 

 

5. 容错性

   #添加key value

   $ etcdctl --endpoints=localhost:2379 put foo bar

 

 #kill掉第二个节点

    $ goreman run stop etcd2


   #可以从第一个节点获取值

    $ etcdctl --endpoints=localhost:2379 get foo

    foo

    bar


  #但是不能从第二个节点获取值

  $ etcdctl --endpoints=localhost:22379 get foo


 会出现:

 

 

  #重启节点后可以从该节点获取值

  $ goreman run restart etcd2

  $ etcdctl --endpoints=localhost:22379 get foo

  foo

  bar



 

  

   

 

 

  

版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/skh2015java/article/details/80714661
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2020-03-07 21:18:33
  • 阅读 ( 1787 )
  • 分类:Linux

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢