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能将请求发送到对应的后端主机上,同时当主HAProxy服务器发生故障后,能立刻将负载均衡服务切换到备用HAProxy服务器上。


主机名

主机IP地址

集群角色

虚机IP/域名

master

172.24.8.10

主HAProxy服务器

172.24.8.100

backup

172.24.8.11

备HAProxy服务器

webapp1

172.24.8.30

后端web服务器

www.lz.com

webapp2

172.24.8.31

static.lz.com

webapp3

172.24.8.32

video.lz.com

1.2 架构规划

二 后端httpd集群部署

2.1 部署httpd集群

  1 [[email protected] ~]# yum -y install httpd
  2 [[email protected] ~]# vi /var/www/html/index.html
  3 This is my www.lz.com!
  4 [[email protected] ~]# vi /var/www/html/index.html
  5 This is my static.lz.com!
  6 [[email protected] ~]# vi /var/www/html/index.html
  7 This is my video.lz.com!
  8 [[email protected] ~]# systemctl start httpd.service
  9 [[email protected] ~]# systemctl enable httpd.service
 10 [[email protected] ~]# systemctl stop firewalld.service
 11 [[email protected] ~]# systemctl disable firewalld.service
 12 [[email protected] ~]# vi /etc/selinux/config
 13 SELINUX=disabled
 14 [[email protected] ~]# setenforce 0			#关闭SELinux及防火墙

注意:后端real server节点都需要安装,本环境针对httpd简单安装即可,无需过多配置。

三 基础NTP部署

3.1 NTP部署

  1 [[email protected] ~]# yum -y install ntp
  2 [[email protected] ~]# systemctl start  ntpd.service

建议:替换附件中的ntp配置文件,建议采用阿里云时钟进行同步。

注意:为了保证集群的稳定性,强烈建议在所有节点均部署NTP同步服务,保证所有时钟一致。

四 Keepalived部署

4.1 编译环境

安装基础环境及依赖:

  1 # yum -y install gcc gcc-c++ make kernel-devel kernel-tools kernel-tools-libs kernel libnl libnl-devel libnfnetlink-devel openssl-devel wget openssh-clients

4.2 安装Keepalived

  1 [[email protected]_master ~]# wget http://www.keepalived.org/software/keepalived-1.3.6.tar.gz
  2 [[email protected]_master ~]# tar -zxvf keepalived-1.3.6.tar.gz
  3 [[email protected]_master ~]# cd keepalived-1.3.6/
  4 [[email protected] keepalived-1.3.6]# ./configure --prefix=/usr/local/keepalived
  5 [[email protected]_master keepalived-1.3.6]# make && make install

注意:CentOS6.8安装高于1.3.6版本会出现未知错误。

4.3 添加Keepalived启动相关服务

  1 [[email protected]_master ~]# mkdir /etc/keepalived
  2 [[email protected]_master ~]# cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
  3 [[email protected]_master ~]# cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
  4 [[email protected]_master ~]# cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
  5 [[email protected]_master ~]# vi /etc/init.d/keepalived	#创建Keepalived启动脚本,见附件
  6 [[email protected]_master ~]# chmod u+x /etc/rc.d/init.d/keepalived

4.4 配置Keepalived

  1 [[email protected]_master ~]# vi /etc/keepalived/keepalived.conf
  2 global_defs {
  3    notification_email {
  4 #……
  5    }
  6    notification_email_from [email protected]
  7    smtp_server 192.168.200.1
  8    smtp_connect_timeout 30
  9    router_id LVS_DEVEL
 10 }
 11
 12 vrrp_script check_haproxy {
 13     script "/usr/bin/killall -0 haproxy"
 14     interval    2
 15     weight      21
 16     }
 17
 18 vrrp_instance HAProxy_HA {
 19     state BACKUP			#在HAProxy主备均设置为BACKUP
 20     interface eth0
 21     virtual_router_id 80
 22     priority 100
 23     advert_int 2
 24     nopreempt				#不抢占模式
 25     authentication {
 26         auth_type PASS
 27         auth_pass 1111
 28     }
 29
 30     notify_master "/etc/keepalived/master.sh"
 31     notify_backup "/etc/keepalived/backup.sh"
 32     notify_fault "/etc/keepalived/fault.sh"
 33
 34     track_script {
 35     check_haproxy
 36     }
 37
 38     virtual_ipaddress {
 39         172.24.8.100 dev eth0
 40     }
 41 }

4.5 编写Keepalived脚本

  1 [[email protected]_master ~]# vi /etc/keepalived/master.sh
  2 #!/bin/bash
  3 LOGFILE=/var/log/keepalived-mysql-state/log
  4 date >>$LOGFILE
  5 echo "[Master]" >>$LOGFILE
  6 [[email protected]_master ~]# vi /etc/keepalived/backup.sh
  7 #!/bin/bash
  8 LOGFILE=/var/log/keepalived-mysql-state/log
  9 date >>$LOGFILE
 10 echo "[BACKUP]" >>$LOGFILE
 11 [[email protected]_master ~]# vi /etc/keepalived/fault.sh
 12 #!/bin/bash
 13 LOGFILE=/var/log/keepalived-mysql-state/log
 14 date >>$LOGFILE
 15 echo "[FAULT]" >>$LOGFILE
 16 [[email protected]_master ~]# chmod u+x /etc/keepalived/backup.sh
 17 [[email protected]_master ~]# chmod u+x /etc/keepalived/master.sh
 18 [[email protected]_master ~]# chmod u+x /etc/keepalived/fault.sh

