lvs+keepalived 配置HA

lvs+keepalived 配置HA


参考资料:sery.blog.51cto.com 田哥文章,非常感谢!
详细keepalived参数配置,详见sery.blog.51cto.com

lvs server
192.168.1.11 lvs master vip 192.168.1.201 soft ipvsadm+keepalived
192.168.1.22 lvs mackup vip 192.168.1.201 soft ipvsadm+keepalived

web server
192.168.1.33 vip 192.168.1.201
192.168.1.44 vip 192.168.1.201

lvs server安装

ipvsadm 安装
wget http: //www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.24.tar.gz
创建连接文件,否则编译会出错,要与当前内核一致(2.6.18-92.el5PAE-i686 )。
ln �Csv /usr/src/kernels/2.6.18-92.el5PAE-i686 /usr/src/linux
tar zxvf ipvsadm-1.24.tar.gz
cd ipvsadm-1.24
./configure
make && make install

keepalived 安装
 
wget http: //www.keepalived.org/software/keepalived-1.1.17.tar.gz
tar zxvf keepalived-1.1.17.tar.gz
cd keepalived-1.1.17
./configure --prefix=/usr/local/keepalived
make && make install

lvs 客户端脚本配置文件

more /usr/local/bin/lvs_real
#!/bin/bash

VIP1=192.168.1.201
/etc/rc.d/init.d/functions
case "$1" in
start)
echo
" start LVS of REALServer"
/sbin/ifconfig lo:0 $VIP1 broadcast $VIP1 netmask 255.255.255.255 up
echo
"1" >/proc/sys/net/ipv4/conf/lo/arp_ignore
echo
"2" >/proc/sys/net/ipv4/conf/lo/arp_announce
echo
"1" >/proc/sys/net/ipv4/conf/all/arp_ignore
echo
"2" >/proc/sys/net/ipv4/conf/all/arp_announce
;;
stop)
/sbin/ifconfig lo:0 down
echo
"close LVS Directorserver"
echo
"0" >/proc/sys/net/ipv4/conf/lo/arp_ignore
echo
"0" >/proc/sys/net/ipv4/conf/lo/arp_announce
echo
"0" >/proc/sys/net/ipv4/conf/all/arp_ignore
echo
"0" >/proc/sys/net/ipv4/conf/all/arp_announce
;;
*)
echo
"Usage: $0 {start|stop}"
exit 1
esac

lvs 客户端测试

执行lvs_real start

使用ip a 命令要看,如下图所示
[root@web01 ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue
        link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
        inet 127.0.0.1/8 scope host lo
      
  inet 192.168.1.201/32 brd 192.168.1.201 scope global lo:0
        inet6 ::1/128 scope host

keepalived master配置

global_defs {
router_id LVS_master_1
}
vrrp_sync_group VGM {
group {
VI_CACHE
}
}
##############################################
# vvrp_instance define #
##############################################
vrrp_instance VI_CACHE {
state MASTER
interface eth0
lvs_sync_daemon_inteface eth0
virtual_router_id 51
priority 180            
advert_int 5

authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.201
}
}
##############################################
# virtual machine setting #
##############################################
# setting port 80 forward
virtual_server 192.168.1.201 80 {
delay_loop 6
lb_algo wlc
lb_kind DR
# persistence_timeout 20
protocol TCP
real_server 192.168.1.33 80 {
weight 100
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 192.168.1.44 80 {
weight 100
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}

}

keepalived backup配置

global_defs {
router_id LVS_backup_2
}
vrrp_sync_group VGM {
group {
VI_CACHE
}
}
##############################################
# vrrp_instance setting #
##############################################
vrrp_instance VI_CACHE {
state BACKUP
interface eth0
lvs_sync_daemon_inteface eth0
virtual_router_id 51
priority 150
advert_int 5

authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.201
}
}
##############################################
# virtual server setting #
##############################################
# setting port 80 forward
virtual_server 192.168.1.201 80 {
delay_loop 6
lb_algo wlc
lb_kind DR
# persistence_timeout 50
protocol TCP
real_server 192.168.1.33 80 {
weight 100
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 192.168.1.44 80 {
weight 100
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}

}

测试keepalived 主从配置
1、当master 宕机时,backup会自动接管master工作,
2、当master 恢复时,backup会自动回到backup状态,master会自动接管工作

ps -ef | grep keepalived | grep -v grep | awk '{print $2}'
22380
22381
22382

kill -9 22380
kill -9 22381
kill -9 22382


结束keepalived进程

backup 日志

tail -f /var/log/message
Mar 22 18:24:00 ws Keepalived_vrrp: VRRP_Instance(VI_CACHE) Transition to MASTER STATE
Mar 22 18:24:00 ws Keepalived_vrrp: VRRP_Group(VGM) Syncing instances to MASTER state
Mar 22 18:24:05 ws Keepalived_vrrp: VRRP_Instance(VI_CACHE) Entering MASTER STATE
Mar 22 18:24:05 ws Keepalived_vrrp: Netlink: skipping nl_cmd msg...
Mar 22 18:24:05 ws avahi-daemon[2758]: Registering
new address record for 192.168.1.201 on eth0.
Mar 22 18:24:05 ws avahi-daemon[2758]: Withdrawing address record
for fe80::2c0:9fff:fe44:7611 on eth0.
Mar 22 18:24:05 ws avahi-daemon[2758]: Withdrawing address record
for 192.168.1.22 on eth0.

从新启动master keepalived

这时查看backup日志,已经由master状态转换为backup状态了
tail -f /var/log/message
Mar 22 18:34:30 ws Keepalived_vrrp: VRRP_Instance(VI_CACHE) Received higher prio advert
Mar 22 18:34:30 ws Keepalived_vrrp: VRRP_Instance(VI_CACHE) Entering BACKUP STATE
Mar 22 18:34:30 ws Keepalived_vrrp: VRRP_Group(VGM) Syncing instances to BACKUP state
Mar 22 18:34:30 ws Keepalived_vrrp: Netlink: skipping nl_cmd msg...
Mar 22 18:34:30 ws avahi-daemon[2758]: Server startup complete. Host name is ws-12135.local. Local service cookie is 2829300000.
Mar 22 18:34:30 ws avahi-daemon[2758]: Withdrawing address record
for 192.168.1.201 on eth0.
Mar 22 18:34:31 ws avahi-daemon[2758]: Service
"SFTP File Transfer on ws-12135" (/services/sftp-ssh.service) successfully established.

master日志,已经为master状态
Mar 22 18:44:26 vhost Keepalived_vrrp: VRRP_Instance(VI_CACHE) Transition to MASTER STATE
Mar 22 18:44:31 vhost Keepalived_vrrp: VRRP_Instance(VI_CACHE) Entering MASTER STATE
Mar 22 18:44:31 vhost Keepalived_vrrp: Netlink: error: File exists, type=(20), seq=1269254665, pid=0
Mar 22 18:44:31 vhost Keepalived_vrrp: VRRP_Group(VGM) Syncing instances to MASTER state


你可能感兴趣的:(LVS,keepalived,职场,HA,休闲)