设为首页 收藏本站
查看: 1318|回复: 0

[经验分享] LVS +DR +keepalived高可用性web集群

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2016-1-26 09:13:30 | 显示全部楼层 |阅读模式

准备机器

    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 LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           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 LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           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
...



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-169623-1-1.html 上篇帖子: LVS-NAT配置 下篇帖子: LVS负载均衡集群服务搭建详解
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表