기술나눔

LVS Keepalived 클러스터

2024-07-08

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

Keepalived 도구 소개
  • LVS 및 HA용으로 특별히 설계된 상태 점검 도구
    • 자동 장애 조치(Failover) 지원
    • 지원 노드 상태 확인(Health Checking)
Keepalived 구현 원리 분석
  • Keepalived는 VRRP 핫 백업 프로토콜을 사용하여 Linux 서버의 다중 머신 핫 백업 기능을 구현합니다.
#主服务器keepalived的安装
[root@localhost ~]# systemctl stop NetworkManager #网络托管 网络管理
[root@localhost ~]# setenforce 0
[root@localhost ~]# systemctl stop firewalld

[root@localhost ~]# yum -y install keepalived ipvsadm
[root@localhost ~]# systemctl enable keepalived

#主服务器keepalived的配置
[root@localhost ~]# cd /etc/keepalived/
[root@localhost keepalived]# cp keepalived.conf keepalived.conf.bak

[root@localhost keepalived]# vi keepalived.conf
global_defs { :w#全局参数
   notification_email {
     [email protected]
     [email protected]
     [email protected]
   }
   notification_email_from [email protected]
   #vrrp_strict #严格模式 禁用单播模式
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id LVS_01 #路由器名称
}

vrrp_instance VI_1 { #示例参数
    state MASTER
    interface ens33
    virtual_router_id 51 #虚拟路由id在群集中需一致
    priority 100   #优先级  0~255
    advert_int 1   #通告发送间隔 
    authentication {     
        auth_type PASS #验证类型
        auth_pass 1111 #验证密码
    }
    virtual_ipaddress {
        192.168.10.172
    }
}

virtual_server 192.168.10.172 80 { #群集ip
    delay_loop 6 #健康检查间隔
    lb_algo wrr #负载均衡算法
    lb_kind DR #负载均衡类型
    nat_mask 255.255.255.0 #非不许
!    persistence_timeout 50
    protocol TCP #虚拟服务器协议

real_server 192.168.10.103 80 { #真实服务器
        weight 1 #权重
        TCP_CHECK { #健康检查
            connect_port 80
            connect_timeout 3  
            nb_get_retry 3  #超时次数
            delay_before_retry 3 #重连时间
        }
    }
real_server 192.168.10.104 80 {
        weight 1
        TCP_CHECK {
            connect_port 80
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }

}
#主服务器内核参数的配置
[root@localhost ~]# vi /etc/sysctl.conf
#添加
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.ens33.send_redirects = 0
[root@localhost ~]# sysctl -p


#开启主服务器的keepalived服务
[root@localhost keepalived]# systemctl start keepalived
[root@localhost keepalived]# ip add show dev ens33
2: ens33: