两个VIP地址:192.168.23.98
192.168.23.99
首先我们的拓扑图,由于双主模型,则最少需要四台服务器:
1.Haproxy特别适用于那些访问量很大,但又需要会话保持或七层应用的业务。Haproxy运行在普通的服务器硬件上,仅仅进行简单的配置就可以支持数以万计的连接。并且他的运行模式使得它可以很简单安全的整合到各种网站的架构中(可以代替lvs,nginx等负载均衡设备),同时使得应用服务器不会暴露到网络上。(NAT模式),因此,我们来拿haproxy来具体说明一下:
第一步配置后端的web服务器:web-01,web-02
[root@centos6 ~]# ech0 "web-01 test page" > /var/www/html/index.html
[root@centos6 html]# cat index.html
web-01 test page
[root@localhost html]# echo "web-02 test page" > /var/www/html/index.html
[root@localhost html]# cat index.html
web-02 test page
然后启动服务 service httpd restart
haproxy测试一下网页的设置,访问后端的web服务
[root@centos7 keepalived]# curl 192.168.23.100
web-01 test page
[root@centos7 keepalived]# curl 192.168.23.101
web-02 test page
2.在hk两个节点上都要安装haproxy和keepalived
例如:yum install haproxy keepalived -y
其次,修改内核参数设置,设置haproxy启动的时候不管有没有vip地址都可以启动
此选项为集群中关键选项,不然VIP地址没有在Haproxy服务器的时候,服务器无法正常启动
[root@centos7 keepalived]# echo "net.ipv4.ip_nonlocal_bind = 1" >> /etc/sysctl.conf
[root@centos7 keepalived]# sysctl -p
net.ipv4.ip_nonlocal_bind = 1
[root@cento7 ~]# echo "net.ipv4.ip_nonlocal_bind= 1" >> /etc/sysctl.conf
[root@cento7 ~]# sysctl -p
net.ipv4.ip_nonlocal_bind = 1
设置haproxy
既然haproxy要实现双主,就必须要做到分别监听两个vip地址,并且两个示例都能得到用户请求负载均衡转发给后端web服务器,使用户不论访问那个节点都可以实现负载均衡。
两个节点需要配置一样的所以用一个haproxy代替
Vim /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