#!/bin/bash set -e # 에러 발생 시 스크립트 종료 ##### 함수 정의 ##### # 로깅 함수 log_info() { echo -e "\e[32m[INFO] $1\e[0m" } log_warn() { echo -e "\e[33m[WARN] $1\e[0m" } log_error() { echo -e "\e[31m[ERROR] $1\e[0m" exit 1 } # 에러 처리 함수 check_error() { if [ $? -ne 0 ]; then case "$1" in "DOWNLOAD") log_error "Failed to download the required file." ;; "EXECUTION") log_error "Failed to execute the script." ;; "INSTALLATION") log_error "Installation failed." ;; "AWS_CLI") log_error "AWS CLI setup failed." ;; "EXTRACT") log_error "Failed to extract the archive." ;; "COMPILE") log_error "Failed to compile the source code." ;; "CONFIG") log_error "Configuration failed." ;; *) log_error "An unknown error occurred." ;; esac fi } # 파일 다운로드 함수 download_file() { local url="$1" local output="${2:-${url##*/}}" log_info "Downloading: $url" wget -q --show-progress -N "$url" -O "$output" check_error "DOWNLOAD" } ##### sudo 권한 확인 ##### if [ "$EUID" -ne 0 ]; then log_error "Please run the script with sudo or as root." fi log_info "Starting LVS and Keepalived installation script..." ##### 필요한 패키지 설치 ##### log_info "Installing required packages..." apt-get update apt-get install -y dstat build-essential libssl-dev libnl-3-dev libnl-genl-3-dev libsnmp-dev libnfnetlink-dev linux-headers-$(uname -r) check_error "INSTALLATION" log_info "Required packages installation completed" ##### Keepalived 설치 ##### log_info "Starting Keepalived 2.3.3 installation..." # Keepalived 소스 다운로드 LVS_PACKAGE_URL="http://api.cws.gscdn.com/api/injun/Ubuntu_NAVER_LIVECLOUD_LVS/keepalived-2.3.3.tar.gz" LVS_PACKAGE_TAR="/root/keepalived-2.3.3.tar.gz" log_info "Downloading LVS package..." download_file "$LVS_PACKAGE_URL" "$LVS_PACKAGE_TAR" # 압축 해제 log_info "Extracting LVS package..." cd /root tar -zxvf "$LVS_PACKAGE_TAR" check_error "EXTRACT" # 디렉토리 이동 및 컴파일 cd /root/keepalived-2.3.3 log_info "Configuring and compiling Keepalived..." ./configure check_error "CONFIG" make check_error "COMPILE" log_info "Installing Keepalived..." make install check_error "INSTALLATION" # 설정 파일 및 서비스 설정 log_info "Setting up Keepalived service..." mkdir -p /etc/keepalived # 이전 설정 파일 다운로드 KEEPALIVED_CONF_URL="http://api.cws.gscdn.com/api/injun/Ubuntu_NAVER_LIVECLOUD_LVS/keepalived.conf" log_info "Downloading Keepalived configuration file..." download_file "$KEEPALIVED_CONF_URL" "/etc/keepalived/keepalived.conf" log_info "Downloaded configuration file to /etc/keepalived/keepalived.conf" # systemd 서비스 파일 설정 if [ -f keepalived/etc/init.d/keepalived.service ]; then cp keepalived/etc/init.d/keepalived.service /etc/systemd/system/ else log_warn "systemd service file not found. Creating one manually." cat > /etc/systemd/system/keepalived.service << EOF [Unit] Description=Keepalived VRRP Framework After=network-online.target Wants=network-online.target [Service] Type=forking PIDFile=/run/keepalived.pid KillMode=process EnvironmentFile=-/etc/sysconfig/keepalived ExecStart=/usr/local/sbin/keepalived \$KEEPALIVED_OPTIONS ExecReload=/bin/kill -HUP \$MAINPID [Install] WantedBy=multi-user.target EOF log_info "Created default systemd service file." fi # systemd 재로드 및 서비스 활성화 log_info "Enabling Keepalived service..." systemctl daemon-reload systemctl enable keepalived check_error "INSTALLATION" # 설치 확인 log_info "Verifying Keepalived installation..." KEEPALIVED_VERSION=$(keepalived --version 2>&1 | head -n 1) if [ $? -eq 0 ]; then log_info "Keepalived successfully installed: $KEEPALIVED_VERSION" else log_warn "Cannot verify Keepalived version." fi ##### Configure Network Interfaces ##### log_info "Configuring network interfaces for optimal performance..." # Find all network interfaces (excluding loopback) log_info "Detecting network interfaces..." INTERFACES=$(ip -o link show | grep -v "lo:" | awk -F': ' '{print $2}') # Create an array to store active interfaces ACTIVE_INTERFACES=() DEFAULT_INTERFACE="" # Find the default route interface log_info "Detecting default route interface..." DEFAULT_INTERFACE=$(ip route | grep default | awk '{print $5}' | head -n 1) if [ ! -z "$DEFAULT_INTERFACE" ]; then log_info "Default route uses interface: $DEFAULT_INTERFACE" else log_warn "Could not determine default route interface" fi # Check each interface for active status and add to array for INTERFACE in $INTERFACES; do # Check if interface is up and has an IP address IP_INFO=$(ip addr show $INTERFACE 2>/dev/null | grep "inet " | head -n 1) LINK_STATE=$(cat /sys/class/net/$INTERFACE/operstate 2>/dev/null) if [ ! -z "$IP_INFO" ] && [ "$LINK_STATE" = "up" ]; then IP_ADDR=$(echo $IP_INFO | awk '{print $2}' | cut -d/ -f1) log_info "Found active interface: $INTERFACE ($IP_ADDR)" ACTIVE_INTERFACES+=("$INTERFACE") else log_info "Interface $INTERFACE is not active or has no IP address. Skipping." fi done # Display summary of detected interfaces log_info "Detected interfaces summary:" log_info "- Total interfaces found: $(echo $INTERFACES | wc -w)" log_info "- Active interfaces found: ${#ACTIVE_INTERFACES[@]}" if [ ${#ACTIVE_INTERFACES[@]} -eq 0 ]; then log_warn "No active interfaces found. Cannot configure ring buffers." log_info "LVS integration installation completed without NIC configuration." exit 0 fi # Interface selection logic SELECTED_INTERFACES=() # Ask user if multiple active interfaces are found if [ ${#ACTIVE_INTERFACES[@]} -gt 1 ]; then log_info "Multiple active interfaces detected. Please select which ones to configure:" echo -e "\e[33m[IMPORTANT] Select interfaces to configure (space-separated numbers, or 'a' for all):\e[0m" # Display list of active interfaces for i in "${!ACTIVE_INTERFACES[@]}"; do INTERFACE=${ACTIVE_INTERFACES[$i]} SPEED=$(ethtool $INTERFACE 2>/dev/null | grep "Speed:" | awk '{print $2}') SPEED=${SPEED:-"Unknown"} # Mark default interface with an asterisk if [ "$INTERFACE" = "$DEFAULT_INTERFACE" ]; then echo " $((i+1))) $INTERFACE - Speed: $SPEED (default route *)" else echo " $((i+1))) $INTERFACE - Speed: $SPEED" fi done # Get user selection read -p "Enter selection: " SELECTION if [ "$SELECTION" = "a" ] || [ "$SELECTION" = "A" ]; then SELECTED_INTERFACES=("${ACTIVE_INTERFACES[@]}") log_info "Selected all active interfaces for configuration" else # Process space-separated list of numbers for NUM in $SELECTION; do if [[ "$NUM" =~ ^[0-9]+$ ]] && [ "$NUM" -ge 1 ] && [ "$NUM" -le ${#ACTIVE_INTERFACES[@]} ]; then SELECTED_INTERFACES+=("${ACTIVE_INTERFACES[$((NUM-1))]}") else log_warn "Invalid selection: $NUM - skipping" fi done fi else # If only one active interface, use it automatically SELECTED_INTERFACES=("${ACTIVE_INTERFACES[@]}") log_info "Single active interface detected. Will configure ${SELECTED_INTERFACES[0]}" fi # Configure selected interfaces log_info "Configuring selected interfaces..." for INTERFACE in "${SELECTED_INTERFACES[@]}"; do log_info "Checking interface: $INTERFACE" # Get interface speed SPEED=$(ethtool $INTERFACE 2>/dev/null | grep "Speed:" | awk '{print $2}') if [ -z "$SPEED" ]; then log_warn "Could not determine speed for interface $INTERFACE. Skipping configuration." continue fi log_info "Detected speed for $INTERFACE: $SPEED" # Configure ring buffer based on NIC speed if [[ "$SPEED" == "10000Mb/s" || "$SPEED" == "10Gb/s" ]]; then log_info "Configuring 10G NIC ring buffer for $INTERFACE..." ethtool -G $INTERFACE rx 4096 if [ $? -eq 0 ]; then log_info "Successfully set ring buffer size to 4096 for 10G interface $INTERFACE" else log_warn "Failed to set ring buffer size for interface $INTERFACE" fi elif [[ "$SPEED" == "25000Mb/s" || "$SPEED" == "25Gb/s" ]]; then log_info "Configuring 25G NIC ring buffer for $INTERFACE..." ethtool -G $INTERFACE rx 8192 if [ $? -eq 0 ]; then log_info "Successfully set ring buffer size to 8192 for 25G interface $INTERFACE" else log_warn "Failed to set ring buffer size for interface $INTERFACE" fi else log_info "Interface $INTERFACE speed ($SPEED) does not require ring buffer adjustment" fi done log_info "Network interface configuration completed" ## 서비스 시작 #log_info "Starting Keepalived service..." #systemctl start keepalived #if [ $? -eq 0 ]; then # log_info "Keepalived service started successfully." #else # log_warn "Failed to start Keepalived service. Check logs with journalctl -xe." #fi # log_info "Keepalived integration installation complete. Please manually configure Keepalived settings before restarting."