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

[经验分享] debian下完成 haproxy +keepalived 高可用web集群架构

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

准备机器

    harpoxy_MASTER - 172.16.8.129
    haproxy_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)

安装软件
haproxy_MASTER上安装配置haproxy和keepalived

$ ssh haproxy_MASTER
$ sudo bash -c "echo 'deb http://ftp.debian.org/debian/ wheezy-backports main' >> /etc/apt/sources.list"
$ sudo apt-get update
$ sudo apt-get install haproxy keepalived

配置haproxy

$ cat /etc/haproxy/haproxy.cfg
global
        log 127.0.0.1   local0 notice
        maxconn 2000
        chroot /var/lib/haproxy
        user haproxy
        group haproxy
        daemon

defaults
        log     global
        mode    http
        option  httplog
        option  dontlognull
        option  redispatch
        contimeout 5000
        clitimeout 50000
        srvtimeout 50000
        retries 3
        errorfile 400 /etc/haproxy/errors/400.http
        errorfile 403 /etc/haproxy/errors/403.http
        errorfile 408 /etc/haproxy/errors/408.http
        errorfile 500 /etc/haproxy/errors/500.http
        errorfile 502 /etc/haproxy/errors/502.http
        errorfile 503 /etc/haproxy/errors/503.http
        errorfile 504 /etc/haproxy/errors/504.http

listen webserver 0.0.0.0:80
    mode http
    stats enable
    stats uri /haproxy?stats
    stats realm Strictly\ Private
    stats auth admin:admin
    balance roundrobin
    #option httpclose
    option http-server-close
    timeout http-keep-alive 3000
    option forwardfor
    cookie SRVNAME insert
    server app1 172.16.8.131:80 cookie S1 check
    server app2 172.16.8.132:80 cookie S2 check

配置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
    }
}

haproxy_BACKUP上安装配置haproxy和keepalived

$ ssh haproxy_BACKUP
$ sudo bash -c "echo 'deb http://ftp.debian.org/debian/ wheezy-backports main' >> /etc/apt/sources.list"
$ sudo apt-get update
$ sudo apt-get install haproxy keepalived

配置haproxy

$ cat /etc/haproxy/haproxy.cfg
global
        log 127.0.0.1   local0 notice
        maxconn 2000
        chroot /var/lib/haproxy
        user haproxy
        group haproxy
        daemon

defaults
        log     global
        mode    http
        option  httplog
        option  dontlognull
        option  redispatch
        contimeout 5000
        clitimeout 50000
        srvtimeout 50000
        retries 3
        errorfile 400 /etc/haproxy/errors/400.http
        errorfile 403 /etc/haproxy/errors/403.http
        errorfile 408 /etc/haproxy/errors/408.http
        errorfile 500 /etc/haproxy/errors/500.http
        errorfile 502 /etc/haproxy/errors/502.http
        errorfile 503 /etc/haproxy/errors/503.http
        errorfile 504 /etc/haproxy/errors/504.http

listen webserver 0.0.0.0:80
    mode http
    stats enable
    stats uri /haproxy?stats
    stats realm Strictly\ Private
    stats auth admin:admin
    balance roundrobin
    #option httpclose
    option http-server-close
    timeout http-keep-alive 3000
    option forwardfor
    cookie SRVNAME insert
    server app1 172.16.8.131:80 cookie S1 check
    server app2 172.16.8.132:80 cookie S2 check

配置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
    }
}

配置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

启动haproxy, keepalived

$ ssh haproxy_MASTER
$ sudo service haproxy restart
$ sudo service keepalived restart

$ ssh haproxy_BACKUP
$ sudo service haproxy 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
...

查看haproxy状态

    用/etc/haproxy/haproxy.cfg中的用户名、密码登录 http://172.16.8.129/haproxy?stats

关闭haproxy_MASTER查看web服务是否正常工作

$ ssh haproxy_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
...

关闭WEB_APP_SERVER_1查看web服务是否正常工作

$ ssh WEB_APP_SERVER_1
$ sudo shutdown -h now

$ while true; do curl 172.16.8.252; curl 172.16.8.252; sleep 1; done
Web App Server 2
Web App Server 2
Web App Server 2
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-169612-1-1.html 上篇帖子: HAProxy基于KeepAlived实现Web高可用及动静分离 下篇帖子: 使用Haproxy搭建WEB群集
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

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

扫描微信二维码查看详情

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


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


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


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



合作伙伴: 青云cloud

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