keepalived与haproxy结合实现网站负债均衡

Haproxy和Keepalived简介

HAProxy功能:

可以针对HTTP请求添加cookie,进行路由后端服务器
可平衡负载至后端服务器,并支持持久连接
支持基于cookie进行调度
支持所有主服务器故障切换至备用服务器
支持专用端口实现监控服务
支持不影响现有连接情况下停止接受新连接请求
可以在双向添加,修改或删除HTTP报文首部
支持基于pattern实现连接请求的访问控制
通过特定的URI为授权用户提供详细的状态信息

Keepalived功能:

基于vrrp协议完成地址流动
为vip地址所在的节点生成ipvs规则(在配置文件中预先定义)
为ipvs集群的各RS做健康状态检测
基于脚本调用接口通过执行脚本完成脚本中定义的功能,进而影响集群事务,以此 支持nginx、haproxy等服务

实验架构图

实验环境

OS:centos7.6x86_64
keepalived:keepalived-1.3.5-6.el7.x86_64
haproxy:haproxy-1.5.18-8.el7.x86_64
Apache:httpd-2.4.6-88.el7.centos.x86_64
php-fpm:php-fpm-5.4.16-46.el7.x86_64
mysql:mariadb-server-1:5.5.60-1.el7_5.x86_64
wordpross:wordpress-4.9.4-zh_CN.tar.gz

实验目的

Haproxy+Keepalived主从高可用,keepalived为Haproxy主从提供高可用保证,主调度器若挂掉,从调度器能无缝接管,haproxy为后端web提供检测功能,实现WEB站点负载均衡+高可用性

实验配置

在两台调度器上分别安装并配置haproxy,且配置相同

IP:192.168.8.143
IP:192.168.8.144

[[email protected] ~]#yum -y install haproxy
[[email protected] ~]#cp /etc/haproxy/haproxy.cfg /etc/haproxy/haproxy.cfg.bak
[[email protected] ~]#vim /etc/haproxy/haproxy.cfg.bak
#在配置文件末尾添加要监听的web服务器的地址
......
listen web_http
    mode http
    bind 192.168.2.100:80
    server web1 192.168.8.146:80 check inter 3000 fall 3 rise 5
    server web2 192.168.8.145:80 check inter 3000 fall 3 rise 5
#在调度器上打开内核路由转发功能,和地址监听功能
[[email protected] ~]#vim /etc/sysctl.d/99-sysctl.conf #编辑配置文件,添加如下两行
net.ipv4.ip_forward = 1
net.ipv4.ip_nonlocal_bind = 1
#使配置文件生效
[[email protected] ~]#sysctl -p
#启动haproxy服务,并且查看地址是否监听
[[email protected] ~]#systemctl start haproxy.service
[[email protected] ~]#systemctl enable haproxy.service
#查看是否已监听VIP地址
[[email protected] ~]#ss -ntl
State      Recv-Q Send-Q             Local Address:Port               Peer Address:Port
LISTEN     0      128                192.168.8.100:80                 *:*    

安装并配置keepalived,并添加lvs的命令行工具ipvsadm,以方便查看生成的lvs规则

keepalived master IP:192.168.8.143

[[email protected] ~]#yum -y install keepalived
[[email protected] ~]#yum -y install ipvsadm
[[email protected] ~]#cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak
[[email protected] ~]#vim /etc/keepalived/keepalived.conf
global_defs {
   notification_email {
        [email protected]
   }
   notification_email_from [email protected]
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id lvs1
   vrrp_skip_check_adv_addr
   vrrp_strict
   vrrp_iptables #关闭防火墙,否则当VIP转移时会自动打开防火墙,拦截所有的连接
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}

vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 70
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.8.100 dev eth0 label eth0:0
    }
}

virtual_server 192.168.8.100 80 {
        delay_loop 3
        lb_algo wrr
        lb_kind DR
        protocol TCP

        sorry_server 192.168.8.149 80
        real_server 192.168.8.145 80 {
                weight 2
                TCP_CHECK {
                        connect_timeout 5
                        nb_get_retry 3
                        delay_befor_retry 3
                        connect_port 80
                }
        }

        real_server 192.168.8.146 80 {
                weight 2
                TCP_CHECK {
                        connect_timeout 5
                        nb_get_retry 3
                        delay_befor_retry 3
                        connect_port 80
                }
        }
}
#启动keepalived服务
[[email protected] ~]#systemctl start keepalived.service
[[email protected] ~]#systemctl enable keepalived.service

