Etcd


etcd.png

etcd 是 CoreOS 团队发起的一个管理配置信息和服务发现( Service Discovery )的项目。

什么是 etcd

etcd 是 CoreOS 团队于 2013 年 6 月发起的开源项目,它的目标是构建一个高可用的分布式键值( key-value )数据库,基于 Go 语言实现。

受到 Apache ZooKeeper 项目和 doozer 项目的启发, etcd 在设计的时候重点考虑了下面四个要素:

  • 简单:具有定义良好、面向用户的 API (gRPC)
  • 安全:支持 HTTPS 方式的访问
  • 快速:支持并发 10 k/s 的写操作
  • 可靠:支持分布式结构,基于 Raft 的一致性算法

Apache ZooKeeper 是一套知名的分布式系统中进行同步和一致性管理的工具。

doozer 是一个一致性分布式数据库。

Raft 是一套通过选举主节点来实现分布式系统一致性的算法,相比于大名鼎鼎的 Paxos 算法,它的过程更容易被人理解。

一般情况下,用户使用 etcd 可以在多个节点上启动多个实例,并添加它们为一个集群。同一个集群中的 etcd 实例将会保持彼此信息的一致性。

安装

以 Mac 为例:

brew install etcd
etcd
etcdctl member list

etcd 集群

下面我们使用 Docker Compose 模拟启动一个 3 节点的 etcd 集群。 编辑 docker-compose.yml 文件:

version: "3"

services:

    node1:
        image: quay.io/coreos/etcd
        volumes:
            - node1-data:/etcd-data
        expose:
            - 2379
            - 2380
        networks:
            cluster_net:
                ipv4_address: 172.16.238.100
        environment:
            - ETCDCTL_API=3
        command:
            - /usr/local/bin/etcd
            - --data-dir=/etcd-data
            - --name
            - node1
            - --initial-advertise-peer-urls
            - http://172.16.238.100:2380
            - --listen-peer-urls
            - http://0.0.0.0:2380
            - --advertise-client-urls
            - http://172.16.238.100:2379
            - --listen-client-urls
            - http://0.0.0.0:2379
            - --initial-cluster
            - node1=http://172.16.238.100:2380,node2=http://172.16.238.101:2380,node3=http://172.16.238.102:2380
            - --initial-cluster-state
            - new
            - --initial-cluster-token
            - docker-etcd

    node2:
        image: quay.io/coreos/etcd
        volumes:
            - node2-data:/etcd-data
        networks:
            cluster_net:
                ipv4_address: 172.16.238.101
        environment:
            - ETCDCTL_API=3
        expose:
            - 2379
            - 2380
        command:
            - /usr/local/bin/etcd
            - --data-dir=/etcd-data
            - --name
            - node2
            - --initial-advertise-peer-urls
            - http://172.16.238.101:2380
            - --listen-peer-urls
            - http://0.0.0.0:2380
            - --advertise-client-urls
            - http://172.16.238.101:2379
            - --listen-client-urls
            - http://0.0.0.0:2379
            - --initial-cluster
            - node1=http://172.16.238.100:2380,node2=http://172.16.238.101:2380,node3=http://172.16.238.102:2380
            - --initial-cluster-state
            - new
            - --initial-cluster-token
            - docker-etcd

    node3:
        image: quay.io/coreos/etcd
        volumes:
            - node3-data:/etcd-data
        networks:
            cluster_net:
                ipv4_address: 172.16.238.102
        environment:
            - ETCDCTL_API=3
        expose:
            - 2379
            - 2380
        command:
            - /usr/local/bin/etcd
            - --data-dir=/etcd-data
            - --name
            - node3
            - --initial-advertise-peer-urls
            - http://172.16.238.102:2380
            - --listen-peer-urls
            - http://0.0.0.0:2380
            - --advertise-client-urls
            - http://172.16.238.102:2379
            - --listen-client-urls
            - http://0.0.0.0:2379
            - --initial-cluster
            - node1=http://172.16.238.100:2380,node2=http://172.16.238.101:2380,node3=http://172.16.238.102:2380
            - --initial-cluster-state
            - new
            - --initial-cluster-token
            - docker-etcd

volumes:
    node1-data:
    node2-data:
    node3-data:

networks:
    cluster_net:
        driver: bridge
        ipam:
          driver: default
          config:
              - subnet: 172.16.238.0/24

使用 docker-compose up 启动集群后使用 docker exec 登录到任意节点测试 etcd 集群。

使用 etcdctl

etcdctl 是一个命令行客户端,它能提供一些简洁的命令,供用户直接跟 etcd 服务打交道,而无需基于 HTTP API 方式。

put

etcdctl put /testdir/testkey "Hello world"

get

etcdctl get testkey

del

etcdctl del testkey

watch

etcdctl watch testkey

member

操作的是 etcd 实例:

etcdctl member list

点赞 取消点赞 收藏 取消收藏

<< 上一篇: Docker 底层实现原理

>> 下一篇: CoreOS