Etcd 做服务器配置数据源

安装 Etcd

我使用的是 Ubuntu, 当然 CentOS 或者 CoreOS 也是一样, 直接去 Github 下载编译好的程序解压缩直接运行.

下载地址: https://github.com/coreos/etcd/releases/

如果想要最新的版本, 那么需要使用源代码 https://github.com/coreos/etcd 进行自编译.

最新版参考文档: https://coreos.com/etcd/docs/latest/

使用 Etcd 快速开始

快速开始脚本, 由于我是在一台机器上进行测试, 所以对配置稍有改动:

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#!/bin/bash

TOKEN=token-01
CLUSTER_STATE=new
NAME_1=machine-1
NAME_2=machine-2
NAME_3=machine-3
HOST_1=www.xxx.com
HOST_2=www.xxx.com
HOST_3=www.xxx.com
CLUSTER=${NAME_1}=http://${HOST_1}:2380,${NAME_2}=http://${HOST_2}:2381,${NAME_3}=http://${HOST_3}:2382

# For machine 1
THIS_NAME=${NAME_1}
THIS_IP=${HOST_1}
etcd --data-dir=data.etcd.2380 --name ${THIS_NAME} \
--initial-advertise-peer-urls http://${THIS_IP}:2380 --listen-peer-urls http://${THIS_IP}:2380 \
--advertise-client-urls http://${THIS_IP}:2280 --listen-client-urls http://${THIS_IP}:2280 \
--initial-cluster ${CLUSTER} \
--initial-cluster-state ${CLUSTER_STATE} --initial-cluster-token ${TOKEN} >> 2380.log 2>&1 &

# For machine 2
THIS_NAME=${NAME_2}
THIS_IP=${HOST_2}
etcd --data-dir=data.etcd.2381 --name ${THIS_NAME} \
--initial-advertise-peer-urls http://${THIS_IP}:2381 --listen-peer-urls http://${THIS_IP}:2381 \
--advertise-client-urls http://${THIS_IP}:2281 --listen-client-urls http://${THIS_IP}:2281 \
--initial-cluster ${CLUSTER} \
--initial-cluster-state ${CLUSTER_STATE} --initial-cluster-token ${TOKEN} >> 2381.log 2>&1 &

# For machine 3
THIS_NAME=${NAME_3}
THIS_IP=${HOST_3}
etcd --data-dir=data.etcd.2382 --name ${THIS_NAME} \
--initial-advertise-peer-urls http://${THIS_IP}:2382 --listen-peer-urls http://${THIS_IP}:2382 \
--advertise-client-urls http://${THIS_IP}:2282 --listen-client-urls http://${THIS_IP}:2282 \
--initial-cluster ${CLUSTER} \
--initial-cluster-state ${CLUSTER_STATE} --initial-cluster-token ${TOKEN} >> 2382.log 2>&1 &

ps -ef | grep etcd | grep -v grep

批量结束脚本:

1
2
3
4
5
#!/bin/bash

ps -ef | grep etcd | grep -v grep | awk '{print $2}' | xargs -I{} kill -9 {}

ps -ef | grep etcd

shell 的简单交互:

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
26
27
28
29
30
31
32
33
34
35
36
37
38
#!/bin/bash

export ETCDCTL_API=3
HOST_1=www.xxx.com
HOST_2=www.xxx.com
HOST_3=www.xxx.com
ENDPOINTS=$HOST_1:2280,$HOST_2:2281,$HOST_3:2282

echo "============DBA:============"

etcdctl --endpoints=$ENDPOINTS member list
etcdctl --endpoints=$ENDPOINTS endpoint status
etcdctl --endpoints=$ENDPOINTS endpoint health

etcdctl --endpoints=$ENDPOINTS snapshot save etcd-snapshot.db
etcdctl --write-out=table --endpoints=$ENDPOINTS snapshot status etcd-snapshot.db

echo "==========Access:==========="

etcdctl --endpoints=$ENDPOINTS put foo "Hello World!"
etcdctl --endpoints=$ENDPOINTS get foo
etcdctl --endpoints=$ENDPOINTS --write-out="json" get foo

echo "==========Prefix:==========="

etcdctl --endpoints=$ENDPOINTS put web1 value1
etcdctl --endpoints=$ENDPOINTS put web2 value2
etcdctl --endpoints=$ENDPOINTS put web3 value3
etcdctl --endpoints=$ENDPOINTS get web --prefix

echo "==========Delete:==========="

etcdctl --endpoints=$ENDPOINTS put key myvalue
etcdctl --endpoints=$ENDPOINTS del key

etcdctl --endpoints=$ENDPOINTS put k1 value1
etcdctl --endpoints=$ENDPOINTS put k2 value2
etcdctl --endpoints=$ENDPOINTS del k --prefix
Donate - Support to make this site better.
捐助 - 支持我让我做得更好.