前言
该教程适用于三节点etcd集群的搭建,平台 CentOS
注意时间必须同步!!!
配置Hosts
Short| Hostname IP Address|
etcd1| 192.168.18.9|
etcd2| 192.168.18.10|
etcd3| 192.168.18.11|
以上 ip
并非真实 ip
,请根据实际情况替换
所有节点上修改 hosts
sudo tee -a /etc/hosts<<EOF
192.168.18.9 etcd1
192.168.18.10 etcd2
192.168.18.11 etcd3
EOF
下载和放置etcd
到 github
的 release
下载指定版本, 并上传服务器
tar xvf *.tar.gz
cd etcd-*/
sudo mv etcd* /usr/local/bin/
cd ~
rm -rf /tmp/etcd
查看版本
$ etcd --version
etcd Version: 3.4.10
Git SHA: 18dfb9cca
Go Version: go1.12.17
Go OS/Arch: linux/amd64
$ etcdctl version
etcdctl version: 3.4.10
API version: 3.4
创建专用用户和用户组
sudo groupadd --system etcd
sudo useradd -s /sbin/nologin --system -g etcd etcd
sudo mkdir -p /var/lib/etcd/
sudo mkdir /etc/etcd
sudo chown -R etcd:etcd /var/lib/etcd/
chmod 700 /var/lib/etcd/
配置所有的etcd节点
我们要填充支持 systemd
管理 etcd
的配置文件,在这之前需要先设置临时变量
# 设置需要绑定的网卡名
INT_NAME="eth0"
# 内部IP地址,用于节点之间交流,如果不是eth0的,是其他的,可以手动设置上相应的ip
ETCD_HOST_IP=(ip addr showINT_NAME | grep "inet\b" | awk '{print $2}' | cut -d/ -f1)
# 不同的节点设置名称为不同,在这里我的设置的是etcd1、etcd2、etcd3,也就是我们刚刚配置/etc/hosts文件域名解析的名称
ETCD_NAME=etcd*
配置 etcd.service
文件
cat <<EOF | sudo tee /etc/systemd/system/etcd.service
[Unit]
Description=etcd service
Documentation=https://github.com/etcd-io/etcd
[Service]
Type=notify
User=etcd
ExecStart=/usr/local/bin/etcd \\
--name {ETCD_NAME} \\
--data-dir=/var/lib/etcd \\
--initial-advertise-peer-urls http://{ETCD_HOST_IP}:2380 \\
--listen-peer-urls http://{ETCD_HOST_IP}:2380 \\
--listen-client-urls http://{ETCD_HOST_IP}:2379,http://127.0.0.1:2379 \\
--advertise-client-urls http://${ETCD_HOST_IP}:2379 \\
--initial-cluster-token etcd-cluster-0 \\
--initial-cluster etcd1=http://etcd1:2380,etcd2=http://etcd2:2380,etcd3=http://etcd3:2380 \\
--initial-cluster-state new \
[Install]
WantedBy=multi-user.target
EOF
为了保证网络连通,可以选择关闭防火墙
sudo setenforce 0
sudo sed -i 's/^SELINUX=.*/SELINUX=permissive/g' /etc/selinux/config
也可以选择在防火墙中放通设置的端口, 这里是 2379
和 2380
# RHEL / CentOS / Fedora firewalld
sudo firewall-cmd --add-port={2379,2380}/tcp --permanent
sudo firewall-cmd --reload
# Ubuntu/Debian
sudo ufw allow proto tcp from any to any port 2379,2380
启动服务
sudo systemctl daemon-reload
sudo systemctl enable etcd
sudo systemctl start etcd
查看状态
$ systemctl status etcd -l
● etcd.service - etcd service
Loaded: loaded (/etc/systemd/system/etcd.service; disabled; vendor preset: disabled)
Active: active (running) since Mon 2019-06-03 18:20:49 UTC; 30s ago
Docs: https://github.com/etcd-io/etcd
Main PID: 5931 (etcd)
CGroup: /system.slice/etcd.service
└─5931 /usr/local/bin/etcd --name etcd1 --data-dir=/var/lib/etcd --initial-advertise-peer-urls http://192.168.18.9:2380 --listen-peer-urls http://192.168.18.9:2380 --listen-client-urls http://192.168.18.9:2379,http://127.0.0.1:2379 --advertise-client-urls http://192.168.18.9:2379 --initial-cluster-token etcd-cluster-0 --initial-cluster etcd1=http://etcd1:2380,etcd2=http://etcd2:2380,etcd3=http://etcd3:2380 --initial-cluster-state new
测试集群状态
查询集群成员
$ etcdctl member list
152d6f8123c6ac97: name=etcd3 peerURLs=http://etcd3:2380 clientURLs=http://192.168.18.11:2379 isLeader=false
332a8a315e569778: name=etcd2 peerURLs=http://etcd2:2380 clientURLs=http://192.168.18.10:2379 isLeader=false
aebb404b9385ccd4: name=etcd1 peerURLs=http://etcd1:2380 clientURLs=http://192.168.18.9:2379 isLeader=true
假如你更换了接口,在使用 etcdctl
命令的时候需要指名接口,以下是查询集群状态
[root@TENCENT64 ~]# etcdctl --endpoints=127.0.0.1:2479 endpoint health
127.0.0.1:2479 is healthy: successfully committed proposal: took = 2.891858ms
[root@TENCENT64 ~]# etcdctl --endpoints=127.0.0.1:2479 endpoint status
127.0.0.1:2479, ce1e2f331994aef9, 3.4.10, 20 kB, true, false, 10, 9, 9,
etcd常见错误
cannot access data directory: directory "/application/kubernetes/data/", "drwxr-xr-x" exist without desired file permission "-rwx------".
解决:将权限设置为700即可
Failed at step CHDIR spawning /usr/bin/etcd: No such file or directory
原因: etcd.service
服务配置文件中设置的工作目录 WorkingDirectory=/var/lib/etcd/
必须存在,否则会报以上错误
health check for peer 90f44a176868590 could not connect: dial tcp xxxxx:2380: connect: connection refused (prober "ROUND_T
解决:删除 /var/lib/etcd
目录下的文件,然后启动 etcd
就可以了,再继续添加节点
conflicting environment variable "ETCD_NAME" is shadowed by corresponding command-line flag (either unset environment variable or disable flag)
原因: ETCD3.4
版本会自动读取环境变量的参数,来自 etcd
的配置文件 etcd.conf
,方便 etcd.service
文件中直接引用、方便后期维护。所以文件中有的参数,不需要再次在 ExecStart
启动参数中添加,二选一,如同时配置,会触发类似报错
etcd监听端口失败, 启动etcd提示 listen tcp 192.168.1.11:2380: bind: cannot assign requested address
请检查 --initial-cluster
参数外其他参数应是自己的IP地址是否正确,检查 --name=
是否重复
publish error: etcdserver: request timed out
我部署时先使用一台进行测试,无论如何启动etcd都无法启动。后来发现 etcd.service
中指定了其他 etcd
主机,所以当单独启动一台是连接其他主机不通,那么 etcd
就启动失败。所以要把 etcd
集群中所有主机都配置好,同时启动就OK了。
常用命令
查看版本号
v3
./etcdctl version
V2
./etcdctl --version
查看集群成员信息
./etcdctl member list
查看集群状态(Leader节点)
V3
./etcdctl --write-out=table endpoint status
V2
./etcdctl cluster-health
查看leader状态
curl http://127.0.0.1:2379/v2/stats/leader
查看自己的状态
curl http://127.0.0.1:2379/v2/stats/self
参数 --endpoints (远程调用)
./etcdctl --endpoints=127.0.0.1:2379 version
./etcdctl --endpoints=192.168.118.131:2379,192.168.118.132:2379,192.168.118.133:2379 --write-out=table endpoint status
引用
etcd集群配置etcd错误总结
Setup Etcd Cluster on CentOS 7/8 / Ubuntu 18.04/16.04 / Debian 10/9
安装etcd集群
评论已关闭!