keepalived slave IP:192.168.8.144(大部分配置文件一样,只需改动少许配置)

[[email protected] ~]#yum -y install keepalived
[[email protected] ~]#yum -y install ipvsadm
[[email protected] ~]#cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak
[[email protected] ~]#vim /etc/keepalived/keepalived.conf
global_defs {
   notification_email {
       [email protected]
   }
   notification_email_from [email protected]
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id lvs1
   vrrp_skip_check_adv_addr
   vrrp_strict
   vrrp_iptables
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}

vrrp_instance VI_1 {
    state BACKUP #改为从模式
    interface eth0
    virtual_router_id 51
    priority 70 #改动优先级为70,要比主服务器优先级小
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.8.100 dev eth0 label eth0:0
    }
}

virtual_server 192.168.8.100 80 {
        delay_loop 3
        lb_algo wrr
        lb_kind DR
        protocol TCP

        sorry_server 192.168.8.149 80
        real_server 192.168.8.145 80 {
                weight 2
                TCP_CHECK {
                        connect_timeout 5
                        nb_get_retry 3
                        delay_befor_retry 3
                        connect_port 80
                }
        }

        real_server 192.168.8.146 80 {
                weight 2
                TCP_CHECK {
                        connect_timeout 5
                        nb_get_retry 3
                        delay_befor_retry 3
                        connect_port 80
                }
        }
}
 #启动keepalived服务
[[email protected] ~]#isystemctl start keepalived.service
[[email protected] ~]#isystemctl enable keepalived.service

安装并配置nfs服务器和wordpross

nfs server IP:192.168.8.149

[[email protected] ~]#yum -y install nfs-utils
[[email protected] ~]#tar xf wordpress-4.9.4-zh_CN.tar.gz
[[email protected] ~]#mkdir /wordpross #创建挂载目录
[[email protected] ~]#mv wordpress/* /wordpross/ #移动配置文件到创建的目录中
......
define( ‘DB_NAME‘, ‘wordpross‘ ); #数据库名称
/** MySQL database username */
define( ‘DB_USER‘, ‘user1‘ ); #管理账号用户名
/** MySQL database password */
define( ‘DB_PASSWORD‘, ‘centos‘ ); #管理账号密码
/** MySQL hostname */
define( ‘DB_HOST‘, ‘www.test.com‘ ); #数据库IP或域名
......
[[email protected] ~]#vim /etc/exports
/wordpross  192.168.8.0/24(rw,no_root_squash)
#启动nfs服务
[[email protected] ~]#systemctl start nfs.service
[[email protected] ~]#systemctl enable nfs.service

在mysql服务器上安装mariadb,并设置配置文件

mysql master IP:192.168.8.150

[[email protected] ~]#yum -y install mariadb-server
[[email protected] ~]#vim /etc/my.cnf #编辑配置文件,添加如下几行
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock

server_id=1
skip_name_resolve
innodb_file_per_table
log_bin
#启动mariadb服务
systemctl start mariadb
systemctl enable mariadb
#进入数据库查看二进制文件的位置
MariaDB [(none)]> show master logs;
+------------------+-----------+
| Log_name         | File_size |
+------------------+-----------+
| mariadb-bin.000001 |     245 |
#添加复制账号
MariaDB [(none)]> grant replication slave on *.* to [email protected]‘192.168.8.134‘ identified by ‘centos‘;

mysql slave IP:192.168.8.134

[[email protected] ~]#yum -y install mariadb-server
[[email protected] ~]#vim /etc/my.cnf #编辑配置文件,添加如下几行
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock

server_id=2
read_only
skip_name_resolve
innodb_file_per_table
#启动数据库服务
[[email protected] ~]#systemctl start mariadb
[[email protected] ~]#systemctl enable mariadb
#进入数据库,添加同步账号
MariaDB [(none)]> CHANGE MASTER TO
    ->   MASTER_HOST=‘192.168.8.150‘,
    ->   MASTER_USER=‘test‘,
    ->   MASTER_PASSWORD=‘centos‘,
    ->   MASTER_PORT=3306,
    ->   MASTER_LOG_FILE=‘mariadb-bin.000001‘,
    ->   MASTER_LOG_POS=245;
#开始进行同步
MariaDB [(none)]> start slave;
#查看同步信息
MariaDB [(none)]> show slave status\G;
Slave_IO_Running: Yes #同步线程以开启
Slave_SQL_Running: Yes
在masyql主服务器上添加wordpross数据库
MariaDB [(none)]> create database wordpross;
MariaDB [(none)]> grant all on wordpross.* to [email protected]‘192.168.8.%‘ identified by ‘centos‘;

