使用Etcd服务入门指南

Etcd 是一个使用一致性哈希算法(Raft)在分布式环境下的 key/value 存储服务。利用 Etcd 的特性,应用程序可以在集群中共享信息、配置或作服务发现,Etcd 会在集群的各个节点中复制这些数据并保证这些数据始终正确。

  • System Requirements >= 8v CPU + 16GB RAM + 50GB SSD
使用Etcd服务入门指南

1. 安装使用

静态就是在配置服务之前已经知道了节点的地址和集群的大小

  • [1] 源码编译安装
############################
1. Build the latest version
############################

1. 1.下载项目并编译
$ git clone https://github.com/etcd-io/etcd.git && cd etcd
$ ./build
To build a vendored etcd from the master branch via go get:

1. 2.设置GOPATH环境变量
$ export GOPATH='/Users/example/go'
$ go get -v go.etcd.io/etcd
$ go get -v go.etcd.io/etcd/etcdctl

1. 3.启动服务
$ ./bin/etcd
$ $GOPATH/bin/etcd

1. 4.简单使用
$ ./bin/etcdctl put foo bar
OK
使用Etcd服务入门指南
  • [2] 部署单机单服务(静态)
##################################
1. Running etcd in standalone mode
##################################

1. 1.设置启动的Node地址
$ export NODE1='172.16.176.52'

1. 2.创建一个逻辑存储
$ docker volume create --name etcd-data

1. 3.启动etcd服务
1. 正式的ectd端口是2379用于客户端连接,而2380用于伙伴通讯
1. --data-dir: 到数据目录的路径
1. --initial-advertise-peer-urls: 集群中节点间通讯的URL地址
1. --listen-peer-urls: 集群中节点间通讯的URL地址
1. --advertise-client-urls: 客户端监听的URL地址
1. --listen-client-urls: 客户端监听的URL地址
1. --initial-cluster: 启动初始化集群配置
$ docker run -p 2379:2379 -p 2380:2380 --name etcd 
    --volume=etcd-data:/etcd-data 
    quay.io/coreos/etcd:latest 
    /usr/local/bin/etcd 
    --data-dir=/etcd-data --name node1 
    --initial-advertise-peer-urls http://${NODE1}:2380 
    --listen-peer-urls http://0.0.0.0:2380 
    --advertise-client-urls http://${NODE1}:2379 
    --listen-client-urls http://0.0.0.0:2379 
    --initial-cluster node1=http://${NODE1}:2380

1. 4.列出现在集群中的服务状态
$ etcdctl --endpoints=http://${NODE1}:2379 member list
使用Etcd服务入门指南
  • [3] 部署分布式集群服务(静态)
################################
1. Running a 3 node etcd cluster
################################

1. node1
docker run -p 2379:2379 -p 2380:2380 --name etcd-node-1 
  --volume=/var/lib/etcd:/etcd-data 
  quay.io/coreos/etcd:latest 
  /usr/local/bin/etcd 
  --data-dir=/etcd-data 
  --initial-advertise-peer-urls "http://10.20.30.1:2380" 
  --listen-peer-urls "http://0.0.0.0:2380" 
  --advertise-client-urls "http://10.20.30.1:2379" 
  --listen-client-urls "http://0.0.0.0:2379" 
  --initial-cluster "etcd-node-1=http://10.20.30.1:2380, etcd-node-2=http://10.20.30.2:2380, etcd-node-3=http://10.20.30.3:2380" 
  --initial-cluster-state "new" 
  --initial-cluster-token "my-etcd-token"

1. node2
docker run -p 2379:2379 -p 2380:2380 --name etcd-node-2 
  --volume=/var/lib/etcd:/etcd-data 
  quay.io/coreos/etcd:latest 
  /usr/local/bin/etcd 
  --data-dir=/etcd-data 
  --initial-advertise-peer-urls "http://10.20.30.2:2380" 
  --listen-peer-urls "http://0.0.0.0:2380" 
  --advertise-client-urls "http://10.20.30.2:2379" 
  --listen-client-urls "http://0.0.0.0:2379" 
  --initial-cluster "etcd-node-1=http://10.20.30.1:2380, etcd-node-2=http://10.20.30.2:2380, etcd-node-3=http://10.20.30.3:2380" 
  --initial-cluster-state "new" 
  --initial-cluster-token "my-etcd-token"

