One-Click Deployment Script for ETCD Cluster
One-Click Deployment Script for ETCD Cluster
This script automates the deployment of a 3-node ETCD cluster. It downloads the ETCD binary, generates configuration files, and distributes them to each node via SSH.
Prerequisites
- Three machines running CentOS 7.
- Install required packages:
yum install -y epel-release vim bash-completion net-tools lrzsz - Disable firewall and SELinux:
Editsystemctl stop firewalld && systemctl disable firewalld/etc/selinux/config:
Run:SELINUX=disabledsetenforce 0 - Synchronize time across nodes:
yum install -y ntp ntpdate && ntpdate pool.ntp.org - Configure SSH passwordless login between the deployment machine and all nodes.
Script Usage
Save the following script as deploy-etcd.sh and run it:
#!/bin/bash
set -x
set -e
# Node IP mapping: replace with your actual IPs
declare -A NODE_MAP=(["etcd0"]="192.168.0.153" ["etcd1"]="192.168.0.154" ["etcd2"]="192.168.0.164")
function download_etcd() {
local ETCD_VER="v3.2.9"
local DOWNLOAD_URL="https://github.com/coreos/etcd/releases/download"
if [[ -f "${PWD}/temp-etcd/etcd" ]]; then
return
fi
curl -L "${DOWNLOAD_URL}/${ETCD_VER}/etcd-${ETCD_VER}-linux-amd64.tar.gz" -o "${PWD}/etcd-${ETCD_VER}-linux-amd64.tar.gz"
mkdir -p "${PWD}/temp-etcd" && tar -zxf "${PWD}/etcd-${ETCD_VER}-linux-amd64.tar.gz" -C "${PWD}/temp-etcd" --strip-components=1
}
function generate_config() {
local node_name=$1
cat > "${PWD}/${node_name}.conf" <<EOF
ETCD_NAME=${node_name}
ETCD_DATA_DIR="/var/lib/etcd"
ETCD_INITIAL_ADVERTISE_PEER_URLS="http://${NODE_MAP[${node_name}]}:2380"
ETCD_LISTEN_PEER_URLS="http://${NODE_MAP[${node_name}]}:2380"
ETCD_LISTEN_CLIENT_URLS="http://${NODE_MAP[${node_name}]}:2379,http://127.0.0.1:2379"
ETCD_ADVERTISE_CLIENT_URLS="http://${NODE_MAP[${node_name}]}:2379"
ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster-378"
ETCD_INITIAL_CLUSTER="etcd0=http://${NODE_MAP['etcd0']}:2380,etcd1=http://${NODE_MAP['etcd1']}:2380,etcd2=http://${NODE_MAP['etcd2']}:2380"
ETCD_INITIAL_CLUSTER_STATE="new"
EOF
}
function generate_systemd_unit() {
cat > "${PWD}/etcd.service" <<'EOF'
[Unit]
Description=Etcd Server
After=network.target
[Service]
Type=notify
WorkingDirectory=/var/lib/etcd
EnvironmentFile=-/etc/etcd/10-etcd.conf
ExecStart=/usr/bin/etcd
Restart=always
RestartSec=8s
LimitNOFILE=40000
[Install]
WantedBy=multi-user.target
EOF
}
SSH_OPTS="-oStrictHostKeyChecking=no -oUserKnownHostsFile=/dev/null -oLogLevel=ERROR -C"
function remote_copy() {
local host=$1
local src=($2)
local dst=$3
scp -r ${SSH_OPTS} ${src[*]} "${host}:${dst}"
}
function remote_exec() {
local host=$1
shift
ssh ${SSH_OPTS} -t "${host}" "$@" >/dev/null 2>&1
}
function remote_exec_background() {
local host=$1
shift
ssh ${SSH_OPTS} -t "${host}" "nohup $@" >/dev/null 2>&1 &
}
function deploy() {
for node_name in "${!NODE_MAP[@]}"; do
generate_config "${node_name}"
remote_exec "root@${NODE_MAP[$node_name]}" "mkdir -p /var/lib/etcd /etc/etcd"
remote_copy "root@${NODE_MAP[$node_name]}" "${node_name}.conf" "/etc/etcd/10-etcd.conf"
remote_copy "root@${NODE_MAP[$node_name]}" "${PWD}/temp-etcd/etcd ${PWD}/temp-etcd/etcdctl" "/usr/bin/"
remote_copy "root@${NODE_MAP[$node_name]}" "etcd.service" "/usr/lib/systemd/system/"
remote_exec "root@${NODE_MAP[$node_name]}" "chmod 755 /usr/bin/etcd*"
remote_exec_background "root@${NODE_MAP[$node_name]}" "systemctl daemon-reload && systemctl enable etcd && nohup systemctl start etcd"
done
}
function cleanup() {
for node_name in "${!NODE_MAP[@]}"; do
rm -f "${PWD}/${node_name}.conf"
done
rm -f "${PWD}/etcd.service"
}
# Uncomment the line below to enable download
# download_etcd
generate_systemd_unit
deploy
cleanup
echo -e "\033[32m Deployment completed! Run 'etcdctl cluster-health' to verify.\033[0m"
Verifciation
After the script finishes, SSH into any node and run:
etcdctl cluster-health
The output should indicate a healthy cluster.