博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
etcd的学习心得和使用
阅读量:7176 次
发布时间:2019-06-29

本文共 7268 字,大约阅读时间需要 24 分钟。

1 etcd key-value 存储结构

 “A highly-available key value store for shared configuration and service discovery.”
  Etcd是coreos开发的分布式服务系统,内部采用raft协议作为一致性算法

raft通过选举的方式来实现一致性,在Raft中,任何一个节点都可能成为Leader

Etcd是一个高可用的 Key/Value 存储系统,主要用于分享配置和服务发现。 
● 简单:支持 curl 方式的用户 API (HTTP+JSON) 

Etcd构建自身高可用集群主要有三种形式:
    1)静态发现: 预先已知 Etcd 集群中有哪些节点,在启动时直接指定好Etcd的各个node节点地址
    2)Etcd动态发现: 通过已有的Etcd集群作为数据交互点,然后在扩展新的集群时实现通过已有集群进行服务发现的机制
    3)DNS动态发现: 通过DNS查询方式获取其他节点地址信息



2 下载地址

###################  演示etcd单机的使用

3. 安装(解压即可使用)

cd /usr/local/src/

wget 

tar xvf etcd-v3.2.5-linux-amd64.tar.gz

cp -r etcd-v3.2.5-linux-amd64 /usr/local/etcd  #将软件放置到常用目录下

4.创建配置文件和数据目录

cd /usr/local/etcd/

touch etcd.conf

mkdir data

## 将启动文件和命令管理文件拷贝到 PATH找到的路径中

cp etcd /usr/local/bin

cp etcdctl /usr/local/bin

5. 编辑配置文件

1
2
3
4
5
6
7
8
9
10
name: ops-cuidehua001
data-dir: "/usr/local/etcd/data"
#监听URL,用于与其他节点通讯 
listen-peer-urls: "http://10.59.87.121:2380"
  
# list of URLs to listen on for client traffic
listen-client-urls: "http://10.59.87.121:2379,http://127.0.0.1:2379"
 
#list of this member's client URLs to advertise to the public 
advertise-client-urls: "http://10.59.87.121:2379"

listen-peer-urls、listen-client-urls、advertise-client-urls的区别?

6、启动

etcd --config-file=/usr/local/etcd/etcd.conf &

7、检查是否启动

ps -ef |grep etcd

或者

[root@ops-cuidehua001 etcd]# etcdctl cluster-health

member 8e9e05c52164694d is healthy: got healthy result from http://10.59.87.121:2379

cluster is healthy

生成的了数据目录

[root@ops-cuidehua001 etcd]# ls data/member/

snap  wal

8、常用命令操作

#查看版本

etcdctl -version 或者  etcd -version

[root@ops-cuidehua001 etcd]# etcdctl -v

etcdctl version: 3.2.5

API version: 2

(可以查看到命令的版本和api接口的版本)

#查看检查状况

etcdctl cluster-health

##etcd主要是做存储k-v的作用,所以经常对其数据进行操作(即etcd的操作 增删改查)

数据库操作围绕对键值和目录的 CRUD (符合 REST 风格的一套操作:Create)完整生命周期的管理。

etcd 在键的组织上采用了层次化的空间结构(类似于文件系统中目录的概念),用户指定的键可以为单独的名字,如 testkey,此时实际上放在根目录 / 下面,也可以为指定目录结构,如 cluster1/node2/testkey,则将创建相应的目录结构。

注:CRUD 即 Create, Read, Update, Delete,是符合 REST 风格的一套 API 操作。

#################### ##数据库操作 #################

##set 指定某个键的值

etcdctl set /testdir/testkey "Hello world"

会创建目录/testdir 和设定这个目录下的testkey的value值为"Hello world"

##查看目录结构,默认是查看根目录下

[root@ops-cuidehua001 etcd]# etcdctl ls

/testdir

[root@ops-cuidehua001 etcd]# etcdctl ls /testdir

/testdir/testkey

##get 获取某个key的value

[root@ops-cuidehua001 etcd]#  etcdctl get /testdir/testkey

Hello world

#不支持则会报错,且key不支持通配符

[root@ops-cuidehua001 etcd]#  etcdctl get /testdir/testkey2

Error:  100: Key not found (/testdir/testkey2) [5]

##update更新建的值,key不存在时会报错

etcdctl update /testdir/testkey "hello"

##rm删除某个键

etcdctl rm /testdir/testkey

[root@ops-cuidehua001 etcd]# etcdctl rm /testdir/testkey

PrevNode.Value: hello

