DEV Community

Cover image for How to install etcd cluster for dummies
giveitatry
giveitatry

Posted on

How to install etcd cluster for dummies

Introduction to etcd

What is etcd?

etcd is a distributed, reliable key-value store designed to safely store and manage the configuration data required by distributed systems. It provides a consistent and highly available data store for cluster management.

Why Do We Need etcd?

etcd is the backbone of many distributed systems like Kubernetes. Here’s why:

  • Consistency: Guarantees that data is the same across all nodes in the cluster.
  • Fault Tolerance: Ensures availability even if some nodes fail.
  • Coordination: Helps in leader election, service discovery, and distributed locking.

In essence, etcd ensures your distributed system operates smoothly, even under challenging network conditions.


Prerequisites for an etcd Cluster

  1. Three Nodes: For high availability, etcd requires at least three nodes.
  2. Node Details:
    • Node1: IP 192.168.1.20
    • Node2: IP 192.168.1.21
    • Node3: IP 192.168.1.22
  3. Root Privileges: Ensure you have sudo or root access on all nodes.
  4. Basic Networking: All nodes should be able to communicate with each other via their respective IP addresses.

Step-by-Step Installation Guide

Preparing the Nodes

Before starting, ensure that:

  • All nodes have their hostnames set correctly (e.g., node1, node2, node3).
  • Networking is configured so nodes can communicate without issues.

Installation Script: etcd-install.sh

The script provided automates the installation and configuration process. It ensures consistency and reduces errors. Here’s the complete script:

#!/bin/bash

# Script for setting up etcd and k3s cluster on 3 nodes
# Usage: Run this script as root or with sudo privileges on each node.

# Variables
ETCD_VER="v3.5.17"
NODE_NAME=$1
NODE_IP=$2
CLUSTER_NODES="node1=http://192.168.1.20:2380,node2=http://192.168.1.21:2380,node3=http://192.168.1.22:2380"

GOOGLE_URL="https://storage.googleapis.com/etcd"
ETCD_BINARY_URL="${GOOGLE_URL}/${ETCD_VER}/etcd-${ETCD_VER}-linux-amd64.tar.gz"
ETCD_DIR="/var/lib/etcd"
ETCD_CONFIG_DIR="/etc/etcd"
ETCD_SERVICE="/etc/systemd/system/etcd.service"

# Check if Node Name and IP are provided
if [ -z "$NODE_NAME" ] || [ -z "$NODE_IP" ]; then
    echo "Usage: $0 <NODE_NAME> <NODE_IP>"
    exit 1
fi

# Step 1: Install etcd
echo "Installing etcd on $NODE_NAME..."

# Download and install etcd binaries
curl -L $ETCD_BINARY_URL -o /tmp/etcd.tar.gz
rm -rf /tmp/etcd-download && mkdir -p /tmp/etcd-download
tar xzvf /tmp/etcd.tar.gz -C /tmp/etcd-download --strip-components=1

sudo mv /tmp/etcd-download/etcd /tmp/etcd-download/etcdctl /usr/local/bin/

# Create necessary directories
sudo mkdir -p $ETCD_DIR
sudo mkdir -p $ETCD_CONFIG_DIR

# Create etcd environment file
sudo tee $ETCD_CONFIG_DIR/etcd.env > /dev/null <<EOF
ETCD_NAME=$NODE_NAME
ETCD_DATA_DIR=$ETCD_DIR
ETCD_INITIAL_CLUSTER=$CLUSTER_NODES
ETCD_INITIAL_CLUSTER_STATE=new
ETCD_INITIAL_ADVERTISE_PEER_URLS=http://$NODE_IP:2380
ETCD_LISTEN_PEER_URLS=http://0.0.0.0:2380
ETCD_ADVERTISE_CLIENT_URLS=http://$NODE_IP:2379
ETCD_LISTEN_CLIENT_URLS=http://0.0.0.0:2379
EOF

# Create etcd systemd service
sudo tee $ETCD_SERVICE > /dev/null <<EOF
[Unit]
Description=etcd
After=network.target

[Service]
EnvironmentFile=$ETCD_CONFIG_DIR/etcd.env
ExecStart=/usr/local/bin/etcd
Restart=always
RestartSec=5

[Install]
WantedBy=multi-user.target
EOF

# Start and enable etcd service
sudo systemctl daemon-reload
sudo systemctl enable etcd
sudo systemctl start etcd

# Verify etcd status
echo "Checking etcd status..."
etcdctl --endpoints=http://$NODE_IP:2379 endpoint status || echo "Etcd might take a moment to initialize. Retry the command later."

echo "Setup completed for $NODE_NAME ($NODE_IP)."
Enter fullscreen mode Exit fullscreen mode

Running the Script

Step 1: Copy the Script to Each Node

Save the script as etcd-install.sh on all three nodes.

Step 2: Run the Script on Each Node

On each node, run the following command, replacing <NODE_NAME> and <NODE_IP> with the respective details:

sudo bash ./etcd-install.sh <NODE_NAME> <NODE_IP>
Enter fullscreen mode Exit fullscreen mode

Examples:

  • On node1: sudo bash ./etcd-install.sh node1 192.168.1.20
  • On node2: sudo bash ./etcd-install.sh node2 192.168.1.21
  • On node3: sudo bash ./etcd-install.sh node3 192.168.1.22

Verifying the Cluster

Check etcd Service

On each node, check the status of the etcd service:

sudo systemctl status etcd
Enter fullscreen mode Exit fullscreen mode

The service should be running without errors.

Check Cluster Health

From any node, check the health of the etcd cluster:

etcdctl --endpoints=http://192.168.1.20:2379,http://192.168.1.21:2379,http://192.168.1.22:2379 endpoint health
Enter fullscreen mode Exit fullscreen mode

Expected Output:

http://192.168.1.20:2379 is healthy: successfully committed proposal: took = 6ms
http://192.168.1.21:2379 is healthy: successfully committed proposal: took = 6ms
http://192.168.1.22:2379 is healthy: successfully committed proposal: took = 6ms
Enter fullscreen mode Exit fullscreen mode

Conclusion

Congratulations! You now have a fully functional etcd cluster running on three nodes. This setup provides a reliable foundation for distributed systems, ensuring data consistency and high availability.

Remember to monitor your etcd cluster regularly to maintain its health and performance. Happy clustering!

Top comments (0)