PS:最近看凉宫春日的忧郁,无语中
建议入门的童鞋最好阅读这篇:http://www.infoq.com/cn/articles/etcd-interpretation-application-scenario-implement-principle
折腾了好一段时间的etcd,主要是打算用它当任务缓存和分布式协调服务,但我想,可能在agent节点还是要引入redis
先是安装吧:
github地址:https://github.com/coreos/etcd
版本信息和下载链接:https://github.com/coreos/etcd/releases
因为我用的是最新的etcd v2.1.0-alpha.0,所以API和2.0以前的有差别。
先下载相关的包,解压即可使用(懒得编译):
curl -L https://github.com/coreos/etcd/releases/download/v2.1.0-alpha.0/etcd-v2.1.0-alpha.0-linux-amd64.tar.gz -o etcd-v2.1.0-alpha.0-linux-amd64.tar.gz tar xzvf etcd-v2.1.0-alpha.0-linux-amd64.tar.gz cd etcd-v2.1.0-alpha.0-linux-amd64 ./etcd #启动etcd,可以按ctrl+c退出
进行简单的测试,在运行etcd的机器上再开一个终端:
./etcdctl set mykey "this is awesome" ./etcdctl get mykey #输出this is awesome
这样就代表etcd运行成功。
etcd的数据文件主要是放在本地(etcd程序所在的那个文件夹中,如果你想重头使用(抛弃旧数据),只要把"XXX.etcd"这样的目录删掉即可
简单的API测试可以看这篇文章:https://github.com/coreos/etcd/blob/master/Documentation/api.md
我只记录句比较重要的话:
Keys that are under this directory work as usual, but when the directory expires, a watcher on a key under the directory will get an expire event
因为之前研究flask RESTful,所以也就不怎么陌生了,如果你觉得它返回的JSON数据很乱,你可以下载一个叫jq的软件(具体用法见官方文档):
wget http://stedolan.github.io/jq/download/linux64/jq chmod +x jq
效果很显著:(这是没有处理过的JSON数据)
root@workgroup0:~# curl http://127.0.0.1:2379/v2/keys/name -XPUT -d value='Hello Cluster' {"action":"set","node":{"key":"/name","value":"Hello Cluster","modifiedIndex":55,"createdIndex":55}} root@workgroup0:~# curl http://127.0.0.1:2379/v2/keys/name {"action":"get","node":{"key":"/name","value":"Hello Cluster","modifiedIndex":55,"createdIndex":55}}
使用jq:
root@workgroup0:~# ls autoinj.py creds etcddir ldocker.sh ovs_launch cdocker.sh default.etcd infra0.etcd openvswitch-2.3.0 pipework celeryapp docker_launch.py jq openvswitch-2.3.0.tar.gz util-linux-2.24 root@workgroup0:~# jq The program 'jq' is currently not installed. You can install it by typing: apt-get install jq root@workgroup0:~# curl http://127.0.0.1:2379/v2/keys/name | ./jq '.' % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 101 100 101 0 0 9973 0 --:--:-- --:--:-- --:--:-- 11222 { "action": "get", "node": { "key": "/name", "value": "Hello Cluster", "modifiedIndex": 55, "createdIndex": 55 } }
或者使用在线json美化工具:http://web.chacuo.net/formatjson/
接下来是构建集群:
构建一个etcd集群,至少需要三个节点(可以参考下这篇:http://www.netfoucs.com/article/jiq408694711/94875.html,但他起动集群的命令并不适合2.1.0)
启动集群的方式有三种,一种是直接使用机器的ip地址,通过环境变量或命令行参数启动集群(我用的就是这种),另外有种基于现有集群让etcd自发现的方式,还有一种是基于DNS的方案
官方文档:https://github.com/coreos/etcd/blob/master/Documentation/clustering.md(包含从0.4版迁移到2.0的指导,在页底)
文档中这些内容不是一定要的,如果你配置使用命令行参数的话:
ETCD_INITIAL_CLUSTER="infra0=http://10.0.1.10:2380,infra1=http://10.0.1.11:2380,infra2=http://10.0.1.12:2380" ETCD_INITIAL_CLUSTER_STATE=new
我得先在三台机器上下载好etcd,然后为了方便,修改了PATH,把etcd所在目录添加到环境变量中
在第一台机器(10.20.10.70):
etcd -name infra0 -initial-advertise-peer-urls http://10.20.10.70:2380 \ -listen-peer-urls http://10.20.10.70:2380 \ -initial-cluster-token etcd-cluster-1 \ -initial-cluster infra0=http://10.20.10.70:2380,infra1=http://10.20.10.71:2380,infra2=http://10.20.10.72:2380 \ -initial-cluster-state new
第二台(10.20.10.71):
etcd -name infra1 -initial-advertise-peer-urls http://10.20.10.71:2380 \ -listen-peer-urls http://10.20.10.71:2380 \ -initial-cluster-token etcd-cluster-1 \ -initial-cluster infra0=http://10.20.10.70:2380,infra1=http://10.20.10.71:2380,infra2=http://10.20.10.72:2380 \ -initial-cluster-state new
第三台(10.20.10.72):
etcd -name infra2 -initial-advertise-peer-urls http://10.20.10.72:2380 \ -listen-peer-urls http://10.20.10.72:2380 \ -initial-cluster-token etcd-cluster-1 \ -initial-cluster infra0=http://10.20.10.70:2380,infra1=http://10.20.10.71:2380,infra2=http://10.20.10.72:2380 \ -initial-cluster-state new
tips:在三台机器上都启动好etcd前,他会在前台不断输出信息,直到第三台的机器启动完成。
我随便在一台机器上(10.20.10.71)启动一个终端:
可以看到在我启动命令的目录中多了一个文件夹infra1.etcd:
root@workgroup1:~# ls autoinj.py etcddir openvswitch-2.3.0.tar.gz SQLAlchemy-0.9.9.tar.gz cdocker.sh infra1.etcd ovs_launch util-linux-2.24 default.etcd ldocker.sh pipework docker_launch.py openvswitch-2.3.0 SQLAlchemy-0.9.9
在其他机器上也有。
测试下数据是否真的全局一致:
root@workgroup1:~/etcddir/etcdcore# curl http://127.0.0.1:2379/v2/keys/hello -XPUT -d value=ok {"action":"set","node":{"key":"/hello","value":"ok","modifiedIndex":21,"createdIndex":21},"prevNode":{"key":"/hello","value":"ok","modifiedIndex":20,"createdIndex":20}} root@workgroup1:~/etcddir/etcdcore# curl http://127.0.0.1:2379/v2/keys/hello | ./jq '.' % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 91 100 91 0 0 10044 0 --:--:-- --:--:-- --:--:-- 11375 { "action": "get", "node": { "key": "/hello", "value": "ok", "modifiedIndex": 21, "createdIndex": 21 } } root@workgroup1:~/etcddir/etcdcore#
再在10.20.10.70上看看:
root@workgroup0:~# curl http://127.0.0.1:2379/v2/keys/hello | ./jq '.' % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 91 100 91 0 0 4209 0 --:--:-- --:--:-- --:--:-- 4550 { "action": "get", "node": { "key": "/hello", "value": "ok", "modifiedIndex": 21, "createdIndex": 21 } } root@workgroup0:~#
可以看到数据在任何节点上都是一样的。但是因为我可以开的虚拟机数量有限,无法测试当一个节点宕机的情况
目前的测试就是这样,接下来要继续研究其他高级内容和python的etcd库
附上参考链接:
https://github.com/coreos/etcd/releases
http://www.netfoucs.com/search?q=etcd
http://www.csdn.net/article/2015-01-28/2823739/1
试验笔记:
下载和安装最新的etcd v2.1.0-alpha.0:
##
curl -L https://github.com/coreos/etcd/releases/download/v2.1.0-alpha.0/etcd-v2.1.0-alpha.0-linux-amd64.tar.gz -o etcd-v2.1.0-alpha.0-linux-amd64.tar.gz
tar xzvf etcd-v2.1.0-alpha.0-linux-amd64.tar.gz
cd etcd-v2.1.0-alpha.0-linux-amd64
./etcd
##
开另一个终端,进入etcd所在目录:
./etcdctl set mykey "this is awesome"
./etcdctl get mykey
可以按ctrl+c退出etcd
本文参考地址:
https://github.com/coreos/etcd/releases
在线json美化工具:http://web.chacuo.net/formatjson/
Keys that are under this directory work as usual, but when the directory expires, a watcher on a key under the directory will get an expire event
http://www.netfoucs.com/search?q=etcd
python中的json和base64处理
Python 2.7.6 (default, Nov 10 2013, 19:24:18) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> import json
>>> import base64
>>> dicdata = {'mission':'launch a docker contianer','args':{'machine':'compute-node1','memory':'2G'}}
>>> from print import pprint
SyntaxError: invalid syntax
>>> import print
SyntaxError: invalid syntax
>>> from pprint import pprint
>>> pprint dicdata
SyntaxError: invalid syntax
>>> pprint(dicdata)
{'args': {'machine': 'compute-node1', 'memory': '2G'},
'mission': 'launch a docker contianer'}
>>> jsondata = json.dumps(dicdata)
>>> jsondata
'{"args": {"machine": "compute-node1", "memory": "2G"}, "mission": "launch a docker contianer"}'
>>> encodedata = base64.b64encode(jsondata)
>>> encodedata
'eyJhcmdzIjogeyJtYWNoaW5lIjogImNvbXB1dGUtbm9kZTEiLCAibWVtb3J5IjogIjJHIn0sICJtaXNzaW9uIjogImxhdW5jaCBhIGRvY2tlciBjb250aWFuZXIifQ=='
>>> recover1 = base64.b64decode('eyJhcmdzIjogeyJtYWNoaW5lIjogImNvbXB1dGUtbm9kZTEiLCAibWVtb3J5IjogIjJHIn0sICJtaXNzaW9uIjogImxhdW5jaCBhIGRvY2tlciBjb250aWFuZXIifQ==')
>>> recover1
'{"args": {"machine": "compute-node1", "memory": "2G"}, "mission": "launch a docker contianer"}'
>>>
参见:http://www.hulufei.com/post/201004161735
http://blog.csdn.net/lxdcyh/article/details/4021476
Json格式化
在控制台输出的Json内容难以直接阅读的,相关的格式化方法很多,这里推荐一个控制台下的开源工具软件jq,下载地址是:http://stedolan.github.io/jq/download/。它其实是一个Json数据的处理器,使用C语言编写,支持Windows、Linux、Mac等平台,使用起来十分方便。格式化Json数据可参考下面的例子,对于更完整的使用方法,请参考jq的官方文档。
wget http://stedolan.github.io/jq/download/linux64/jq
chmod +x jq
curl -L http://127.0.0.1:4001/v2/keys/coreos.com?recursive=true| ./jq '.'
export ETCD_INITIAL_CLUSTER="infra0=http://10.20.10.70:2380,infra1=http://10.20.10.71:2380"
export ETCD_INITIAL_CLUSTER="infra0=http://10.20.10.70:2380,infra1=http://10.20.10.71:2380,infra2=http://10.20.10.72:2380"
export ETCD_INITIAL_CLUSTER_STATE=new
etcd -name infra0 -initial-advertise-peer-urls http://10.20.10.70:2380 \
-listen-peer-urls http://10.20.10.70:2380 \
-initial-cluster-token etcd-cluster-1 \
-initial-cluster infra0=http://10.20.10.70:2380,infra1=http://10.20.10.71:2380,infra2=http://10.20.10.72:2380 \
-initial-cluster-state new
etcd -name infra1 -initial-advertise-peer-urls http://10.20.10.71:2380 \
-listen-peer-urls http://10.20.10.71:2380 \
-initial-cluster-token etcd-cluster-1 \
-initial-cluster infra0=http://10.20.10.70:2380,infra1=http://10.20.10.71:2380,infra2=http://10.20.10.72:2380 \
-initial-cluster-state new
etcd -name infra2 -initial-advertise-peer-urls http://10.20.10.72:2380 \
-listen-peer-urls http://10.20.10.72:2380 \
-initial-cluster-token etcd-cluster-1 \
-initial-cluster infra0=http://10.20.10.70:2380,infra1=http://10.20.10.71:2380,infra2=http://10.20.10.72:2380 \
-initial-cluster-state new
http://www.csdn.net/article/2015-01-28/2823739/1