keepalived双机热备原理及实例部署LVS+keepalived

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了keepalived双机热备原理及实例部署LVS+keepalived相关的知识,希望对你有一定的参考价值。

keepalived简介

它是由C语言编写的路由热备软件,主要提供简单高效的负载均衡及高可用解决方案。keepalived通过VRRP协议实现高可用架构。lvs实现集群分发,keepalived实现调度器的主备方案,两者结合实现集群系统的高负载高可用。

VRRP协议是为了防止单点故障而设计的主从灾备协议,VRRP将两台或者多台路由设备虚拟成一个设备,对外提供一个虚拟路由ip地址,主路由设备处于启用状态时,不断的发送自身信息到备用设备,以表明主路由状态正常,当备用设备收不到主设备的信息时,备用设备会根据自身的优先级选举出新的主设备,并保证提供所有业务功能。



LVS+keepalived实例

服务器名称                                        网络接口                                        IP地址

router外网接口eth1122.126.152.183
内网接口eth0192.168.1.1
lvs_1.test.cometh0192.168.1.5
eth0 VIP192.168.1.2
lvs_2.test.cometh0192.168.1.6
eth0 VIP192.168.1.2
web1.test.cometh0192.168.1.7
lo:0 VIP192.168.1.2
web1.test.cometh0192.168.1.8
lo:0 VIP192.168.1.2

首先安装配置web1和web2

设置web1网路参数及回环设备lo:0设置VIP

vim /etc/sysconfig/network-scripts/ifcfg-eth0

DEVICE=eth0

BOOTPROTO=static

IPADDR=192.168.1.7

NETMASK=255.255.255.0

GATEWAY=192.168.1.1

ONBOOT=yes


vim /etc/sysconfig/network-scripts/ifcfg-lo:0

DEVICE=lo:0

BOOTPROTO=static

IPADDR=192.168.1.2

NETMASK=255.255.255.255

GATEWAY=192.168.1.1

ONBOOT=yes


修改ARP参数

vim /etc/sysctl.conf

net.ipv4.conf.eth0.arp_ignore = 1

net.ipv4.conf.eth0.arp_announce = 2

net.ipv4.conf.all.arp_ignore = 1

net.ipv4.conf.all.arp_announce = 2


执行 sysctl -p

service network restart

iptables -F

iptables -X

service iptables save

setenforce 0

sed -i s/enforcing/disabled/g /etc/sysconfig/selinux

yum install -y httpd

echo "web1  192.168.1.7" > /var/www/html/index.html

service httpd restart

chkconfig httpd on

web2机器上执行与web1相同步骤,注意修改部分参数


lvs_1调度器操作

首先设置网络

vim /etc/sysconfig/network-scripts/ifcfg-eth0

DEVICE=eth0

BOOTPROTO=static

IPADDR=192.168.1.5

NETMASK=255.255.255.0

GATEWAY=192.168.1.1

ONBOOT=yes

TYPE=Ethernet


service network restart

安装部署keepalived ipvsadm

yum install -y gcc openssl-devel popt-devel libnl libnl-devel kernel-devel ipvsadm

下载keepalived

wget http://www.keepalived.org/software/keepalived-1.3.0.tar.gz

tar zxf keepalived-1.3.0.tar.gz -C /usr/src/

cd /usr/src/keepalived-1.3.0/

./configure

make && make install

ln -s /usr/local/etc/keepalived/ /etc/

ln -s /usr/local/etc/rc.d/init.d/keepalived /etc/init.d/

ln -s /usr/local/etc/sysconfig/keepalived /etc/sysconfig/

ln -s /usr/local/sbin/keepalived /usr/sbin/


lvs_1为主设备,lvs_2为从设备,下面修改配置文件

vim /etc/keepalived/keepalived.conf

global_defs {

    notification_email {

        [email protected]

        }

        notification_email_from [email protected]

        smtp_server 127.0.0.1

        smtp_connect_timeout 30

        router_id lvs_1

    }


vrrp_instance LVS_HA {

    state MASTER                #lvs_2为BACKUP

    interface eth0

    virtual_router_id 60        #lvs_2的VRID必须与lvs-1相同

    priority 100                    #lvs-2为从设备,必须小于 lvs-1的参数

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 1111

    }

    virtual_ipaddress {

    192.168.1.2/24

    }

}


virtual_server 192.168.1.2 80 {

    delay_loop 6

    lb_algo rr

    lb_kind DR

    nat_mask 255.255.255.0

    persistence_timeout 50

    protocol TCP

    real_server 192.168.1.7 80 {

        weight 1

    TCP_CHECK {

        connect_timeout 20

        connect_port 80

        nb_get_retry 3

    }

    }

 real_server 192.168.1.8 80 {

        weight 1

    TCP_CHECK {

        connect_timeout 20

        connect_port 80

        nb_get_retry 3

    }

    }

}


service keepalived start

chkconfig keepalived on

ip addr show        #查看虚拟ip

ipvsadm -Ln        #查看lvs规则

iptables -F

iptables -X

service iptables save

setenforce 0

sed -i s/enforcing/disabled/g /etc/sysconfig/selinux

lvs_2机器上执行相同操作,注意修改部分参数


下面配置router机器,这里用做软路由,生产环境直接用硬件路由设备

vim /etc/sysconfig/network-scripts/ifcfg-eth0

DEVICE=eth0

BOOTPROTO=static

IPADDR=192.168.1.1

NETMASK=255.255.255.0

ONBOOT=yes

DNS=8.8.8.8

vim /etc/sysconfig/network-scripts/ifcfg-eth1

DEVICE=eth1

BOOTPROTO=static

IPADDR=122.126.152.183

NETMASK=255.255.255.254

ONBOOT=yes

DNS=8.8.8.8


service network restart

iptables -F

iptables -X

iptables -t nat -F

iptables -t nat -X

iptables -t nat -I PREROUTING -d 122.126.152.183 -p tcp -dport 80 -j DANT --to--destination 192.168.1.2:80

iptables -t nat -I PREROUTING -s 192.168.1.0/24 -p tcp -j SNAT --to-source 122.126.152.183

service iptables save

setenforce 0

sed -i s/enforcing/disabled/g /etc/sysconfig/selinux

vim /etc/sysctl.conf

net.ipv4..ip_forward = 1

sysctl -p


客户端测试

浏览器访问http://122.126.152.183 将得到不同real server的数据包




本文出自 “实用Linux知识技能分享” 博客,请务必保留此出处http://superleedo.blog.51cto.com/12164670/1894886

以上是关于keepalived双机热备原理及实例部署LVS+keepalived的主要内容,如果未能解决你的问题,请参考以下文章

centos 7之keepalived双机热备理论+配置文件详解

基于LVS负载均衡群集来实现Keepalived的部署 技术

Keepalived+lvs 双机热备

LVS负载均衡群集——keepalived+DR模式(实战!)

Keepalived双机热备

Lvs+keepalived+Nginx双机热备实现Nginx高可用