WDCP环境下的Apache负载均衡及keepalived配置
测试环境:
创新互联专注为客户提供全方位的互联网综合服务,包含不限于成都网站设计、网站制作、兴海网络推广、微信平台小程序开发、兴海网络营销、兴海企业策划、兴海品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们大的嘉奖;创新互联为所有大学生创业者提供兴海建站搭建服务,24小时服务热线:18980820575,官方网址:www.cdcxhl.comserverA为web主站点,IP地址为192.168.1.201,网卡eth0
server B为web副站点,IP地址为192.168.1.202,网卡eth0
web站点底层环境为wdcp管理平台(linux+apache+mysql+php),具体安装方法见:51cto.com/wdcp/install.html,使用源码方式安装
任务目标:使用apache模块功能实现两台web服务器的负载均衡,使用keepalived实现两台服务器的双机热备
部署过程:
一.负载均衡
为apache安装支持负载均衡的模块
cd/root/lanmp/httpd-2.2.24/modules/proxy/ #进入源码目录,
/www/wdlinux/httpd-2.2.24/bin/apxs-i -c -a mod_proxy.c proxy_util.c #编译安装
/www/wdlinux/httpd-2.2.24/bin/apxs-i -c -a mod_proxy_http.c
/www/wdlinux/httpd-2.2.24/bin/apxs-i -c -a mod_proxy_ftp.c
/www/wdlinux/httpd-2.2.24/bin/apxs-i -c -a mod_proxy_connect.c
/www/wdlinux/httpd-2.2.24/bin/apxs-i -c -a mod_proxy_balancer.c
模块安装完成后会在主配置文件中看到以下信息:
vi/www/wdlinux/httpd-2.2.24/conf/httpd.conf
模块安装完成后,登录到web管理平台编辑站点配置文件
站点列表—文件管理
Apache配置文件—选择站点配置文件
在配置文件中添加配置信息
保存后重启httpd服务即可
命令解释:
ProxyRequests Off //使用反向代理,on是正向代理
Proxypass / balancer://proxy/ //“/”代表发送Web请求的URL前缀
Order Deny,Allow
Allow from all //这两行是访问控制
BalancerMember http://192.168.1.8:80
BalancerMember http://192.168.1.9:80status=+H //主服务器及辅助服务器,“+H”表示开启热备份功能
ProxySet lbmethod=bytraffic //根据流量分配主机资源
更详细的配置参照http://zhumeng8337797.blog.163.com/blog/static/100768914201242211633248/
二.Keepalived配置
keepalived官方地址:http://www.keepalived.org/download.html,可以到这里下载最新版本的keepalived
1.环境配置:安装make 和gcc openssl openssl-devel等等
yum -y install gcc make openssl openssl-devel wgetkernel-devel
mkdir -p /usr/local/src/hasoft
cd /usr/local/src/hasoft
wget http://www.keepalived.org/software/keepalived-1.2.2.tar.gz
tar -zxvf keepalived-1.2.2.tar.gz
cd keepalived-1.2.2
./configure --prefix=/usr/local/keepalived--with-kernel-dir=/usr/src/kernels/2.6.18-238.19.1.el5-i686/
这里的软件版本及系统内核版本要根据自己的服务器进行修改
预编译后出现:
Keepalived configuration
------------------------
Keepalived version : 1.2.2
Compiler : gcc
Compiler flags : -g -O2 -DETHERTYPE_IPV6=0x86dd
Extra Lib : -lpopt -lssl -lcrypto
Use IPVS Framework : Yes
IPVS sync daemon support : Yes
IPVS use libnl : No
Use VRRP Framework : Yes
Use Debug flags : No
2.编译安装
make && make install
复制代码
这里用的是通用安装方法,如果没有用到LVS可以把lvs去掉即
./configure --prefix=/usr/local/keepalived--with-kernel-dir=/usr/src/kernels/2.6.18-238.19.1.el5-i686/--disable-lvs-syncd --disable-lvs
3.整理管理文件:
cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
cp /usr/local/keepalived/etc/sysconfig/keepalived/etc/sysconfig/
cp /usr/local/keepalived/etc/rc.d/init.d/keepalived/etc/init.d/
4.建立配置文件目录
mkdir -p /etc/ keepalived/
两台服务器(两个节点)都这样安装即可
5.编辑配置文件
节点A配置如下:
vi /etc/keepalived/keepalived.conf
global_defs
{
notification_email
{
admin@example.com
}
notification_email_from admin@example.com
smtp_server 127.0.0.1
stmp_connect_timeout 30
router_id lnmp_node1
}
vrrp_instance lnmp {
state MASTER //这里住服务器配制成MASTER,备机配制成BACKUP
interface eth0 //绑定虚拟IP的网卡
virtual_router_id 100 //两台服务器的virtual_router_id要完全一致
priority 200 //数值越大,优先级越高
advert_int 5 //vrrp信息检测时间间隔,这里是5秒
track_interface { //监听的网卡
eth0
}
authentication { //主备服务器的验证方式,密码要完全一致
auth_type PASS
auth_pass 123456
}
virtual_ipaddress { //使用的虚拟IP,默认绑定在主服务器上,如果主服务器宕机,则漂移到备服务器上
192.168.1.10
}
}
节点B配置如下:
vi /etc/keepalived/keepalived.conf
global_defs
{
notification_email
{
admin@example.com
}
notification_email_from admin@example.com
smtp_server 127.0.0.1
stmp_connect_timeout 30
router_id lnmp_node1
}
vrrp_instance lnmp {
state BACKUP
interface eth0
virtual_router_id 100
priority 150
advert_int 5
track_interface {
eth0
}
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
192.168.1.10
}
}
6.添加开机启动项
chkconfig --add keepalived
chkconfig --level 35 keepalived on
7.添加防火墙策略
vi /etc/sysconfig/iptables
添加一行:
-A INPUT –s 192.168.1.0/24 -p vrrp -j ACCEPT
8.启动测试
在节点A上启动
/usr/local/keepalived/sbin/keepalived
启动日志:
cat /var/log/messages
Sep 818:26:02 centosa Keepalived_vrrp: Registering Kernel netlink reflector
Sep 818:26:02 centosa Keepalived_vrrp: Registering Kernel netlink command channel
Sep 818:26:02 centosa Keepalived_vrrp: Registering gratutious ARP shared channel
Sep 818:26:02 centosa Keepalived_vrrp: Opening file\'/etc/keepalived/keepalived.conf\'.
Sep 818:26:02 centosa Keepalived_vrrp: Configuration is using : 36076 Bytes
Sep 818:26:02 centosa Keepalived_vrrp: Using LinkWatch kernel netlink reflector...
Sep 818:26:02 centosa Keepalived: Starting VRRP child process, pid=5606
Sep 818:26:07 centosa Keepalived_vrrp: VRRP_Instance(lnmp)Transition to MASTER STATE
Sep 818:26:12 centosa Keepalived_vrrp: VRRP_Instance(lnmp)Entering MASTER STATE
Sep 818:26:12 centosa avahi-daemon[2528]: Registering new address record for192.168.1.10 on eth0.
在节点B上启动
/usr/local/keepalived/sbin/keepalived
查看启动日志:
cat /var/log/messages
Sep 818:30:02 centosb Keepalived_vrrp: Opening file\'/etc/keepalived/keepalived.conf\'.
Sep 818:30:02 centosb Keepalived_vrrp: Configuration is using : 36252 Bytes
Sep 818:30:02 centosb Keepalived_vrrp: Using LinkWatch kernel netlink reflector...
Sep 818:30:02 centosb Keepalived_healthcheckers: Configuration is using : 6271 Bytes
Sep 818:30:02 centosb Keepalived_healthcheckers: Using LinkWatch kernel netlinkreflector...
Sep 818:30:02 centosb Keepalived_vrrp: VRRP_Instance(lnmp)Entering BACKUP STATE
从日志可以看出,启动都没有问题,并且安装我给的优先级完成了竞选,各自成就了各自的状态
关闭节点A的网卡测试切换是否正常
ifdown eth0
观察节点B的日志:
Sep 818:32:55 centosb Keepalived_vrrp: VRRP_Instance(lnmp) Transition to MASTERSTATE
Sep 818:33:00 centosb Keepalived_vrrp: VRRP_Instance(lnmp)Entering MASTER STATE
Sep 818:33:00 centosb avahi-daemon[2531]: Registering new address record for192.168.1.10 on eth0.
启动节点A的网卡测试切换是否正常
ifup eth0
观察节点B的日志:
Sep 818:33:31 centosb Keepalived_vrrp: VRRP_Instance(lnmp) Received higher prioadvert
Sep 818:33:31 centosb Keepalived_vrrp: VRRP_Instance(lnmp) EnteringBACKUP STATE
Sep 818:33:31 centosb avahi-daemon[2531]: Withdrawing address record for 192.168.1.10on eth0.
Received higher prio advert:表示接收到更高优先级的公告(advert公告的意思)
Withdrawing:撤回的意思,可以看出切换过程一目了然
9.使用脚本监测服务
下面使用脚本监控apache、php及mysql三个服务,若有一个出现问题便立即停止keepalived服务,以切换到另一台服务器
mkdir /root/shell/
cd /root/shell
vi keepcheck.sh
#!/bin/bash
while:
do
mysqlcheck=`/www/wdlinux/mysql-5.1.63/bin/mysqladmin-pwdlinux.cn -uroot ping 2>&1`
mysqlcode=`echo $?`
phpcheck=`ps aux | grep -c php-fpm | wc -l`
apachecheck80=`lsof -i:80|wc -l`
apachecheck8080=`lsof -i:8080|wc -l`
keepalivedcheck=`ps -C keepalived --no-header| wc -l`
if [$apachecheck80 -eq 0 ]||[ $apachecheck8080 -eq 0 ]||[ $phpcheck -eq 0 ]||[$mysqlcode -ne 0 ];then
if [ $keepalivedcheck -ne 0];then
killall -TERM keepalived
else
echo "keepalived isstoped"
fi
else
if [ $keepalivedcheck -eq 0];then
/etc/init.d/keepalived start
else
echo "keepalived isrunning"
fi
fi
sleep5
done
注意,用/etc/init.d/keepalived start如果起不来,可以用/usr/local/keepalived/sbin/keepalived二进制文件直接执行启动即可
启动脚本:
chmod +x /root/shell/keepcheck.sh
nohup sh /root/shell/keepcheck.sh &
节点B也用这个脚本
写入/etc/rc.local开机自动启动
vi /etc/rc.local
nohup sh /root/shell/keepcheck.sh &
至此全部配置已经完成,将域名或nat指向到创建的虚拟ip即可使用服务
分享文章:WDCP环境下的Apache负载均衡及keepalived配置
本文网址:http://pwwzsj.com/article/chsjdi.html