美文网首页
Etcd:1.简单使用

Etcd:1.简单使用

作者: 小六的昵称已被使用 | 来源:发表于2019-05-12 09:48 被阅读0次

环境

[root@test-node-3 ~]# cat /etc/centos-release
CentOS Linux release 7.6.1810 (Core) 

[root@test-node-3 ~]# etcd --version
etcd Version: 3.3.11
Git SHA: 2cf9e51
Go Version: go1.10.3
Go OS/Arch: linux/amd64

[root@test-node-3 ~]# etcdctl version
etcdctl version: 3.3.11
API version: 3.3

第一步:

1.安装

yum install -y etcd.x86_64

2.启动单节点

systemctl enable etcd.service
systemctl stop etcd.service
systemctl start etcd.service
systemctl status etcd.service

3.静态发现集群

## 节点1
etcd --name infra0 --initial-advertise-peer-urls http://192.168.30.81:2380 \
  --listen-peer-urls http://192.168.30.81:2380 \
  --listen-client-urls http://192.168.30.81:2379,http://127.0.0.1:2379 \
  --advertise-client-urls http://192.168.30.81:2379 \
  --initial-cluster-token etcd-cluster-1 \
  --initial-cluster infra0=http://192.168.30.81:2380,infra1=http://192.168.30.82:2380,infra2=http://192.168.30.83:2380 \
  --initial-cluster-state new

## 节点2
etcd --name infra1 --initial-advertise-peer-urls http://192.168.30.82:2380 \
  --listen-peer-urls http://192.168.30.82:2380 \
  --listen-client-urls http://192.168.30.82:2379,http://127.0.0.1:2379 \
  --advertise-client-urls http://192.168.30.82:2379 \
  --initial-cluster-token etcd-cluster-1 \
  --initial-cluster infra0=http://192.168.30.81:2380,infra1=http://192.168.30.82:2380,infra2=http://192.168.30.83:2380 \
  --initial-cluster-state new

## 节点3
etcd --name infra2 --initial-advertise-peer-urls http://192.168.30.83:2380 \
  --listen-peer-urls http://192.168.30.83:2380 \
  --listen-client-urls http://192.168.30.83:2379,http://127.0.0.1:2379 \
  --advertise-client-urls http://192.168.30.83:2379 \
  --initial-cluster-token etcd-cluster-1 \
  --initial-cluster infra0=http://192.168.30.81:2380,infra1=http://192.168.30.82:2380,infra2=http://192.168.30.83:2380 \
  --initial-cluster-state new

    etcd侦听listen-client-urls接受客户端流量
    etcd成员将指定的URL通告advertise-client-urls给其他成员,代理,客户端。
    
## 显示集群信息
export ETCDCTL_API=3
etcdctl --write-out=table --endpoints=localhost:2379 member list
## 列出集群内所有成员
etcdctl member list

第二步:和etcd交互

默认,为了向后兼容 etcdctl 使用 v2 API 来和 etcd 服务器通讯。为了让 etcdctl 使用 v3 API 来和etcd通讯,API 版本必须通过环境变量 ETCDCTL_API 设置为版本3。

## 通过环境变量 ETCDCTL_API 设置为版本3
export ETCDCTL_API=3

## 写入键(这是设置键 foo 的值为 bar 的命令:)
etcdctl put foo bar
etcdctl put foo1 bar
etcdctl put foo2 bar
etcdctl put foo3 bar

## 读取所有键、读取一个键、读取 foo to foo3 的键
etcdctl get *
etcdctl get foo
etcdctl get foo foo3

## 读取所有以`foo`开头的键、只显示前两个
etcdctl get --prefix foo
etcdctl get --prefix --limit=2 foo

## 删除键
etcdctl del foo
etcdctl del foo foo9
etcdctl del --prev-kv zoo

## 观察键的变化、观察 foo to foo9范围内键的变化、观察前缀为 foo 的键
etcdctl watch foo
etcdctl watch foo foo9
etcdctl watch --prefix foo

## 观察多个键 foo 和 zoo 的命令
etcdctl watch -i
watch foo
watch zoo

## 从修订版本 2 开始观察键 `foo` 的改动
etcdctl watch --rev=2 foo

## 在键 `foo` 上观察变更并返回被修改的值和上个修订版本的值
etcdctl watch --prev-kv foo

## 压缩修订版本(在压缩修订版本之前的任何修订版本都不可访问)
etcdctl compact 5
etcdctl get --rev=4 foo     ## 会报错
etcdctl get --rev=4 foo     ## 可以使用,但是为空,待测试
etcdctl watch --rev=4 foo   ## 会报错
etcdctl watch --rev=5 foo   ## 可以使用

附录:

参考:http://etcd.doczh.cn/documentation/

参考:https://github.com/etcd-io/etcd/blob/master/Documentation/op-guide/clustering.md

官网:https://coreos.com/etcd/docs/latest/

https://www.cnblogs.com/skymyyang/p/9067280.html

https://yq.aliyun.com/articles/623228

相关文章

网友评论

      本文标题:Etcd:1.简单使用

      本文链接:https://www.haomeiwen.com/subject/orgqaqtx.html