如何创建浮动IP并使用它来配置HAProxy [英] How to create Floating IP and use it to configure HAProxy
本文介绍了如何创建浮动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)
步骤:
- 为keepalived添加防火墙规则#224.0.0.18是keepalived多播地址
- Add a firewall rule for keepalived # 224.0.0.18 is the keepalived multicast address
-
sudo ufw allow in from 198.51.100.20 to 224.0.0.18
#on 198.51.100.10 -
sudo ufw allow in from 198.51.100.10 to 224.0.0.18
#on 198.51.100.20
sudo ufw allow in from 198.51.100.20 to 224.0.0.18
# on 198.51.100.10sudo ufw allow in from 198.51.100.10 to 224.0.0.18
# on 198.51.100.20
- 编辑
/etc/sysctl.conf
- 设置
net.ipv4.ip_nonlocal_bind=1
-
sudo sysctl -p
#重新加载配置更改
- edit
/etc/sysctl.conf
- set
net.ipv4.ip_nonlocal_bind=1
sudo sysctl -p
# reload config change
-
sudo apt-get install keepalived
- 编辑/创建
/etc/keepalived/keepalived.conf
- 请参阅下面的示例文件#主服务器和次服务器上的优先级必须不同!
- sudo服务keepalived重新启动
- 编辑
/etc/haproxy/haproxy.cfg
-
bind 198.51.100.50:80
- Edit
/etc/haproxy/haproxy.cfg
bind 198.51.100.50:80
-
sudo service haproxy restart
- 主要:
sudo ip addr show | grep eth0
#应列出共享IP - 第二:
sudo ip addr show | grep eth0
#不应列出共享IP - 主要:
sudo service haproxy stop
- 主要:
sudo ip addr show | grep eth0
#不应列出共享IP - 第二:
sudo ip addr show | grep eth0
#应该列出共享的IP - 主要:
sudo service haproxy start
- 主要:
sudo ip addr show | grep eth0
#应列出共享IP - 第二:
sudo ip addr show | grep eth0
#不应列出共享IP
- primary:
sudo ip addr show | grep eth0
# should list the shared IP - secondary:
sudo ip addr show | grep eth0
# should NOT list the shared IP - primary:
sudo service haproxy stop
- primary:
sudo ip addr show | grep eth0
# should NOT list the shared IP - secondary:
sudo ip addr show | grep eth0
# should list the shared IP - primary:
sudo service haproxy start
- primary:
sudo ip addr show | grep eth0
# should list the shared IP - 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屋!
查看全文