提示:本环境采用测试脚本,真实环境建议采用自动发送邮件通知运维员的脚本。

4.5 Bakcup节点配置

  1 [[email protected]_master ~]# scp /etc/keepalived/keepalived.conf 172.24.8.11:/etc/keepalived/keepalived.conf    #将配置好的Master节点的配置文件复制到Backup节点
  2 [[email protected]_slave ~]# vi /etc/keepalived/keepalived.conf
  3 state BACKUP
  4 priority 80

注意:在HAProxy备节点也设置为BACKUP,priority修改为低于HAProxy主角色的优先级即可,同时去掉nopreempt。

  1 [[email protected]_master ~]# scp /etc/keepalived/*.sh 172.24.8.11:/etc/keepalived/
  2 #将对应的脚本也复制至backup节点。

五 HAProxy部署

5.1 HAProxy安装

  1 [[email protected]_master ~]# yum -y install haproxy

提示:需要在主备HAProxy节点都安装。

5.2 HAProxy配置

  1 [[email protected]_master ~]# vi /etc/haproxy/haproxy.cfg
  2 global
  3     log         127.0.0.1 local0 info
  4     chroot      /var/lib/haproxy
  5     pidfile     /var/run/haproxy.pid
  6     maxconn     4096
  7     user        haproxy
  8     group       haproxy
  9     daemon
 10     nbproc      1
 11 defaults
 12     mode                    http
 13     log                     global
 14     retries                 3
 15     timeout connect         5s
 16     timeout client          30s
 17     timeout server          30s
 18     timeout check           2s
 19 frontend        www
 20     bind        172.24.8.100:80
 21     mode        http
 22     option      httplog
 23     option      forwardfor
 24     log         global
 25
 26     acl host_www        hdr_dom(host)   -i  www.lz.com	#配置不同域名分发不同后端策略
 27     acl host_static     hdr_dom(host)   -i  static.lz.com
 28     acl host_video      hdr_dom(host)   -i  video.lz.com
 29
 30     use_backend     server_www      if      host_www	#配置不同域名分发不同后端策略
 31     use_backend     server_static   if      host_static
 32     use_backend     server_video    if      host_video
 33 backend         server_www		#后端真是服务器
 34     mode        http
 35     option      redispatch
 36     option      abortonclose
 37     balance     roundrobin
 38     option      httpchk GET /index.html
 39     server      webapp1 172.24.8.30:80  weight 6 check inter 2000 rise 2 fall 3
 40 backend         server_static		#后端真是服务器
 41     mode        http
 42     option      redispatch
 43     option      abortonclose
 44     balance     roundrobin
 45     option      httpchk GET /index.html
 46     server      webapp2 172.24.8.31:80  weight 6 check inter 2000 rise 2 fall 3
 47 backend         server_video		#后端真是服务器
 48     mode        http
 49     option      redispatch
 50     option      abortonclose
 51     balance     roundrobin
 52     option      httpchk GET /index.html
 53     server      webapp3 172.24.8.32:80  weight 6 check inter 2000 rise 2 fall 3
 54 [[email protected]_master ~]# scp /etc/haproxy/haproxy.cfg [email protected]:/etc/haproxy/haproxy.cfg		#将配置文件复制至HAProxy备节点

5.3 打开转发

  1 [[email protected]_master ~]# vi /etc/sysctl.conf
  2 net.ipv4.ip_nonlocal_bind = 1
  3 [[email protected]_master ~]# sysctl -p

注意:绑定非本机的IP必须在sysctl.conf文件中配置。

六 启动服务

  1 [[email protected] ~]# systemctl start httpd

提示:三个节点的httpd服务均启动。

  1 [[email protected]_master ~]# service haproxy start

提示:必须先启动HAProxy服务,之后启动Keepalived,因为Keepalived会先检测HAProxy服务进程。

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

七 验证测试

7.1 高可用验证

  1 [[email protected]_master ~]# ip add

  1 [[email protected]_master ~]# service haproxy stop		#停止HAProxy主节点的HAProxy进程
  2 [[email protected]_master ~]# tail -f /var/log/messages	#观察HAProxy主节点日志

  1 [[email protected]_slave ~]# ip addr				#查看备HAProxy节点的IP

结论:通过测试可知当主HAProxy节点服务异常,Keepalived会检测到,同时HAProxy会将vip从主节点移除,备HAProxy会接管。

  1 [[email protected]_master ~]# service haproxy start

结论:由于配置了非抢占模式,主HAProxy恢复进程之后,依旧由备HAProxy提供服务,vip不会切回至主HAProxy。

7.2 负载均衡测试

主机hosts中添加如下解析:

  1 172.24.8.100	www.lz.com
  2 172.24.8.100	static.lz.com
  3 172.24.8.100	video.lz.com

浏览器分别访问不用的三个域名:

原文地址:https://www.cnblogs.com/guarderming/p/10254743.html

时间: 2024-10-07 04:51:06

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 高可用负载均衡解决方案

IP 信息列表: 名称     IP -----------------------------------VIP      192.168.200.254 Haproxy-1    192.168.200.202Haproxy-2    192.168.200.204Nginx1     192.168.200.202 Nginx2     192.168.200.203 1.在Nginx1/2上编译安装nginx服务1.1首先安装Nginx1[[email protected] ~]# yu

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群集结构中,至少包括两台热备的负载调度器,两台以上的节点服务器,