HAProxy+Keepalived高可用负载均衡
一 基础准备
1.1 部署环境及说明
主机名
|
主机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 [root@webapp1 ~]# yum -y install httpd
2 [root@webapp1 ~]# vi /var/www/html/index.html
3 This is my www.lz.com!
4 [root@webapp1 ~]# vi /var/www/html/index.html
5 This is my static.lz.com!
6 [root@webapp1 ~]# vi /var/www/html/index.html
7 This is my video.lz.com!
8 [root@webapp1 ~]# systemctl start httpd.service
9 [root@webapp1 ~]# systemctl enable httpd.service
10 [root@webapp1 ~]# systemctl stop firewalld.service
11 [root@webapp1 ~]# systemctl disable firewalld.service
12 [root@webapp1 ~]# vi /etc/selinux/config
13 SELINUX=disabled
14 [root@webapp1 ~]# setenforce 0 #关闭SELinux及防火墙
三 基础NTP部署
3.1 NTP部署
1 [root@webapp1 ~]# yum -y install ntp
2 [root@webapp1 ~]# systemctl start ntpd.service
四 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 [root@haproxy_master ~]# wget http://www.keepalived.org/software/keepalived-1.3.6.tar.gz
2 [root@haproxy_master ~]# tar -zxvf keepalived-1.3.6.tar.gz
3 [root@haproxy_master ~]# cd keepalived-1.3.6/
4 [root@lvsmaster keepalived-1.3.6]# ./configure --prefix=/usr/local/keepalived
5 [root@haproxy_master keepalived-1.3.6]# make && make install
4.3 添加Keepalived启动相关服务
1 [root@haproxy_master ~]# mkdir /etc/keepalived
2 [root@haproxy_master ~]# cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
3 [root@haproxy_master ~]# cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
4 [root@haproxy_master ~]# cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
5 [root@haproxy_master ~]# vi /etc/init.d/keepalived #创建Keepalived启动脚本,见附件
6 [root@haproxy_master ~]# chmod u+x /etc/rc.d/init.d/keepalived
4.4 配置Keepalived
1 [root@haproxy_master ~]# vi /etc/keepalived/keepalived.conf
2 global_defs {
3 notification_email {
4 #……
5 }
6 notification_email_from Alexandre.Cassen@firewall.loc
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 [root@haproxy_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 [root@haproxy_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 [root@haproxy_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 [root@haproxy_master ~]# chmod u+x /etc/keepalived/backup.sh
17 [root@haproxy_master ~]# chmod u+x /etc/keepalived/master.sh
18 [root@haproxy_master ~]# chmod u+x /etc/keepalived/fault.sh
4.5 Bakcup节点配置
1 [root@haproxy_master ~]# scp /etc/keepalived/keepalived.conf 172.24.8.11:/etc/keepalived/keepalived.conf #将配置好的Master节点的配置文件复制到Backup节点
2 [root@haproxy_slave ~]# vi /etc/keepalived/keepalived.conf
3 state BACKUP
4 priority 80
1 [root@haproxy_master ~]# scp /etc/keepalived/*.sh 172.24.8.11:/etc/keepalived/
2 #将对应的脚本也复制至backup节点。
五 HAProxy部署
5.1 HAProxy安装
1 [root@haproxy_master ~]# yum -y install haproxy
5.2 HAProxy配置
1 [root@haproxy_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 [root@haproxy_master ~]# scp /etc/haproxy/haproxy.cfg root@172.24.8.11:/etc/haproxy/haproxy.cfg #将配置文件复制至HAProxy备节点
5.3 打开转发
1 [root@haproxy_master ~]# vi /etc/sysctl.conf
2 net.ipv4.ip_nonlocal_bind = 1
3 [root@haproxy_master ~]# sysctl -p
六 启动服务
1 [root@webapp1 ~]# systemctl start httpd
1 [root@haproxy_master ~]# service haproxy start
1 [root@haproxy_master ~]# service keepalived start
七 验证测试
7.1 高可用验证
1 [root@haproxy_master ~]# ip add
1 [root@haproxy_master ~]# service haproxy stop #停止HAProxy主节点的HAProxy进程
2 [root@haproxy_master ~]# tail -f /var/log/messages #观察HAProxy主节点日志
1 [root@haproxy_slave ~]# ip addr #查看备HAProxy节点的IP
1 [root@haproxy_master ~]# service haproxy start
7.2 负载均衡测试
1 172.24.8.100 www.lz.com
2 172.24.8.100 static.lz.com
3 172.24.8.100 video.lz.com
HAProxy+Keepalived高可用负载均衡的更多相关文章
- 案例一(haproxy+keepalived高可用负载均衡系统)【转】
1.搭建环境描述: 操作系统: [root@HA-1 ~]# cat /etc/redhat-release CentOS release 6.7 (Final) 地址规划: 主机名 IP地址 集群角 ...
- HAProxy+Keepalived 高可用负载均衡
转自 https://www.jianshu.com/p/95cc6e875456 Keepalived+haproxy实现高可用负载均衡 Master backup vip(虚拟IP) 192.16 ...
- 基于HAProxy+Keepalived高可用负载均衡web服务的搭建
一 原理简介 1.HAProxyHAProxy提供高可用性.负载均衡以及基于TCP和HTTP应用的代理,支持虚拟主机,它是免费.快速并且可靠的一种解决方案.HAProxy特别适用于那些负载特大的web ...
- 005.HAProxy+Keepalived高可用负载均衡
一 基础准备 1.1 部署环境及说明 系统OS:CentOS 6.8 64位 HAProxy软件:HA-Proxy version 1.5.18 Keepalived软件:keepalived-1.3 ...
- Haproxy+Keepalived高可用负载均衡详细配置
本文所使用的环境: 10.6.2.128 centos6.5 10.6.2.129 centos6.5 VIP 为10.6.2.150 要实现的目标: 实现10.6.2.128和10.6 ...
- 测试LVS+Keepalived高可用负载均衡集群
测试LVS+Keepalived高可用负载均衡集群 1. 启动LVS高可用集群服务 此时查看Keepalived服务的系统日志信息如下: [root@localhost ~]# tail -f /va ...
- LVS+Keepalived高可用负载均衡集群架构实验-01
一.为什么要使用负载均衡技术? 1.系统高可用性 2. 系统可扩展性 3. 负载均衡能力 LVS+keepalived能很好的实现以上的要求,LVS提供负载均衡,keepalived提供健康检查, ...
- 使用Ansible实现nginx+keepalived高可用负载均衡自动化部署
本篇文章记录通过Ansible自动化部署nginx的负载均衡高可用,前端代理使用nginx+keepalived,端web server使用3台nginx用于负载效果的体现,结构图如下: 部署前准备工 ...
- Keepalived+HAProxy 搭建高可用负载均衡
转载自:https://mp.weixin.qq.com/s/VebiWftaRa26x1aA21Jqww 1. 概述 软件负载均衡技术是指可以为多个后端服务器节点提供前端IP流量分发调度服务的软件技 ...
随机推荐
- aria2 资料
https://www.jianshu.com/p/8124b5b6ef95https://quan.ithome.com/0/331/853.htmhttp://www.360doc.com/con ...
- 原生JavaScript实现轮播图
---恢复内容开始--- 实现原理 通过自定义的animate函数来改变元素的left值让图片呈现左右滚动的效果 HTML: <!DOCTYPE html> <html> &l ...
- layui 框架 table插件 实现键盘快捷键 切换单元格编辑
最近使用layui的框架时,发现table插件不支持键盘快捷键切换单元格,花了点时间实现此功能. 分享给有需要的朋友们~~~ 效果图 代码: 1.支持 enter,上,下,右键 切换单元格,支持隐藏列 ...
- linux下测试某网址或IP端口能否访问
参考这篇 Linux系统下怎么测试端口的连通性 测试端口连通 nc -v -w 1 183.xx.oo.ee -z 82 1linux主机centos操作系统,项目里面需要访问一个URL路径的接口ht ...
- Abp vNext抽茧剥丝01 使用using临时更改当前租户
在Abp vNext中,如果开启了多租户功能,在业务代码中默认使用当前租户的数据,如果我们需要更改当前租户,可以使用下面的方法 /* 此时当前租户 */ using (CurrentTenant.Ch ...
- Django-模型层(多表操作)
目录 1.创建模型 1.1方式一:自行创建第三张表 1.2方式二:通过ManyToManyField自动创建第三张表 1.3关于db_column和verbose_name 1.4关于on_delet ...
- 使用批处理打包C#开发程序
最近项目接近尾声,测试比较频繁,每天需要发布多个版本 @echo off set zip=C:\Program Files\7-Zip\7z.exe set timestamp=%date:~6,4% ...
- phantomJS+Python 隐形浏览器
phantomjs解压后,把文件夹bin中的phantomjs.exe移到python文件夹中的Scripts中 实例: from selenium import webdriver driver = ...
- Linux安装部署项目实例
本次安装jdk,mysql,maven,redis,nginx,tomcat 安装之前先升级系统 使用命令:/bin/yum - y update 1.安装jdk 先建立一个项目的目录-jiaoton ...
- Spring源码窥探之:声明式事务
1. 导入驱动,连接池,jdbc和AOP的依赖 <!-- c3p0数据库连接池 --> <dependency> <groupId>c3p0</groupId ...