[root@ops-cuidehua001 etcd]# etcdctl get /testdir/testkey

Error:  100: Key not found (/testdir/testkey) [7]

当删除某个目录时

etcdctl rm -h 查看命令帮助

etcdctl rm -r 

[root@ops-cuidehua001 etcd]# etcdctl rm -r /testdir

## 创建目录mkdir setdir

etcdctl setdir /testdir

(mkdir 如果目录存在则会报错 setdir 也会报错,但是不一样? 待研究两者不同)

[root@ops-cuidehua001 etcd]# etcdctl setdir /testdir

Error:  102: Not a file (/testdir) [11]

###################### 非数据库操作 #################

# 备份etcd数据目录

[root@ops-cuidehua001 etcd]# du -sh data/

123Mdata/

[root@ops-cuidehua001 etcd]# etcdctl backup --data-dir=/usr/local/etcd/data/ --backup-dir=/tmp/data

[root@ops-cuidehua001 etcd]# du -sh /tmp/data/

62M/tmp/data/

(临时目录文件不会备份)

#watch 观察一个值的变化(只是针对key 不针对目录)

观察到变化后,打印值并watch退出

可以用选项: -f 

forever watch a key until CTRL+C

#exce-wathc 监听到值有变化,就执行指定的命令(且不退出执行的可以是shell命令)

etcdctl exec-watch /testdir/testkey -- sh -c 'pwd'

#member 集群用途将其他成员添加到cluster中或者从cluster中删除

[root@ops-cuidehua001 etcd]# etcdctl  member list

8e9e05c52164694d: name=ops-cuidehua001 peerURLs=http://localhost:2380 clientURLs=http://10.59.87.121:2379 isLeader=true

(因为是单点操作所以就一个节点)

#################### API 接口 ####

可以通过浏览器访问,或者通过curl命令增删改查

基本接口:

[root@ops-cuidehua001 etcd]#  curl -s http://10.59.87.121:2379/v2/keys/ | jq "."

1
2
3
4
5
6
7
8
9
10
11
12
13
14
{
  
"node": {
    
"nodes": [
      
{
        
"createdIndex": 4,
        
"modifiedIndex": 4,
        
"dir": true,
        
"key": "/testdir"
      
}
    
],
    
"dir": true
  
},
  
"action": "get"
}

可获取目录下的所有key-value和

[root@ops-cuizhiliang001 etcd]#  curl -s http://10.59.87.121:2379/v2/keys/testdir | jq "."

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
{
  
"node": {
    
"createdIndex": 4,
    
"modifiedIndex": 4,
    
"nodes": [
      
{
        
"createdIndex": 4,
        
"modifiedIndex": 12,
        
"value": "yes3",
        
"key": "/testdir/testkey"
      
},
      
{
        
"createdIndex": 10,
        
"modifiedIndex": 10,
        
"value": "yes3",
        
"key": "/testdir/testkey2"
      
}
    
],
    
"dir": true,
    
"key": "/testdir"
  
},
  
"action": "get"
}

## 删除指定的key,返回被删掉的内容(目录是不能删除的)

curl -s http://10.59.87.121:2379/v2/keys/testdir/testkey2 -XDELETE |jq "."

## set指定的值

curl -s http://10.59.87.121:2379/v2/keys/testdir/testkey3 -XPUT -d value="hello world" |jq "."

################### 配置集群  ###############

可以在初始知道的时候就加入,还可在后面在加入

目前支持三种发现方式:Static,etcd Discovery,DNS Discovery。 
● Static适用于有固定IP的主机节点 
● etcd Discovery适用于DHCP环境 
● DNS Discovery依赖DNS SRV记录 
这里我们采用Static方式,创建etcd0脚本,方便配置etcd启动参数

方法1: (参考)

可以在知道有哪些作为集群的时候,使用

● –initial-cluster-token 集群的ID 
● –initial-cluster 集群中所有节点 
● –initial-cluster-state 集群状态,new为新创建集群,existing为已存在的集群

(以下 只是作为知识说明,在此文章中并未使用到)

#初始化名称

INITIAL_CLUSTER_TOKEN=etcd_cluster_1

#初始化群集列表

INITIAL_CLUSTER="node1=http://10.59.72.221:2380,node2=http://10.59.72.191:2380,node3=http://10.59.72.192:2380"

#初始化状态

INITIAL_CLUSTER_STATE=new


方法2:(这里举例使用了这种方法 演示)

后面add的节点(比如从一台机器的集群  变成多台的集群)


