如何创建浮动IP并使用它来配置HAProxy [英] How to create Floating IP and use it to configure HAProxy

查看:610
本文介绍了如何创建浮动IP并使用它来配置HAProxy的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近尝试使用HAProxy负载平衡我的应用程序,并能够成功完成它.后来,我遇到了一个称为浮动IP"的概念,该概念可以与keepalived一起使用,以使负载均衡器高度可用.但是,我无法理解如何创建浮动IP.如何创建浮动IP并使用它来配置HAProxy? 谢谢.

I have recently tried to load balance my application using HAProxy and was able to do it successfully. Later, I have come across a concept called Floating IP, which can be used along with keepalived to make the load balancer highly available. I wasn't able to understand how to create the floating Ip though. How can I create Floating IP and use it to configure HAProxy? Thanks.

推荐答案

假设:

  • 这适用于Ubuntu 14.04
  • haproxy-primary IP:198.51.100.10
  • haproxy-secondary IP:198.51.100.20
  • 共享IP:198.51.100.50
  • 任何DNS规则都应指向共享IP(198.51.100.50)

步骤:

  1. 为keepalived添加防火墙规则#224.0.0.18是keepalived多播地址
  1. Add a firewall rule for keepalived # 224.0.0.18 is the keepalived multicast address
  1. sudo ufw allow in from 198.51.100.20 to 224.0.0.18#on 198.51.100.10
  2. sudo ufw allow in from 198.51.100.10 to 224.0.0.18#on 198.51.100.20
  1. sudo ufw allow in from 198.51.100.20 to 224.0.0.18 # on 198.51.100.10
  2. sudo ufw allow in from 198.51.100.10 to 224.0.0.18 # on 198.51.100.20

  • 允许访问共享的IP地址

  • Allow access to a shared IP address

    1. 编辑/etc/sysctl.conf
    2. 设置net.ipv4.ip_nonlocal_bind=1
    3. sudo sysctl -p#重新加载配置更改
    1. edit /etc/sysctl.conf
    2. set net.ipv4.ip_nonlocal_bind=1
    3. sudo sysctl -p # reload config change

  • 安装keepalived

  • Install keepalived

    1. sudo apt-get install keepalived

  • 在两台服务器上配置keepalived

  • Configure keepalived on both servers

    1. 编辑/创建/etc/keepalived/keepalived.conf
    2. 请参阅下面的示例文件#主服务器和次服务器上的优先级必须不同!

  • 重新启动keepalived

  • Restart keepalived

    1. sudo服务keepalived重新启动

  • 收听共享的IP地址

  • Listen on the shared IP address

    1. 编辑/etc/haproxy/haproxy.cfg
    2. bind 198.51.100.50:80
    1. Edit /etc/haproxy/haproxy.cfg
    2. bind 198.51.100.50:80

  • 重新启动haproxy(在两个haproxy服务器上)

  • Restart haproxy (on both haproxy servers)

    1. sudo service haproxy restart

  • 验证正确的故障转移

  • Verify proper failover

    1. 主要:sudo ip addr show | grep eth0#应列出共享IP
    2. 第二:sudo ip addr show | grep eth0#不应列出共享IP
    3. 主要:sudo service haproxy stop
    4. 主要:sudo ip addr show | grep eth0#不应列出共享IP
    5. 第二:sudo ip addr show | grep eth0#应该列出共享的IP
    6. 主要:sudo service haproxy start
    7. 主要:sudo ip addr show | grep eth0#应列出共享IP
    8. 第二:sudo ip addr show | grep eth0#不应列出共享IP
    1. primary: sudo ip addr show | grep eth0 # should list the shared IP
    2. secondary: sudo ip addr show | grep eth0 # should NOT list the shared IP
    3. primary: sudo service haproxy stop
    4. primary: sudo ip addr show | grep eth0 # should NOT list the shared IP
    5. secondary: sudo ip addr show | grep eth0 # should list the shared IP
    6. primary: sudo service haproxy start
    7. primary: sudo ip addr show | grep eth0 # should list the shared IP
    8. secondary: sudo ip addr show | grep eth0 # should NOT list the shared IP

  • /etc/keepalived/keepalived.conf

         vrrp_script chk_haproxy {      # Requires keepalived-1.1.13
           script "killall -0 haproxy"  # cheaper than pidof
           interval 2 # check every 2 seconds
           weight 2 # add 2 points of priority if OK
         }
         vrrp_instance VI_1 {
           interface eth0
           state MASTER
           virtual_router_id 51
           priority 101 # 101 on primary, 100 on secondary
           virtual_ipaddress {
             198.51.100.50
           }
           track_script {
             chk_haproxy
           }
         }
    

    这篇关于如何创建浮动IP并使用它来配置HAProxy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