设为首页 收藏本站
查看: 834|回复: 0

[经验分享] apache+tomcat 实现负载均衡集群

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2016-7-19 09:56:10 | 显示全部楼层 |阅读模式
环境介绍:
192.168.101.58apache代理服务器apache,apr,apr-util,tomcat-connector
192.168.101.62tomcat-A服务器tomcat,jdk

192.168.101.63tomcat-B服务器tomcat多实例,ajp_port:8019
192.168.101.63tomcat-默认页面服务器tomcat多实例,ajp_port:8029

apache支持两种代理协议
    1.http

    2.ajp


tomcat及jdk的安装略过。
编译安装apache的httpd
1、编译安装apr
1
2
3
4
5
[iyunv@localhost src]# wget
[iyunv@localhost src]# tar -zxf apr-1.5.2.tar.gz
[iyunv@localhost src]# cd apr-1.5.2
[iyunv@localhost src]# ./configure --prefix=/usr/local/apr
[iyunv@localhost src]# make && make install



2、编译安装apr-util
1
2
3
4
5
[iyunv@localhost src]# wget http://apache.fayea.com/apr/apr-util-1.5.4.tar.gz
[iyunv@localhost src]# tar -zxvf apr-util-1.5.4.tar.gz
[iyunv@localhost src]# cd apr-util-1.5.4
[iyunv@localhost src]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr/
[iyunv@localhost src]# make && make install



3、编译安装apache
3.1、安装
1
2
3
4
5
[iyunv@localhost src]# wget
[iyunv@localhost src]# tar -zxvf httpd-2.4.20.tar.gz
[iyunv@localhost src]# cd httpd-2.4.20
[iyunv@localhost src]#  ./configure --prefix=/usr/local/http --sysconfdir=/etc/httpd --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --enable-mpms-shared=all --with-mpm=event --enable-proxy --enable-proxy-http --enable-proxu-ajp --enable-proxy-balancer --enable-lbmethod-heartbeat --enable-heartbeat  --enable-slotmem-shm --enable-slotmem-plain --enable-watchdog
[iyunv@localhost src]# make && make install



3.2、编写启动脚本
1
[iyunv@localhost src]# vim /etc/init.d/httpd



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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/bin/bash
#
# httpd        Startup script for the Apache HTTP Server
#
# chkconfig: - 85 15
# description: Apache is a World Wide Web server.  It is used to serve \
#          HTML files and CGI.
# processname: httpd
# config: /etc/httpd/conf/httpd.conf
# config: /etc/sysconfig/httpd
# pidfile: /var/run/httpd.pid

# Source function library.
. /etc/rc.d/init.d/functions

if [ -f /etc/sysconfig/httpd ]; then
        . /etc/sysconfig/httpd
fi

# Start httpd in the C locale by default.
HTTPD_LANG=${HTTPD_LANG-"C"}

# This will prevent initlog from swallowing up a pass-phrase prompt if
# mod_ssl needs a pass-phrase from the user.
INITLOG_ARGS=""

# Set HTTPD=/usr/sbin/httpd.worker in /etc/sysconfig/httpd to use a server
# with the thread-based "worker" MPM; BE WARNED that some modules may not
# work correctly with a thread-based MPM; notably PHP will refuse to start.

# Path to the apachectl script, server binary, and short-form for messages.
apachectl=/usr/local/http/bin/apachectl
httpd=${HTTPD-/usr/local/http/bin/httpd}
prog=httpd
pidfile=${PIDFILE-/var/run/httpd.pid}
lockfile=${LOCKFILE-/var/lock/subsys/httpd}
RETVAL=0

start() {
        echo -n $"Starting $prog: "
        LANG=$HTTPD_LANG daemon --pidfile=${pidfile} $httpd $OPTIONS
        RETVAL=$?
        echo
        [ $RETVAL = 0 ] && touch ${lockfile}
        return $RETVAL
}

stop() {
    echo -n $"Stopping $prog: "
    killproc -p ${pidfile} -d 10 $httpd
    RETVAL=$?
    echo
    [ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}
}
reload() {
    echo -n $"Reloading $prog: "
    if ! LANG=$HTTPD_LANG $httpd $OPTIONS -t >&/dev/null; then
        RETVAL=$?
        echo $"not reloading due to configuration syntax error"
        failure $"not reloading $httpd due to configuration syntax error"
    else
        killproc -p ${pidfile} $httpd -HUP
        RETVAL=$?
    fi
    echo
}

# See how we were called.
case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  status)
        status -p ${pidfile} $httpd
    RETVAL=$?
    ;;
  restart)
    stop
    start
    ;;
  condrestart)
    if [ -f ${pidfile} ] ; then
        stop
        start
    fi
    ;;
  reload)
        reload
    ;;
  graceful|help|configtest|fullstatus)
    $apachectl $@
    RETVAL=$?
    ;;
  *)
    echo $"Usage: $prog {start|stop|restart|condrestart|reload|status|fullstatus|graceful|help|configtest}"
    exit 1
esac

exit $RETVAL



3.3、启动脚本赋予权限
1
2
[iyunv@localhost src]# chmod +x /etc/init/d/httpd
[iyunv@localhost src]# chkconfig --add httpd



3.4、启动apache(故障排查)
    3.4.1、查看httpd进程

1
2
3
[iyunv@localhost src]# service httpd start
[iyunv@localhost src]# ps -ef |grep httpd
root     14445 14189  0 15:21 pts/0    00:00:00 grep httpd



    3.4.2、查看端口状态