安装http服务器,并配置php-fpm,两台服务器的配置相同

web1 IP:192.168.8.145
web2 IP:192.168.8.146

[[email protected] ~]# yum -y install httpd
[[email protected] ~]# yum -y install php-fpm
[[email protected] ~]# yum -y install nfs-utils
#启动httpb服务
[[email protected] ~]# systemctl start httpd
[[email protected] ~]# systemctl enable httpd
[[email protected] ~]# vim /etc/php-fpm.d/www.conf
#注释掉这一行
;listen.allowed_clients = 127.0.0.1
#启动php-fpm服务
[[email protected] ~]#systemctl restart php-fpm
[[email protected] ~]#systemctl enable php-fpm
#查看9000端口是否监听
[[email protected] ~]#iss -ntl
State       Recv-Q Send-Q   Local Address:Port                  Peer Address:Port
LISTEN      0      128          127.0.0.1:9000                  *:*
#挂载nfs设备
[[email protected] ~]#ivim /etc/fstab
192.168.8.149:/wordpross /var/www/html nfs defaults 0 0
#使配置文件生效
[[email protected] ~]#mount -a
#查看挂载是否成功
[[email protected] ~]#df
192.168.8.149:/wordpross 151967360  1462528 150504832    1% /var/www/html
#在回环网卡上绑定VIP地址,并关闭网卡的广播功能,需要脚本来实现
[[email protected] ~]#vim lvs_dr_rs.sh
#!/bin/bash
vip=192.168.8.100 #写入VIP的地址
mask=‘255.255.255.255‘
dev=lo:1
rpm -q httpd &> /dev/null || yum -y install httpd &>/dev/null
service httpd start &> /dev/null && echo "The httpd Server is Ready!"
case $1 in
start)
    echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
    echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
    echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
    echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
    ifconfig $dev $vip netmask $mask #broadcast $vip up
    #route add -host $vip dev $dev
    echo "The RS Server is Ready!"
    ;;
stop)
    ifconfig $dev down
    echo 0 > /proc/sys/net/ipv4/conf/all/arp_ignore
    echo 0 > /proc/sys/net/ipv4/conf/lo/arp_ignore
    echo 0 > /proc/sys/net/ipv4/conf/all/arp_announce
    echo 0 > /proc/sys/net/ipv4/conf/lo/arp_announce
    echo "The RS Server is Canceled!"
    ;;
*)
    echo "Usage: $(basename $0) start|stop"
    exit 1
    ;;
esac
#执行脚本并查看是否绑定成功
[[email protected] ~]#bash lvs_dr_rs.sh start
[[email protected] ~]#ifconfig
lo:1: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 192.168.8.100  netmask 255.255.255.255
        loop  txqueuelen 1000  (Local Loopback)
#编辑host文件,将解析指向mysql服务器的地址
[[email protected] ~]#vim /etc/hosts
192.168.8.150   www.test.com

客户机测试:

client IP :192.168.8.132

注册后可直接登录

在msyql从服务器上查看是否同步注册信息

MariaDB [(none)]> use wordpross;
MariaDB [wordpross]> select * from wp_users\G;
*************************** 1. row ***************************
                 ID: 1
         user_login: admin
          user_pass: $P$BV98wIJbUmTzsprC6v7Dea1VGKUFIU/
      user_nicename: admin
         user_email: [email protected]
           user_url:
    user_registered: 2019-06-09 06:36:37
user_activation_key:
        user_status: 0
       display_name: admin

原文地址:https://blog.51cto.com/14163901/2406480

时间: 2024-10-18 10:07:13

keepalived与haproxy结合实现网站负债均衡的相关文章

Keepalived+HAproxy实现高可用负载均衡

总概:       Keepalived是一个类似于layer3, 4 & 5交换机制的软件,也就是我们平时说的第3层.第4层和第5层交换.Keepalived的作用是检测web服务器的状态,如果有一台web服务器死机,或工作出现故障,Keepalived将检测到,并将有故障的web服务器从系统中剔除,当web服务器工作正常后Keepalived自动将web服务器加入到服务器群中,这些工作全部自动完成,不需要人工干涉,需要人工做的只是修复故障的web服务器       HAProxy提供高可用性

使用HAproxy,keepalived实现简单LAMP架构的负载均衡与高可用

