etcd基础介绍

860 阅读3分钟

etcd基础介绍

1.简介

2.安装

2.1 单机版

#安装epel源,安装etcd
yum install epel etcd

cat  /usr/lib/systemd/system/etcd.service
mkdir /data1/etcd2379
chown -R etcd:etcd /data1/etcd2379
cat /etc/etcd/etcd.conf
 
 
#配置
cat /etc/etcd/etcd.conf |grep -v '#'
ETCD_DATA_DIR="/data1/etcd2379"
ETCD_LISTEN_PEER_URLS="http://192.168.0.3:2380,http://localhost:2380"
ETCD_LISTEN_CLIENT_URLS="http://192.168.0.3:2379,http://127.0.0.1:2379"
ETCD_NAME="etcd63"
ETCD_ADVERTISE_CLIENT_URLS="http://192.168.0.3:2379"
  
启动:
systemctl start etcd
 
 
测试:
ps aux |grep etcd
etcd      4019  1.1  0.0 11507680 21160 ?      Ssl  15:40   0:00 /usr/bin/etcd --name=etcd63 --data-dir=/data1/etcd2379 --listen-client-urls=http://192.168.0.3:2379,http://127.0.0.1:2379
  
etcdctl member list
8e9e05c52164694d: name=etcd63 peerURLs=http://localhost:2380 clientURLs=http://192.168.0.3:2379 isLeader=true
  
systemctl status etcd.service
● etcd.service - Etcd Server
   Loaded: loaded (/usr/lib/systemd/system/etcd.service; disabled; vendor preset: disabled)
   Active: active (running) since Fri 2019-09-06 15:40:36 CST; 1min 17s ago
 Main PID: 4019 (etcd)
   Memory: 12.8M
   CGroup: /system.slice/etcd.service
           └─4019 /usr/bin/etcd --name=etcd63 --data-dir=/data1/etcd2379 --listen-client-urls=http://192.168.0.3:2379,http://127.0.0.1:2379
 
 
tree /data1/etcd2379/
/data1/etcd2379/
└── member
    ├── snap
    │   └── db
    └── wal
        ├── 0000000000000000-0000000000000000.wal
        └── 0.tmp
  
#测试
curl -L http://127.0.0.1:2379/health
{"health":"true"}
  
etcdctl cluster-health
member 8e9e05c52164694d is healthy: got healthy result from http://192.168.0.3:2379
cluster is healthy

2.2 集群版

#安装epel源,安装etcd
yum install epel etcd
  
systemctl enable etcd.service
cat  /usr/lib/systemd/system/etcd.service
  
建目录:
mkdir /data1/etcd2379
chown -R etcd:etcd /data1/etcd2379
  
配置host文件
vim /etc/hosts
192.168.94.60 etcd-094060
192.168.94.61 etcd-094061
192.168.94.62 etcd-094062
 
 
配置etcd文件
cat /etc/etcd/etcd.conf  |grep -v '#'
ETCD_DATA_DIR="/data1/etcd2379"
ETCD_LISTEN_PEER_URLS="http://192.168.94.60:2380"
ETCD_LISTEN_CLIENT_URLS="http://192.168.94.60:2379,http://127.0.0.1:2379"
ETCD_NAME="etcd-094060"
ETCD_INITIAL_ADVERTISE_PEER_URLS="http://192.168.94.60:2380"
ETCD_ADVERTISE_CLIENT_URLS="http://192.168.94.60:2379"
ETCD_INITIAL_CLUSTER="etcd-094060=http://192.168.94.60:2380,etcd-094061=http://192.168.94.61:2380,etcd-094062=http://192.168.94.62:2380"
ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster-60"
ETCD_INITIAL_CLUSTER_STATE="new"
  
  
启动/停止
systemctl start etcd #60,61,62 三台,至少启动2台才行。
#
systemctl stop etcd.service
systemctl start etcd.service
systemctl status etcd.service
  
##测试
etcdctl member list
3814a239d889ff53: name=etcd-094061 peerURLs=http://192.168.94.61:2380 clientURLs=http://192.168.94.61:2379 isLeader=false
7a02e6bc420705de: name=etcd-094062 peerURLs=http://192.168.94.62:2380 clientURLs=http://192.168.94.62:2379 isLeader=false
9023ca76e4e422ea: name=etcd-094060 peerURLs=http://192.168.94.60:2380 clientURLs=http://192.168.94.60:2379 isLeader=true
  
