IP规划:
haproxy-master(DIP) : 192.168.88.155
haproxy-backup(DIP) : 192.168.88.206
nginx-1 : 192.168.88.203
nginx-2 : 192.168.88.202
VIP : 192.168.88.188
安装nginx
给两台web服务器配置安装nginx 202|3
yum install nginx -y
echo "Hello World is 192.168.88.203" > /usr/share/nginx/html/index.html
echo "Hello World is 192.168.88.202" > /usr/share/nginx/html/index.html
安装Haproxy调度器
#155 和 206 都要操作
yum -y install haproxy
#备份
cp -rf /etc/haproxy/haproxy.cfg{,.bak}
cat > /etc/haproxy/haproxy.cfg << EOF
global
log 127.0.0.1 local2 info
pidfile /var/run/haproxy.pid
maxconn 4000 #优先级低
user haproxy
group haproxy
daemon #以后台形式运行ha-proxy
nbproc 1 #工作进程数量 cpu内核是几就写几
defaults
mode http #工作模式 http ,tcp 是 4 层,http是 7 层
log global
retries 3 #健康检查。3次连接失败就认为服务器不可用,主要通过后面的check检查
option redispatch #服务不可用后重定向到其他健康服务器。
maxconn 4000 #优先级中
contimeout 5000 #ha服务器与后端服务器连接超时时间,单位毫秒ms
clitimeout 50000 #客户端超时
srvtimeout 50000 #后端服务器超时
listen stats
bind *:81
stats enable
stats uri /haproxy #使用浏览器访问 http://192.168.88.188:81/haproxy,可以看到服务器状态
stats auth we:123 #用户认证,客户端使用elinks浏览器的时候不生效
frontend web
mode http
bind *:80 #监听哪个ip和什么端口
option httplog #日志类别 http 日志格式
acl html url_reg -i \.html\$ #1.访问控制列表名称html。规则要求访问以html结尾的url(可选)
use_backend httpservers if html #2.如果满足acl html规则,则推送给后端服务器httpservers
default_backend httpservers #默认使用的服务器组
backend httpservers #名字要与上面的名字必须一样
balance roundrobin #负载均衡的方式
server http1 192.168.88.202:80 maxconn 2000 weight 1 check inter 1s rise 2 fall 2
server http2 192.168.88.203:80 maxconn 2000 weight 1 check inter 1s rise 2 fall 2
EOF
#启动haproxy
systemctl start haproxy
安装Keepalived实现HA高可用
#安装keepalived 155 和 206 都要操作
yum install -y keepalived
cp /etc/keepalived/keepalived.conf{ ,.bak}
cat > /etc/keepalived/keepalived.conf<< EOF
global_defs {
router_id directory1 #调度器的名称,备调度器改为directory2(两个名字一定不能一样)
}
vrrp_script check_haproxy {
script "/etc/keepalived/check_haproxy_status.sh"
interval 5 #每5秒执行一次
}
vrrp_instance VI_1 {
state MASTER #定义主还是备,备用的话写backup
interface ens33 #VIP绑定接口
virtual_router_id 80 #整个集群的调度器一致(在同一个集群)
priority 100 #主调度器优先级 备调度器改为50
advert_int 1 #发包
authentication {
auth_type PASS #主备节点认证信息
auth_pass 1111
}
virtual_ipaddress {
192.168.88.188/24 #VIP 设置为自己网段
}
track_script {
check_haproxy
}
}
EOF
cp /etc/keepalived/keepalived.conf{ ,.bak}
cat > /etc/keepalived/keepalived.conf<< EOF
global_defs {
router_id directory2 #调度器的名称,备调度器改为directory2(两个名字一定不能一样)
}
vrrp_script check_haproxy {
script "/etc/keepalived/check_haproxy_status.sh"
interval 1 #每1秒执行一次
weight -30
fall 3
rise 2
timeout 2
}
vrrp_instance VI_1 {
state backup #定义主还是备,备用的话写backup
interface ens33 #VIP绑定接口
nopreempt #设置到backup上面,不抢占资源
virtual_router_id 80 #整个集群的调度器一致(在同一个集群)
priority 50
advert_int 1 #发包
authentication {
auth_type PASS #主备节点认证信息
auth_pass 1111
}
virtual_ipaddress {
192.168.88.188/24 #VIP 设置为自己网段
}
track_script {
check_haproxy
}
}
EOF
添加检测脚本
cat > /etc/keepalived/check_haproxy_status.sh<< EOF
#!/bin/bash
/usr/bin/curl -I http://localhost &>/dev/null
if [ $? -ne 0 ];then
systemctl stop keepalived
fi
EOF
chmod a+x /etc/keepalived/check_haproxy_status.sh
#启动keepalived
systemctl start keepalived
#检测vip 是否绑定
ip add |grep 192.168.88.188
inet 192.168.88.188/24 scope global secondary ens33