1 Apache负载均衡
0、关闭防火墙(service iptables stop; chkconfig --level 35 iptables off),
关闭selinux vi /etc/selinux/config SELINUX=disabled
setenforce 0 生效
1、将mod_jk-1.2.31-httpd-2.2.x.so上传至/etc/httpd/modules目录下
2、
vi /etc/httpd/conf/httpd.conf
在Include conf.d/*.conf下
增加
Include /etc/httpd/conf/mod_jk.conf
配置
ServerName=192.168.1.X:80
3、
vi /etc/httpd/conf/mod_jk.conf
增加
LoadModule jk_module /etc/httpd/modules/mod_jk.so
JkWorkersFile /etc/httpd/conf/workers.properties
JkLogFile /etc/httpd/logs/mod_jk.log
JkLogLevel warn
JkLogStampFormat "[%a %b %d %H:%M:%S %Y]"
JkMount /*.* controller
4、
vi /etc/httpd/conf/workers.properties
增加
worker.list=controller
worker.tomcat1.port=8009
worker.tomcat1.host=192.168.1.152
worker.tomcat1.type=ajp13
worker.tomcat1.lbfactor=1
worker.tomcat2.port=8009
worker.tomcat2.host=192.168.1.155
worker.tomcat2.type=ajp13
worker.tomcat2.lbfactor=1
worker.tomcat3.port=8009
worker.tomcat3.host=192.168.1.156
worker.tomcat3.type=ajp13
worker.tomcat3.lbfactor=1
worker.controller.type=lb
worker.retries=3
worker.controller.balance_workers=tomcat1,tomcat2,tomcat3
worker.controller.sticky_session=false
2 Tomcat session复制
1、对于每一个server.xml
<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat2">
2、在Engine标签内增加
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
3、在程序的web.xml的web-app结束前增加 <distributable/>
4、session复制需用组播,添加static route
vi /etc/sysconfig/network-scripts/route-eth0
增加
192.0.2.0/24 dev eth0 scope host
default via 192.0.2.1
192.168.2.0/24 via 192.168.13.1 dev eth0
service network restart
reboot 有效
3 HA(高可用性)keepalived配置
1、apache安装在105和106上
2、两台apache配置保持一致,除了httpd.conf的SERVERNAME配置的ip地址
3、两台机器分别安装keepalived
yum -y install keepalived
4、配置keepalived(105)
vi /etc/keepalived/keepalived.conf
========================================================
! Configuration File for keepalived
global_defs {
notification_email {
}
notification_email_from [email protected]
smtp_server 192.168.1.0
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script chk_httpd {
script "killall -0 httpd"
interval 1
weight -2
}
vrrp_instance httpd_1 {
state MASTER
interface eth0
virtual_router_id 58
priority 100 #priority
advert_int 1
authentication {
auth_type PASS
auth_pass 1058
}
virtual_ipaddress {
192.168.1.101 #vip
}
track_script {
chk_httpd
}
}
=========================================================
service keepalived start #启动keepalived服务
chkconfig keepalived on
5、配置keepalived(106)
vi /etc/keepalived/keepalived.conf
=========================================================
! Configuration File for keepalived
global_defs {
notification_email {
}
notification_email_from [email protected]
smtp_server 192.168.1.0
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script chk_httpd {
script "killall -0 httpd"
interval 1
weight -2
}
vrrp_instance httpd_1 {
state BACKUP
interface eth0
virtual_router_id 58
priority 99 #priority
advert_int 1
authentication {
auth_type PASS
auth_pass 1058
}
virtual_ipaddress {
192.168.1.101 #vip
}
track_script {
chk_httpd
}
}
========================================================
访问http://192.168.1.101
<!--EndFragment-->