免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 4502 | 回复: 1

Debian 7 (wheezy)上配置LVS + keepalived [复制链接]

论坛徽章:
0
发表于 2014-06-28 11:29 |显示全部楼层

最近测试LVS+Keepalived, 正好才这里找些资料,就把自己的配置过程也分享下。

直接从 http://ox0spy.github.io/debian/2 ... epalived-on-debian/ 负责过来的,CU上格式不太好。

准备机器
LVS_MASTER - 172.16.8.129
LVS_BACKUP - 172.16.8.130
WEB_APP_SERVER_1 - 172.16.8.131
WEB_APP_SERVER_2 - 172.16.8.132
注:

virtual server: 172.16.8.252 (非真实机器, 只需要在web app server上配下这个IP)
所有机器都安装的Debian 7.5 (wheezy)
安装软件
LVS_MASTER上安装配置LVS和keepalived
$ ssh LVS_MASTER
$ sudo apt-get install ipvsadm keepalived
配置LVS
$ cat /etc/default/ipvsadm
# ipvsadm

# if you want to start ipvsadm on boot set this to true
AUTO="false"

# daemon method (none|master|backup)
DAEMON="master"

# use interface (eth0,eth1...)
IFACE="eth0"

# syncid to use
SYNCID="1"
配置keepalived
$ cat /etc/keepalived/keepalived.conf
global_defs {
   router_id LVS_MASTER   #BACKUP上修改为LVS_BACKUP
}

vrrp_instance VI_1 {
    state MASTER          #BACKUP上修改为BACKUP
    interface eth0
    virtual_router_id 51  #与备机的id必须一致
    priority 100          #BACKUP上修改为80
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        172.16.8.252     #virtual server
    }
}

virtual_server 172.16.8.252 80 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
    #lb_kind NAT
    #persistence_timeout 5
    protocol TCP

    real_server 172.16.8.131 80 {
        weight 3
        TCP_CHECK {
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
            connect_port 80
        }
    }

    real_server 172.16.8.132 80 {
        weight 3
        TCP_CHECK {
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
            connect_port 80
        }
    }
}
LVS_BACKUP上安装配置LVS和keepalived
$ ssh LVS_BACKUP
$ sudo apt-get install ipvsadm keepalived
配置LVS
$ cat /etc/default/ipvsadm
# ipvsadm

# if you want to start ipvsadm on boot set this to true
AUTO="false"

# daemon method (none|master|backup)
DAEMON="backup"

# use interface (eth0,eth1...)
IFACE="eth0"

# syncid to use
SYNCID="1"
配置keepalived
$ cat /etc/keepalived/keepalived.conf
global_defs {
   router_id LVS_BACKUP #BACKUP上修改为LVS_BACKUP
}

vrrp_instance VI_1 {
    state BACKUP          #BACKUP上修改为BACKUP
    interface eth0
    virtual_router_id 51  #与备机的id必须一致
    priority 80           #BACKUP上修改为80
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        172.16.8.252     #virtual server
    }
}

virtual_server 172.16.8.252 80 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
    #lb_kind NAT
    #persistence_timeout 5
    protocol TCP

    real_server 172.16.8.131 80 {
        weight 3
        TCP_CHECK {
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
            connect_port 80
        }
    }

    real_server 172.16.8.132 80 {
        weight 3
        TCP_CHECK {
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
            connect_port 80
        }
    }
}
配置WEB_APP_SERVER_1
安装nginx
$ ssh WEB_APP_SERVER_1
$ sudo apt-get install nginx
$ sudo bash -c 'echo "Web App Server 1" > /usr/share/nginx/www/index.html'
配置IP
$ sudo ifconfig eth0:0 172.16.8.252 netmask 255.255.255.255 up
修改配置文件 (上面的配置重启后需要重新输入命令)

$ cat /etc/network/interfaces
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

# The loopback network interface
auto lo
auto eth0
auto eth0:0

iface lo inet loopback

# The primary network interface
allow-hotplug eth0
#iface eth0 inet dhcp
iface eth0 inet static
        address 172.16.8.131
        netmask 255.255.255.0
        gateway 172.16.8.2