ETCDCTL_API=2 etcdctl cluster-health
ETCDCTL_API=2  /bin/etcdctl ls
ETCDCTL_API=3 etcdctl get foo
ENDPOINTS=http://192.168.94.60:2379,http://192.168.94.61:2379,http://192.168.94.62:2379
etcdctl --endpoints=$ENDPOINTS  member list
etcdctl member list
etcdctl user list
etcdctl put foo 'hello'
ETCDCTL_API=3 etcdctl --write-out=table  endpoint status
ETCDCTL_API=3 etcdctl --endpoints=http://192.168.94.60:2379 endpoint health

启动:

$ 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 \
  --initial-cluster-token etcd-cluster-1 \
  --initial-cluster infra0=http://10.0.1.10:2380,infra1=http://10.0.1.11:2380,infra2=http://10.0.1.12:2380 \
  --initial-cluster-state new
  
/usr/bin/etcd --name=etcd-094060 --data-dir=/data1/etcd2379 --listen-client-urls=http://192.168.94.60:2379,http://127.0.0.1:2379
启动参数说明如下:
  --name:节点的名称,唯一标识这个节点;
  --data-dir:存储数据的目录,当没有指定wal-dir的时候,日志文件也会存储在这个目录下,主要用来存储节点的信息和集群的信息,还有快照文件;
  --initial-advertise-peer-urls:告知集群其他节点的这个成员的url信息,可以有多个;
  --listen-peer-urls:监听其他节点的url,主要用来和其他的节点进行通信;
  --listen-client-urls:监听客户端的url,主要用来接受客户端的信息;
  --advertise-client-urls:对外的监听的客户端的url,用来接受客户端的信息,不过是public的;
  --initial-cluster-tocken:集群启动的时候的tocken信息,相当于集群id;
  --initial-cluster:集群启动时候的配置信息(也就是集群的所有member节点);
  --initial-cluster-state:初始化的时候集群的状态(new或者existing)

3. etcd运维

3.1 常用命令

etcd -version
etcd Version: 3.3.11
etcdctl --help
systemctl status etcd
 
export ETCDCTL_ENDPOINT=http://192.168.94.60:2379,http://192.168.94.61:2379,http://192.168.94.62:2379
ETCDCTL_API=2  etcdctl member list
 
curl -L http://127.0.0.1:2379/health
etcdctl cluster-health
  
curl http://127.0.0.1:2379/config/local/log -XPUT -d '{"Level":"DEBUG"}'
$ curl http://127.0.0.1:2379/config/local/log -XPUT -d '{"Level":"INFO"}'
# debug logging disabled

3.2 etcdctl

etcdctl属于ETCD的客户端,在使用etcdctl时,必须在命令开头指明所使用的API版本。

etcd2和etcd3是不兼容的,两者的api参数也不一样,详细请查看 etcdctl -h 。可以使用api2 和 api3 写入 etcd3 数据,但是需要注意,使用不同的api版本写入数据需要使用相应的api版本读取数据。

#apiv2 缺省使用  ETCDCTL_API=2
ETCDCTL_API=2 etcdctl ls /
#apiv3
ETCDCTL_API=3 etcdctl get /
ETCDCTL_API=3 etcdctl get --from-key '' #查询全部
## 通过环境变量 ETCDCTL_API 设置为版本3
export ETCDCTL_API=3
ETCDCTL_API=3 etcdctl put key1 "A"  //返回OK表示添加key1:'A'键值对成功
etcdctl --endpoints=http://192.168.0.3:2379 put foo 'hello'
etcdctl --write-out=table  endpoint status
etcdctl --endpoints=http://192.168.0.3:2379 endpoint health
## 写入键(这是设置键 foo 的值为 bar 的命令:)
etcdctl put foo bar
etcdctl put foo1 bar
etcdctl put foo2 bar
etcdctl put foo3 bar
 
## 读取所有键、读取一个键、读取 foo to foo3 的键
etcdctl get *
etcdctl get foo
etcdctl get foo foo3
 
## 读取所有以`foo`开头的键、只显示前两个
etcdctl get --prefix foo
etcdctl get --prefix --limit=2 foo
 
