etcdv3使用

安装

下载etcdv3包

1
2
cd /usr/local/src/ 
wget https://github.com/coreos/etcd/releases/download/v3.2.15/etcd-v3.2.15-linux-amd64.tar.gz

解压

1
2
tar -zxvf etcd-v3.2.15-linux-amd64.tar.gz
mv etcd-v3.2.15-linux-amd64 etcd

添加v3环境变量

将代码路径加入环境变量中,并使用etcdv3的api接口

1
2
3
vim /etc/profile
export PATH=/usr/local/src/etcd:$PATH
export ETCDCTL_API=3

生效 source /etc/profile

简易启动local standlone cluster

参考:https://coreos.com/etcd/docs/latest/dev-guide/local_cluster.html

切换到:/usr/local/src/etc 目录直接启动etcd服务进程

1
2
3
4
5
6
7
8
9
root@ubuntu:/usr/local/src/etcd# ./etcd&
[1] 19228
root@ubuntu:/usr/local/src/etcd# 2018-03-07 13:40:16.719105 I | etcdmain: etcd Version: 3.2.15
2018-03-07 13:40:16.719217 I | etcdmain: Git SHA: 1b3ac99e8
2018-03-07 13:40:16.719235 I | etcdmain: Go Version: go1.8.5
2018-03-07 13:40:16.719244 I | etcdmain: Go OS/Arch: linux/amd64
2018-03-07 13:40:16.719254 I | etcdmain: setting maximum number of CPUs to 4, total number of available CPUs is 4
2018-03-07 13:40:16.719274 W | etcdmain: no data-dir provided, using default data-dir ./default.etcd
2018-03-07 13:40:16.719422 N | etcdmain: the server is already initialized as

判别是否启动成功

查看 2379 和 2380 两个端口服务都已经启动成功

etcdctl操作etcd存储

额外说明:etcdctl get “” –from-key 是查看所有数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
root@ubuntu:~# etcdctl  del f
1
root@ubuntu:~# etcdctl get "" --from-key
fo
xxx
foo
bar
root@ubuntu:~# etcdctl put q qq
OK
root@ubuntu:~# etcdctl get "" --from-key
fo
xxx
foo
bar
q
qq
root@ubuntu:~# etcdctl put q qqq
OK
root@ubuntu:~# etcdctl get "" --from-key
fo
xxx
foo
bar
q
qqq

python API命令操作etcd

安装api库

1
pip install etcd3gw

操作方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import etcd3gw

e3gw = etcd3gw.client('127.0.0.1', 2379, ca_cert=None,
cert_cert=None, timeout=None)
# add one key value
e3gw.create('sdn', '4')

# delete one key value
e3gw.delete('sdn')

# replace one key value
e3gw.replace('sdn', '4', '40')

# get one key's value
print e3gw.get('sdn')

# get all
print e3gw.get_all()