和第一个节点一样搭建了如下第二个节点。

这里要将 这个第二个节点加入到 上面的第一个节点中去。

[root@ops-rpmbuild001 etcd]# etcdctl member list

8e9e05c52164694d: name=etcd-node-002 peerURLs=http://localhost:2380 clientURLs=http://10.59.87.11:2379 isLeader=true



(记住 一定要在配置中补充)  补充脚本

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
#!/bin/bash
#
#初始化Etcd
#
#ETCD 名称
ETCD_NAME=ops-cuidehua001
#ETCD存储目录 
ETCD_DATA_DIR=
/usr/local/etcd/data
#本机IP地址
LOCAL_IP=10.59.87.121
#初始化名称
INITIAL_CLUSTER_TOKEN=etcd_cluster_cuidehua
#初始化群集列表
INITIAL_CLUSTER=
"ops-cuizhiliang001=http://10.59.87.121:2380,etcd-node-002=http://10.59.87.11:2380"
#初始化状态
INITIAL_CLUSTER_STATE=new
#初始化
M1(){
    
#
    
[ -d ${ETCD_DATA_DIR} ] &&  
/bin/rm 
-rf ${ETCD_DATA_DIR} >
/dev/null 
2>&1 
    
#
    
/bin/mkdir 
${ETCD_DATA_DIR} >
/dev/null 
2>&1
    
/usr/local/etcd/etcd 
--name ${ETCD_NAME} --data-
dir 
${ETCD_DATA_DIR} \
        
--initial-advertise-peer-urls http:
//
${LOCAL_IP}:2380 \
        
--listen-peer-urls http:
//
${LOCAL_IP}:2380 \
        
--listen-client-urls http:
//
${LOCAL_IP}:2379,http:
//127
.0.0.1:2379 \
        
--advertise-client-urls http:
//
${LOCAL_IP}:2379 \
        
--initial-cluster-token ${INITIAL_CLUSTER_TOKEN} \
        
--initial-cluster ${INITIAL_CLUSTER} \
        
--initial-cluster-state ${INITIAL_CLUSTER_STATE}
}
M2(){
    
/usr/local/etcd/etcd 
--config-
file 
/usr/local/etcd/etcd
.conf 
}
M${1}

每台机器上执行

sh init.sh 1

sh init.sh 2 &>/dev/null &

查看:

# etcdctl member list

bfb265066811202: name=ops-cuidehua001 peerURLs=http://10.59.87.121:2380 clientURLs=http://10.59.87.121:2379 isLeader=false

4ee240ac8ec54efb: name=etcd-node-002 peerURLs=http://10.59.87.11:2380 clientURLs=http://10.59.87.11:2379 isLeader=true

## 集群成员操作(不管否是leader 都可以执行这个操作,执行完后,对方就从集群中踢出去了,在从新选举Leader)

etcdctl member remove bfb265066811202



增加成员时候,INITTAL_CLUSTER_STATE 一定要标记为

1
2
#初始化状态
INITIAL_CLUSTER_STATE=existing
本文转自残剑博客51CTO博客,原文链接http://blog.51cto.com/cuidehua/1957659如需转载请自行联系原作者
cuizhiliang
你可能感兴趣的文章
Scrum为什么不喜欢“来自客户的频繁变更”?
查看>>
RadioGroup控件实现单选题效果
查看>>
PL/SQL Developer使用技巧|F=Forwarding|
查看>>
工作三年的思考
查看>>
Java工具类 Apache Commons:commons-lang
查看>>
C++ 原来 const 中所使用的函数 必须 全都具有 const 才行
查看>>
浏览器中 for in 反射 对象成员 的差异
查看>>
关于Linux启动时挂载rootfs的几种方式
查看>>
vs2015 系统找不到指定的文件(异常来自HRESULT:0x80070002)问题的解决方法
查看>>
tomcat之 Tomcat 7.0.78 单机多实例配置
查看>>
2018年总结
查看>>
34个漂亮的应用程序后台管理界面
查看>>
java JDK6的可变参数
查看>>
初入职场程序员的五大钻石法则
查看>>
Node.js学习笔记(一)概述
查看>>
split的3种方法
查看>>
忽略PNG透明区域的事件(AS/Flash)
查看>>
文本框只能输入正整数(大于0的整数)代码
查看>>
一步一个脚印学习WCF系列之WCF概要—WCF服务的创建与调用HelloWorld实例,通过配置文件方式(六)...
查看>>
只需简单一步,android自带的示例程序 BluetoothChat 变蓝牙串口助手
查看>>