码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2017-02-05 01:11:03      阅读:372      评论:0      收藏:0      [点我收藏+]

标签: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 {

        test@163.com

        }

        notification_email_from root@localhost

        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

标签:keepalived双机热备原理及实例部署lvs+keepalived

原文地址:http://superleedo.blog.51cto.com/12164670/1894886

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!