Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

One-Click Deployment Script for ETCD Cluster

Tech 1

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

  1. Three machines running CentOS 7.
  2. Install required packages:
    yum install -y epel-release vim bash-completion net-tools lrzsz
    
  3. Disable firewall and SELinux:
    systemctl stop firewalld && systemctl disable firewalld
    
    Edit /etc/selinux/config:
    SELINUX=disabled
    
    Run:
    setenforce 0
    
  4. Synchronize time across nodes:
    yum install -y ntp ntpdate && ntpdate pool.ntp.org
    
  5. 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.

Tags: etcdcluster

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.