iface eth0:0 inet static
        address 172.16.8.252
        netmask 255.255.255.255
        gateway 172.16.8.2
配置WEB_APP_SERVER_2
安装nginx
$ ssh WEB_APP_SERVER_2
$ sudo apt-get install nginx
$ sudo bash -c 'echo "Web App Server 2" > /usr/share/nginx/www/index.html'
配置IP
$ sudo ifconfig eth0:0 172.16.8.252 netmask 255.255.255.255 up
修改配置文件 (上面的配置重启后需要重新输入命令)

$ cat /etc/network/interfaces
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

# The loopback network interface
auto lo
auto eth0
auto eth0:0

iface lo inet loopback

# The primary network interface
allow-hotplug eth0
#iface eth0 inet dhcp
iface eth0 inet static
        address 172.16.8.132
        netmask 255.255.255.0
        gateway 172.16.8.2

iface eth0:0 inet static
        address 172.16.8.252
        netmask 255.255.255.255
        gateway 172.16.8.2
启动LVS, keepalived
$ ssh LVS_MASTER
$ sudo service ipvsadm restart
$ sudo service keepalived restart
$ ssh LVS_BACKUP
$ sudo service ipvsadm restart
$ sudo service keepalived restart
测试
查看服务器状态
$ while true; do curl 172.16.8.252; curl 172.16.8.252; sleep 1; done
Web App Server 1
Web App Server 2
Web App Server 1
Web App Server 2
...
$ ssh LVS_MASTER
$ sudo ipvsadm -ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddressort Scheduler Flags
  -> RemoteAddressort           Forward Weight ActiveConn InActConn
TCP  172.16.8.252:80 rr
  -> 172.16.8.131:80              Route   3      0          29
  -> 172.16.8.132:80              Route   3      0          30
将WEB_APP_SERVER_1的nginx进程结束,查看效果
$ ssh WEB_APP_SERVER_1
$ sudo service nginx stop
$ while true; do curl 172.16.8.252; curl 172.16.8.252; sleep 1; done
curl: (7) couldn't connect to host
Web App Server 2
curl: (7) couldn't connect to host
Web App Server 2
curl: (7) couldn't connect to host
Web App Server 2
Web App Server 2
Web App Server 2
Web App Server 2
...
$ sudo ipvsadm -ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddressort Scheduler Flags
  -> RemoteAddressort           Forward Weight ActiveConn InActConn
TCP  172.16.8.252:80 rr
  -> 172.16.8.132:80              Route   3      0          114
$ ssh WEB_APP_SERVER_1
$ sudo service nginx stop
$ while true; do curl 172.16.8.252; curl 172.16.8.252; sleep 1; done
Web App Server 1
Web App Server 2
Web App Server 1
Web App Server 2
...
将LVS_MASTER关闭,查看请求web app server的结果
$ ssh LVS_MASTER
$ sudo shutdown -h now
$ while true; do curl 172.16.8.252; curl 172.16.8.252; sleep 1; done
Web App Server 1
Web App Server 2
Web App Server 1
Web App Server 2
...

求职 : Linux运维
论坛徽章:
19
CU大牛徽章
日期:2013-03-13 15:15:0815-16赛季CBA联赛之山东
日期:2016-10-31 10:40:10综合交流区版块每日发帖之星
日期:2016-07-06 06:20:00IT运维版块每日发帖之星
日期:2016-02-08 06:20:00数据库技术版块每日发帖之星
日期:2016-01-15 06:20:00IT运维版块每日发帖之星
日期:2016-01-15 06:20:00IT运维版块每日发帖之星
日期:2016-01-10 06:20:00黄金圣斗士
日期:2015-11-24 10:45:10IT运维版块每日发帖之星
日期:2015-09-01 06:20:00IT运维版块每日发帖之星
日期:2015-08-13 06:20:00IT运维版块每日发帖之星
日期:2015-07-30 09:40:012015年亚洲杯之巴勒斯坦
日期:2015-05-05 10:19:03
发表于 2014-07-06 19:19 |显示全部楼层
好贴一定要顶!!!!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP