本文共 3394 字,大约阅读时间需要 11 分钟。
主机1 | 主机2 | 主机3 |
---|---|---|
10.25.72.164 | 10.25.72.233 | 10.25.73.196 |
yum install -y etcd
安装etcd
编辑etcd配置文件
vim /etc/etcd/etcd.conf
ETCD_DATA_DIR="/var/lib/etcd/default.etcd" #etcd数据保存目录ETCD_LISTEN_CLIENT_URLS="http://10.25.72.164:2379,http://localhost:2379" #供外部客户端使用的urlETCD_ADVERTISE_CLIENT_URLS="http://10.25.72.164:2379,http://localhost:2379" #广播给外部客户端使用的urlETCD_NAME="etcd1" #etcd实例名称ETCD_LISTEN_PEER_URLS="http://10.25.72.164:2380" #集群内部通信使用的URLETCD_INITIAL_ADVERTISE_PEER_URLS="http://10.25.72.164:2380" #广播给集群内其他成员访问的URLETCD_INITIAL_CLUSTER="etcd1=http://10.25.72.164:2380,etcd2=http://10.25.72.233:2380,etcd3=http://10.25.73.196:2380" #初始集群成员列表ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster" #集群的名称ETCD_INITIAL_CLUSTER_STATE="new" #初始集群状态,new为新建集群
然后执行systemctl start etcd
启动etcd进程
etcd2和etcd3为加入etcd-cluster集群的实例,需要将其ETCD_INITIAL_CLUSTER_STATE设置为"exist"
ETCD_DATA_DIR="/var/lib/etcd/default.etcd" ETCD_LISTEN_CLIENT_URLS="http://10.25.72.233:2379,http://localhost:2379" ETCD_ADVERTISE_CLIENT_URLS="http://10.25.72.233:2379,http://localhost:2379" ETCD_NAME="etcd2" ETCD_LISTEN_PEER_URLS="http://10.25.72.233:2380" ETCD_INITIAL_ADVERTISE_PEER_URLS="http://10.25.72.233:2380"ETCD_INITIAL_CLUSTER="etcd1=http://10.25.72.164:2380,etcd2=http://10.25.72.233:2380,etcd3=http://10.25.73.196:2380" ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster"ETCD_INITIAL_CLUSTER_STATE="exist"
搭建完毕,可以查看集群节点来确定有没有搭建成功
[root@SZD-L0110301 default.etcd]# etcdctl member list4536e7d0bdb3b43c: name=etcd3 peerURLs=http://10.25.73.196:2380 clientURLs=http://10.25.73.196:2379,http://localhost:2379 isLeader=falsec441e6c11a47ff3d: name=etcd1 peerURLs=http://10.25.72.164:2380 clientURLs=http://10.25.72.164:2379,http://localhost:2379 isLeader=trueddc007546c89f163: name=etcd2 peerURLs=http://10.25.72.223:2380 clientURLs=http://10.25.72.223:2379,http://localhost:2379 isLeader=false
[root@SZD-L0110301 default.etcd]# etcdctl set /testdir/testkey "hello world"hello world
/路径/key
dir
和key
不存在,该命令会创建对应的项切换到另外一个节点
[root@SZD-L0110303 etcd]# etcdctl get /testdir/testkeyhello world
不存在的时候会报错
当键不存在时,会报错
[root@SZD-L0110303 etcd]# etcdctl update /testdir/testkey "hello bruce"hello bruce
[root@SZD-L0110303 etcd]# etcdctl rm /testdir/testkeyPrevNode.Value: hello bruce
更多的操作命令省略,可以见help
[root@SZD-L0072834 ~]# etcdctl -versionetcdctl version: 3.1.10API version: 2
export ETCDCTL_API=3
[root@SZD-L0072834 ~]# etcdctl -versionetcdctl version: 3.1.10API version: 2
export ETCDCTL_API=3
[root@SZD-L0110301 default.etcd]# etcdctl cluster-healthmember 4536e7d0bdb3b43c is healthy: got healthy result from http://10.25.73.196:2379member c441e6c11a47ff3d is healthy: got healthy result from http://10.25.72.164:2379member ddc007546c89f163 is healthy: got healthy result from http://10.25.72.223:2379cluster is healthy
备份 etcd 的数据,参数有:
--data-dir etcd 的数据目录
--backup-dir 备份到指定路径
监测一个键值的变化,一旦键值发生更新,就会输出最新的值并退出。
[root@SZD-L0110301 default.etcd]# etcdctl watch /testdir/testkeyhello bruce
[root@SZD-L0110303 etcd]# etcdctl update /testdir/testkey "hello bruce"
--forever
参数, 这样就会一直监测,直到用户按 CTRL+C
退出监测一个键值的变化,一旦键值发生更新,就执行给定命令。
[root@SZD-L0110301 default.etcd]# etcdctl exec-watch /testdir/testkey -- sh -c 'ls'member
转载地址:http://gfibl.baihongyu.com/