zj2092 发表于 2018-12-30 09:38:18

用haproxy结合keepalived实现基于LNMP的负载均衡和高可用

  今天我们讲haproxy结合keepalived实现LNMP的负载均衡和高可用,现在的公司大部分都基于haproxy实现负载均衡。下面以一个事例去给大家详细讲解如何去实现:

一、用haproxy结合keepalived实现基于lnmp的负载均衡和高可用服务,要求:
    (1)实现动静分离,图片和css,js都分离到专门的静态服务器上
    (2)只允许172.17网段用户访问
    (3)对于动态请求,要求实现基于cookie的会话保持
  整体架构如下:
https://s2.运维网.com/oss/201711/11/3cfa417c75e614495370286c3c08035f.jpg
  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 {
      root@localhost                        #邮件地址
   }
   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 authadmin: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 {
      root@localhost
   }
   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 authadmin: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
https://s5.运维网.com/oss/201711/11/c4b9028308eb9ef7171a958fe1d93b13.jpg
  (2)DR模式
      在调度器keepalived中不用配置DIP的VIP,因为把网关指向路由器就行
      ip route add default via 172.17.0.1
https://s4.运维网.com/oss/201711/11/1d19d680d003da2dd6c8b503524b6ca1.jpg
      还要在回环网卡上加入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就可以了!




页: [1]
查看完整版本: 用haproxy结合keepalived实现基于LNMP的负载均衡和高可用