192.172.0.84/85/86搭建etcd集群
--permanent永久生效,没有此参数重启后失效,防火墙打开2379和2380端口,同时刷新防火墙
firewall-cmd --zone=public --add-port=2379/tcp --permanent
firewall-cmd --zone=public --add-port=2380/tcp --permanent
firewall-cmd --reload
下载并解压etcd
cd /home/vitess/
wget https://github.com/etcd-io/etcd/releases/download/v3.4.1/etcd-v3.4.1-linux-amd64.tar.gz
tar -zxvf etcd-v3.4.1-linux-amd64.tar.gz -C /usr/local/
mv /usr/local/etcd-v3.4.1-linux-amd64 /usr/local/etcd
添加环境变量
vi /etc/profile
在最后添加一行
export ETCDCTL_API=3
执行source /etc/profile刷新环境变量
mkdir -p /var/lib/etcd/
mkdir -p /etc/etcd/
将etcd加入开机自启动中,创建对应的service文件
tee /usr/lib/systemd/system/etcd.service <<-'EOF'
[Unit]
Description=Etcd Server
After=network.target
After=network-online.target
Wants=network-online.target
[Service]
Type=notify
WorkingDirectory=/var/lib/etcd/
EnvironmentFile=-/etc/etcd/etcd.conf
ExecStart=/bin/bash -c "GOMAXPROCS=$(nproc) /usr/local/etcd/etcd --name=${NAME} --data-dir=${DATA_DIR} --initial-advertise-peer-urls ${INITIAL_ADVERTISE_PEER_URLS} --listen-client-urls=${LISTEN_CLIENT_URLS} --listen-peer-urls=${LISTEN_PEER_URLS} --advertise-client-urls=${ADVERTISE_CLIENT_URLS} --initial-cluster-token=${INITIAL_CLUSTER_TOKEN} --initial-cluster=${INITIAL_CLUSTER} --initial-cluster-state=${INITIAL_CLUSTER_STATE} "
Restart=on-failure
LimitNOFILE=65536
[Install]
WantedBy=multi-user.target
EOF
上面的开机启动文件中使用了/etc/etcd/etcd.conf文件,下面是具体的conf文件,每台机器对应的IP地址是不一样的
tee /etc/etcd/etcd.conf <<-'EOF'
84节点
NAME=etcd84
DATA_DIR="/var/lib/etcd/default.etcd"
LISTEN_PEER_URLS="http://192.172.0.84:2380"
LISTEN_CLIENT_URLS="http://192.172.0.84:2379,http://127.0.0.1:2379"
INITIAL_ADVERTISE_PEER_URLS="http://192.172.0.84:2380"
ADVERTISE_CLIENT_URLS="http://192.172.0.84:2379"
INITIAL_CLUSTER_STATE="new"
INITIAL_CLUSTER_TOKEN="etcd-cluster1"
INITIAL_CLUSTER="etcd85=http://192.172.0.85:2380,etcd86=http://192.172.0.86:2380,etcd84=http://192.172.0.84:2380"
EOF
tee /etc/etcd/etcd.conf <<-'EOF'
85节点
NAME=etcd85
DATA_DIR="/var/lib/etcd/default.etcd"
LISTEN_PEER_URLS="http://192.172.0.85:2380"
LISTEN_CLIENT_URLS="http://192.172.0.85:2379,http://127.0.0.1:2379"
INITIAL_ADVERTISE_PEER_URLS="http://192.172.0.85:2380"
ADVERTISE_CLIENT_URLS="http://192.172.0.85:2379"
INITIAL_CLUSTER_STATE="new"
INITIAL_CLUSTER_TOKEN="etcd-cluster1"
INITIAL_CLUSTER="etcd85=http://192.172.0.85:2380,etcd86=http://192.172.0.86:2380,etcd84=http://192.172.0.84:2380"
EOF
tee /etc/etcd/etcd.conf <<-'EOF'
86节点
NAME=etcd86
DATA_DIR="/var/lib/etcd/default.etcd"
LISTEN_PEER_URLS="http://192.172.0.86:2380"
LISTEN_CLIENT_URLS="http://192.172.0.86:2379,http://127.0.0.1:2379"
INITIAL_ADVERTISE_PEER_URLS="http://192.172.0.86:2380"
ADVERTISE_CLIENT_URLS="http://192.172.0.86:2379"
INITIAL_CLUSTER_STATE="new"
INITIAL_CLUSTER_TOKEN="etcd-cluster1"
INITIAL_CLUSTER="etcd85=http://192.172.0.85:2380,etcd86=http://192.172.0.86:2380,etcd84=http://192.172.0.84:2380"
EOF
通过systemctl start命令启动etcd服务,同时使用systemctl enable 命令将服务加入开机启动列表中
systemctl daemon-reload
systemctl enable etcd
systemctl start etcd
systemctl status etcd
查看集群信息
./etcdctl member list