见下面多节点配置中:“新节点加入集群”,有详细说明
-
命令:
etcdctl member remove id
【ID用命令:etcdctl member list
查看,开头的一串数字就是ID】 -
如,我删除158这个成员
[root@etcd1 ~]# etcdctl member list | grep 158
240bc0d12da09d72: name=etcd-158 peerURLs=http://192.168.59.158:2380 clientURLs=http://192.168.59.158:2379,http://localhost:2379 isLeader=false
[root@etcd1 ~]#
[root@etcd1 ~]# etcdctl member remove 240bc0d12da09d72
Removed member 240bc0d12da09d72 from cluster
[root@etcd1 ~]#
[root@etcd1 ~]# etcdctl member list | grep 158
[root@etcd1 ~]#
-
如果是通过快照恢复,还会清除默认数据目录的所有内容,所以备份整个数据目录和快照是冲突的,主要是说明一下etcdctl中backup的使用以及etcd的默认数据目录罢了。
-
默认的数据目录为
/var/lib/etcd/
[root@etcd1 ~]# etcdctl backup --data-dir /var/lib/etcd --backup-dir /tmp/etcd
2021-07-14 10:40:01.869919 I | open /var/lib/etcd/member/snap: no such file or directory
[root@etcd1 ~]# cd /tmp/etcd
[root@etcd1 etcd]# ls
member
[root@etcd1 etcd]#
-
参数说明:
-
--data-dir
:指明数据目录的位置 -
--backup-dir
:指明备份的位置
=================================================================================
安装etcd包
- 安装etcd包:
yum -y install etcd
配置文件编辑
-
3台虚拟机,仅用一个节点操作即可【是单节点配置】。
-
安装完毕后配置文件如下:
[root@etcd1 etcd]# cd /etc/etcd/
[root@etcd1 etcd]# ls
etcd.conf
[root@etcd1 etcd]#
- 编辑前还是先备份一下源文件:
[root@etcd1 etcd]# cp /etc/etcd/etcd.conf /
- 注意,配置文件中的所有参数在下面这篇博客中有详细说明,自行去查看对比,我在这就不对参数做说明,仅对修改内容做说明
博客连接。。。。。。
- 需要修改的内容如下:
[root@etcd1 etcd]# vim /etc/etcd/etcd.conf
1 #[Member]
2 #下面行是存储位置,可以自定义位置【我是用的默认】
3 ETCD_DATA_DIR=“/var/lib/etcd/default.etcd”
4 #下面3380需要取消注释,和3379后面都需要加上自己ip:port【默认用的是回环端口】
5 ETCD_LISTEN_PEER_URLS=“http://localhost:2380,http://192.168.59.156:2380”
6 ETCD_LISTEN_CLIENT_URLS=“http://localhost:2379,http://192.168.59.156:2379”
7 #
8 #下面是etcd名字,可以自定义【我用的默认】
9 ETCD_NAME=“default”
19 #[Clustering]
20 #添加下面的ip,让其监听该端口
21 ETCD_ADVERTISE_CLIENT_URLS=“http://localhost:2379,http://192.168.59.156:2379”
22 #ETCD_DISCOVERY=“”
#其他参数可以不用动,直接保存退出
服务加入开机启动
因为我不是第一次执行,所以没有打印任何内容。
[root@etcd1 ~]# systemctl enable etcd --now
[root@etcd1 ~]#
[root@etcd1 ~]# systemctl is-active etcd
active
[root@etcd1 ~]#
-
这里面有使用的说明,多看看这里面。
-
方式一:
etcdctl --help
-
方式二:
man etcdctl
-
etcd写入数据有2个版本,版本2和版本3,默认使用的版本2
注:版本2个版本3写入数据的方式是不一样的且不可混用。
- 默认使用的是版本2,如果切换到版本3了,使用下面命令切换版本2的环境。
命令:export ETCDCTL_API=2
查看所有命令
- 执行:
etcdctl --help
下拉,有一个COMMANDS
,这里面就是2版本的所有命令了【和linux命令类似】
COMMANDS:
backup backup an etcd directory
cluster-health check the health of the etcd cluster
mk make a new key with a given value
mkdir make a new directory
rm remove a key or a directory
rmdir removes the key if it is an empty directory or a key-value pair
get retrieve the value of a key
ls retrieve a directory
set set the value of a key
setdir create a new directory or update an existing directory TTL
update update an existing key with a given value
updatedir update an existing directory
watch watch a key for changes
exec-watch watch a key for changes and exec an executable
member member add, remove and list subcommands
user user add, grant and revoke subcommands
role role add, grant and revoke subcommands
auth overall auth controls
help, h Shows a list of commands or help for one command
如:创建、查看、删除
[root@etcd1 ~]# etcdctl ls /
[root@etcd1 ~]#
[root@etcd1 ~]# etcdctl mkdir /aa
[root@etcd1 ~]#
[root@etcd1 ~]# etcdctl ls /
/aa
[root@etcd1 ~]#
[root@etcd1 ~]# etcdctl rmdir /aa
[root@etcd1 ~]#
[root@etcd1 ~]# etcdctl ls /
[root@etcd1 ~]#
其他节点访问该etcd
- 准确来说,叫做远程执行其他etcd命令罢了【注意,这仅仅是远程访问而已,并不是该集群的一部分】
命令:etcdctl --endpoints IP:2379 这跟需要执行的命令
【2和3的版本命令不一样】
- 如下,我在157上查看156这个etc的数据
[root@etcd2 ~]# etcdctl --endpoints http://192.168.59.156:2379 ls /
/aa
[root@etcd2 ~]#
[root@etcd2 ~]# ip a | grep 59
inet 192.168.59.157/24 brd 192.168.59.255 scope global ens32
[root@etcd2 ~]#
- 需要执行一个环境变量,然后就默认使用版本3了。
命令:export ETCDCTL_API=3
查看所有命令
- 我执行了3的环境变量后,直接执行:
etcdctl --help
下拉,有一个COMMANDS
,这里面就是3版本的所有命令了【这个更像数据库】,可以看到比2多很多,而且使用方式也变了
COMMANDS:
get Gets the key or a range of keys
put Puts the given key into the store
del Removes the specified key or range of keys [key, range_end)
txn Txn processes all the requests in one transaction
compaction Compacts the event history in etcd
alarm disarm Disarms all alarms
alarm list Lists all alarms
defrag Defragments the storage of the etcd members with given endpoints
endpoint health Checks the healthiness of endpoints specified in --endpoints
flag
endpoint status Prints out the status of endpoints specified in --endpoints
flag
endpoint hashkv Prints the KV history hash for each endpoint in --endpoints
move-leader Transfers leadership to another etcd cluster member.
watch Watches events stream on keys or prefixes
version Prints the version of etcdctl
lease grant Creates leases
lease revoke Revokes leases
lease timetolive Get lease information
lease list List all active leases
lease keep-alive Keeps leases alive (renew)
member add Adds a member into the cluster
member remove Removes a member from the cluster
member update Updates a member in the cluster
member list Lists all members in the cluster
snapshot save Stores an etcd node backend snapshot to a given file
snapshot restore Restores an etcd member snapshot to an etcd directory
snapshot status Gets backend snapshot status of a given file
make-mirror Makes a mirror at the destination etcd cluster
migrate Migrates keys in a v2 store to a mvcc store
lock Acquires a named lock
elect Observes and participates in leader election
auth ena