YUM安装
# yum安装
yum -y install keepalived
# 查看安装版本
rpm -qa keepalived
# 查看安装路径
rpm -ql keepalived
或是使用源码安装
到这里下载 https://www.keepalived.org/download.html
# 安装依赖
yum -y install gcc openssl-devel libnfnetlink-devel
下载源码包
wget https://www.keepalived.org/software/keepalived-2.2.7.tar.gz # 在这里直接将版本号2.2.7修改想要得版本号即可
3)解压
tar -zxvf keepalived-2.2.7.tar.gz -C /usr/src
4)编译安装
cd /usr/src/keepalived-2.2.7/
./configure && make -j 4 && make install
这里是三台主机做一个虚拟IP得例子(2021年)
修改配置文件内容 /etc/keepalived/keepalived.conf
启动服务使用指令:service keepalived restart
# master节点配置内容:
! Configuration File for keepalived
vrrp_instance VI_1 {state MASTER #这里确定master节点,多个master不知道会不会脑裂,没试interface ens33virtual_router_id 51priority 100advert_int 1unicast_src_ip 192.168.163.134unicast_peer {192.168.163.132192.168.163.133}authentication {auth_type PASSauth_pass 1111 #密码随便,每个主机要求都一样}virtual_ipaddress {192.168.163.135}
}# node1节点配置内容:
! Configuration File for keepalived
vrrp_instance VI_1 {state BACKUP #这里确定backup节点interface ens33virtual_router_id 51priority 50advert_int 1unicast_src_ip 192.168.163.132unicast_peer {192.168.163.133192.168.163.134}authentication {auth_type PASSauth_pass 1111}virtual_ipaddress {192.168.163.135}
}# node2节点配置
! Configuration File for keepalived
vrrp_instance VI_1 {state BACKUP #这里确定backup节点interface ens33virtual_router_id 51priority 50advert_int 1unicast_src_ip 192.168.163.133unicast_peer {192.168.163.132192.168.163.134}authentication {auth_type PASSauth_pass 1111}virtual_ipaddress {192.168.163.135}
}
这里是两台机器做的例子(2022年)
# master节点配置内容:
! Configuration File for keepalivedglobal_defs {notification_email {acassen@firewall.locfailover@firewall.locsysadmin@firewall.loc}notification_email_from Alexandre.Cassen@firewall.locsmtp_server 127.0.0.1smtp_connect_timeout 30router_id LVS_DEVELvrrp_skip_check_adv_addrvrrp_strictvrrp_garp_interval 0vrrp_gna_interval 0
}vrrp_script web {script "/etc/keepalived/check_web_port.sh"interval 1weight -20vrrp_instance VI_1 {state MASTERinterface ens33virtual_router_id 51priority 100advert_int 1authentication {auth_type PASSauth_pass 1111}virtual_ipaddress {192.168.163.11}track_script {web}
}virtual_server 192.168.163.11 8080 {delay_loop 6lb_algo rrlb_kind NATpersistence_timeout 50persistence_timeout 1protocol TCPreal_server 192.168.163.136 8080 {weight 1TCP_CHECK {connect_timeout 1nb_get_retry 3delay_before_retry 3connect_port 8080}}real_server 192.168.163.137 8080 {weight 1TCP_CHECK {connect_timeout 1nb_get_retry 3delay_before_retry 3connect_port 8080}}
}# node节点配置内容:
! Configuration File for keepalivedglobal_defs {notification_email {acassen@firewall.locfailover@firewall.locsysadmin@firewall.loc}notification_email_from Alexandre.Cassen@firewall.locsmtp_server 127.0.0.1smtp_connect_timeout 30router_id LVS_DEVELvrrp_skip_check_adv_addrvrrp_strictvrrp_garp_interval 0vrrp_gna_interval 0
}vrrp_script web {script "/etc/keepalived/check_web_port.sh"interval 1weight -20vrrp_instance VI_1 {state BACKUPinterface ens33virtual_router_id 51priority 100advert_int 1authentication {auth_type PASSauth_pass 1111}virtual_ipaddress {192.168.163.11}track_script {web}
}virtual_server 192.168.163.11 8080 {delay_loop 6lb_algo rrlb_kind NATpersistence_timeout 50persistence_timeout 1protocol TCPreal_server 192.168.163.136 8080 {weight 1TCP_CHECK {connect_timeout 1nb_get_retry 3delay_before_retry 3connect_port 8080}}real_server 192.168.163.137 8080 {weight 1TCP_CHECK {connect_timeout 1nb_get_retry 3delay_before_retry 3connect_port 8080}}
}#涉及得脚本 /etc/keepalived/check_web_port.sh"
#!/bin/bash
count=`nmap 127.0.0.1|grep 8080/tcp|wc -l`
if [ $count != 0 ]
thenexit 0
elseexit 1
fi