keepalived+haproxy高可用集群

环境:
主Haproxy服务器 192.168.80.100 keepalived+Haproxy
备Haproxy服务器 192.168.80.101 keepalived+Haproxy
web服务器1 192.168.80.102 httpd(nginx、tomcat)
web服务器2 192.168.80.103 httpd

首先部署两台web服务器:
systemctl stop firewalld

setenforce 0

yum install httpd -y

vi /etc/httpd/conf/httpd.conf
修改以下内容:
ServerName … 去掉注释符号

cd /var/www/html/

192.168.80.102:
echo “

server aa

” > index.html
192.168.80.103
echo “

server bb

” > index.html

systemctl start httpd

主Haproxy服务器:
yum install -y epel-release

yum install keepalived -y

vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
route_id haproxy-01
}
vrrp_script haproxy { 定义一个热备脚本取名为haproxy
script “/opt/haproxy.sh” # 检测 haproxy 状态的脚本路径
interval 2 # 检测时间间隔
weight 2 # 如果条件成立,权重+2
}
vrrp_instance VI_1 {
state MASTER
interface ens32
virtual_router_id 51
priority 150
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script { # 将 track_script 块加入 instance 配置块
haproxy # 检查 HAProxy 服务是否存活
}
virtual_ipaddress {
192.168.80.188
}
}

vi /opt/haproxy.sh
#!/bin/bash
if [ $(ps -C haproxy --no-header | wc -l) -eq 0 ];then
/usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg 启动haproxy服务
fi
sleep 2 等待2秒
if [ $(ps -C haproxy --no-header | wc -l) -eq 0 ];then
service keepalived stop
fi

chmod +x /opt/haproxy.sh

tar xf haproxy-1.5.15.tar.gz -C /opt/

cd /opt/haproxy-1.5.15/

yum install -y pcre-devel bzip2-devel gcc gcc-c++ make

make TARGET=linux26 PREFIX=/usr/local/haproxy

make install PREFIX=/usr/local/haproxy

mkdir /etc/haproxy

useradd -s /sbin/nologin -M haproxy

id haproxy

cp /opt/haproxy-1.5.15/examples/haproxy.cfg /etc/haproxy/

vi /etc/haproxy/haproxy.cfg
修改以下内容:

chroot /usr/share/haproxy 注释这一行

在default模块下
listen admin_stats
bind 0.0.0.0:8089
stats enable
mode http
log global
stats uri /stats
stats realm Haproxy\ Statistics
stats auth admin:admin
#stats hide-version
stats admin if TRUE
stats refresh 30s

listen webcluster
bind 0.0.0.0:80
mode http
option httpchk GET /index.html
log global
maxconn 3000
balance roundrobin
server web01 192.168.80.102:80 check inter 2000 fall 5
server web02 192.168.80.103:80 check inter 2000 fall 5

cp examples/haproxy.init /etc/init.d/haproxy

chmod 755 /etc/init.d/haproxy

chkconfig --add haproxy

ln -s /usr/local/haproxy/sbin/haproxy /usr/sbin/haproxy

systemctl status keepalived

ip addr show ens32

netstat -anpt | grep haproxy

备haproxy服务器:
yum install -y epel-release

yum install keepalived -y

vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
route_id haproxy-02
}
vrrp_script haproxy { 定义一个热备脚本取名为haproxy
script “/opt/haproxy.sh” # 检测 haproxy 状态的脚本路径
interval 2 # 检测时间间隔
weight 2 # 如果条件成立,权重+2
}
vrrp_instance VI_1 {
state BACKUP
interface ens32
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script { # 将 track_script 块加入 instance 配置块
haproxy # 检查 HAProxy 服务是否存活
}
virtual_ipaddress {
192.168.80.188
}
}

vi /opt/haproxy.sh
#!/bin/bash
#Filename:
A=$(ip addr | grep 192.168.80.188/32 | grep -v grep | wc -l)
if [ $A -gt 0 ]; then
service haproxy start
else
service haproxy stop
fi

chmod +x /opt/haproxy.sh

tar xf haproxy-1.5.15.tar.gz -C /opt/

cd /opt/haproxy-1.5.15/

yum install -y pcre-devel bzip2-devel gcc gcc-c++ make

make TARGET=linux26 PREFIX=/usr/local/haproxy

make install PREFIX=/usr/local/haproxy

mkdir /etc/haproxy

useradd -s /sbin/nologin -M haproxy

id haproxy

cp /opt/haproxy-1.5.15/examples/haproxy.cfg /etc/haproxy/

vi /etc/haproxy/haproxy.cfg
修改以下内容:

chroot /usr/share/haproxy 注释这一行

在default模块下
listen admin_stats
bind 0.0.0.0:8089
stats enable
mode http
log global
stats uri /stats
stats realm Haproxy\ Statistics
stats auth admin:admin
#stats hide-version
stats admin if TRUE
stats refresh 30s

listen webcluster
bind 0.0.0.0:80
mode http
option httpchk GET /index.html
log global
maxconn 3000
balance roundrobin
server web01 192.168.80.102:80 check inter 2000 fall 5
server web02 192.168.80.103:80 check inter 2000 fall 5

cp examples/haproxy.init /etc/init.d/haproxy

chmod 755 /etc/init.d/haproxy

chkconfig --add haproxy

ln -s /usr/local/haproxy/sbin/haproxy /usr/sbin/haproxy

systemctl status keepalived

ip addr show ens32

netstat -anpt | grep haproxy

测试:
在浏览器中输入192.168.80.188测试

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值