1. node3
docker run -p 2379:2379 -p 2380:2380 --name etcd-node-3 
  --volume=/var/lib/etcd:/etcd-data 
  quay.io/coreos/etcd:latest 
  /usr/local/bin/etcd 
  --data-dir=/etcd-data 
  --initial-advertise-peer-urls "http://10.20.30.3:2380" 
  --listen-peer-urls "http://0.0.0.0:2380" 
  --advertise-client-urls "http://10.20.30.3:2379" 
  --listen-client-urls "http://0.0.0.0:2379" 
  --initial-cluster "etcd-node-1=http://10.20.30.1:2380, etcd-node-2=http://10.20.30.2:2380, etcd-node-3=http://10.20.30.3:2380" 
  --initial-cluster-state "new" 
  --initial-cluster-token "my-etcd-token"

1. run etcdctl using API version 3
docker exec etcd /bin/sh -c "export ETCDCTL_API=3 && /usr/local/bin/etcdctl put foo bar"
  • [4] 部署分布式集群服务
# 编辑docker-compose.yml文件
version: "3.6"

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 -d

1. 之后使用如下命令登录到任一节点测试etcd集群
docker exec -it node1 bash

1. etcdctl member list
422a74f03b622fef, started, node1, http://172.16.238.100:2380, http://172.16.238.100:2379
ed635d2a2dbef43d, started, node2, http://172.16.238.101:2380, http://172.16.238.101:2379
daf3fd52e3583ffe, started, node3, http://172.16.238.102:2380, http://172.16.238.102:2379
  • [5] etcd 常用配置参数
配置参数 参数说明
--name 指定节点名称
--data-dir 指定节点的数据存储目录,用于保存日志和快照
--addr 公布的 IP 地址和端口;默认为 127.0.0.1:2379
--bind-addr 用于客户端连接的监听地址;默认为–addr 配置
--peers 集群成员逗号分隔的列表;例如 127.0.0.1:2380,127.0.0.1:2381
--peer-addr 集群服务通讯的公布的 IP 地址;默认为 127.0.0.1:2380
-peer-bind-addr 集群服务通讯的监听地址;默认为-peer-addr 配置
--wal-dir 指定节点的 wal 文件的存储目录,若指定了该参数 wal 文件会和其他数据文件分开存储
--listen-client-urls 监听 URL;用于与客户端通讯
--listen-peer-urls 监听 URL;用于与其他节点通讯
--initial-advertise-peer-urls 告知集群其他节点 URL
--advertise-client-urls 告知客户端 URL
--initial-cluster-token 集群的 ID
--initial-cluster 集群中所有节点
--initial-cluster-state new 表示从无到有搭建 etcd 集群
--discovery-srv 用于 DNS 动态服务发现,指定 DNS SRV 域名
--discovery 用于 etcd 动态发现,指定 etcd 发现服务的 URL

2. 数据存储

etcd 的数据存储有点像 PG 数据库的存储方式

etcd 目前支持 V2 和 V3 两个大版本,这两个版本在实现上有比较大的不同,一方面是对外提供接口的方式,另一方面就是底层的存储引擎,V2 版本的实例是一个纯内存的实现,所有的数据都没有存储在磁盘上,而 V3 版本的实例就支持了数据的持久化。

使用Etcd服务入门指南

我们都知道 etcd 为我们提供了 key/value 的服务目录存储。

# 设置键值对
$ etcdctl set name escape

1. 获取方式
$ etcdctl get name
escape

使用 etcd 之后,我们会疑问数据都存储到的那里呢?数据默认会存放在 /var/lib/etcd/default/ 目录。我们会发现数据所在的目录,会被分为两个文件夹中,分别是 snap 和 wal目录。

  • snap
    • 存放快照数据,存储etcd的数据状态
    • etcd防止WAL文件过多而设置的快照
  • wal
    • 存放预写式日志
    • 最大的作用是记录了整个数据变化的全部历程
    • 在etcd中,所有数据的修改在提交前都要先写入到WAL中