Etcd v3.1.15 在Linux CentOS7 集群配置 (单机改端口实现)
注意Etcd在每个版本都会有命令或功能变化,看教程时注意版本。
时间同步:
#根据提示选择所在大洲、国家和城市 [root@Cent0S7 ~]# tzselect #使用阿里 NTP 校时 [root@Cent0S7 ~]# ntpdate ntp.aliyun.com #系统时间同步BIOS硬件时间 [root@Cent0S7 ~]# hwclock -w #-R 查看时区 [root@Cent0S7 ~]# date -R
单机部署三份(最少3台node才能组成集群,所以要三份),修改端口实现。
PS:很多人学习时,条件有限,没有3台电脑或3台VM虚拟机都开不起来。
新建目录:
[root@Cent0S7 ~]# mkdir /var/lib/etcd/ [root@Cent0S7 ~]# mkdir /var/lib/etcd1/ [root@Cent0S7 ~]# mkdir /var/lib/etcd2/
复制etcd(防止用一份出现奇怪的问题)
[root@Cent0S7 ~]# tar -zxf etcd-v3.1.5-linux-amd64.tar.gz [root@Cent0S7 ~]# cd etcd-v3.1.5-linux-amd64/ [root@Cent0S7 ~]# cp etcd /usr/local/sbin/etcd [root@Cent0S7 ~]# cp etcd /usr/local/sbin/etcd1 [root@Cent0S7 ~]# cp etcd /usr/local/sbin/etcd2
创建守护进程 etcd.service 文件
[root@Cent0S7 ~]# cat <<EOF | sudo tee /etc/systemd/system/etcd.service [Unit] Description=Etcd Server Documentation=https://github.com/coreos/etcd After=network.target [Service] User=root Type=notify EnvironmentFile=/etc/etcd.conf ExecStart=/usr/local/bin/etcd Restart=on-failure RestartSec=10s LimitNOFILE=40000 [Install] WantedBy=multi-user.target EOF
创建守护进程 etcd1.service 文件
[root@Cent0S7 ~]# cat <<EOF | sudo tee /etc/systemd/system/etcd1.service [Unit] Description=Etcd1 Server Documentation=https://github.com/coreos/etcd After=network.target [Service] User=root Type=notify EnvironmentFile=/etc/etcd1.conf ExecStart=/usr/local/bin/etcd1 Restart=on-failure RestartSec=10s LimitNOFILE=40000 [Install] WantedBy=multi-user.target EOF
创建守护进程 etcd2.service 文件
[root@Cent0S7 ~]# cat <<EOF | sudo tee /etc/systemd/system/etcd2.service [Unit] Description=Etcd2 Server Documentation=https://github.com/coreos/etcd After=network.target [Service] User=root Type=notify EnvironmentFile=/etc/etcd2.conf ExecStart=/usr/local/bin/etcd2 Restart=on-failure RestartSec=10s LimitNOFILE=40000 [Install] WantedBy=multi-user.target EOF
创建配置文件 etcd.conf 文件,端口2379、2380
[root@Cent0S7 ~]# cat <<EOF | sudo tee /etc/etcd.conf #节点名称 ETCD_NAME=etcd #数据存放位置 ETCD_DATA_DIR=/var/lib/etcd/ ETCD_LISTEN_PEER_URLS="http://192.168.200.132:2380" ETCD_LISTEN_CLIENT_URLS="http://192.168.200.132:2379,http://127.0.0.1:2379" #[cluster] #leader多久发送一次心跳到followers,默认值是100ms ETCD_HEARTBEAT_INTERVAL="200" ETCD_INITIAL_ADVERTISE_PEER_URLS="http://192.168.200.132:2380" ETCD_INITIAL_CLUSTER_STATE="new" ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster" ETCD_ADVERTISE_CLIENT_URLS="http://192.168.200.132:2379" ETCD_INITIAL_CLUSTER="etcd=http://192.168.200.132:2380,etcd1=http://192.168.200.132:3380,etcd2=http://192.168.200.132:4380" EOF
ps : 如果不写127.0.0.1或localhost 在测试时 回环地址将被拒绝链接。
创建配置文件 etcd1.conf 文件,端口3379、3380
[root@Cent0S7 ~]# cat <<EOF | sudo tee /etc/etcd1.conf #节点名称 ETCD_NAME=etcd1 #数据存放位置 ETCD_DATA_DIR=/var/lib/etcd1/ ETCD_LISTEN_PEER_URLS="http://192.168.200.132:3380" ETCD_LISTEN_CLIENT_URLS="http://192.168.200.132:3379,http://127.0.0.1:2379" #[cluster] #leader多久发送一次心跳到followers,默认值是100ms ETCD_HEARTBEAT_INTERVAL="200" ETCD_INITIAL_ADVERTISE_PEER_URLS="http://192.168.200.132:3380" ETCD_INITIAL_CLUSTER_STATE="new" ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster" ETCD_ADVERTISE_CLIENT_URLS="http://192.168.200.132:3379" ETCD_INITIAL_CLUSTER="etcd=http://192.168.200.132:2380,etcd1=http://192.168.200.132:3380,etcd2=http://192.168.200.132:4380" EOF
ps : 如果不写127.0.0.1或localhost 在测试时 回环地址将被拒绝链接。
创建配置文件 etcd2.conf 文件,端口4379、4380
[root@Cent0S7 ~]# cat <<EOF | sudo tee /etc/etcd2.conf #节点名称 ETCD_NAME=etcd2 #数据存放位置 ETCD_DATA_DIR=/var/lib/etcd2/ ETCD_LISTEN_PEER_URLS="http://192.168.200.132:4380" ETCD_LISTEN_CLIENT_URLS="http://192.168.200.132:4379,http://127.0.0.1:2379" #[cluster] #leader多久发送一次心跳到followers,默认值是100ms ETCD_HEARTBEAT_INTERVAL="200" ETCD_INITIAL_ADVERTISE_PEER_URLS="http://192.168.200.132:4380" ETCD_INITIAL_CLUSTER_STATE="new" ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster" ETCD_ADVERTISE_CLIENT_URLS="http://192.168.200.132:4379" ETCD_INITIAL_CLUSTER="etcd=http://192.168.200.132:2380,etcd1=http://192.168.200.132:3380,etcd2=http://192.168.200.132:4380" EOF
ps : 如果不写127.0.0.1或localhost 在测试时 回环地址将被拒绝链接。
配置都完成了,刷新
[root@Cent0S7 ~]# systemctl daemon-reload
启动节点
PS:启动时光标一直闪,也没报错,因为其他节点还没启动,所以在查找,不用管,多开几个CRT的Shell窗口启动每个service就可以 了。
[root@Cent0S7 ~]# systemctl start etcd.service
[root@Cent0S7 ~]# systemctl start etcd1.service
[root@Cent0S7 ~]# systemctl start etcd2.service
开机启动
[root@Cent0S7 ~]# systemctl enable etcd.service Created symlink from /etc/systemd/system/multi-user.target.wants/etcd.service to /etc/systemd/system/etcd.service. [root@Cent0S7 ~]# systemctl enable etcd1.service Created symlink from /etc/systemd/system/multi-user.target.wants/etcd1.service to /etc/systemd/system/etcd1.service. [root@Cent0S7 ~]# systemctl enable etcd2.service Created symlink from /etc/systemd/system/multi-user.target.wants/etcd2.service to /etc/systemd/system/etcd2.service.
查看列表里状态,状态enabled,设置成功
[root@Cent0S7 etc]# systemctl list-unit-files etcd*.service UNIT FILE STATE etcd.service enabled etcd1.service enabled etcd2.service enabled 3 unit files listed.
开放防火墙端口
firewall-cmd --zone=public --add-port=2379/tcp --permanent firewall-cmd --zone=public --add-port=2380/tcp --permanent firewall-cmd --reload firewall-cmd --list-all
日志滚动显示最新的,用Ctrl+c退出
[root@Cent0S7 ~]# journalctl -ef
查看服务状态
[root@Cent0S7 ~]# systemctl status etcd -l ● etcd.service - Etcd Server Loaded: loaded (/etc/systemd/system/etcd.service; enabled; vendor preset: disabled) Active: active (running) since Mon 2020-11-30 15:40:52 CST; 6min ago Docs: https://github.com/coreos/etcd Main PID: 2338 (etcd) CGroup: /system.slice/etcd.service └─2338 /usr/local/bin/etcd Nov 30 15:40:56 Cent0S7 etcd[2338]: cannot get the version of member e9f1bc7810aec2f3 (Get http://192.168.200.132:3380/version: dial tcp 192.168.200.132:3380: getsockopt: connection refused) Nov 30 15:40:59 Cent0S7 etcd[2338]: health check for peer e9f1bc7810aec2f3 could not connect: dial tcp 192.168.200.132:3380: getsockopt: connection refused Nov 30 15:40:59 Cent0S7 etcd[2338]: peer e9f1bc7810aec2f3 became active Nov 30 15:40:59 Cent0S7 etcd[2338]: established a TCP streaming connection with peer e9f1bc7810aec2f3 (stream MsgApp v2 reader) Nov 30 15:40:59 Cent0S7 etcd[2338]: established a TCP streaming connection with peer e9f1bc7810aec2f3 (stream Message reader) Nov 30 15:40:59 Cent0S7 etcd[2338]: established a TCP streaming connection with peer e9f1bc7810aec2f3 (stream MsgApp v2 writer) Nov 30 15:40:59 Cent0S7 etcd[2338]: established a TCP streaming connection with peer e9f1bc7810aec2f3 (stream Message writer) Nov 30 15:41:00 Cent0S7 etcd[2338]: updating the cluster version from 3.0 to 3.1 Nov 30 15:41:00 Cent0S7 etcd[2338]: updated the cluster version from 3.0 to 3.1 Nov 30 15:41:00 Cent0S7 etcd[2338]: enabled capabilities for version 3.1
验证:
[root@Cent0S7 ~]# etcdctl --endpoints="http://192.168.200.132:2379,http://192.168.200.132:3379,http://192.168.200.132:4379" member list 232f7a21c3110c92, started, etcd2, http://192.168.200.132:4380, http://192.168.200.132:4001,http://192.168.200.132:4379 3942148265deeadf, started, etcd, http://192.168.200.132:2380, http://192.168.200.132:2379,http://192.168.200.132:4001 e9f1bc7810aec2f3, started, etcd1, http://192.168.200.132:3380, http://192.168.200.132:3379,http://192.168.200.132:4001
[root@Cent0S7 ~]# etcdctl -w="table" --endpoints=192.168.200.132:2379 member list +------------------+---------+-------+-----------------------------+---------------------------------------------------------+ | ID | STATUS | NAME | PEER ADDRS | CLIENT ADDRS | +------------------+---------+-------+-----------------------------+---------------------------------------------------------+ | 232f7a21c3110c92 | started | etcd2 | http://192.168.200.132:4380 | http://192.168.200.132:4001,http://192.168.200.132:4379 | | 3942148265deeadf | started | etcd | http://192.168.200.132:2380 | http://192.168.200.132:2379,http://192.168.200.132:4001 | | e9f1bc7810aec2f3 | started | etcd1 | http://192.168.200.132:3380 | http://192.168.200.132:3379,http://192.168.200.132:4001 | +------------------+---------+-------+-----------------------------+---------------------------------------------------------+
[root@Cent0S7 ~]# etcdctl --write-out=table --endpoints=192.168.200.132:2379,192.168.200.132:3379,192.168.200.132:4379 endpoint status +----------------------+------------------+---------+---------+-----------+-----------+------------+ | ENDPOINT | ID | VERSION | DB SIZE | IS LEADER | RAFT TERM | RAFT INDEX | +----------------------+------------------+---------+---------+-----------+-----------+------------+ | 192.168.200.132:2379 | 3942148265deeadf | 3.1.5 | 25 kB | true | 7 | 9 | | 192.168.200.132:3379 | e9f1bc7810aec2f3 | 3.1.5 | 25 kB | false | 7 | 9 | | 192.168.200.132:4379 | 232f7a21c3110c92 | 3.1.5 | 25 kB | false | 7 | 9 | +----------------------+------------------+---------+---------+-----------+-----------+------------+
批量关闭、清除缓存:
[root@Cent0S7 ~]# cat <<EOF | sudo tee etcd_all_stop.sh # /bin/bash echo " ------ etcd_all stop--------------"
echo "" echo " ------ etcd stop--------------" systemctl stop etcd.service echo " ------ etcd2 stop--------------" systemctl stop etcd1.service echo " ------ etcd3 stop--------------" systemctl stop etcd2.service echo "" echo " ------ clear etcd cache --------------" rm -rf /var/lib/etcd/ mkdir -p /var/lib/etcd/ echo " ------ clear etcd1 cache --------------" rm -rf /var/lib/etcd1/ mkdir -p /var/lib/etcd1/ echo " ------ clear etcd2 cache --------------" rm -rf /var/lib/etcd2/ mkdir -p /var/lib/etcd2/ echo "" echo "-------etcd_all stop and clear OK ------------"
echo
echo EOF
以下是配置变量说明参考:
#单机配置 # [member] # etcd集群中的节点名,这里可以随意,可区分且不重复就行 ETCD_NAME=$(hostname -s) #缓存数据存放位置,保存日志和快照的目录,默认为当前工作目录default.etcd/目录下 #新该配置后,要删除缓存,否则启动还是要用之前的缓存。 ETCD_DATA_DIR=/var/lib/etcd/ #ETCD_WAL_DIR="" #指定有多少事务被提交时,触发和截取快照保存到磁盘 #ETCD_SNAPSHOT_COUNT="10000" #leader多久发送一次心跳到followers,默认值是100ms #ETCD_HEARTBEAT_INTERVAL="100" #重新投票的超时时间,如果follow在该时间间隔没有收到心跳包,会触发重新投票,默认为1000ms。 #ETCD_ELECTION_TIMEOUT="1000" #监听的用于节点之间通信的url,可监听多个,集群内部将通过这些url进行数据交互(如选举,数据同步等) ETCD_LISTEN_PEER_URLS="http://localhost:2380" #监听的用于客户端通信的url,同样可以监听多个。 #如果不写htt://localhost:2379,http://127.0.0.1:2379的本地回环地址,测试时localhost和127.0.0.1将拒绝连接 ETCD_LISTEN_CLIENT_URLS="http://localhost:4001,http://localhost:2379" #要保留的最大快照文件数(0表示不受限制),Windows上的用户默认值不受限制 #ETCD_MAX_SNAPSHOTS="5" #要保留的最大wal文件数(0表示不受限制),Windows上的用户默认值不受限制 #ETCD_MAX_WALS="5" #逗号分隔的CORS原始白名单(跨源资源共享) #ETCD_CORS="" # #集群配置 #[cluster] #告知集群其他节点的URL,建议用于节点之间通信的url,节点间将以该值进行通信,一般是2380端口。 ETCD_INITIAL_ADVERTISE_PEER_URLS="http://localhost:2380" #也就是集群中所有的initial-advertise-peer-urls 的合集, #所有节点的IP都写在下面,etcd-node1、etcd-node2、etcd-node3根据实际情况改成相应ip或hostname ETCD_INITIAL_CLUSTER="etcd-node1=http://etcd-node1:2380,etcd-node2=http://etcd-node2:2380,etcd-master=http://etcd-master:2380" #新建集群的标志 new和existing,即加入与被加入 ETCD_INITIAL_CLUSTER_STATE="new" #自定义的一个token值,设置该值后集群将生成唯一id,并为每个节点也生成唯一id,当使用相同配置文件再启动一个集群时,只要该token值不一样,etcd集群就不会相互影响。 ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster" #建议使用的客户端通信url,该值用于etcd代理或etcd成员与etcd节点通信。 ETCD_ADVERTISE_CLIENT_URLS="http://localhost:2379"
##################### 报错 #################################
错误:
Error: grpc: timed out when dialing
解决方法:将心跳值设置到200在试试
##################### 报错 #################################
参考文章:
http://blog.51cto.com/zlyang/1951164
http://cnblogs.com/davygeek/p/7154780.html 老版本端口4001 V3.3.5
http://dazhuanlan.com/2019/12/13/5df3ad5424c46/
http://cnblogs.com/linuxws/p/11194403.html 配置文件详解
http://localhost:2379