整个架构简单拓扑图如下所示: 其中: 192.168.10.66与192.168.10.6两台主机提供httpd服务 192.168.10.27与192.168.10.87两台主机提供varnish缓存服务 192.168.10.17与192.168.10.77两台主机安装HAProxy实现客户端请求的调控 192.168.10.7主机安装mariadb,提供数据存储服务 服务器与客户端连接ip地址为172.16.10.99/16, 一.httpd服务 1.分别于192.168.10.6与192

HAproxy+keepalived/pacemaker实现高可用,负载均衡技术

HAproxy+keepalived/pacemaker Haproxy+keepalived的结合,实现负载均衡与高可用的完美结合,既解决了提供服务的负载均衡,又解决了作负载均衡器的单点故障问题.这样架构就很健壮了. 材料准备: keepalived-1.2.20.tar.gz 步骤: #tar -zxf  keepalived-1.2.20.tar.gz    :解压源码包,进行编译安装 #cd   keepalived-1.2.20 #yum  install  -y  gcc  open

基于keepalived的Haproxy高可用配置

一.概述: HAProxy是一个用于4层或7层的高性能负载均衡软件,在大型网站的大型Web服务器群集中,HAProxy可用来替代专业的硬件负载均衡设备,节省大量的开支. 通常情况下,为了避免整个体系中出现单点故障,在至关重要的架构中,都需要部署备份设备,同样,负载均衡设备也不能部署单台,一旦主设备出现问题之后,备份设备可对主设备进行接管.实现不间断的服务,这便是Keepalived的作用. 于是,HAProxy和Keepalived的组合便成了省钱高效的Web服务器负载均衡架构. 拓扑图: 二.

使用keepalived实现haproxy的高可用

一.haproxy和keepalived的解释及本次实验的拓扑图: 1.haproxy:haproxy是免费.极速且可靠的用于为TCP和基于HTTP应用程序提供负载均衡和代理服务的解决方案,尤其适用于高负载且需要持久连接或7层处理机制的web站点. 2.haproxy的特性:客户端侧的长连接(client-side keep-alive):TCP加速(TCP speedups): 响应池(response buffering):RDP协议:基于源的粘性(source-based stickine

KeepAlived实现HAProxy双主并实现资源的动静分离

KeepAlived实现HAProxy双主并实现资源的动静分离 大纲 前言 HAProxy介绍 实验拓扑 实验环境 实验步骤 配置后端httpd服务器 配置HAProxy实现backend负载均衡 测试动静分离效果 配置KeepAlived 测试KeepAlived 配置DNS DNS测试 最终测试 总结 前言 我们这次使用HAProxy作为负载均衡调度器来实现后端httpd服务的负载均衡和动静分离,并使用KeepAlived让HAproxy实现双主高可用, 再使用DNS轮询将用户对www.an

Hapoxy+keepalived实现双主高可用负载均衡

在测试了Nginx+Keepalived的负载均衡后,也对Haproxy+Keepalived双主模式做了测试,双主的模式充分利用了服务器资源,这样不会导致服务器浪费. 这里举例说明: 默认情况下,第一台负载均衡器主要分发 www.breaklinux的请求,第二台负载均衡器主要分发img.breaklinux.com 的请求. 任意一台宕机都不会影响网站分发. 实验环境 VIP1=192.168.137.200 (www.zhirs.com) VIP2=192.168.137.100 (img

Haproxy+keepalied实现web负载均衡

一.实验拓扑图: keepalived: keepalived的主要目的是为ipvs提供高可用性,因此keepalived除了能够实现资源转移之外,自身能够调用ipvsadm命令来生成规则,而且还能够检测后端Rserver服务的健康状态检测.keepalived自身就是一个服务 ,启动之后工作在两个节点上(可以是多个节点),一个为当前活动节点,一个为备用节点简称为一主(master)一备(backup).master会不时的向backup传递通告信息(这种通告信息机制是基于vrrp协议),备用节

大型网站--负载均衡架构

负载均衡 (Load Balancing) 负载均衡建立在现有网络结构之上,它提供了一种廉价有效透明的方法扩展网络设备和服务器的带宽.增加吞吐量.加强网络数据处理能力.提高网络的灵活性和可用性. 大型网站负载均衡的利器 全局负载均衡系统(GSLB) 内容缓存系统(CDN) 服务器负载均衡系统(SLB) DNS域名解析的基本过程 最初的负载均衡解决方案(DNS轮询) 优点 基本上无成本,因为往往域名注册商的这种解析都是免费的: 部署方便,除了网络拓扑的简单扩增,新增的Web服务器只要增加一个公网I