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

lvs+keepalived

时间:2017-09-12 17:48:00      阅读:248      评论:0      收藏:0      [点我收藏+]

标签:nbsp   res   cas   div   ann   tool   style   add   ble   

环境

10.0.0.20   主负载
10.0.0.21   负负载
10.0.0.22   web
10.0.0.23   web
10.0.0.24   web

部署LVS

10.0.0.20

[root@node1 ~]# yum -y install ipvsadm

进入到 /usr/src 目录查看结果,如果有ip_vs_rr、ip_vs,表示正常 

[root@node1 ~]# cd /usr/src/
[root@node1 src]# lsmod|grep ip_vs
ip_vs 136798 0 
nf_conntrack 105702 1 ip_vs
libcrc32c 12644 2 xfs,ip_vs

如果没有显示加载一下,在此查看

技术分享
[root@node1 src]# lsmod|grep ip_vs
[root@node1 src]# modprobe  ip_vs 
[root@node1 src]# lsmod|grep ip_vs
ip_vs                 136798  0 
nf_conntrack          105702  1 ip_vs
libcrc32c              12644  2 xfs,ip_vs
技术分享

添加VIP(虚拟IP),虚拟IP需和实际IP为一个网段

[root@node1 ~]# ip addr add 10.0.0.28/24 dev eth0 label eth0:0

查看虚拟IP

技术分享
[root@node1 ~]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:29:39:b8 brd ff:ff:ff:ff:ff:ff
    inet 10.0.0.20/24 brd 10.0.0.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet 10.0.0.28/24 scope global secondary eth0:0
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fe29:39b8/64 scope link 
       valid_lft forever preferred_lft forever
技术分享

清空ipvsadm记录

[root@node1 ~]# ipvsadm -C

配置ipvs,并查看

技术分享
[root@node1 ~]# ipvsadm -A -t 10.0.0.28:80 -s wrr
[root@node1 ~]# ipvsadm -a -t 10.0.0.28:80 -r 10.0.0.22:80 -g -w 1
[root@node1 ~]# ipvsadm -a -t 10.0.0.28:80 -r 10.0.0.23:80 -g -w 1
[root@node1 ~]# ipvsadm -a -t 10.0.0.28:80 -r 10.0.0.24:80 -g -w 1 [root@node1 ~]# ipvsadm -Ln IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn TCP 10.0.0.28:80 wrr -> 10.0.0.22:80 Route 1 0 0 -> 10.0.0.23:80 Route 1 0 0
-> 10.0.0.24:80 Route 1 0 0
技术分享

加入开机启动

[root@node1 ~]# systemctl enable ipvsadm

Web

绑定虚拟IP(VIP)

[root@node2 ~]# ip addr add 10.0.0.28/32 dev lo label lo:0  每台web都要执行

抑制VIP,防止LVS和RSVIP冲突

[root@node2 ~]# echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore
[root@node2 ~]# echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce
[root@node2 ~]# echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore
[root@node2 ~]# echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce

安装keepalived

[root@node1 ~]# yum -y install keepalived

查看版本

[root@node1 ~]# keepalived -v

 配置 keepalived.conf 文件

[root@node1 ~]# vim /etc/keepalived/keepalived.conf 

! Configuration File for keepalived

global_defs {
   router_id wl
}

vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 100
    nopreempt
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 111
    }
    virtual_ipaddress {
        10.0.0.28
    }
}

virtual_server 10.0.0.28 80 {
    delay_loop 6
    lb_algo wrr
    lb_kind DR
    nat_mask 255.255.255.0
    persistence_timeout 0
    protocol TCP

    real_server 10.0.0.22 80 {
        weight 1
        TCP_CHECK {
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
            connect_port 80
        }
    }
    real_server 10.0.0.23 80 {
        weight 1
        TCP_CHECK {
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
            connect_port 80
        }
    }
    real_server 10.0.0.24 80 {
        weight 1
        TCP_CHECK {
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
            connect_port 80
        }
    }

}

重启keepalived

[root@node1 ~]# systemctl restart keepalived

 

 

 

 

升水、

 

 

 

 

lvs+keepalived

标签:nbsp   res   cas   div   ann   tool   style   add   ble   

原文地址:http://www.cnblogs.com/wanglan/p/7510972.html

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