etcd

作者: 云烟1f3bca320ba6 | 来源:发表于2016-12-07 22:43 被阅读0次

    etcd

    分布式一致性key-value存储

    版本

    v2

    使用场景

    1. 注册中心,服务注册与发现
    2. 配置中心,配置推送
    3. 分布式锁
    4. 消息发布与订阅
    5. 分布式cas
    6. 分布式队列

    特点

    1. 简单,api友好(gPRC)
    2. 安全,tls支持
    3. 性能,10000/s写入速度
    4. 可靠,使用Raft协议

    REST命令

    查看版本 curl -L http://127.0.0.1:2379/version
    获得值 curl http://127.0.0.1:2379/v2/keys/message
    插入值 curl http://127.0.0.1:2379/v2/keys/message -XPUT -d value="Hello world"
    修改值 curl http://127.0.0.1:2379/v2/keys/message -XPUT -d value="Hello etcd"
    删除值 curl http://127.0.0.1:2379/v2/keys/message -XDELETE
    使用ttl curl http://127.0.0.1:2379/v2/keys/foo -XPUT -d value=bar -d ttl=5
    监听 curl http://127.0.0.1:2379/v2/keys/foo?wait=true
    创建文件夹 curl http://127.0.0.1:2379/v2/keys/dir -XPUT -d dir=true
    删除文件夹 curl http://127.0.0.1:2379/v2/keys/foo_dir?dir=true -XDELETE
    上传文件 curl http://127.0.0.1:2379/v2/keys/afile -XPUT --data-urlencode value@afile.txt
    主信息 curl http://127.0.0.1:2379/v2/stats/leader
    节点信息 curl http://127.0.0.1:2379/v2/stats/self
    存储信息 curl http://127.0.0.1:2379/v2/stats/store
    cas curl http://127.0.0.1:2379/v2/keys/foo?prevValue=one -XPUT -d value=two
    

    集群搭建

    1. 静态,在集群启动之前就知道集群机器情况

    示例:准备三台机器,安装完成etcd

    192.168.1.100
    192.168.1.101
    192.168.1.102
    

    每个机器上分别执行命令

    $ etcd --name infra0 --initial-advertise-peer-urls http://192.168.1.100:2380 \
      --listen-peer-urls http://192.168.1.100:2380 \
      --listen-client-urls http://192.168.1.100:2379 \
      --advertise-client-urls http://192.168.1.100:2379 \
      --initial-cluster-token etcd-cluster-1 \
      --initial-cluster infra0=http://192.168.1.100:2380,infra1=http://192.168.1.101:2380,infra2=http://192.168.1.102:2380 \
      --initial-cluster-state new
        
    $ etcd --name infra1 --initial-advertise-peer-urls http://192.168.1.101:2380 \
      --listen-peer-urls http://192.168.1.101:2380 \
      --listen-client-urls http://192.168.1.101:2379 \
      --advertise-client-urls http://192.168.1.101:2379 \
      --initial-cluster-token etcd-cluster-1 \
      --initial-cluster infra0=http://192.168.1.100:2380,infra1=http://192.168.1.101:2380,infra2=http://192.168.1.102:2380 \
      --initial-cluster-state new
        
    $ etcd --name infra2 --initial-advertise-peer-urls http://192.168.1.102:2380 \
      --listen-peer-urls http://192.168.1.102:2380 \
      --listen-client-urls http://192.168.1.102:2379 \
      --advertise-client-urls http://192.168.1.102:2379 \
      --initial-cluster-token etcd-cluster-1 \
      --initial-cluster infra0=http://192.168.1.100:2380,infra1=http://192.168.1.101:2380,infra2=http://192.168.1.102:2380 \
      --initial-cluster-state new
    
    1. etcd发现
    • 使用存在的etcd集群做自动发现

      指定集群数目

       $ curl -X PUT https://myetcd.local/v2/keys/discovery/6c007a14875d53d9bf0ef5a6fc0257c817f0fb83/_config/size -d value=3
    

    ​ 启动集群

       $ etcd --name infra0 --initial-advertise-peer-urls http://10.0.1.10:2380 \
         --listen-peer-urls http://10.0.1.10:2380 \
         --listen-client-urls http://10.0.1.10:2379,http://127.0.0.1:2379 \
         --advertise-client-urls http://10.0.1.10:2379 \
         --discovery https://myetcd.local/v2/keys/discovery/6c007a14875d53d9bf0ef5a6fc0257c817f0fb83
    
       $ etcd --name infra1 --initial-advertise-peer-urls http://10.0.1.11:2380 \
         --listen-peer-urls http://10.0.1.11:2380 \
         --listen-client-urls http://10.0.1.11:2379,http://127.0.0.1:2379 \
         --advertise-client-urls http://10.0.1.11:2379 \
         --discovery https://myetcd.local/v2/keys/discovery/6c007a14875d53d9bf0ef5a6fc0257c817f0fb83
    
       $ etcd --name infra2 --initial-advertise-peer-urls http://10.0.1.12:2380 \
         --listen-peer-urls http://10.0.1.12:2380 \
         --listen-client-urls http://10.0.1.12:2379,http://127.0.0.1:2379 \
         --advertise-client-urls http://10.0.1.12:2379 \
         --discovery https://myetcd.local/v2/keys/discovery/6c007a14875d53d9bf0ef5a6fc0257c817f0fb83
    
    • 使用公共服务
       $ curl https://discovery.etcd.io/new?size=3
       https://discovery.etcd.io/3e86b59982e49066c5d813af1c2e2579cbf573de
    
       $ etcd --name infra0 --initial-advertise-peer-urls http://10.0.1.10:2380 \
         --listen-peer-urls http://10.0.1.10:2380 \
         --listen-client-urls http://10.0.1.10:2379,http://127.0.0.1:2379 \
         --advertise-client-urls http://10.0.1.10:2379 \
         --discovery https://discovery.etcd.io/3e86b59982e49066c5d813af1c2e2579cbf573de
       $ etcd --name infra1 --initial-advertise-peer-urls http://10.0.1.11:2380 \
         --listen-peer-urls http://10.0.1.11:2380 \
         --listen-client-urls http://10.0.1.11:2379,http://127.0.0.1:2379 \
         --advertise-client-urls http://10.0.1.11:2379 \
         --discovery https://discovery.etcd.io/3e86b59982e49066c5d813af1c2e2579cbf573de
       $ etcd --name infra2 --initial-advertise-peer-urls http://10.0.1.12:2380 \
         --listen-peer-urls http://10.0.1.12:2380 \
         --listen-client-urls http://10.0.1.12:2379,http://127.0.0.1:2379 \
         --advertise-client-urls http://10.0.1.12:2379 \
         --discovery https://discovery.etcd.io/3e86b59982e49066c5d813af1c2e2579cbf573de
    

    3.DNA发现

    节点监控

    curl http://127.0.0.1:2379/v2/stats/leader
    
    id: the unique identifier for the member
    leaderInfo.leader: id of the current leader member
    leaderInfo.uptime: amount of time the leader has been leader
    name: this member's name
    recvAppendRequestCnt: number of append requests this node has processed
    recvBandwidthRate: number of bytes per second this node is receiving (follower only)
    recvPkgRate: number of requests per second this node is receiving (follower only)
    sendAppendRequestCnt: number of requests that this node has sent
    sendBandwidthRate: number of bytes per second this node is sending (leader only). This value is undefined on single member clusters.
    sendPkgRate: number of requests per second this node is sending (leader only). This value is undefined on single member clusters.
    state: either leader or follower
    startTime: the time when this node was started
    
    curl http://127.0.0.1:2379/v2/stats/self
    
    curl http://127.0.0.1:2379/v2/stats/store
    

    参考

    1. https://github.com/coreos/etcd
    2. https://raft.github.io/
    3. http://www.ruanyifeng.com/blog/2011/09/curl.html

    相关文章

      网友评论

          本文标题:etcd

          本文链接:https://www.haomeiwen.com/subject/ufyjmttx.html