环境准备:
IP | VIP | |
master+haproxy | 192.168.134.170 | 192.168.134.100 |
backup+haproxy | 192.168.134.172 | 192.168.134.100 |
web1 | 192.168.134.171 | |
web2 | 192.168.134.173 | |
web3 | 192.168.134.174 |
1、首先配置啊三台web服务器,并写好测试页面。
web1(配置jsp页面)
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo # 配置扩展源
yum install tomcat tomcat-webapps -y # 配置tomcat环境
cd /var/lib/tomcat/webapps
mv ROOT/ ROOT.bak # 备份ROOT文件
mkdir ROOT # 创建新的ROOT文件,准备编辑jsp文件
cd ROOT
vim index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!doctype html>
<html>
<head>
<title>第一个jsp程序</title>
</head>
<body>
<%
out.println("Hello World! ");
%>
</body>
</html>
systemctl restart tomcat # 重启tomcat服务器
~
web2(配置static页面)
yum install httpd -y # 安装http服务器,用httpd服务器进行测试
echo "web static page , ip is `hostname -I`." >> /var/www/html/index.html
systemctl start httpd
web3(配置video页面)
yum install httpd -y
echo "video test page , ip is `hostname -I`." >> /var/www/html/index.html
systemctl start httpd
2、在master节点上进行haproxy配置(backup节点也做相同配置)
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo # 安装扩展源
yum install haproxy
vim /etc/haproxy/haproxy.cfg # 修改haproxy配置文件,以下中列出修改过的地方。
frontend www
bind *:80 # 指定监听地址
option httplog
option forwardfor
log global
acl host_www hdr_dom(host) -i www.openlab.cn # 指定通过www.openlab.cn会访问到hdr_www
acl host_static hdr_dom(host) -i static.openlab.cn
acl host_video hdr_dom(host) -i video.openlab.cn
use_backend server_www if host_www # 指定如果访问到hdr_www,调用后端backend server_www
use_backend server_static if host_static
use_backend server_video if host_video
default_backend server_static
backend server_static
balance roundrobin
option redispatch
option abortonclose
option httpchk GET /index.html # 指定访问的页面
server web2 192.168.134.173:80 check
backend server_www
balance roundrobin # 指定算法
option redispatch
option abortonclose
option httpchk GET /index.jsp
server web1 192.168.134.171:8080 check
backend server_video
balance roundrobin
option redispatch
option abortonclose
option httpchk GET /index.html
server web3 192.168.134.174:80 check
listen stats
bind *:88
stats refresh 30s
stats uri /haproxy-status
stats realm Welcome login \ Haproxy
stats auth admin:123456
stats hide-version
vim /etc/rsyslog.conf # 修改日志
# Save haproxy messages also to haproxy.log
local2.* /var/log/haproxy.log
# Provides UDP syslog reception
$ModLoad imudp
$UDPServerRun 514
# Provides TCP syslog reception
$ModLoad imtcp
$InputTCPServerRun 514
systemctl start haproxy.service rsyslog.service
3、修改keepalived配置文件
主节点
! Configuration File for keepalived
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL1
}
vrrp_script check_haproxy {
script "killall -0 haproxy"
#script "/etc/keepalived/check_nginx.sh"
interval 2
#weight -30
}
vrrp_instance VI_1 {
state MASTER
# nopreempt
interface ens33
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
check_haproxy
}
virtual_ipaddress {
192.168.134.100
}
}
备节点修改:
state BACKUP
priority 80
systemctl restart keepalived.service haproxy.service
4、修改hosts文件
192.168.134.100 www.openlab.cn static.openlab.cn video.openlab.cn
5、测试如下:
(1)、可以通过域名访问(成功)
(2)keepalived故障(成功)
(3)keepalived恢复后,VIP回切(成功)
(4)测试haproxy故障切换(成功)