1.编写sh脚本
简单的sh配置文件如下:
#!/bin/bash
# 定义节点名称和IP地址
NAME_1=etcd-node-1
HOST_1=127.0.0.1
NAME_2=etcd-node-2
HOST_2=127.0.0.1
NAME_3=etcd-node-3
HOST_3=127.0.0.1
# 定义集群配置
CLUSTER=${NAME_1}=http://${HOST_1}:2380,${NAME_2}=http://${HOST_2}:2382,${NAME_3}=http://${HOST_3}:2384
# 启动每个节点的函数
function start_node {
NAME=$1
HOST=$2
PEER_PORT=$3
CLIENT_PORT=$4
LOG_FILE=$5
etcd --data-dir=data.${NAME} --name ${NAME} \
--initial-advertise-peer-urls http://${HOST}:${PEER_PORT} \
--listen-peer-urls http://${HOST}:${PEER_PORT} \
--advertise-client-urls http://${HOST}:${CLIENT_PORT} \
--listen-client-urls http://${HOST}:${CLIENT_PORT} \
--initial-cluster ${CLUSTER} \
--initial-cluster-state new \
> ${LOG_FILE} 2>&1 &
}
# 启动所有节点
start_node $NAME_1 $HOST_1 2380 2379 "etcd-node-1.log"
start_node $NAME_2 $HOST_2 2382 2381 "etcd-node-2.log"
start_node $NAME_3 $HOST_3 2384 2383 "etcd-node-3.log"
2.使用goreman
2.1安装goreman
首先,需要安装 goreman
。可以通过 go install
命令来安装:
go install github.com/mattn/goreman@latest
检查goreman是否安装成功
which goreman
2.2创建procfile
在当前随机目录创建文件Procfile,其他文件名应该也可。
cat <<EOF > Procfile
node1: etcd --name node1 --initial-advertise-peer-urls http://127.0.0.1:2380 --listen-peer-urls http://127.0.0.1:2380 --advertise-client-urls http://127.0.0.1:2379 --listen-client-urls http://127.0.0.1:2379 --initial-cluster node1=http://127.0.0.1:2380,node2=http://127.0.0.1:2382,node3=http://127.0.0.1:2384 --initial-cluster-state new --data-dir=/tmp/etcd/node1
node2: etcd --name node2 --initial-advertise-peer-urls http://127.0.0.1:2382 --listen-peer-urls http://127.0.0.1:2382 --advertise-client-urls http://127.0.0.1:22379 --listen-client-urls http://127.0.0.1:22379 --initial-cluster node1=http://127.0.0.1:2380,node2=http://127.0.0.1:2382,node3=http://127.0.0.1:2384 --initial-cluster-state new --data-dir=/tmp/etcd/node2
node3: etcd --name node3 --initial-advertise-peer-urls http://127.0.0.1:2384 --listen-peer-urls http://127.0.0.1:2384 --advertise-client-urls http://127.0.0.1:32379 --listen-client-urls http://127.0.0.1:32379 --initial-cluster node1=http://127.0.0.1:2380,node2=http://127.0.0.1:2382,node3=http://127.0.0.1:2384 --initial-cluster-state new --data-dir=/tmp/etcd/node3
EOF
2.3启动 etcd
集群
使用 goreman
读取 Procfile
并启动所有定义的 etcd
节点:
goreman -f /path/to/etcd-cluster/Procfile start
验证 etcd
集群,使用 etcdctl
工具检查集群状态和成员列表,确保所有节点都已成功启动并加入集群:
etcdctl --endpoints=http://127.0.0.1:2379,http://127.0.0.1:22379,http://127.0.0.1:32379 member list
2.4关闭 etcd
集群
goreman -f /path/to/etcd-cluster/Procfile stop
测试集群是否没问题可以按照官网的流程进行尝试。