主备(keepalived+haproxy)

Posted derrickrose

tags:

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

系统:centos6.9 mini

主机名    ip                                           虚拟ip

kh1     192.168.126.210

kh2     192.168.126.220                     192.168.126.100

web11   192.168.126.230

web22   192.168.126.240

 

1、在kh1和kh2安装keepalived和haproxy

[[email protected] ~]# yum install -y keepavlivd haproxy

[[email protected] ~]# yum install -y keepavlivd haproxy

 

2、在web1和web2上部署web服务

[[email protected] ~]# yum install -y httpd

[[email protected] ~]# echo "web1">/var/www/html/index.html

[[email protected] ~]# service httpd restart

[[email protected] ~]# chkconfig httpd on

[[email protected] ~]# yum install -y httpd

[[email protected] ~]# echo "web2">/var/www/html/index.html

[[email protected] ~]# service httpd restart

[[email protected] ~]# chkconfig httpd on

 

3、在kh1和kh2 配置keepalived,在kh1上查看虚拟ip

[[email protected] ~]# cat /etc/keepalived/keepalived.conf

! Configuration File for keepalived

global_defs {

   router_id kh1

}

vrrp_instance VI_1 {

    state MASTER

    interface eth0

    virtual_router_id 51

    priority 100

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 1111

    }

    virtual_ipaddress {

        192.168.126.100

    }

}

[[email protected] ~]# /etc/init.d/keepalived restart

[[email protected] ~]# ip addr list

link/ether 00:0c:29:da:01:36 brd ff:ff:ff:ff:ff:ff

    inet 192.168.126.210/24 brd 192.168.126.255 scope global eth0

    inet 192.168.126.100/32 scope global eth0

 

[[email protected] ~]# cat /etc/keepalived/keepalived.conf

! Configuration File for keepalived

global_defs {

   router_id kn2

}

 vrrp_instance VI_1 {

    state BACKUP

    interface eth0

    virtual_router_id 51

    priority 90

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 1111

    }

    virtual_ipaddress {

        192.168.126.100

    }

}

[[email protected] ~]# /etc/init.d/keepalived restart

 

4、在kh1和kh2上配置haproxy(两个节点的配置一样的,红色部分为添加部分)

[[email protected] ~]# cat /etc/haproxy/haproxy.cfg

#---------------------------------------------------------------------

# Example configuration for a possible web application.  See the

# full configuration options online.

#

#   http://haproxy.1wt.eu/download/1.4/doc/configuration.txt

#

#---------------------------------------------------------------------

 

#---------------------------------------------------------------------

# Global settings

#---------------------------------------------------------------------

global

    # to have these messages end up in /var/log/haproxy.log you will

    # need to:

    #

    # 1) configure syslog to accept network log events.  This is done

    #    by adding the ‘-r‘ option to the SYSLOGD_OPTIONS in

    #    /etc/sysconfig/syslog

    #

    # 2) configure local2 events to go to the /var/log/haproxy.log

    #   file. A line like the following can be added to

    #   /etc/sysconfig/syslog

    #

    #   local2.*                       /var/log/haproxy.log

    #

    log         127.0.0.1 local2

 

    chroot      /var/lib/haproxy

    pidfile     /var/run/haproxy.pid

    maxconn     4000

    user        haproxy

    group       haproxy

    daemon

 

    # turn on stats unix socket

    stats socket /var/lib/haproxy/stats

 

#---------------------------------------------------------------------

# common defaults that all the ‘listen‘ and ‘backend‘ sections will

# use if not designated in their block

#---------------------------------------------------------------------

defaults

    mode                    http

    log                       global

    option                  httplog

    option                  dontlognull

    option http-server-close

    option forwardfor       except 127.0.0.0/8

    option                  redispatch

    retries                 3

    timeout http-request    10s

    timeout queue           1m

    timeout connect         10s

    timeout client             1m

    timeout server            1m

    timeout http-keep-alive     10s

    timeout check             10s

    maxconn                 3000

    stats uri               /haproxy-stats    #监控页面的url

    stats refresh           30s             #更新页面时间

    stats auth              admin:admin    #监控页面的提示信息

    stats hide-version                       #隐藏统计页面上的HAproxy版本信息 

#---------------------------------------------------------------------

# main frontend which proxys to the backends

#---------------------------------------------------------------------

frontend  http_80_in

      bind *:80       #监听端口,即haproxy提供web服务的端口,和lvs的vip端口类似

          mode http    #http的7层模式

          log global     

          default_backend      test1  

 

#---------------------------------------------------------------------

# static backend for serving up images, stylesheets and such

#---------------------------------------------------------------------

#backend static

#    balance     roundrobin

 

#---------------------------------------------------------------------

# round robin balancing between the various backends

#---------------------------------------------------------------------

backend  test1  

    balance     roundrobin

    server        web11 192.168.126.230:80 weight 1 check inter 15000 rise 2 fall 4

    server      web22 192.168.126.240:80 weight 2 check inter 15000 rise 2 fall 4

#web集群配置,服务器定义web11,webv22 ,check inter 1500是检测心跳频率rise 2是2次正确认为服务器可用,

fall 4是4次失败认为服务器不可用,weight代表权重

 

[[email protected] ~]# scp /etc/haproxy/haproxy.cfg [email protected]:/etc/haproxy/haproxy.cfg

#复制配置文件到kh2上

[[email protected] ~]# /etc/init.d/haproxy restart

[[email protected] ~]# /etc/init.d/haproxy restart

 

5、验证

5.1 在kh1上关掉keepalived和haproxy 服务,vip 转移到kh2上,后端服务正常

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

Stopping keepalived:                                       [  OK  ]

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

Stopping haproxy:   

[[email protected] ~]# ip addr list

2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000

    link/ether 00:0c:29:80:2e:09 brd ff:ff:ff:ff:ff:ff

    inet 192.168.126.220/24 brd 192.168.126.255 scope global eth0

    inet 192.168.126.100/32 scope global eth0

    inet6 fe80::20c:29ff:fe80:2e09/64 scope link

       valid_lft forever preferred_lft forever   

 

[[email protected] ~]# curl http://192.168.126.100

web2

[[email protected] ~]# curl http://192.168.126.100

web2

[[email protected] ~]# curl http://192.168.126.100

web1

 5.2 访问haproxy 监控页面,在浏览器输入http://192.168.126.100/haproxy-stats  ,填入用户名admin和密码admin(图一),接着看到的是监控的页面(图二),可以看到web11,web2都是up的,当web11 宕机了,再次刷新,可以看到web11的状态是down 的,颜色变成了红色(图三)

 技术分享图片

                                                                                                                                                  

 

 技术分享图片

                                                                                                                                        

 技术分享图片

                                                                                                                                                  

以上是关于主备(keepalived+haproxy)的主要内容,如果未能解决你的问题,请参考以下文章

haproxy+keepalived+mycat+mysql (读写分离)

高可用篇之Keepalived (HAProxy+keepalived 搭建高可用负载均衡集群)

keepalived + haproxy + mysql 构建高可用数据库

RedHat 7配置keepalived实现HAproxy群集

CentOS7 haproxy+keepalived实现高可用集群搭建

keepalived高可用部署