Haproxy + keepalived 高可用负载均衡解决方案

IP 信息列表:

名称     IP
-----------------------------------
VIP      192.168.200.254
Haproxy-1    192.168.200.202
Haproxy-2    192.168.200.204
Nginx1     192.168.200.202
Nginx2     192.168.200.203

1、在Nginx1/2上编译安装nginx服务
1.1首先安装Nginx1
[[email protected] ~]# yum -y install pcre-devel zlib-devel
[[email protected] ~]# useradd -M -s /sbin/nologin  nginx
[[email protected] ~]# tar xf nginx-1.6.2.tar.gz -C /usr/src
[[email protected] ~]# cd /usr/src/nginx-1.6.2
[[email protected] nginx-1.6.2]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx && make && make install
[[email protected] nginx-1.6.2]# cd /usr/local/nginx/html/
[[email protected] html]# echo "server 192.168.200.202" > index.html
[[email protected] html]# /usr/local/nginx/sbin/nginx
[[email protected] html]# netstat -anpt |grep nginx
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      4503/nginx

1.2安装Nginx2, 同Nginx1搭建方式是一样的。
与Nginx1唯一不同的是:
[[email protected] html]# echo "server 192.168.200.203" > index.html

2.安装Haproxy-1与Haproxy-2 两台机器配置一致:
[[email protected] ~]# yum -y install pcre-devel bzip2-devel
[[email protected] ~]# tar xf haproxy-1.4.24.tar.gz -C /usr/src/
[[email protected] ~]# cd /usr/src/haproxy-1.4.24/
[[email protected] haproxy-1.4.24]# make TARGET=linux26 && make install

2.1Haproxy服务器配置
建立haproxy的配置目录及文件
[[email protected] haproxy-1.4.24]# mkdir /etc/haproxy
[[email protected] haproxy-1.4.24]# cp examples/haproxy.cfg /etc/haproxy/

2.2 haproxy配置项的介绍
haproxy的配置文件通常分为三部分: global(全局配置部分) defaults(默认配置部分) listen(应用组件部分)

[[email protected] ~]# vim /etc/haproxy/haproxy.cfg
# this config needs haproxy-1.1.28 or haproxy-1.2.1

global
 log 127.0.0.1 local0
 log 127.0.0.1 local1 notice
 #log loghost local0 info
 maxconn 4096
 #chroot /usr/share/haproxy
 uid 99
 gid 99
 daemon
 #debug
 #quiet

defaults
 log global
 mode http
 option httplog
 option dontlognull
 retries 3
 #redispatch
 maxconn 2000
 contimeout 5000
 clitimeout 50000
 srvtimeout 50000

listen web-cluster 0.0.0.0:80
 option httpchk GET /index.html
 balance roundrobin
 server inst1 192.168.200.202:80 check inter 2000 fall 3
 server inst2 192.168.200.203:80 check inter 2000 fall 3

2.3 创建自启动脚本
[[email protected] ~]# cp /usr/src/haproxy-1.4.24/examples/haproxy.init /etc/init.d/haproxy
[[email protected] ~]# ln -s /usr/local/sbin/haproxy /usr/sbin/haproxy
[[email protected] ~]# chmod +x /etc/init.d/haproxy
[[email protected] ~]# /etc/init.d/haproxy start
Starting haproxy:                                          [确定]

2.4 测试:
用浏览器打开 http://192.168.200.204
再次打开一个新的浏览器再次访问 http://192.168.200.204

用浏览器打开 http://192.168.200.202
再次打开一个新的浏览器再次访问 http://192.168.200.202

可以验证两次访问到的结果分别为:
server 192.168.200.202
server 192.168.200.203

3、编译安装keepalived服务
[[email protected] ~]# yum -y install kernel-devel openssl-devel popt-devel
[[email protected] ~]# tar xf keepalived-1.2.13.tar.gz
[[email protected] ~]# cd keepalived-1.2.13
[[email protected] keepalived-1.2.13]# ./configure --prefix=/ --with-kernel-dir=/usr/src/kernels/2.6.18-194.el5-i686 && make && make install

3.1 配置keepalibed 开机启动脚本
[[email protected] ~]# chkconfig --add keepalived
[[email protected] ~]# chkconfig keepalived on
[[email protected]y-1 ~]# chkconfig --list keepalived
[[email protected] ~]# service keepalived start|stop

3.2.1 配置keepalibed 主配置文件
[[email protected] ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived

vrrp_script chk_http_port {
script "/etc/keepalived/check_haproxy.sh"
interval 2

global_defs {
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}

track_script {
chk_http_port
}

virtual_ipaddress {
192.168.200.254
}
}
}

3.2.2 第二台Haproxy配置keepalibed 主配置文件
[[email protected] ~]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived

vrrp_script chk_http_port {
script "/etc/keepalived/check_haproxy.sh"
interval 2
weight 2

global_defs {
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 50
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}

track_script {
chk_http_port
}

virtual_ipaddress {
192.168.200.254
}
}
}

4、两台机器上都配置haproxy检测脚本
[[email protected] ~]# cat /etc/keepalived/check_haproxy.sh
#!/bin/bash
A=`ps -C haproxy --no-header |wc -l`
if [ $A -eq 0 ]
then
 /usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/conf/haproxy.cfg
 sleep 3
 if [ `ps -C haproxy --no-header |wc -l` -eq 0 ]
 then
  /etc/init.d/keepalived stop
 fi
fi

[[email protected] ~]# chmod +x /etc/keepalived/check_haproxy.sh

[[email protected] ~]# service keepalived start
[[email protected] ~]# service keepalived start

5.1、测试VIP地址
[[email protected] ~]# ip addr show dev eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:cc:18:a2 brd ff:ff:ff:ff:ff:ff
    inet 192.168.200.201/24 brd 192.168.200.255 scope global eth0
    inet 192.168.200.254/32 scope global eth0
    inet6 fe80::20c:29ff:fecc:18a2/64 scope link
       valid_lft forever preferred_lft forever

[[email protected] ~]# /etc/init.d/keepalived stop
停止 keepalived:                                          [确定]

[[email protected] ~]# ip addr show dev eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:fd:8a:4e brd ff:ff:ff:ff:ff:ff
    inet 192.168.200.204/24 brd 192.168.200.255 scope global eth0
    inet 192.168.200.254/32 scope global eth0
    inet6 fe80::20c:29ff:fefd:8a4e/64 scope link
       valid_lft forever preferred_lft forever
   
5.2、测试Haproxy健康检查  
[[email protected] ~]# service haproxy stop
Shutting down haproxy:                                     [确定]
[[email protected] ~]# service haproxy status
haproxy (pid 59717) 正在运行...
   
   
5.3 网页测试:
用浏览器打开 http://192.168.200.254
再次打开一个新的浏览器再次访问 http://192.168.200.254

可以验证两次访问到的结果分别为:
server 192.168.200.202
server 192.168.200.203

DNS:
www IN A 192.168.200.254
www IN A 192.168.200.253

时间: 2024-10-15 08:50:30

Haproxy + keepalived 高可用负载均衡解决方案的相关文章

案例一(haproxy+keepalived高可用负载均衡系统)【转】

1.搭建环境描述: 操作系统: [[email protected] ~]# cat /etc/redhat-release CentOS release 6.7 (Final) 地址规划: 主机名 IP地址 集群角色 虚拟IP haproxy-server 10.0.0.35 主HAProxyServer 10.0.0.40 haproxy-backup 10.0.0.36 备用HAProxyServer webapp1 10.0.0.150 Backend Server 无 webapp2

HAProxy+Keepalived 高可用负载均衡

转自 https://www.jianshu.com/p/95cc6e875456 Keepalived+haproxy实现高可用负载均衡 Master 192.168.0.69 haproxy.keepalived Centos7.4 backup 192.168.0.70 haproxy.keepalived Centos7.4 vip(虚拟IP) 192.168.0.180 192.168.0.181 1.两台都安装haproxy,参照安装haproxy文档 2. 两台都安装Keepali

基于HAProxy+Keepalived高可用负载均衡web服务的搭建

一 原理简介 1.HAProxyHAProxy提供高可用性.负载均衡以及基于TCP和HTTP应用的代理,支持虚拟主机,它是免费.快速并且可靠的一种解决方案.HAProxy特别适用于那些负载特大的web站点,这些站点通常又需要会话保持或七层处理.HAProxy运行在时下的硬件上,完全可以支持数以万计的并发连接.并且它的运行模式使得它可以很简单安全的整合进当前的架构中, 同时可以保护web服务器不被暴露到网络上.2.KeepalivedKeepalived 是一个基于VRRP协议来实现的LVS服务高

HAProxy+Keepalived高可用负载均衡

一 基础准备 1.1 部署环境及说明 系统OS:CentOS 6.8 64位 HAProxy软件:HA-Proxy version 1.5.18 Keepalived软件:keepalived-1.3.6.tar.gz 官方链接:http://www.haproxy.org/ (国内可能无法打开) 下载连接:http://pkgs.fedoraproject.org/repo/pkgs/haproxy/ 部署说明:当用户访问对应的域名时,HAProxy能将请求发送到对应的后端主机上,同时当主HA

HAProxy+Keepalived高可用负载均衡配置

一.系统环境:系统版本:CentOS5.5 x86_64master_ip:172.20.27.40backup_ip:172.20.27.50 vip:172.20.27.200web_1: 172.20.27.90web_2:172.20.27.100二.haproxy安装:1.首先172.20.27.40安装上安装:1.1安装 tar zxvf haproxy-1.3.20.tar.gz cd haproxy-1.3.20 make TARGET=linux26 PREFIX=/usr/l

(2)LVS+Keepalived高可用负载均衡架构原理及配置

1.keepalived 介绍2.keepalived 优缺点3.keepalived 应用场景4.keepalived 安装配置5.keepalived+lvs 高可用6.keepalived+nginx 高可用7.keepalived 切换原理8.性能优化9.常见故障 一.keepalived 介绍 1.keepalived 定义keepalived是一个基于VRRP(virtual route redundent protocol)协议来实现的LVS服务高可用方案,可以利用其来避免单点故障

虚拟机 搭建LVS + DR + keepalived 高可用负载均衡

一:环境说明:   LVS-DR-Master:    10.3.0.82   LVS-DR-Backup:    10.3.0.70   VIP:                10.3.0.60   RealServer1:    10.3.0.69   RealServer1:    10.3.0.83   Centos:5.7    二:安装LVS和Keepalvied软件包   1. 下载相关软件包    mkdir /usr/local/src/lvs    cd /usr/loca

配置nginx+keepalived高可用负载均衡的时候。主从服务器都出现了VIP 阿里云问题

配置nginx+keepalived高可用负载均衡的时候.主从服务器都出现了VIP 出现这问题的场景是在阿里VPS云服务器网络环境中,因为路由交换层禁用了ARP的广播限制,造成KEEPALIVE主备协议无法通过广播的方式进行通信,造成主备两台服务器都强占HAVIP地址,出现同时两台服务器都有VIP地址的情况出现,必须通过配置来指定IP的两台服务器间进行通讯(阿里说明文档中解释只能支持两台使用同一个HAVIP地址),基于以下方法可以的情况下,多备方式用同样的方式也应该可行 ,有需要的兄弟可以测试下

LVS+Keepalived高可用负载均衡集群架构

实验环境: LVS和keepalived介绍: 1.keepalived的设计目标是构建高可用的LVS负载均衡群集,可以调用ipvsadm工具来创建虚拟服务器,管理服务器池,而不仅仅用来做双机热备. 使用keepalived构建LVS群集更加简便易用,主要优势体现在: 对LVS负载调度器实现热备切换,提高可用性: 对服务器池中的节点进行健康检查,自动移除失效节点,恢复后再重新加入. 2.在基于LVS+Keepalived实现的LVS群集结构中,至少包括两台热备的负载调度器,两台以上的节点服务器,