???haproxy??????keepalived????????????LNMP???????????????????????????

Posted

tags:

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

?????????lnmp??????????????????????????????

???????????????haproxy??????keepalived??????LNMP???????????????????????????????????????????????????????????????haproxy????????????????????????????????????????????????????????????????????????????????????

?????????haproxy??????keepalived????????????lnmp?????????????????????????????????????????????

    ???1?????????????????????????????????css,js???????????????????????????????????????

    ???2????????????172.17??????????????????

    ???3??????????????????????????????????????????cookie???????????????

?????????????????????

    ????????????

1????????????????????????????????????selinux????????????????????????

    ??????????????????iptables -F

    ??????selinux???setenforce 0

    ???????????????ntpdate 172.17.0.1(??????????????????)

2?????????????????????

    (1)?????????keepalived

    vim /etc/keepalived/keepalived.conf(??????NAT??????)  

global_defs {                                 #????????????

   notification_email {

        [email protected]                        #????????????

   }

   notification_email_from root

   smtp_server 127.0.0.1                      #?????????????????????

   smtp_connect_timeout 30

   router_id KEEPALIVED_LVS

}

vrrp_instance VI_1 {

    state MASTER                              #?????????MASTER

    interface eth1                            #???????????????

    virtual_router_id 74                      #VIP??????????????????????????????????????????????????????

    priority 200                              #?????????

    advert_int 1

    authentication {                          #????????????

        auth_type PASS

        auth_pass magedu

    }

    virtual_ipaddress {

        172.17.177.176                        #?????????VIP

    }

}

vrrp_instance VI_2 {

    state MASTER

    interface eth0

    virtual_router_id 75

    priority 200

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass magedu

    }

    virtual_ipaddress {

        192.168.153.153                        #?????????DIP???VIP??????????????????????????????????????????DIP???????????????

    }

}

     ???????????????VIP??????????????????????????????DIP???VIP??????

     ????????????systemctl start keepalived

     ??????????????????vim /etc/sysctl.conf

                 net,ipv4,ip_forward=1

     sysctl -p    ??????

    ???2????????????haproxy

        vim /etc/haproxy/haproxy.cfg

            global(???????????????)

    log         127.0.0.1 local2

 

    chroot      /var/lib/haproxy

    pidfile     /var/run/haproxy.pid

    maxconn     4000

    user        haproxy

    group       haproxy

    daemon

    stats socket /var/lib/haproxy/stats

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

frontend http

    bind 172.17.177.176:80               #80???????????????VIP???

    stats enable                         #??????????????????

    stats uri /haproxy                   #?????????????????????

    stats auth  admin:admin              #???????????????????????????

    stats refresh                        #10s???????????????10?????????????????????

    stats admin if TRUE                  #?????????????????????

    acl imagejingtai path_end -i .jpj .png .bmp .gif .js .css          #??????????????????

    acl imagephp path_end -i .php                                      #????????????php??????

use_backend webserver1 if imagephp                                 #???????????????imagephp????????????webserver1???

    acl host src 172.17.0.0/16                                         #??????172.17????????????

    block if ! host                                                    #????????????host???????????????????????????

    default_backend webserver                                          #?????????backend???webserver

backend webserver1                                                     #??????????????????

    balance roundrobin                                                 #???????????????

    server web1 192.168.153.177:80 check weight 1

backend webserver2

    balance roundrobin

    server web2 192.168.153.176:80 check weight 1

backend webserver

    balance roundrobin

    cookie SRV insert nocache                                          #??????cookie??????

    server http1 192.168.153.177:80 check weight 1 cookie srv1         #??????cookie???srv1??????????????????

server http1 192.168.153.176:80 check weight 1

    ????????????systemctl start haproxy

    ??????80?????????????????????VIP???  ss -ntl

3????????????????????????

    ???1???????????????keepalived

        vim /etc/keepalived/keepalived.conf    

global_defs {

   notification_email {

        [email protected]

   }

   notification_email_from root

   smtp_server 127.0.0.1

   smtp_connect_timeout 30

   router_id KEEPALIVED_LVS

}