## 删除键
etcdctl del foo
etcdctl del foo foo9
etcdctl del --prev-kv zoo
 
## 观察键的变化、观察 foo to foo9范围内键的变化、观察前缀为 foo 的键
etcdctl watch foo
etcdctl watch foo foo9
etcdctl watch --prefix foo
 
## 观察多个键 foo 和 zoo 的命令
etcdctl watch -i
watch foo
watch zoo
 
## 从修订版本 2 开始观察键 `foo` 的改动
etcdctl watch --rev=2 foo
 
## 在键 `foo` 上观察变更并返回被修改的值和上个修订版本的值
etcdctl watch --prev-kv foo
 
## 压缩修订版本(在压缩修订版本之前的任何修订版本都不可访问)
etcdctl compact 5
etcdctl get --rev=4 foo     ## 会报错
etcdctl get --rev=4 foo     ## 可以使用,但是为空,待测试
etcdctl watch --rev=4 foo   ## 会报错
etcdctl watch --rev=5 foo   ## 可以使用

3.3 成员管理

   member add          Adds a member into the cluster
    member remove       Removes a member from the cluster
    member update       Updates a member in the cluster
    member list         Lists all members in the cluster
 
etcdctl --endpoints=http://192.168.0.3:2379 member list -w table
+------------------+---------+--------+-----------------------+--------------------------+
|        ID        | STATUS  |  NAME  |      PEER ADDRS       |       CLIENT ADDRS       |
+------------------+---------+--------+-----------------------+--------------------------+
| 8e9e05c52164694d | started | etcd63 | http://localhost:2380 | http://192.168.0.3:2379 |
+------------------+---------+--------+-----------------------+--------------------------+
 
原文链接:https://blog.csdn.net/huwh_/article/details/80225902

3.4 查看日志

sudo systemctl status etcd.service -l --no-pager
sudo journalctl -u etcd.service -l --no-pager|less
sudo journalctl -f -u etcd.service

3.5 Watch

ETCDCTL_API=2  etcdctl watch foo   #watch 键值发生更新,就会输出最新的值并退出
ETCDCTL_API=2 etcdctl exec-watch /foo -- /bin/ls /root  #一旦键值发生更新,就执行给定命令
ETCDCTL_API=2 etcdctl exec-watch /foo -- /bin/echo 'hello etcd'

3.6 lease

etcd可以为key设置超时时间,但与redis不同,etcd需要先创建lease,然后使用put命令加上参数–lease=

ETCDCTL_API=3 etcdctl lease -h
ETCDCTL_API=3 etcdctl lease grant 60     #创建lease,返回lease ID ttl秒
ETCDCTL_API=3 etcdctl lease list
ETCDCTL_API=3 etcdctl lease revoke  leaseId   #删除lease,并删除所有关联的key
ETCDCTL_API=3 etcdctl lease keep-alive leaseId     keep-alive会不间断的刷新lease时间,从而保证lease不会过期。
ETCDCTL_API=3 etcdctl put testlease bar --lease=leaseId
ETCDCTL_API=3 etcdctl lease timetolive leaseId 取得lease的总时间和剩余时间
ETCDCTL_API=3 etcdctl get foo

3.7 REST API

etcd支持rest风格的接口,可直接利用curl直接与etcd交互.

curl http://127.0.0.1:2379/version //查看版本
etcdctl --endpoints=$ENDPOINTS put foo "Hello World!"
 //V2版本
 curl http://127.0.0.1:2379/v2/keys/hello -XPUT -d value="world" // 创建键值对(hello:"world")
 curl http://127.0.0.1:2379/v2/keys/hello //查看hello键的值
 curl http://127.0.0.1:2379/v2/keys/hello -X DELETE //删除hello键值对
 
//V3版本,注意在V3版本中所有的key和value都必须转换为base64编码然后才可以存储
// foo is 'Zm9v' in Base64
// bar is 'YmFy' in Base64
curl -L http://127.0.0.1:2379/v3beta/kv/put \
    -X POST -d '{"key": "Zm9v", "value": "YmFy"}'
// 创建键值对 foo:bar
 
curl -L http://127.0.0.1:2379/v3beta/kv/range \
    -X POST -d '{"key": "Zm9v"}'
 
// 查看键值对 foo

参考: