[iyunv@lamp ~]# yum -y install httpd php mysql-server######创建测试页[iyunv@lamp ~]# vim /var/www/html/index.phpLAMP: 172.16.14.5
2、安装WEB1服务器的HTTPD服务并访问测试
[iyunv@WEB1 ~]# yum -y install httpd[iyunv@WEB1 ~]# service httpd start[iyunv@WEB1 ~]# chkconfig httpd on[iyunv@WEB1 ~]# echo "WEB1:172.16.14.3" > /var/www/html/index.html
3、安装WEB2服务器的HTTPD服务并访问测试
[iyunv@WEB2 ~]# yum -y install httpd[iyunv@WEB2 ~]# service httpd start[iyunv@WEB2 ~]# chkconfig httpd on[iyunv@WEB2 ~]# echo "WEB2:172.16.14.4" > /var/www/html/index.html
四、安装并配置Haproxy
1、在HA1服务器安装Haproxy
[backcolor=white !important]
######安装开发环境
[backcolor=white !important]
[iyunv@HA1 ~]# yum -y install gcc gcc-c++
[backcolor=white !important]
[iyunv@HA1 ~]# tar xf haproxy-1.4.24.tar.gz
[backcolor=white !important]
[iyunv@HA1 ~]# cd haproxy-1.4.24
[backcolor=white !important]
[iyunv@HA1 haproxy-1.4.24]# uname -r
[backcolor=white !important]
2.6.32-358.el6.x86_64
[backcolor=white !important]
[iyunv@HA1 haproxy-1.4.24]# make TARGET=linux26 ARCH=x86_64
[backcolor=white !important]
[iyunv@HA1 haproxy-1.4.24]# make install
[backcolor=white !important]
######注释:TARGET指定内核版本,而uname -r是查看内核版本;ARCH指定CPU架构
[backcolor=white !important]
----------------------------------------------------------------------
[backcolor=white !important]
######为Haproxy提供主配置文件与服务脚本,并将其添加到系统服务
[backcolor=white !important]
[iyunv@HA1 haproxy-1.4.24]# mkdir /etc/haproxy
[backcolor=white !important]
[iyunv@HA1 haproxy-1.4.24]# cp examples/haproxy.cfg /etc/haproxy
[backcolor=white !important]
[iyunv@HA1 haproxy-1.4.24]# cp examples/haproxy.init /etc/init.d/haproxy
[backcolor=white !important]
[iyunv@HA1 haproxy-1.4.24]# ln -s /usr/local/sbin/haproxy /usr/sbin/haproxy
[backcolor=white !important]
[iyunv@HA1 haproxy-1.4.24]# chmod +x /etc/init.d/haproxy
[backcolor=white !important]
[iyunv@HA1 haproxy-1.4.24]# chkconfig --add haproxy
[backcolor=white !important]
2、修改主配置文件
[backcolor=white !important]
[iyunv@HA1 ~]# vim /etc/haproxy/haproxy.cfg######创建Haproxy的工作目录
[backcolor=white !important]
[iyunv@HA1 ~]# mkdir /usr/share/haproxy
[backcolor=white !important]
# this config needs haproxy-1.1.28 or haproxy-1.2.1
[backcolor=white !important]
global
[backcolor=white !important]
log 127.0.0.1 local0 #日志配置,所有日志都记录在本地,通过local0输出
[backcolor=white !important]
log 127.0.0.1 local1 notice
[backcolor=white !important]
maxconn 25600 #最大连接数
[backcolor=white !important]
chroot /usr/share/haproxy #改变Haproxy的工作目录
[backcolor=white !important]
uid 99 #用户的UID
[backcolor=white !important]
gid 99 #用户的GID
[backcolor=white !important]
nbproc 1 #进程数据(可以设置多个)
[backcolor=white !important]
daemon #以后台守护进程方式运行Haproxy
[backcolor=white !important]
#debug #是否开启调试
[backcolor=white !important]
defaults
[backcolor=white !important]
log global
[backcolor=white !important]
mode http #默认使用协议,可以为{http|tcp|health} http:是七层协议 tcp:是四层 health:只返回OK
[backcolor=white !important]
option httplog #详细记录http日志
[backcolor=white !important]
option dontlognull #不记录健康检查的日志信息
[backcolor=white !important]
retries 3 #3次连接失败则认为服务不可用
[backcolor=white !important]
option redispatch #ServerID对应的服务器宕机后,强制定向到其他运行正常的服务器
[backcolor=white !important]
maxconn 30000 #默认的最大连接数
[backcolor=white !important]
contimeout 5000 #连接超时
[backcolor=white !important]
clitimeout 50000 #客户端超时
[backcolor=white !important]
srvtimeout 50000 #服务器超时
[backcolor=white !important]
timeout check 1s #心跳检测超时
[backcolor=white !important]
timeout http-request 10s #默认http请求超时时间
[backcolor=white !important]
timeout queue 1m #默认队列超时时间
[backcolor=white !important]
timeout connect 10s #默认连接超时时间
[backcolor=white !important]
timeout client 1m #默认客户端超时时间
[backcolor=white !important]
timeout server 1m #默认服务器超时时间
[backcolor=white !important]
timeout http-keep-alive 10s #默认持久连接超时时间
[backcolor=white !important]
listen stats
[backcolor=white !important]
mode http
[backcolor=white !important]
bind 0.0.0.0:8090 #指定IP地址与Port
[backcolor=white !important]
stats enable #开启Haproxy统计状态
[backcolor=white !important]
stats refresh 3s #统计页面自动刷新时间间隔
[backcolor=white !important]
stats hide-version #状态页面不显示版本号
[backcolor=white !important]
stats uri /allen #统计页面的uri为"/allen"
[backcolor=white !important]
stats realm Haproxy allen #统计页面认证时提示内容信息
[backcolor=white !important]
stats auth admin:admin #统计页面的用户名与密码
[backcolor=white !important]
stats admin if TRUE #启用或禁用状态页面
[backcolor=white !important]
frontend allen #定义前端服务器
[backcolor=white !important]
bind *:80
[backcolor=white !important]
mode http
[backcolor=white !important]
option httpclose #每次请求完成主动关闭http连接
[backcolor=white !important]
option forwardfor #后端服务器获取客户端的IP地址,可以从http header中获取
[backcolor=white !important]
acl url_static path_end -i .html .jpg .gif #定义ACL规则以如".html"结尾的文件;-i:忽略大小写
[backcolor=white !important]
acl url_dynamic path_end -i .php
[backcolor=white !important]
default_backend webservers #客户端访问时默认调用后端服务器地址池
[backcolor=white !important]
use_backend lamp if url_dynamic #调用后端服务器并检查ACL规则是否被匹配
[backcolor=white !important]
backend webservers #定义后端服务器
[backcolor=white !important]
balance roundrobin #定义算法;基于权重进行轮询
[backcolor=white !important]
server web1 172.16.14.3:80 check rise 2 fall 1 weight 2
[backcolor=white !important]
server web2 172.16.14.4:80 check rise 2 fall 1 weight 2
[backcolor=white !important]
backend lamp
[backcolor=white !important]
balance source #定义算法;源地址hash运算;类似于Nginx的ip_hash
[backcolor=white !important]
server lamp 172.16.14.5:80 check rise 2 fall 1
[backcolor=white !important]
----------------------------------------------------------------------
[backcolor=white !important]
#####注释:check:启动对后端server的健康状态检测;rise:离线的server转换到正常状态成功检查的次数;fall:确认server从正常状态转换为不可用状态需要检查的次数;weight:权重,数量越大,超重越高
[backcolor=white !important]
3、启动Haproxy服务并做访问测试
[backcolor=white !important]
1
[backcolor=white !important]
[iyunv@HA1 ~]# service haproxy start
[backcolor=white !important]
[backcolor=white !important]4、在HA2服务器上安装Haproxy;这里就不在介绍了,安装与配置方法与在HA1服务器上安装相同
[backcolor=white !important]五、安装配置Keepalived
[backcolor=white !important]1、在HA1与HA2服务器上安装Keepalived
[backcolor=white !important]
[iyunv@HA1 ~]# yum -y install keepalived
[backcolor=white !important]
[iyunv@HA2 ~]# yum -y install keepalived
[backcolor=white !important]
2、修改HA1服务器的主配置文件
[backcolor=white !important]
global_defs {[iyunv@HA1 ~]# vim /etc/keepalived/keepalived.conf
[backcolor=white !important]
! Configuration File for keepalived
[backcolor=white !important]
notification_email {
[backcolor=white !important]
root@localhost
[backcolor=white !important]
}
[backcolor=white !important]
notification_email_from admin@allen.com
[backcolor=white !important]
smtp_server 127.0.0.1
[backcolor=white !important]
smtp_connect_timeout 30
[backcolor=white !important]
router_id LVS_ALLEN
[backcolor=white !important]
}
[backcolor=white !important]
vrrp_script chk_proess {
[backcolor=white !important]
script "killall -0 haproxy"
[backcolor=white !important]
interval 1
[backcolor=white !important]
weight -2
[backcolor=white !important]
}
[backcolor=white !important]
vrrp_instance ha_1 {
[backcolor=white !important]
state MASTER
[backcolor=white !important]
interface eth0
[backcolor=white !important]
virtual_router_id 56
[backcolor=white !important]
priority 100
[backcolor=white !important]
advert_int 1
[backcolor=white !important]
authentication {
[backcolor=white !important]
auth_type PASS
[backcolor=white !important]
auth_pass 1056
[backcolor=white !important]
}
[backcolor=white !important]
virtual_ipaddress {
[backcolor=white !important]
172.16.14.10
[backcolor=white !important]
}
[backcolor=white !important]
track_script {
[backcolor=white !important]
chk_proess
[backcolor=white !important]
}
[backcolor=white !important]
}
[backcolor=white !important]
vrrp_instance ha_2 {
[backcolor=white !important]
state BACKUP
[backcolor=white !important]
interface eth0
[backcolor=white !important]
virtual_router_id 58
[backcolor=white !important]
priority 92
[backcolor=white !important]
advert_int 1
[backcolor=white !important]
authentication {
[backcolor=white !important]
auth_type PASS
[backcolor=white !important]
auth_pass 1058
[backcolor=white !important]
}
[backcolor=white !important]
virtual_ipaddress {
[backcolor=white !important]
172.16.14.11
[backcolor=white !important]
}
[backcolor=white !important]
}
[backcolor=white !important]
3、修改HA2服务器的主配置文件
[backcolor=white !important]
global_defs {[iyunv@HA2 ~]# vim /etc/keepalived/keepalived.conf
[backcolor=white !important]
! Configuration File for keepalived
[backcolor=white !important]
notification_email {
[backcolor=white !important]
root@localhost
[backcolor=white !important]
}
[backcolor=white !important]
notification_email_from admin@allen.com
[backcolor=white !important]
smtp_server 127.0.0.1
[backcolor=white !important]
smtp_connect_timeout 30
[backcolor=white !important]
router_id LVS_ALLEN
[backcolor=white !important]
}
[backcolor=white !important]
vrrp_script chk_proess {
[backcolor=white !important]
script "killall -0 haproxy"
[backcolor=white !important]
interval 1
[backcolor=white !important]
weight -2
[backcolor=white !important]
}
[backcolor=white !important]
vrrp_instance ha_1 {
[backcolor=white !important]
state BACKUP
[backcolor=white !important]
interface eth0
[backcolor=white !important]
virtual_router_id 56
[backcolor=white !important]
priority 99
[backcolor=white !important]
advert_int 1
[backcolor=white !important]
authentication {
[backcolor=white !important]
auth_type PASS
[backcolor=white !important]
auth_pass 1056
[backcolor=white !important]
}
[backcolor=white !important]
virtual_ipaddress {
[backcolor=white !important]
172.16.14.10
[backcolor=white !important]
}
[backcolor=white !important]
}
[backcolor=white !important]
vrrp_instance ha_2 {
[backcolor=white !important]
state MASTER
[backcolor=white !important]
interface eth0
[backcolor=white !important]
virtual_router_id 58
[backcolor=white !important]
priority 93
[backcolor=white !important]
advert_int 1
[backcolor=white !important]
authentication {
[backcolor=white !important]
auth_type PASS
[backcolor=white !important]
auth_pass 1058
[backcolor=white !important]
}
[backcolor=white !important]
virtual_ipaddress {
[backcolor=white !important]
172.16.14.11
[backcolor=white !important]
}
[backcolor=white !important]
track_script {
[backcolor=white !important]
chk_proess
[backcolor=white !important]
}
[backcolor=white !important]
}
[backcolor=white !important]
4、启动Keepalived服务并查看HA1与HA2服务器上的虚拟IP地址
[backcolor=white !important]
2: eth0:
mtu 1500 qdisc pfifo_fast state UP qlen 1000[backcolor=white !important]
[iyunv@HA1 ~]# service keepalived start
[backcolor=white !important]
[iyunv@HA1 ~]# ip addr show eth0
[backcolor=white !important]
link/ether 00:0c:29:2c:1a:24 brd ff:ff:ff:ff:ff:ff
[backcolor=white !important]
inet 172.16.14.1/16 brd 172.16.255.255 scope global eth0
[backcolor=white !important]
inet 172.16.14.10/32 scope global eth0
[backcolor=white !important]
inet6 fe80::20c:29ff:fe2c:1a24/64 scope link
[backcolor=white !important]
valid_lft forever preferred_lft forever
[backcolor=white !important]
------------------------------------------------------------------------
[backcolor=white !important]
[iyunv@HA2 ~]# service keepalived start
[backcolor=white !important]
[iyunv@HA2 ~]# ip addr show eth0
[backcolor=white !important]
2: eth0:
mtu 1500 qdisc pfifo_fast state UP qlen 1000
[backcolor=white !important]
link/ether 00:0c:29:ec:f6:3f brd ff:ff:ff:ff:ff:ff
[backcolor=white !important]
inet 172.16.14.2/16 brd 172.16.255.255 scope global eth0
[backcolor=white !important]
inet 172.16.14.11/32 scope global eth0
[backcolor=white !important]
inet6 fe80::20c:29ff:feec:f63f/64 scope link
[backcolor=white !important]
valid_lft forever preferred_lft forever
[backcolor=white !important]六、验证服务
[backcolor=white !important]1、访问验证服务器的负载均衡功能
[backcolor=white !important]
[backcolor=white !important]
[backcolor=white !important]2、验证是否实现动、静分离的效果
[backcolor=white !important]
[backcolor=white !important]3、假设前端服务器其中一台出现了故障,验证IP地址能否自动切换到备份服务器上
[backcolor=white !important]
######停止HA1服务器的haproxy服务[iyunv@HA1 ~]# service haproxy stop------------------------------------------------------------------------######查看HA2服务器的IP地址[iyunv@HA2 ~]# ip addr show eth02: eth0:
mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:ec:f6:3f brd ff:ff:ff:ff:ff:ff inet 172.16.14.2/16 brd 172.16.255.255 scope global eth0 inet 172.16.14.11/32 scope global eth0 inet 172.16.14.10/32 scope global eth0 inet6 fe80::20c:29ff:feec:f63f/64 scope link valid_lft forever preferred_lft forever注释:由上可见,虚拟IP地址已成功切换到HA2服务器
[backcolor=white !important]4、假如这里将服务器修复好重新上线,IP地址会切换回到原来的服务器,这里就不在验证;将HA2服务器上的haproxy或keepalived服务关闭亦是如此
[backcolor=white !important]到此Keepalived+Haproxy实现负载均衡及动静分离已完成;后续博客会更新其他相关内容,敬请关注...