1
2
[iyunv@localhost src]# netstat -tlnp |grep 80
tcp        0      0 :::80            :::*          LISTEN      14300/httpd[iyunv@localhost src]#



    3.4.3、查看错误日志

1
2
3
4
5
6
[iyunv@localhost src]# cat /usr/local/http/logs/error_log
[Mon Jul 18 11:49:53.889462 2016] [proxy_balancer:emerg] [pid 14087:tid 140187546703616] AH01177: Failed to lookup provider 'shm' for 'slotmem': is mod_slotmem_shm loaded??
[Mon Jul 18 11:49:53.889750 2016] [:emerg] [pid 14087:tid 140187546703616] AH00020: Configuration Failed, exiting
[Mon Jul 18 11:50:01.041040 2016] [proxy_balancer:emerg] [pid 14127:tid 140379317155584] AH01177: Failed to lookup provider 'shm' for 'slotmem': is mod_slotmem_shm loaded??
[Mon Jul 18 11:50:01.041275 2016] [:emerg] [pid 14127:tid 140379317155584] AH00020: Configuration Failed, exiting
[Mon Jul 18 11:50:02.193534 2016] [proxy_balancer:emerg] [pid 14139:tid 140297643288320] AH01177: Failed to lookup provider 'shm' for 'slotmem': is mod_slotmem_shm loaded??



好像是slotmem模块没有加载,编辑httpd配置文件启用slotmem模块
1
2
3
[iyunv@localhost src]# vim /etc/httpd/httpd.conf
LoadModule slotmem_shm_module modules/mod_slotmem_shm.so
LoadModule slotmem_plain_module modules/mod_slotmem_plain.so




3.4.4、重新启动测试
1
2
3
4
5
6
7
8
9
10
11
[iyunv@localhost src]# service httpd start
[iyunv@localhost src]# ps -ef |grep httpd
root     14300     1  0 12:52 ?        00:00:00 /usr/local/http/bin/httpd
daemon   14302 14300  0 12:52 ?        00:00:01 /usr/local/http/bin/httpd
daemon   14303 14300  0 12:52 ?        00:00:01 /usr/local/http/bin/httpd
daemon   14304 14300  0 12:52 ?        00:00:01 /usr/local/http/bin/httpd
root     14443 14428  0 15:17 pts/1    00:00:00 vim /etc/init.d/httpd
root     14451 14189  0 15:28 pts/0    00:00:00 grep httpd

[iyunv@localhost src]# netstat -tlnp |grep 80
tcp        0      0 :::80          :::*            LISTEN      14300/httpd




4、编译apache的jk模块
1
2
3
4
5
[iyunv@localhost src]# wget
[iyunv@localhost src]# tar -zxvf tomcat-connectors-1.2.41-src.tar.gz
[iyunv@localhost src]# cd tomcat-connectors-1.2.41-src/native/
[iyunv@localhost native]# ./configure --with-apxs=/usr/local/http/bin/apxs
[iyunv@localhost native]# make && make install




5、配置(在httpd.conf中virtual host中添加jk的配置文件)
1
2
[iyunv@localhost native]# vim /etc/httpd/httpd.conf
include /etc/httpd/extra/httpd-jk.conf



5.1编辑httpd-jk.conf
1
2
3
4
5
6
[iyunv@localhost native]# vim /etc/httpd/extra/httpd-jk.conf
LoadModule jk_module modules/mod_jk.so
JkWorkersFile /etc/httpd/extra/workers.properties
JkLogFile logs/mod_jk.log
JkLogLevel error
JkMount /* controller



说明:
    LoadModule:表示装载模块

    JkWorkersFile:定义workers属性的配置文件

    JkLogFile:       定义work日志的路径

    JkMount:        将/下的所有请求都交给controller集群处理,controller必须要在workers.properties中定义,否则报错   
5.2、编辑workers.properties配置文件,增加如下内容
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
worker.list=controller                #定义控制器

#========tomcat1========
worker.tomcat1.port=8009               #tomcat的ajp端口
worker.tomcat1.host=192.168.101.62     #tomcat的ip
worker.tomcat1.type=ajp13              #ajp协议版本,目前1.3
worker.tomcat1.lbfactor=1              #权重

#========tomcat2========
worker.tomcat2.port=8019
worker.tomcat2.host=192.168.101.63
worker.tomcat2.type=ajp13
worker.tomcat2.lbfactor=1

#========tomcat3========
worker.tomcat3.port=8029
worker.tomcat3.host=192.168.101.63
worker.tomcat3.type=ajp13
worker.tomcat3.lbfactor=1

#========controller负载平衡控制器========
worker.controller.type=lb                          #指定controller类型
worker.controller.balanced_workers=tomcat1,tomcat2,tomcat3 #指定负载平衡的tomcat
worker.controller.sticky_session=true              #指定是否粘性session
worker.controller.sticky_session_force=false
worker.connection_pool_size=3000
worker.connection_pool_minsize=50
worker.connection_pool_timeout=50000
# session配置说明:  
#当sticky_session,sticky_session_force都为true时不复制session,  
#sticky_session_force=false指集群中某台服务器多次请求没有响应,则转发到其它服务器处理,  
#sticky_session=false不使用粘性session,同时配置不复制session时,注意转发请求后可能会找不到原来的session.




到此,配置完成。下面访问http://192.168.101.58测试。


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-246158-1-1.html 上篇帖子: httpd-2.2和httpd-2.4基于virtualhost构建安全的http服务 下篇帖子: apache-kylin完整安装流程 代理服务器
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表