Etcd配置集群
来自Linux78|wiki
修改etcd配置文件,环境是在/opt/etcd/config/etcd.conf,请根据实际情况修改。
目录
etcd1配置示例
编辑配置文件
vim /opt/etcd/config/etcd.conf ETCD_NAME=etcd1 ETCD_DATA_DIR="/var/lib/etcd/etcd1" ETCD_LISTEN_PEER_URLS="http://192.168.2.210:2380" ETCD_LISTEN_CLIENT_URLS="http://192.168.2.210:2379,http://192.168.2.210:4001" ETCD_INITIAL_ADVERTISE_PEER_URLS="http://192.168.2.210:2380" ETCD_INITIAL_CLUSTER="etcd1=http://192.168.2.210:2380,etcd2=http://192.168.2.211:2380,etcd3=http://192.168.2.212:2380" ETCD_INITIAL_CLUSTER_STATE="new" ETCD_INITIAL_CLUSTER_TOKEN="hilinux-etcd-cluster" ETCD_ADVERTISE_CLIENT_URLS="http://192.168.2.210:2379,http://192.168.2.210:4001"
etcd2配置示例
编辑配置文件
vim /opt/etcd/config/etcd.conf ETCD_NAME=etcd2 ETCD_DATA_DIR="/var/lib/etcd/etcd2" ETCD_LISTEN_PEER_URLS="http://192.168.2.211:2380" ETCD_LISTEN_CLIENT_URLS="http://192.168.2.211:2379,http://192.168.2.211:4001" ETCD_INITIAL_ADVERTISE_PEER_URLS="http://192.168.2.211:2380" ETCD_INITIAL_CLUSTER="etcd1=http://192.168.2.210:2380,etcd2=http://192.168.2.211:2380,etcd3=http://192.168.2.212:2380" ETCD_INITIAL_CLUSTER_STATE="new" ETCD_INITIAL_CLUSTER_TOKEN="hilinux-etcd-cluster" ETCD_ADVERTISE_CLIENT_URLS="http://192.168.2.211:2379,http://192.168.2.211:4001"
etcd3配置示例
编辑配置文件
vim /opt/etcd/config/etcd.conf ETCD_NAME=etcd3 ETCD_DATA_DIR="/var/lib/etcd/etcd3" ETCD_LISTEN_PEER_URLS="http://192.168.2.212:2380" ETCD_LISTEN_CLIENT_URLS="http://192.168.2.212:2379,http://192.168.2.212:4001" ETCD_INITIAL_ADVERTISE_PEER_URLS="http://192.168.2.212:2380" ETCD_INITIAL_CLUSTER="etcd1=http://192.168.2.210:2380,etcd2=http://192.168.2.211:2380,etcd3=http://192.168.2.212:2380" ETCD_INITIAL_CLUSTER_STATE="new" ETCD_INITIAL_CLUSTER_TOKEN="hilinux-etcd-cluster" ETCD_ADVERTISE_CLIENT_URLS="http://192.168.2.212:2379,http://192.168.2.212:4001"
配置参数解释
ETCD_NAME :ETCD的节点名 ETCD_DATA_DIR:ETCD的数据存储目录 ETCD_SNAPSHOT_COUNTER:多少次的事务提交将触发一次快照 ETCD_HEARTBEAT_INTERVAL:ETCD节点之间心跳传输的间隔,单位毫秒 ETCD_ELECTION_TIMEOUT:该节点参与选举的最大超时时间,单位毫秒 ETCD_LISTEN_PEER_URLS:该节点与其他节点通信时所监听的地址列表,多个地址使用逗号隔开,其格式可以划分为scheme://IP:PORT,这里的scheme可以是http、https ETCD_LISTEN_CLIENT_URLS:该节点与客户端通信时监听的地址列表 ETCD_INITIAL_ADVERTISE_PEER_URLS:该成员节点在整个集群中的通信地址列表,这个地址用来传输集群数据的地址。因此这个地址必须是可以连接集群中所有的成员的。 ETCD_INITIAL_CLUSTER:配置集群内部所有成员地址,其格式为:ETCD_NAME=ETCD_INITIAL_ADVERTISE_PEER_URLS,如果有多个使用逗号隔开 ETCD_ADVERTISE_CLIENT_URLS:广播给集群中其他成员自己的客户端地址列表 ETCD_INITIAL_CLUSTER_STATE:初始化集群状态,new表示新建 ETCD_INITIAL_CLUSTER_TOKEN:初始化集群token
注意:所有ETCD_MY_FLAG的配置参数也可以通过命令行参数进行设置,但是命令行指定的参数优先级更高,同时存在时会覆盖环境变量对应的值。
下面给出常用配置的参数和它们的解释:
--name:方便理解的节点名称,默认为default,在集群中应该保持唯一,可以使用 hostname
--data-dir:服务运行数据保存的路径,默认为 ${name}.etcd
--snapshot-count:指定有多少事务(transaction)被提交时,触发截取快照保存到磁盘
--heartbeat-interval:leader 多久发送一次心跳到 followers。默认值是 100ms
--eletion-timeout:重新投票的超时时间,如果 follow 在该时间间隔没有收到心跳包,会触发重新投票,默认为 1000 ms
--listen-peer-urls:和同伴通信的地址,比如 http://ip:2380,如果有多个,使用逗号分隔。需要所有节点都能够访问,所以不要使用 localhost!
--listen-client-urls:对外提供服务的地址:比如 http://ip:2379,http://127.0.0.1:2379,客户端会连接到这里和 etcd 交互
--advertise-client-urls:对外公告的该节点客户端监听地址,这个值会告诉集群中其他节点
--initial-advertise-peer-urls:该节点同伴监听地址,这个值会告诉集群中其他节点
--initial-cluster:集群中所有节点的信息,格式为 node1=http://ip1:2380,node2=http://ip2:2380,…。注意:这里的 node1 是节点的 --name 指定的名字;后面的 ip1:2380 是 -- initial-advertise-peer-urls 指定的值
--initial-cluster-state:新建集群的时候,这个值为new;假如已经存在的集群,这个值为 existing
--initial-cluster-token:创建集群的token,这个值每个集群保持唯一。这样的话,如果你要重新创建集群,即使配置和之前一样,也会再次生成新的集群和节点 uuid;否则会导致多个集群之间的冲突,造成未知的错误
所有以--init开头的配置都是在bootstrap集群的时候才会用到,后续节点的重启会被忽略。
测试etcd集群
按上面配置好各集群节点后,分别在各节点启动etcd。
systemctl start etcd
启动完成后,在任意节点执行etcdctl member list可列所有集群节点信息,如下所示:
tcdctl --endpoints "http://192.168.2.210:2379" member list a3ba19408fd4c829: name=etcd3 peerURLs=http://192.168.2.212:2380 clientURLs=http://192.168.2.212:2379,http://192.168.2.212:4001 isLeader=true a8589aa8629b731b: name=etcd1 peerURLs=http://192.168.2.210:2380 clientURLs=http://192.168.2.210:2379,http://192.168.2.210:4001 isLeader=false e4a3e95f72ced4a7: name=etcd2 peerURLs=http://192.168.2.211:2380 clientURLs=http://192.168.2.211:2379,http://192.168.2.211:4001 isLeader=false
这里显示指定的集群地址,是因为在上面的配置中绑定了IP。如不指定会报如下错误:
etcdctl member list Error: client: etcd cluster is unavailable or misconfigured; error #0: dial tcp 127.0.0.1:4001: getsockopt: connection refused ; error #1: dial tcp 127.0.0.1:2379: getsockopt: connection refused error #0: dial tcp 127.0.0.1:4001: getsockopt: connection refused error #1: dial tcp 127.0.0.1:2379: getsockopt: connection refused
etcd集群基本管理
查看集群健康状态
etcdctl --endpoints "http://192.168.2.210:2379" cluster-health member a3ba19408fd4c829 is healthy: got healthy result from http://192.168.2.212:2379 member a8589aa8629b731b is healthy: got healthy result from http://192.168.2.210:2379 member e4a3e95f72ced4a7 is healthy: got healthy result from http://192.168.2.211:2379 cluster is healthy
查看集群成员
在任一节点上执行,可以看到集群的节点情况,并能看出哪个是leader节点。
etcdctl --endpoints "http://192.168.2.210:2379" member list
a3ba19408fd4c829: name=etcd3 peerURLs=http://192.168.2.212:2380 clientURLs=http://192.168.2.212:2379,http://192.168.2.212:4001 isLeader=true a8589aa8629b731b: name=etcd1 peerURLs=http://192.168.2.210:2380 clientURLs=http://192.168.2.210:2379,http://192.168.2.210:4001 isLeader=false e4a3e95f72ced4a7: name=etcd2 peerURLs=http://192.168.2.211:2380 clientURLs=http://192.168.2.211:2379,http://192.168.2.211:4001 isLeader=false
更新一个节点
如果你想更新一个节点的IP(peerURLS),首先你需要知道那个节点的ID
etcdctl --endpoints "http://192.168.2.210:2379" member list a3ba19408fd4c829: name=etcd3 peerURLs=http://192.168.2.212:2380 clientURLs=http://192.168.2.212:2379,http://192.168.2.212:4001 isLeader=false a8589aa8629b731b: name=etcd1 peerURLs=http://192.168.2.210:2380 clientURLs=http://192.168.2.210:2379,http://192.168.2.210:4001 isLeader=false e4a3e95f72ced4a7: name=etcd2 peerURLs=http://192.168.2.211:2380 clientURLs=http://192.168.2.211:2379,http://192.168.2.211:4001 isLeader=true
更新一个节点
etcdctl --endpoints "http://192.168.2.210:2379" member update a8589aa8629b731b http://192.168.2.210:2380 Updated member with ID a8589aa8629b731b in cluster
删除一个节点
etcdctl --endpoints "http://192.168.2.210:2379" member list a3ba19408fd4c829: name=etcd3 peerURLs=http://192.168.2.212:2380 clientURLs=http://192.168.2.212:2379,http://192.168.2.212:4001 isLeader=false a8589aa8629b731b: name=etcd1 peerURLs=http://192.168.2.210:2380 clientURLs=http://192.168.2.210:2379,http://192.168.2.210:4001 isLeader=false e4a3e95f72ced4a7: name=etcd2 peerURLs=http://192.168.2.211:2380 clientURLs=http://192.168.2.211:2379,http://192.168.2.211:4001 isLeader=true etcdctl --endpoints "http://192.168.2.210:2379" member remove a8589aa8629b731b Removed member a8589aa8629b731b from cluster etcdctl --endpoints "http://192.168.2.211:2379" member list a3ba19408fd4c829: name=etcd3 peerURLs=http://192.168.2.212:2380 clientURLs=http://192.168.2.212:2379,http://192.168.2.212:4001 isLeader=false e4a3e95f72ced4a7: name=etcd2 peerURLs=http://192.168.2.211:2380 clientURLs=http://192.168.2.211:2379,http://192.168.2.211:4001 isLeader=true
增加一个新节点
注意:步骤很重要,不然会报集群ID不匹配。
将目标节点添加到集群
etcdctl --endpoints "http://192.168.2.211:2379" member add etcd1 http://192.168.2.210:2380 Added member named etcd1 with ID baab0aae8b58c802 to cluster ETCD_NAME="etcd1" ETCD_INITIAL_CLUSTER="etcd3=http://192.168.2.212:2380,etcd1=http://192.168.2.210:2380,etcd2=http://192.168.2.211:2380" ETCD_INITIAL_CLUSTER_STATE="existing"
查看新增成员列表
etcd1状态现在为unstarted
etcdctl --endpoints "http://192.168.2.211:2379" member list a3ba19408fd4c829: name=etcd3 peerURLs=http://192.168.2.212:2380 clientURLs=http://192.168.2.212:2379,http://192.168.2.212:4001 isLeader=false baab0aae8b58c802[unstarted]: peerURLs=http://192.168.2.210:2380 e4a3e95f72ced4a7: name=etcd2 peerURLs=http://192.168.2.211:2380 clientURLs=http://192.168.2.211:2379,http://192.168.2.211:4001 isLeader=true
清空目标节点数据
目标节点从集群中删除后,成员信息会更新。新节点是作为一个全新的节点加入集群,如果data-dir有数据,etcd启动时会读取己经存在的数据,仍然用旧的memberID会造成无法加入集群,所以一定要清空新节点的data-dir。
rm -rf /var/lib/etcd/etcd1
目标节点启动新增加成员
修改配置文件中ETCD_INITIAL_CLUSTER_STATE标记为existing,如果为new,则会自动生成一个新的memberID,这和前面添加节点时生成的ID不一致,故日志中会报节点ID不匹配的错。
vim /opt/etcd/config/etcd.conf
ETCD_INITIAL_CLUSTER_STATE="existing"
启动etcd
systemctl start etcd
查看新节点是否成功加入
etcdctl --endpoints "http://192.168.2.210:2379" member list a3ba19408fd4c829: name=etcd3 peerURLs=http://192.168.2.212:2380 clientURLs=http://192.168.2.212:2379,http://192.168.2.212:4001 isLeader=false baab0aae8b58c802: name=etcd1 peerURLs=http://192.168.2.210:2380 clientURLs=http://192.168.2.210:2379,http://192.168.2.210:4001 isLeader=false e4a3e95f72ced4a7: name=etcd2 peerURLs=http://192.168.2.211:2380 clientURLs=http://192.168.2.211:2379,http://192.168.2.211:4001 isLeader=true