vrrp_instance VI_1 {

    state BACKUP                              #MASTER????????????BACKUP

    interface eth1

    virtual_router_id 74

    priority 190                              #?????????????????????

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass magedu

    }

    virtual_ipaddress {

        172.17.177.176

    }

}

vrrp_instance VI_2 {

    state BACKUP                              #MASTER????????????BACKUP

    interface eth0

    virtual_router_id 75

    priority 190                              #?????????????????????

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass magedu

    }

    virtual_ipaddress {

        192.168.153.153

    }

}

    ????????????systemctl start keepalived

    ??????????????????vim /etc/sysctl.conf

                net,ipv4,ip_forward=1

    sysctl -p    ??????

    ???2???????????????haproxy(?????????haproxy????????????)

        vim /etc/haproxy/haproxy.cfg    

            global(???????????????)

    log         127.0.0.1 local2 

    chroot      /var/lib/haproxy

    pidfile     /var/run/haproxy.pid

    maxconn     4000

    user        haproxy

    group       haproxy

    daemon

    stats socket /var/lib/haproxy/stats

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

frontend http

    bind 172.17.177.176:80               #80???????????????VIP???

    stats enable                         #??????????????????

    stats uri /haproxy                   #?????????????????????

    stats auth  admin:admin              #???????????????????????????

    stats refresh                        #10s???????????????10?????????????????????

    stats admin if TRUE                  #?????????????????????

    acl imagejingtai path_end -i .jpj .png .bmp .gif .js .css          #??????????????????

    acl imagephp path_end -i .php                                      #????????????php??????

use_backend webserver1 if imagephp                                 #???????????????imagephp????????????webserver1???

    acl host src 172.17.0.0/16                                         #??????172.17????????????

    block if ! host                                                    #????????????host???????????????????????????

    default_backend webserver                                          #?????????backend???webserver

backend webserver1                                                     #??????????????????

    balance roundrobin                                                 #???????????????

    server web1 192.168.153.177:80 check weight 1

backend webserver2

    balance roundrobin

    server web2 192.168.153.176:80 check weight 1

backend webserver

    balance roundrobin

    cookie SRV insert nocache                                          #??????cookie??????

    server http1 192.168.153.177:80 check weight 1 cookie srv1         #??????cookie???srv1??????????????????

server http1 192.168.153.176:80 check weight 1

    ????????????service haproxy start

4??????????????????web?????????    

      ???????????????????????????LNMP??????????????????????????????????????????????????????????????????LNMP?????????????????????????????????

    ???1???NAT??????

        ???80???3306???9000????????????

        systemctl start nginx   php-fpm    mariadb

        ?????????????????????DIP???VIP

        ip route add default via 192.168.153.153

       ????????????

    ???2???DR??????        

        ????????????keepalived???????????????DIP???VIP???????????????????????????????????????

        ip route add default via 172.17.0.1

       ????????????

        ??????????????????????????????VIP??????

            ifconfig lo:0 172.17.166.165 broadcast 172.17.166.165 netmask 255.255.255.255 up

        ??????????????????VIP???lo:0

            route add -host 172.17.166.165 dev lo:0

        ???????????????IP??????????????????????????????????????????ARP????????????

            echo "1" > /proc/sys/net/ipv4/conf/lo/arp_ignore

        ?????????????????????????????????????????????.??????????????????????????????IP???????????????????????????????????????????????????????????????????????????

5??????????????????????????????VIP??????????????????????????????????????????????????????????????????web?????????

    ???????????????http://192.168.153.177/haproxy???????????????web?????????

    ????????????????????????????????????????????????????????????web???????????????????????????haproxy???????????????server???????????????

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

阿里云ecs能搭建haproxy+keepalive吗

实现 KeepAlive + Haproxy + Nginx 七层负载均衡 + 动静分离

keepalive高可用haproxy实现URL资源的动静分离

RabbitMQ---集群,Haproxy+Keepalive 实现高可用负载均衡,Federation Exchange和Federation Queue

基于haproxy+keepalive+varnish实现lnmp企业级架构

高可用pxc+rocketmq+es+redis+minio+keepalive+haproxy 实操