Centos7+Haproxy实现Apache负载均衡

简介:

说到Linux下的负载均衡,其实有很多服务是可以实现的,比如:haproxy,lvs,nginx,这些服务都可以做负载均衡,而我们今天主要介绍的是Haproxy实现Apache的负载均衡。一般做法是通过Haproxy+Keeplive实现高可用负载均衡的,我们将会在后面的文章介绍。

目的:访问Haproxy自动轮询分配后台服务器

环境介绍:

Hostname:A-S

IP:192.168.5.21

Role:Apache

Hostname:B-S

IP:192.168.5.22

Role:Apache

Hostname:C-S

IP:192.168.5.23

Role:Haproxy

我们先看看安装完默认的Haproxy的配置介绍:

1
vim  /etc/haproxy/haproxy .cfg
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#---------------------------------------------------------------------
# Example configuration for a possible web application. See the
# full configuration options online.
#
# http://haproxy.1wt.eu/download/1.4/doc/configuration.txt
#
#---------------------------------------------------------------------
#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
global
# to have these messages end up in /var/log/haproxy.log you will
# need to:
#
# 1) configure syslog to accept network log events. This is done
# by adding the '-r' option to the SYSLOGD_OPTIONS in
# /etc/sysconfig/syslog
#
# 2) configure local2 events to go to the /var/log/haproxy.log
# file. A line like the following can be added to
# /etc/sysconfig/syslog
#
# local2.* /var/log/haproxy.log
#
log 127.0.0.1 local2  #日志输出配置,所有日志都记录在本机,通过local2输出
chroot  /var/lib/haproxy  #改变当前工作目录。
pidfile  /var/run/haproxy .pid  #所属运行的gid
maxconn 4000  #最大连接数
user haproxy
group haproxy
daemon  #以后台形式运行haproxy
# turn on stats unix socket
stats socket  /var/lib/haproxy/stats
#---------------------------------------------------------------------
# common defaults that all the 'listen' and 'backend' sections will
# use if not designated in their block
#---------------------------------------------------------------------
defaults
mode http  #默认的模式mode { tcp|http|health },tcp是4层,http是7层,health只会返回OK
log global
option httplog
option dontlognull
option http-server-close
option forwardfor except 127.0.0.0 /8
option redispatch  #当serverId对应的服务器挂掉后,强制定向到其他健康的服务器
retries 3  #两次连接失败就认为是服务器不可用
timeout http-request 10s
timeout queue 1m
timeout connect 10s  #连接超时
timeout client 1m  #客户端超时
timeout server 1m  #服务器超时
timeout http-keep-alive 10s
timeout check 10s  #心跳检测超时
maxconn 3000  #默认的最大连接数
#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
frontend main *:5000  #haproxy服务器监听端口
acl url_static path_beg -i  /static  /images  /javascript  /stylesheets
acl url_static path_end -i .jpg .gif .png .css .js
use_backend static  if  url_static
default_backend app
#---------------------------------------------------------------------
# static backend for serving up images, stylesheets and such
#---------------------------------------------------------------------
backend static
balance roundrobin
#banlance roundrobin 轮询,balance source 保存session值,支持static-rr,leastconn,first,uri等参数
server static 127.0.0.1:4331 check
#---------------------------------------------------------------------
# round robin balancing between the various backends
#---------------------------------------------------------------------
backend app
balance roundrobin
server app1 127.0.0.1:5001 check
server app2 127.0.0.1:5002 check
server app3 127.0.0.1:5003 check
server app4 127.0.0.1:5004 check

接下来我们开始正式环境配置介绍:首先是安装Apache服务,然后配置显示页面:

1
Yum  install  httpd

clip_image001

安装完成

clip_image002

查看httpd 版本

clip_image003

接下来我们首先要为apache定义一个 默认的页面,方便区分

1
Vim  /var/www/httml/index .html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
< /html >
<!DOCTYPE html>
<html>
< head >
<title>Welcome to Apache< /title >
<style>
body {
35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
< /style >
<style  type = "text/css" >
h1{color:red}
h2{color:blue}
h3{color:green}
h4{color:yellow}
}
< /style >
< /head ><body bgcolor= '#46A3FF' >
<h1>Welcome to A-S Apache< /h1 >
<h2>HostName:A-S< /h2 >
<h3>IP:192.168.5.21< /h3 >
<h4>Service:Apache< /h4 >
<input  type =button value= "Refresh"  onclick= "window.location.href('http://192.168.5.21')" >
< /body >
< /html >

clip_image004

启动服务

1
Systemctl start httpd

clip_image005

然后添加默认的防火墙端口8o

1
Firewall-cmd --zone=public --add-port= '80/tcp'  --permanent

clip_image006

或者vim /etc/firewalld/zone/public.xml

添加一下格式

1
<port portocal= 'tcp'  port= '80' >

