Keepalived实现HAProxy高可用搭建
Keepalived实现HAProxy高可用
文章目录
- Keepalived实现HAProxy高可用
- 拓扑表格如下
- 拓扑结构(示例)
- nginx安装(所有server)
- HAProxy安装(主备机相同)
- HAProxy配置(主备机相同)
- 注释如下内容:
- Keepalived安装与配置
- Keepalived安装(主备机相同)
- Keepalived配置
- 验证
- 负载均衡验证
- 检查主备ip
- 关闭主机HAProxy服务
拓扑表格如下
主机名 | IP | 环境 |
---|---|---|
master | 192.168.242.67 | Keepalived + HAProxy |
master-backup | 192.168.242.68 | Keepalived + HAProxy |
server1 | 192.168.242.69 | nginx |
server2 | 192.168.242.70 | nginx |
拓扑结构(示例)
nginx安装(所有server)
# 添加nginx yum源
vi /etc/yum.repos.d/nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
# 安装nginx
yum -y install nginx
# 修改网页内容(便于区分)
vi /usr/share/nginx/html/index.html
# 启动
systemctl start nginx
HAProxy安装(主备机相同)
# 安装HAProxy
yum install haproxy -y
# 安装psmisc,便于后面使用killall命令
yum install psmisc -y
HAProxy配置(主备机相同)
vi /etc/haproxy/haproxy.cfg
注释如下内容:
# 末尾添加如下: frontend web_lb # 前端负载均衡 bind *:80 stats uri /ha_stats default_backend web_back backend web_back # 后台负载均衡 balance roundrobin server web01 192.168.242.69:80 check weight 1 server web02 192.168.242.70:80 check weight 1
# 启动haproxy systemctl restart haproxy
Keepalived安装与配置
Keepalived安装(主备机相同)
# 安装依赖
yum install -y curl gcc openssl-devel libnl3-devel net-snmp-devel
# 安装keepalived
yum install -y keepalived
Keepalived配置
vi /etc/keepalived/keepalived.conf
# -------------------------------------------------主机------------------------------------
! Configuration File for keepalived
global_defs {
router_id LVS_DEVEL # 虚拟路由名称
}
# 健康检查脚本内容
vrrp_script chk_haproxy {
script "killall -0 haproxy" # 使用killall-0检查haproxy实例是否存在,性能高于ps命令
interval 2 # 脚本运行周期
weight 2 # 每次检查的加权权重值
}
# 虚拟路由配置
vrrp_instance VI_1 {
state MASTER # 本机实例状态,MASTER/BACKUP,备机配置文件中请写BACKUP
interface ens33 # 本机网卡名称,使用ifcoffig命令查看
virtual_router_id 67 # 虚拟路由编号,主备机保持一致
priority 100 # 本机初始权重,备机请填写小于主机的值
advert_int 1 # 争抢虚地址的周期,秒
virtual_ipaddress {
192.168.242.100 # 虚地址IP,主备机保持一致
}
track_script {
chk_haproxy # 对应的健康检查配置
}
}
# -------------------------------------------------备机------------------------------------
! Configuration File for keepalived
global_defs {
router_id LVS_DEVEL # 虚拟路由名称
}
# 健康检查脚本内容
vrrp_script chk_haproxy {
script "killall -0 haproxy" # 使用killall-0检查haproxy实例是否存在,性能高于ps命令
interval 2 # 脚本运行周期
weight 2 # 每次检查的加权权重值
}
# 虚拟路由配置
vrrp_instance VI_1 {
state BACKUP # 本机实例状态,MASTER/BACKUP,备机配置文件中请写BACKUP
interface ens33 # 本机网卡名称,使用ifcoffig命令查看
virtual_router_id 67 # 虚拟路由编号,主备机保持一致
priority 99 # 本机初始权重,备机请填写小于主机的值
advert_int 1 # 争抢虚地址的周期,秒
virtual_ipaddress {
192.168.242.100 # 虚地址IP,主备机保持一致
}
track_script {
chk_haproxy # 对应的健康检查配置
}
}
启动Keepalived systemctl start keepalived
验证
负载均衡验证
检查主备ip
主
备
可以发现主机现在承担vip服务
关闭主机HAProxy服务
主
备
可见ip成功漂移(并且浏览器192.168.242.100的虚拟ip依然能够访问)