LVS+Keepalived

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LVS+Keepalived相关的知识,希望对你有一定的参考价值。

关闭dada1 2 heartbeat

[email protected] ~]# /etc/init.d/heartbeat stop

[[email protected] ~]# chkconfig heartbeat off

[[email protected] ~]# tar zxf keepalived-1.2.24.tar.gz

[[email protected] ~]# cd keepalived-1.2.24

[[email protected] keepalived-1.2.24]# ./configure --prefix=/usr/local/keepalived

[[email protected] keepalived-1.2.24]# yum install -y openssl-devel kernel-devel

[[email protected] keepalived-1.2.24]# ./configure --prefix=/usr/local/keepalived

make make install

cd /usr/local/keepalived/etc

[[email protected] etc]# ln -s /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/

[[email protected] etc]# ln -s /usr/local/keepalived/etc/keepalived/ /etc/

[[email protected] etc]# ln -s /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/

[[email protected] etc]# cd rc.d/init.d/

[[email protected] init.d]# chmod +x keepalived /etc/init.d/keepalived脚本加执行权限

cd /usr/local/keepalived

[[email protected] etc]# ll /etc/sysconfig/keepalived

lrwxrwxrwx 1 root root 46 6月   3 12:06 /etc/sysconfig/keepalived -> /usr/local/keepalived/etc/sysconfig/keepalived

[[email protected] etc]# ll /etc/keepalived/keepalived.conf

-rw-r--r-- 1 root root 3598 6月   3 12:02 /etc/keepalived/keepalived.conf

[[email protected] etc]# ll /etc/init.d/keepalived

lrwxrwxrwx 1 root root 48 6月   3 12:08 /etc/init.d/keepalived -> /usr/local/keepalived/etc/rc.d/init.d/keepalived

vim /etc/keepalived/keepalived.conf

 

 

[[email protected] etc]# cd /usr/local/keepalived/

[[email protected] keepalived]# cd ..

[[email protected] local]# scp -r keepalived/ 172.25.27.2:/usr/local/

[[email protected] local]# cd /etc/keepalived/

[[email protected] keepalived]# scp keepalived.conf 172.25.27.2:/etc/keepalived/

[[email protected] keepalived]# ln -s /usr/local/keepalived/sbin/keepalived /sbin/

[[email protected] keepalived]# /etc/init.d/keepalived start

 

 

 

 

[[email protected] ~]# cd /usr/local/

[[email protected] local]# ls

bin  etc  games  include  keepalived  lib  lib64  libexec  sbin  share  src

[[email protected] local]# ln -s /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/

[[email protected] local]# ln -s /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/

[[email protected] local]# chmod +x /etc/init.d/keepalived

[[email protected] local]# mkdir /etc/keepalived

[[email protected] local]# vim /etc/keepalived/keepalived.conf

把主改成从 在改一下优先级

[[email protected] local]# ln -s /usr/local/keepalived/sbin/keepalived /sbin/

 

 

dada3 4上做法相同

[[email protected] ~]# vim /etc/rc.local

 ip addr add 172.25.27.100/32 dev eth0

[[email protected] ~]# /etc/rc.local

ip addr show

 eth0  inet 172.25.27.3/24 brd 172.25.27.255 scope global eth0

       inet 172.25.27.100/32 scope global eth0

arptables管理arp包过滤的软件 过滤arp包 基本思路和iptables一样 不过 arptables处理arp协议有关的包 这些包在iptables中并不会处理  arptables可用于灵活的arp管理

[[email protected] ~]# arptables -A IN -d 172.25.27.100 -j DROP

[[email protected] ~]# arptables -A OUT -s 172.25.27.100 -j mangle --mangle --mangle-ip-s 172.25.27.3   vip看不到 看到的是realserver自己真正的ip172.25.27.3

[[email protected] ~]# /etc/init.d/arptables_jf save

Saving current rules to /etc/sysconfig/arptables:          [  OK  ]

[[email protected] ~]# /etc/init.d/httpd start

 

 dada1 keepalived start 测试

[[email protected] keepalived]# ipvsadm -l

IP Virtual Server version 1.2.1 (size=4096)

Prot LocalAddress:Port Scheduler Flags

  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn

TCP  172.25.27.100:http rr persistent 50

  -> server3.example.com:http     Route   1      0          0         

  -> server4.example.com:http     Route   1      0          0         

如果dada1 2keepalived同时开 vip realservel都在1上 因为1是主 优先级高是100 2backup从优先级低是50  如果1关了 2打开 vip realservel才会跑到2

 

 

! Configuration File for keepalived

 

global_defs {

   notification_email {

     [email protected]     接收警报的email地址 可以添加多个([email protected]

   }

   notification_email_from [email protected]设置邮件的发送地址

   smtp_server 127.0.0.1  设置smtp server地址

   smtp_connect_timeout 30设置连接smtp服务器超时时间

   router_id LVS_DEVEL    load balance的标识ID 用于email警报

   vrrp_skip_check_adv_addr

   vrrp_strict

   vrrp_garp_interval 0

   vrrp_gna_interval 0

}

 

vrrp_instance VI_1 {     备机改为BACKUP,此状态是由priority的值决定的

    state MASTER      当前priority的值小于备机的值,那么将会失去master状态

    interface eth0       HA检测网络接口

    virtual_router_id 89 主,备机的virtual_router_id必须相同,取值0-255

    priority 100   主机的优先级,备份机为50,主机优先机一定要大于备机

    advert_int 1   主备之间的通告间隔秒数

    authentication {  主备切换时的验证

        auth_type PASS 设置验证类型 主要有PASSAH两种

        auth_pass 1111 设置验证密码 在一个vrrp_instance下,masterbackup

    }                  必须使用相同的密码才能通信

    virtual_ipaddress {  设置虚拟IP地址 可以设置多个虚拟IP地址 每行一个

        172.25.27.100

    }

}

virtual_server 172.25.27.100 80 { 定义虚拟服务器

    delay_loop 6     每隔6秒查询realserver状态

lb_algo rr       LVS调度算法 这里使用轮叫

lb_kind DR      LVS是用DR模式

persistence_timeout 50

    protocol TCP

    real_server 172.25.27.3 80 {

        weight 1

        TCP_CHECK {

            connect_timeout 3

            nb_get_retry 3

            delay_before_retry 3

        }

    }

   real_server 172.25.27.4 80 {

        weight 1

        TCP_CHECK {

            connect_timeout 3

            nb_get_retry 3

            delay_before_retry 3

        }

    }

}


本文出自 “阳光蜜汁” 博客,谢绝转载!

以上是关于LVS+Keepalived的主要内容,如果未能解决你的问题,请参考以下文章

LVS DR模式搭建,keepalived + lvs

keepalived + lvs + nginx 主备配置案例

LVS+Keepalived 高可用群集的介绍和搭建步骤

LVS+keepalived做Mysql集群的负载均衡

Linux集群架构

Keepalived LVS+DR合设配置方法以及存在的问题