start-stop-and-restart-apache

Ubuntu 14.04搭建Apache web server服务器集群

有时候Apache 服务器出现故障或者访问量太大,导致网站或页面加载缓慢,我们需要另外的服务器来分流或者来承担一部分,我们就想到了服务器集群。

Apache Web 服务器集群运行环境:Ubuntu 14.04

负载均衡节点2台电脑
主机名:lbserver1 ip:192.168.31.251
主机名:lbserver2 ip:192.168.31.250

web服务器节点2台电脑
主机名:rs1 ip:192.168.31.252
主机名:rs2 ip:192.168.31.253

虚拟IP:192.168.31.254

所需要的软件apache2,haproxy,keepalived

一)在两台web服务器上安装apache2以及相关配置

sudo apt-get install apache2

1.1修改两台web服务器apaache的日志配置文件:

sudo vi /etc/apache2/apache2.conf

#LogFormat "%h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined

1.2在两台web服务器中/var/www/html/中创建一个文件check.txt 用来测试能不能正常访问负载节点

sudo touch /var/www/html/check.txt

check.txt内容随便写

1.3修改两台web服务器的虚拟主机文件

sudo vi /etc/apache2/sites-available/000-default.conf

文件中添加

SetEnvIf Request_URI "^/check\.txt$" dontlog

#CustomLog ${APACHE_LOG_DIR}/access.log combined
CustomLog /var/log/apache2/access.log combined env=!dontlog

1.4重启apache

sudo /etc/init.d/apache2 restart

二)在两台负载节点的电脑上安装haproxy以及相关配置

sudo apt-get install haproxy

2.1配置haproxy.cfg,配置前我先备份,两台负载节点配置一样

sudo vi /etc/haproxy/haproxy.cfg

内容如下:

global
log 127.0.0.1 local0
log 127.0.0.1 local1 notice
maxconn 4096
user haproxy
group haproxy

defaults
log global
mode http
option httplog
option dontlognull
retries 3
option redispatch
contimeout 5000
clitimeout 50000
srvtimeout 50000
errorfile 400 /etc/haproxy/errors/400.http
errorfile 403 /etc/haproxy/errors/403.http
errorfile 408 /etc/haproxy/errors/408.http
errorfile 500 /etc/haproxy/errors/500.http
errorfile 502 /etc/haproxy/errors/502.http
errorfile 503 /etc/haproxy/errors/503.http
errorfile 504 /etc/haproxy/errors/504.http

listen webfarm 192.168.31.254:80
mode http
stats enable
stats auth admin:password
balance roundrobin
cookie JSESSIONID prefix
option httpclose
option forwardfor
option httpchk HEAD /check.txt HTTP/1.0
server webA 192.168.31.252:80 cookie A check
server webB 192.168.31.253:80 cookie B check

2.2修改/etc/sysctl.conf 为了让haproxy能绑定到虚拟的IP地址:

sudo vi /etc/sysctl.conf

在文的最后添加

net.ipv4.ip_nonlocal_bind-1

然后运行:

sudo sysctl -p

2.3使haproxy开机自动启动

sudo vi /etc/default/haproxy

#ENABLED=0
ENABLED=1

三)在负载节点的2台电脑上安装keepalived,我将lbserver1设置为主,lbserver2设置为从

sudo apt-get install keepalived

3.1 配置keepalived

sudo vi /etc/keepalived/keepalived.conf

在lbserver1的配置:

vrrp_script chk_haproxy {
script "killall -0 haproxy"
interval 2
weight 2
}

vrrp_instance VI_1 {
interface eth0
state MASTER
virtual_router_id 51
priority 101
virtual_ipaddress {
192.168.31.254
}
track_script {
chk_haproxy
}
}

在lbserver2的配置:

vrrp_script chk_haproxy {
script "killall -0 haproxy"
interval 2
weight 2
}

vrrp_instance VI_1 {
interface eth0
state MASTER
virtual_router_id 51
priority 100
virtual_ipaddress {
192.168.31.254
}
track_script {
chk_haproxy
}
}

3.2 在两台电脑上重启keepalived服务

sudo /etc/init.d/keepalived restart

3.3 分别查看负载节点电脑的IP信息:

ip addr sh eth0

在lbserver1上信息显示如下,可以看到虚拟IP地址 192.168.31.254:

2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 08:00:27:ec:a5:c7 brd ff:ff:ff:ff:ff:ff
inet 192.168.31.250/24 brd 192.168.31.255 scope global eth0
valid_lft forever preferred_lft forever
inet 192.168.31.254/32 scope global eth0
valid_lft forever preferred_lft forever
inet6 fe80::a00:27ff:feec:a5c7/64 scope link
valid_lft forever preferred_lft forever

在lbserver2上则无法看到虚拟IP地址

2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 08:00:27:dc:3b:d7 brd ff:ff:ff:ff:ff:ff
inet 192.168.31.251/24 brd 192.168.31.255 scope global eth0
valid_lft forever preferred_lft forever
inet6 fe80::a00:27ff:fedc:3bd7/64 scope link
valid_lft forever preferred_lft forever

3.4 重启haproxy服务

sudo /etc/init.d/haproxy restart

四)测试服务是否正常

直接在浏览器中访问192.168.31.254地址,能访问就是正常,也可以访问haproxy的统计页面:

http://192.168.31.254/haproxy?stats

打赏