haproxy服务器1:192.168.79.150
haproxy服务器2:192.168.79.160
WEB1(http服务器):192.168.79.20
WEB2(http服务器):192.168.79.21
keepalived服务器:192.168.79.150
VIP 192.168.79.10
httpd WEB1,2服务器
yum -y install httpd
systemctl start httpd
cd /var/www/html
vim index.html
WEB1:a
WEB2:b
haproxy1服务器:
yum -y install haproxy
[root@localhost haproxy]# haproxy -v
HA-Proxy version 1.5.18 2016/05/10
Copyright 2000-2016 Willy Tarreau <willy@haproxy.org>
vim /etc/haproxy/haproxy.cfg
frontend main *:80
64 acl url_static path_beg -i /static /images /javascript /
stylesheets 65 acl url_static path_end -i .jpg .gif .png .css .js
66
67 # use_backend static if url_static
68 ! use_backend static
69 # default_backend app
74 backend static #通过backend关键字定义一个名为static的后端真是服务器池 使用静态,动态分离如果url——path匹配.jpg gif png css js 静态文件 则访问此后端 定义了一个名为 app的后端真实服务器池 用来指定默认的后端真实服务器池
75 balance #:算法 roundrobin #简单轮询调度
76 ! server static 192.168.79.20:80 check inter 2000 fall 3
77 ! server static 192.168.79.21:80 check inter 2000 fall 3
78
79 #---------------------------------------------------------------------
80 # round robin balancing between the various backends
81 #---------------------------------------------------------------------
82 backend app
83 balance roundrobin
84 server app1 127.0.0.1:5001 check
85 server app2 127.0.0.1:5002 check
在最后添加
listen stats
bind 0.0.0.0:8080
stats refresh 30s
stats uri /stats
stats realm Haproxy Manager
stats auth admin:admin
stats hide-version
stats admin if TRUE
重启
在192.168.79.150 haproxy调度器服务器上安装热备份工具
yum -y install keepalived
[root@localhost haproxy]# cd /etc/keepalived/
[root@localhost keepalived]# ls
keepalived.conf
vim keepalived.conf
global_defs{
router_id HA_TEST_R1
}
vrrp_script check_haproxy {
script "/etc/haproxy/check.sh"
interval 2
}
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 1
priority 120
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.79.111
}
track_script {
check_haproxy
}
}
[root@localhost keepalived]# cd /etc/haproxy/
[root@localhost haproxy]# ls
check.sh haproxy.cfg
[root@localhost haproxy]# vim check.sh
#!/bin/bash
A=`ps -C haproxy --no-header |wc -l`
if [ $A -eq 0 ];
then
systemctl start haproxy
if [ `ps -C haproxy --no-header |wc -l` -eq 0];
then
systemctl stop keepalived
sleep 3600
fi
fi
systemctl restart keepalived
systemctl restart haproxy
ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state
UP group default qlen 1000 link/ether 00:0c:29:96:34:2c brd ff:ff:ff:ff:ff:ff
inet 192.168.79.150/24 brd 192.168.79.255 scope global noprefixroute ens
33 valid_lft forever preferred_lft forever
inet 192.168.79.111/32 scope global ens33
调度器HAPROXY2服务器:
yum -y install keepalived
yum -y install haproxy
在调度机1上 SCP root@192.168.79.160:/etc/haproxy/haproxy.cfg
SCP root@192.168.79.160:/etc/keepalived/keepalived.conf
修改
global_defs {
router_id HA_TEST_R2
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 1
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.79.111
}
}
systemctl start keepalived
systemctl start haproxy
init 0 进入调度机2 测试 飘逸ip
进入79.160
ip a
inet 192.168.79.160/24 brd 192.168.79.255 scope global noprefixroute ens
33 valid_lft forever preferred_lft forever
inet 192.168.79.111/32 scope global ens33
成功
脚本
150 调度机1
[root@localhost haproxy]# systemctl stop haproxy
[root@localhost haproxy]# ps -C haproxy
PID TTY TIME CMD
3609 ? 00:00:00 haproxy
3610 ? 00:00:00 haproxy
vim haproxy.cfg
26 log 127.0.0.1 local2 info #添加 info
vim /etc/rsyslog.conf
15 $ModLoad imudp
16 $UDPServerRun 514
73 local7.* /var/log/boot.lo
g 74 local2.* /var/log/haproxy.log
[root@localhost log]# ls #网页刷一下
anaconda dmesg lastlog spooler wtmp
audit dmesg.old maillog tallylog yum.log
boot.log firewalld messages tuned
btmp grubby_prune_debug rhsm vmware-vgauthsvc.log.0
cron haproxy.log
wtmp
audit dmesg.old maillog tallylog yum.log
boot.log firewalld messages tuned
btmp grubby_prune_debug rhsm vmware-vgauthsvc.log.0
cron haproxy.log