简介
etcd是一个开源的用于配置共享和服务发现的高性能的键值存储系统,由CoreOS团队开发,也是CoreOS的核心组件,负责CoreOS的自动安全更新、容器相关的覆盖网络设置等功能。
利用 etcd 的特性,应用程序可以在集群中共享信息、配置或作服务发现,etcd 会在集群的各个节点中复制这些数据并保证这些数据始终正确。
etcd 无论是在 CoreOS 还是 Kubernetes 体系中都是不可或缺的一环。
本文介绍etcd的安装和集群搭建,供初次使用者上手。
安装和基本用法
etcd代码在github上维护,Github地址 直接在relase页面找到想安装的版本的二进制包,下载地址
安装和启动etcd
curl -L https://github.com/coreos/etcd/releases/download/v2.2.2/etcd-v2.2.2-linux-amd64.tar.gz -o etcd-v2.2.2-linux-amd64.tar.gz
tar xzvf etcd-v2.2.2-linux-amd64.tar.gz
cd etcd-v2.2.2-linux-amd64
# 使用默认配置启动etcd
./etcd
# 可以看到启动log,服务监听了如下4个端口
2015-12-24 07:34:49.094992 I | etcdmain: listening for peers on http://localhost:2380
2015-12-24 07:34:49.095038 I | etcdmain: listening for peers on http://localhost:7001
2015-12-24 07:34:49.095078 I | etcdmain: listening for client requests on http://localhost:2379
2015-12-24 07:34:49.095116 I | etcdmain: listening for client requests on http://localhost:4001
使用etcdctl来测试etcd
./etcdctl set test_key "the value is for testing."
./etcdctl get test_key
也可以使用curl直接发送http请求,来测试etcd的接口
curl -L http://127.0.0.1:4001/version
{"etcdserver":"2.2.2","etcdcluster":"2.2.0"}
curl -L http://127.0.0.1:4001/v2/keys/test_key -XPUT -d value="changed by curl"
{"action":"set","node":{"key":"/test_key","value":"changed by curl","modifiedIndex":5,"createdIndex":5},"prevNode":{"key":"/test_key","value":"the value is for testing.","modifiedIndex":4,"createdIndex":4}}
curl -L http://127.0.0.1:4001/v2/keys/test_key
{"action":"get","node":{"key":"/test_key","value":"changed by curl","modifiedIndex":5,"createdIndex":5}}
搭建etcd集群
首先,找三台服务器,可以购买云主机,或本地启动虚拟机。 ip如下,注意iptables等防火墙设置,测试网络互通。 infra0 192.168.1.116 infra1 192.168.1.118 infra2 192.168.1.117
Static方式搭建etcd集群
在192.168.1.116这台机器上
./etcd -name infra0 -initial-advertise-peer-urls http://192.168.1.116:2380 \
-listen-peer-urls http://192.168.1.116:2380 \
-listen-client-urls http://192.168.1.116:2379,http://127.0.0.1:2379 \
-advertise-client-urls http://192.168.1.116:2379 \
-initial-cluster-token etcd-cluster-1 \
-initial-cluster infra0=http://192.168.1.116:2380,infra1=http://192.168.1.118:2380,infra2=http://192.168.1.117:2380 \
-initial-cluster-state new
# log
etcdserver: published {Name:infra0 ClientURLs:[http://192.168.1.116:2379]} to cluster f075b34d7da81569
在192.168.1.118这台机器上
./etcd -name infra1 -initial-advertise-peer-urls http://192.168.1.118:2380 \
-listen-peer-urls http://192.168.1.118:2380 \
-listen-client-urls http://192.168.1.118:2379,http://127.0.0.1:2379 \
-advertise-client-urls http://192.168.1.118:2379 \
-initial-cluster-token etcd-cluster-1 \
-initial-cluster infra0=http://192.168.1.116:2380,infra1=http://192.168.1.118:2380,infra2=http://192.168.1.117:2380 \
-initial-cluster-state new
# log
etcdserver: published {Name:infra1 ClientURLs:[http://192.168.1.118:2379]} to cluster f075b34d7da81569
在192.168.1.117这台机器上
./etcd -name infra2 -initial-advertise-peer-urls http://192.168.1.117:2380 \
-listen-peer-urls http://192.168.1.117:2380 \
-listen-client-urls http://192.168.1.117:2379,http://127.0.0.1:2379 \
-advertise-client-urls http://192.168.1.117:2379 \
-initial-cluster-token etcd-cluster-1 \
-initial-cluster infra0=http://192.168.1.116:2380,infra1=http://192.168.1.118:2380,infra2=http://192.168.1.117:2380 \
-initial-cluster-state new
# log
etcdserver: published {Name:infra2 ClientURLs:[http://192.168.1.117:2379]} to cluster f075b34d7da81569
测试:
# 在192.168.1.116节点写入key
curl -L http://192.168.1.116:2379/v2/keys/cluster_test -XPUT -d value="for cluster"
{"action":"set","node":{"key":"/cluster_test","value":"for cluster","modifiedIndex":9,"createdIndex":9}}
# 在其他节点读到key
curl -L http://192.168.1.118:2379/v2/keys/cluster_test
{"action":"get","node":{"key":"/cluster_test","value":"for cluster","modifiedIndex":9,"createdIndex":9}}
总结
本文讲述了etcd的安装方法和基本使用,还包括etcd集群搭建方法等,让读者简明的了解etcd这个软件的基本用法。