HAproxy+Keepalived负载均衡
haproxy+keepalived负载均衡高可用web站http://s5.运维网.com/wyfs02/M01/8B/D4/wKiom1hZ-yqBKsg7AADnBlBPVZo456.png-wh_500x0-wm_3-wmp_4-s_2292954439.png
OS
IP
子网掩码
路由网关
Centos6.6
HAproxy
Keepalived
Eth0:192.168.26.210
255.255.252.0
192.168.25.3
VIP:192.168.27.210
Centos6.6
HAporxy
Keepalived
Eth0:192.168.26.211
255.255.252.0
192.168.25.3
VIP:192.168.27.210
Centos6.6(WEB)
Eth0:192.168.26.212
255.255.252.0
192.168.25.3
Centos6.6(WEB)
Eth0:192.168.26.218
255.255.252.0
192.168.25.3
1、安装Apache服务192.168.26.212和192.168.26.218:(yum install httpd –y)略启动
启动Apache,分别在两台服务器上创建WEB页并确保网络能正常访问:
http://s5.运维网.com/wyfs02/M01/8B/D0/wKioL1hZ-0nDYxZMAAANkjkBv84876.png-wh_500x0-wm_3-wmp_4-s_2605294190.png
http://s1.运维网.com/wyfs02/M02/8B/D4/wKiom1hZ-1GRglcvAABJnRtsxyk903.png-wh_500x0-wm_3-wmp_4-s_1819329336.png
2、安装HAproxy:192.168.26.210和192.168.26.211:
Yum install –y haproxy
http://s1.运维网.com/wyfs02/M02/8B/D4/wKiom1hZ-1qA0rzvAAAPx_aZo1w541.png-wh_500x0-wm_3-wmp_4-s_1349315902.png
编辑配置文件: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 1m
timeout connect 10s
timeout client 1m
timeout server 1m
timeout http-keep-alive 10s
timeout check 10s
maxconn 3000
#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
#frontendmain *:5000
# acl url_static path_beg -i /static /images /javascript /stylesheets
# acl url_static path_end -i .jpg .gif .png .css .js
#
# use_backend static if url_static
# default_backend app
#
##---------------------------------------------------------------------
## static backend for serving up images, stylesheets and such
##---------------------------------------------------------------------
#backend static
# balance roundrobin
# server static 127.0.0.1:4331 check
frontend websrv *:80
default_backend webservers
backend webservers
balanceroundrobin
server node3 192.168.26.212:80 check
server node4 192.168.26.218:80 check
listen statick
bind *:1024
stats enable
stats uri /haadmin?stats
stats auth admin:admin
stats hide-version
stats admin if TRUE
http://s1.运维网.com/wyfs02/M02/8B/D0/wKioL1hZ-2rywZ87AAAhNwgnkS8267.png-wh_500x0-wm_3-wmp_4-s_1201986816.png
192.168.26.211上配置文件也一样:因此我们直接SCP过去。
scp -p haproxy.cfg node2:/etc/haproxy/
http://s5.运维网.com/wyfs02/M02/8B/D0/wKioL1hZ-3TAn_xCAADAjCcRIgk956.png-wh_500x0-wm_3-wmp_4-s_3390152132.png
启动两台服务器上的HAproxy服务。
http://s1.运维网.com/wyfs02/M00/8B/D4/wKiom1hZ-3_y4Z1rAAAgzQHl-OU677.png-wh_500x0-wm_3-wmp_4-s_2690850148.png
http://s1.运维网.com/wyfs02/M00/8B/D4/wKiom1hZ-4mhq7BeAAC9vr1Ql_c264.png-wh_500x0-wm_3-wmp_4-s_1559868620.png
安装keepalived:192.168.26.210和192.168.26.211
首先192.168.26.210配置:
Yum install –y keepalived
http://s5.运维网.com/wyfs02/M01/8B/D0/wKioL1hZ-5TgQjW9AACFiUvWy5Y154.png-wh_500x0-wm_3-wmp_4-s_1093082122.png
编辑配置文件:vim /etc/keepalived/keepalived.conf
http://s5.运维网.com/wyfs02/M01/8B/D0/wKioL1hZ-6GTA2qrAACHaR3_zHM034.png-wh_500x0-wm_3-wmp_4-s_1946766224.png
配置文件:
! Configuration File for keepalived
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from jwenshan@163.com
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script chk_haproxy {
script "/etc/keepalived/chk.sh" //检查haproxy的脚本
interval 2 //每两秒检查一次
}
vrrp_instance VI_1 {
state BACKUP //定义为BACKUP节点
nopreempt //开启不抢占
interface eth0
virtual_router_id 51
priority 100 //开启了不抢占,所以此处优先级必须高于另一台
advert_int 1
authentication {
auth_type PASS
auth_pass jerry
}
virtual_ipaddress {
192.168.27.210 //配置VIP
}
track_script {
chk_haproxy //调用检查脚本
}
notify_backup "/etc/init.d/haproxy restart"
notify_fault "/etc/init.d/haproxy stop"
}
创建脚本文件:主要用于检测haproxy状态。
Vim /etc/keepalived/Chk.sh
#!/bin/bash
#
if [ $(ps -C haproxy --no-header | wc -l) -eq 0 ]; then
/etc/init.d/keepalived stop
Fi
http://s1.运维网.com/wyfs02/M02/8B/D4/wKiom1hZ-7GTNIecAAARmX33a5g307.png-wh_500x0-wm_3-wmp_4-s_3043756955.png
启动Keepalived服务:
service keepalived start
192.168.26.211配置:
Yum install –y keepalived
http://s4.运维网.com/wyfs02/M02/8B/D0/wKioL1hZ-7mCgTQYAAAKJ5VdAiI707.png-wh_500x0-wm_3-wmp_4-s_891352467.png
编辑配置文件:Vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from jwenshan@163.com
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script chk_haproxy {
script "/etc/keepalived/chk.sh"
interval 2
}
vrrp_instance VI_1 {
state BACKUP
nopreempt
interface eth0
virtual_router_id 51
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass jerry
}
virtual_ipaddress {
192.168.27.210
}
track_script {
chk_haproxy
}
notify_backup "/etc/init.d/haproxy restart"
notify_fault "/etc/init.d/haproxy stop"
}
脚本文件:vim /etc/keepalived/chk.sh
#!/bin/bash
#
if [ $(ps -C haproxy --no-header | wc -l) -eq 0 ]; then
/etc/init.d/keepalived stop
Fi
http://s4.运维网.com/wyfs02/M02/8B/D4/wKiom1hZ-8SwJRX6AAARZQSVU9E510.png-wh_500x0-wm_3-wmp_4-s_3347932491.png
启动Keepalived服务
Service keepalived start
测试VIP网络是否通畅:ping 192.168.27.210 –t
http://s5.运维网.com/wyfs02/M02/8B/D0/wKioL1hZ-8_ymEwxAACLWUIot6k073.png-wh_500x0-wm_3-wmp_4-s_2189402260.png
访问VIP网络:
Http://192.168.27.210
不断刷新浏览器观察显示结果
http://s5.运维网.com/wyfs02/M00/8B/D4/wKiom1hZ-9rjUxFsAADWKfT425A948.png-wh_500x0-wm_3-wmp_4-s_2710547753.png
http://s3.运维网.com/wyfs02/M00/8B/D0/wKioL1hZ--fQSyEuAADDWLvOkWo264.png-wh_500x0-wm_3-wmp_4-s_1914214806.png
测试通过VIP访问后台监控页面:
http://s5.运维网.com/wyfs02/M00/8B/D0/wKioL1hZ-_Si7efrAADB7V994X8042.png-wh_500x0-wm_3-wmp_4-s_434034079.png
http://s2.运维网.com/wyfs02/M01/8B/D4/wKiom1hZ_ASxt5qSAAE5Y_EkzEw778.png-wh_500x0-wm_3-wmp_4-s_210134897.png
访问成功。
测试高可用:
http://s5.运维网.com/wyfs02/M01/8B/D4/wKiom1hZ_BHxlQDhAAFft5RxGN8314.png-wh_500x0-wm_3-wmp_4-s_1826150241.png
停掉192.168.26.210上Keepalived 观察结果:
Service keepalived stop
首先VIP网络出现波动:
http://s2.运维网.com/wyfs02/M01/8B/D0/wKioL1hZ_B_hty5gAAFhLtQDmz0296.png-wh_500x0-wm_3-wmp_4-s_3116872649.png
再次访问VIP成功:
http://s5.运维网.com/wyfs02/M02/8B/D4/wKiom1hZ_CvzLugKAADlKyBUj5M320.png-wh_500x0-wm_3-wmp_4-s_1575823615.png
http://s3.运维网.com/wyfs02/M02/8B/D0/wKioL1hZ_DWz4ai0AADLVw4cd60858.png-wh_500x0-wm_3-wmp_4-s_3060179235.png
VIP已经转移动192.168.26.211上:
http://s5.运维网.com/wyfs02/M01/8B/D0/wKioL1hZ_EDQo7a1AACkV9GlR8c692.png-wh_500x0-wm_3-wmp_4-s_2164320638.png
恢复192.168.26.210keepalived观察
:vip没有自动转移回192.168.26.210 这和我们的设置参数有关。
http://s2.运维网.com/wyfs02/M00/8B/D4/wKiom1hZ_FiAJXfBAAGV_eHnL9o881.png-wh_500x0-wm_3-wmp_4-s_659886495.png
http://s4.运维网.com/wyfs02/M02/8B/D0/wKioL1hZ_GXAmdDyAAJ2SsPJxIY993.png-wh_500x0-wm_3-wmp_4-s_1355988361.png
本试验keepalived配置中涉及几个重要参数,nopreemptstate MASTER/ state BACKUPpriority 可以更改其设置观察其变化,适应不同场景中的应用。
页:
[1]