clip_image007

接下来我们在本地访问测试一下

clip_image008

接下来我们也同样按照方法部署第二台apache服务,方法跟上面完全一样

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
< /html >
<!DOCTYPE html>
<html>
< head >
<title>Welcome to Apache< /title >
<style>
body {
35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
< /style >
<style  type = "text/css" >
h1{color:red}
h2{color:blue}
h3{color:green}
h4{color:yellow}
}
< /style >
< /head ><body bgcolor= '#CA8EFF' >
<h1>Welcome to B-S Apache< /h1 >
<h2>HostName:B-S< /h2 >
<h3>IP:192.168.5.22< /h3 >
<h4>Service:Apache< /h4 >
<input  type =button value= "Refresh"  onclick= "window.location.href('http://192.168.5.22')" >
< /body >
< /html >

clip_image009

测试访问

clip_image010

最后我们开始部署haproxy了

我们使用192.168.5.33来部署haproxy了

1
yum  install  -y haproxy

clip_image011

clip_image012

我们可以编辑haproxy的默认配置文件

1
vim  /etc/haproxy/haproxy .nfg

clip_image013

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#---------------------------------------------------------------------
# Example configuration for a possible web application. See the
# full configuration options online.
#
# http://haproxy.1wt.eu/download/1.4/doc/configuration.txt
#
#---------------------------------------------------------------------
#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
global
# to have these messages end up in /var/log/haproxy.log you will
# need to:
#
# 1) configure syslog to accept network log events. This is done
# by adding the '-r' option to the SYSLOGD_OPTIONS in
# /etc/sysconfig/syslog
#
# 2) configure local2 events to go to the /var/log/haproxy.log
# file. A line like the following can be added to
# /etc/sysconfig/syslog
#
# local2.* /var/log/haproxy.log
#
log 127.0.0.1 local2
chroot  /var/lib/haproxy
pidfile  /var/run/haproxy .pid
maxconn 4000
user haproxy
group haproxy
daemon
# turn on stats unix socket
stats socket  /var/lib/haproxy/stats
#---------------------------------------------------------------------
# common defaults that all the 'listen' and 'backend' sections will
# use if not designated in their block
#---------------------------------------------------------------------
defaults
mode http
log global
option httplog
option dontlognull
option http-server-close
option forwardfor except 127.0.0.0 /8
option redispatch
retries 3
timeout http-request 10s
timeout queue 1m
timeout connect 10s
timeout client 1m
timeout server 1m
timeout http-keep-alive 10s
timeout check 10s
maxconn 3000
#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
frontend main *:5000
acl url_static path_beg -i  /static  /images  /javascript  /stylesheets
acl url_static path_end -i .jpg .gif .png .css .js
use_backend static  if  url_static
default_backend app
#---------------------------------------------------------------------
# static backend for serving up images, stylesheets and such
#---------------------------------------------------------------------
backend static
balance roundrobin
server static 127.0.0.1:4331 check
#---------------------------------------------------------------------
# round robin balancing between the various backends
#---------------------------------------------------------------------
backend app
balance roundrobin
server app1 127.0.0.1:5001 check
server app2 127.0.0.1:5002 check
server app3 127.0.0.1:5003 check
server app4 127.0.0.1:5004 check

我们在配置文件的最后面,将默认的端口从4331修改成80

然后添加服务器

更改前

clip_image014

更改后

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#---------------------------------------------------------------------
# Example configuration for a possible web application.  See the
# full configuration options online.
#
#   http://haproxy.1wt.eu/download/1.4/doc/configuration.txt
#
#---------------------------------------------------------------------
#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
global
     # to have these messages end up in /var/log/haproxy.log you will
     # need to:
     #
     # 1) configure syslog to accept network log events.  This is done
     #    by adding the '-r' option to the SYSLOGD_OPTIONS in
     #    /etc/sysconfig/syslog
     #
     # 2) configure local2 events to go to the /var/log/haproxy.log
     #   file. A line like the following can be added to
     #   /etc/sysconfig/syslog
     #
     #    local2.*                       /var/log/haproxy.log
     #
     log         127.0.0.1 local2
     chroot       /var/lib/haproxy
     pidfile      /var/run/haproxy .pid
     maxconn     4000
     user        haproxy
     group       haproxy
     daemon
     # turn on stats unix socket
     stats socket  /var/lib/haproxy/stats
#---------------------------------------------------------------------
# common defaults that all the 'listen' and 'backend' sections will
# use if not designated in their block
#---------------------------------------------------------------------
defaults
     mode                    http
     log                     global
     option                  httplog
     option                  dontlognull
     option http-server-close
     option forwardfor       except 127.0.0.0 /8
     option                  redispatch
     retries                 3
     timeout http-request    10s
     timeout queue           1m
     timeout connect         10s
     timeout client          1m
     timeout server          1m
     timeout http-keep-alive 10s
     timeout check           10s
     maxconn                 3000
#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
frontend  main *:80
     acl url_static       path_beg       -i  /static  /images  /javascript  /stylesheets
     acl url_static       path_end       -i .jpg .gif .png .css .js
     use_backend static           if  url_static
     default_backend             app
#---------------------------------------------------------------------
# static backend for serving up images, stylesheets and such
#---------------------------------------------------------------------
backend static
     balance     roundrobin
     server      static 127.0.0.1:80 check
#---------------------------------------------------------------------
# round robin balancing between the various backends
#---------------------------------------------------------------------
backend app
     balance     roundrobin
     server  app1 127.0.0.1:5001 check
     server  app2 127.0.0.1:5002 check
     server  app3 127.0.0.1:5003 check
     server  app4 127.0.0.1:5004 check
     server  app5 192.168.5.21:80 check
     server  app6 192.168.5.22:80 check

clip_image015

然后启动haproxy服务

1
systemctl start haproxy

clip_image016

然后测试访问haproxy访问

clip_image017

我们通过本地查看端口监听状态

1
netstat  -anlpt

我们发现默认的端口是5000

clip_image018

我们使用5000端口在访问一下

clip_image019

clip_image020

接下来我们更改默认端口

1
vim  /etc/haproxy/haproxy .cfg

clip_image021

修改为80

clip_image022

修改后我们重启

clip_image023

我们查看端口监听状态

clip_image024

测试访问

clip_image025

clip_image026



本文转自 高文龙 51CTO博客,原文链接:http://blog.51cto.com/gaowenlong/1883159,如需转载请自行联系原作者

相关实践学习
部署高可用架构
本场景主要介绍如何使用云服务器ECS、负载均衡SLB、云数据库RDS和数据传输服务产品来部署多可用区高可用架构。
负载均衡入门与产品使用指南
负载均衡(Server Load Balancer)是对多台云服务器进行流量分发的负载均衡服务,可以通过流量分发扩展应用系统对外的服务能力,通过消除单点故障提升应用系统的可用性。 本课程主要介绍负载均衡的相关技术以及阿里云负载均衡产品的使用方法。
相关文章
|
6月前
|
负载均衡 算法 应用服务中间件
百度搜索:蓝易云【Linux系统如何使用 HAProxy、Nginx 和 Keepalived 进行负载均衡?】
通过上述步骤,你可以在Linux系统中使用HAProxy、Nginx和Keepalived来实现负载均衡。这些工具可以帮助你将流量分配到多个后端服务器上,提高系统的性能、可靠性和可扩展性。
90 0
|
3天前
|
负载均衡 监控 网络协议
使用haproxy实现负载均衡集群
【4月更文挑战第14天】HAProxy提供高可用性、负载均衡以及基于TCP和HTTP应用的代理,快速并且可靠的一种解决方案。
8 1
|
1月前
|
存储 缓存 负载均衡
【Apache ShenYu源码】如何实现负载均衡模块设计
整个模块为ShenYu提供了什么功能。我们可以看下上文我们提到的工厂对象。/***/核心方法很清晰,我们传入Upsteam列表,通过这个模块的负载均衡算法,负载均衡地返回其中一个对象。这也就是这个模块提供的功能。
18 1
|
5月前
|
负载均衡 前端开发 网络协议
Keepalived+HAProxy 搭建高可用负载均衡(二)
Keepalived+HAProxy 搭建高可用负载均衡
|
5月前
|
负载均衡 算法 调度
Keepalived+HAProxy 搭建高可用负载均衡
Keepalived+HAProxy 搭建高可用负载均衡
224 0
|
5月前
|
负载均衡 算法 应用服务中间件
Haproxy负载均衡
Haproxy负载均衡
|
6月前
|
负载均衡 前端开发 应用服务中间件
企业实战(22)基于Haproxy负载均衡+Keepalived高可用集群实战详解
企业实战(22)基于Haproxy负载均衡+Keepalived高可用集群实战详解
|
8月前
|
负载均衡 应用服务中间件 Linux
百度搜索:蓝易云【Centos7系统Nginx负载均衡如何安装和配置?】
在本文中,我们将介绍如何在CentOS 7系统中安装和配置Nginx负载均衡。本教程适用于初学者和经验丰富的用户。
122 0
|
8月前
|
负载均衡 监控 安全
apache+tomcat实现动静分离和负载均衡
apache+tomcat实现动静分离和负载均衡
131 0
|
11月前
|
运维 负载均衡 监控
在 Linux 中如何使用 HAProxy、Nginx 和 Keepalived 进行负载均衡?
在 Linux 中如何使用 HAProxy、Nginx 和 Keepalived 进行负载均衡?
672 0
在 Linux 中如何使用 HAProxy、Nginx 和 Keepalived 进行负载均衡?