环境准备
安装环境:
-
Haproxy版本:Haproxy-1.8.20
-
keepalived版本:keepalived v1.3.5
-
Haproxy:172.24.77.241(master)
-
Haproxy:172.24.77.242(backup)
-
VIP地址:172.24.77.245
Haproxy安装:master和backup服务器安装Haproxy
-
安装依赖包
yum install gcc gcc-c++ glibc glibc-devel pcre pcre-devel openssl openssl-devel systemd-devel net-tools vim iotop bc zip unzip zlib-devel lrzsz tree screen lsof tcpdump wget ntpdate -y
-
下载并解压安装包
cd /usr/local/src/
wget https://src.fedoraproject.org/repo/pkgs/haproxy/haproxy-1.8.20.tar.gz/sha512/c288ccf223da71a29ae0f08f3c9753002735816229ea83ca497d46c860fb8a4bd422077a94652aa475e48aefb78787979cdce7f1bd0b5919dc671ba25212c133/haproxy-1.8.20.tar.gz
tar xvf haproxy-1.8.20.tar.gz
cd haproxy-1.8.20
-
编译安装Haproxy
make ARCH=x86_64 TARGET=linux2628 USE_PCRE=1 USE_OPENSSL=1 USE_ZLIB=1 USE_SYSTEMD=1 USE_CPU_AFFINITY=1 PREFIX=/usr/local/haproxy
make install PREFIX=/usr/local/haproxy
-
复制启动程序
cp haproxy /usr/sbin/
-
创建启动脚本
vim /usr/lib/systemd/system/haproxy.service
[Unit] Description=HAProxy Load Balancer After=syslog.target network.target [Service] ExecStartPre=/usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -c -q ExecStart=/usr/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid ExecReload=/bin/kill -USR2 $MAINPID [Install] WantedBy=multi-user.target
:wq
-
创建目录和用户
mkdir /etc/haproxy
cd /etc/haproxy
-
修改默认haproxy配置文件
vim /etc/haproxy/haproxy.cfg
global chroot /usr/local/haproxy
stats socket /var/lib/haproxy/haproxy.sock mode 600 level admin
user haproxy group haproxy daemon
pidfile /usr/local/haproxy/run/haproxy.pid log 127.0.0.1 local3 info defaults option http-keep-alive option forwardfor maxconn 100000 mode http timeout connect 300000ms timeout client 300000ms timeout server 300000ms listen stats mode http bind 0.0.0.0:9999 stats enable log global stats uri /haproxy-status stats auth haadmin: listen web_port bind 0.0.0.0:80 mode http log global server web1 172.24.77.241:8080 weight 5 check inter 2000 rise 2 fall 3 server web1 172.24.77.242:8080 weight 5 check inter 2000 rise 2 fall 3
-
创建启动用户
useradd haproxy -s /sbin/nologin
-
授权目录
mkdir /var/lib/haproxy
chown haproxy.haproxy /var/lib/haproxy/ -R
-
启动Haproxy
systemctl start haproxy
-
检查启动结果
ps -ef |grep haproxy |grep -v "grep"
lsof -i:80
-
设置开机启动
systemctl enable haproxy
Keepalive安装:master和backup服务器安装Keepalive
-
安装keepalive
yum install keepalived -y
-
查看安装版本信息
rpm -qi keepalived
-
配置Keepalived,master配置如下
vim /etc/keepalived/keepalived.conf
!删除多余配置
:35,156d
!修改为以下内容
! Configuration File for keepalived
global_defs { notification_email { root@locahost } notification_email_from keepalived@localhost smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id ha1.jay.com vrrp_skip_check_adv_addr vrrp_strict vrrp_garp_interval 0 vrrp_gna_interval 0 vrrp_mcast_group4 224.0.0.18 }
vrrp_instance VI_1 { state MASTER interface ens33 virtual_router_id 80 priority 100 advert_int 1 authentication { auth_type PASS auth_pass silence2t }
virtual_ipaddress { 172.24.77.245 dev ens33 lable ens33:0
}track_script {
chk_ haproxy
}
vrrp_script chk_haproxy{
script "/data/sh/check_haproxy.sh"
interval 1
weight 80
fall 3
rise 5
timeout 2
}
}
}
-
配置Keepalived,backup配置如下
vim /etc/keepalived/keepalived.conf
!删除所有配置
:%d
!粘贴以下内容
! Configuration File for keepalived
global_defs { notification_email { root@locahost } notification_email_from keepalived@localhost smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id ha1.jay.com vrrp_skip_check_adv_addr vrrp_strict vrrp_garp_interval 0 vrrp_gna_interval 0 vrrp_mcast_group4 224.0.0.18 }
vrrp_instance VI_1 { state BACKUP interface ens33 virtual_router_id 80 priority 90 advert_int 1 authentication { auth_type PASS auth_pass silence2t } virtual_ipaddress { 172.24.77.245 dev ens33 lable ens33:0
}track_script {
chk_ haproxy
}
vrrp_script chk_haproxy{
script "/data/sh/check_haproxy.sh"
interval 1
weight 80
fall 3
rise 5
timeout 2
}
}
-
查看VIP地址
ip addr
-
查看Iptalbes策略并删除
iptables -vnL --line-number
iptables -D INPUT 1
-
测试访问VIP
ping 172.24.77.245
-
测试keepalive高可用
!将占有VIP地址的主机关机,查看
systemctl stop keepalived
killall keepalived
创建check_ haproxy脚本
-
haproxy.sh脚本内容如下:创建目录
mkdir -p /data/sh/
-
安装killall
yum install psmisc -y
-
创建脚本文件
vim /data/sh/check_haproxy.sh
#!/bin/bash
#auto check haproxy process
#by author jay
/usr/bin/killall -0 haproxy
if
[[ $? -ne 0 ]]; then
/usr/bin/systemctl stop keepalived
fi
-
设置执行权限
chmod +x check_haproxy.sh
-
测试关闭Haproxy服务,查看keepavlive是否生效
killall -0 haproxy