1. 程式人生 > >keepalived+httpd+tomcat實現高可用負載均衡

keepalived+httpd+tomcat實現高可用負載均衡

一、環境
centos 6.5
keepalived keepalived-1.2.19.tar.gz
httpd httpd-2.4.12.tar.gz
tomcat apache-tomcat-7.0.63.tar.gz
二、部署
安裝 httpd

tar -zxvf httpd-2.4.12.tar.gz
cd httpd-2.4.12
./configure --prefix=/usr/local/apache2 --enable-mods-share=all
這裡會報錯提示:
configure: error: APR not found.  Please read the documentation.
解決辦法如下:
到Apache官網下載缺少的軟體包,下載地址http://apr.apache.org/download.cgi
下載 apr-1.5.2.tar.gz apr-util-1.5.4.tar.gz 安裝缺少的依賴的軟體包 tar -zxvf apr-1.5.2.tar.gz cd apr-1.5.2 ./configure --prefix=/usr/local/apr 這裡會提示沒有gcc 提示內容如下 checking for chosen layout... apr checking for gcc... no checking for cc... no checking for cl.exe... no configure: error: in `/opt/httpd/apr-1.5.2': configure: error: no acceptable C compiler found in $PATH See `config.log'
for more details 需要新增gcc支援 yum -y install gcc yum -y install gcc-c++ 完成之後我們接著安裝apr ./configure --prefix=/usr/local/apr make && make install tar -zxvf apr-util-1.5.4.tar.gz cd apr-util-1.5.4 ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr/bin/apr-1-config make && make install 然後接著配置httpd ./configure --prefix=/usr/local/apache2 --enable-mods-share=all 在這個後面新增 -with
-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util 合起來之後是 ./configure --prefix=/usr/local/apache2 --enable-mods-share=all -with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util 好無語到了這裡竟敢有提示缺少依賴包!!! 提示內容如下: configure: error: pcre-config for libpcre not found. PCRE is required and available from http://pcre.org 沒辦法我們接著新增依賴,wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.37.tar.gz tar -zxvf pcre-8.37.tar.gz cd pcre-8.37 ./configure --prefix=/usr/local/pcre make && make install 然後,我們接著裝httpd ./configure --prefix=/usr/local/apache2 --enable-mods-share=all -with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --with-pcre=/usr/local/pcre/bin/pcre-config 終於完成配置了! make && make install

配置httpd

vim /usr/local/apache2/conf/httpd.conf
在其中新增
# Load mod_jk module
LoadModule jk_module modules/mod_jk.so
# Specify jk log file.
JkLogFile /log/httpd/mod_jk.log
JkShmFile /var/log/httpd/mod_jk.shm
# Specify jk log level [debug/error/info]
JkLogLevel info
# Specify workers.properties, this file tell jk:
# how many nodes and where they are.
JkWorkersFile /opt/tomcat/conf/workers.properties
# Specify which requests should handled by which node.
JkMount /* controller

並且放開
# Server-pool management (MPM specific)
Include conf/extra/httpd-mpm.conf
這裡是配置httpd併發數,他有三種工作模式
Apache Prefork、Worker和Event!詳細可參閱其他的文章
或者是參考官方文件(http://httpd.apache.org/docs/2.4/mod/mpm_common.html)
這裡特別提醒如果想支援併發數超過400的需要新增
ServerLimit 256這個屬性,這個支援最大併發數6400
如果還需要更大則需要從新編譯httpd!

在啟動httpd的時候發現沒有成功可以到httpd的日誌中檢視原因!!!

安裝Apache Tomcat Connector(mod_jk)
tar -zxvf tomcat-connectors-1.2.40-src.tar.gz
cd tomcat-connectors-1.2.40-src/native/
./buildconf.sh
這裡會提示缺少autoconf
autoconf not found.
You need autoconf version 2.59 or newer installed to build mod_jk from SVN.
yum -y install autoconf

然後,我們繼續./buildconf.sh
靠,還讓不讓人活啊,又缺少libtool
libtool not found.
You need libtool version 1.4 or newer installed to build mod_jk from SVN.
yum -y install libtool

接著./buildconf.sh
./configure –with-apxs=/usr/local/apache2/bin/apxs
make && make install
如果成功結束,你可以在/usr/local/apache2/modules/下找到mod_jk.so檔案。
安裝tomcat
需要修改配置檔案server.xml,

在後新增如下內容

<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"
                channelSendOptions="8">

                <Manager className="org.apache.catalina.ha.session.DeltaManager"
                    expireSessionsOnShutdown="false" notifyListenersOnReplication="true" />

                <Channel className="org.apache.catalina.tribes.group.GroupChannel">
                    <Membership className="org.apache.catalina.tribes.membership.McastService"
                        address="228.0.0.4" port="45564" frequency="500" dropTime="3000" />
                    <Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"
                        address="auto" port="4000" autoBind="100" selectorTimeout="5000"
                        maxThreads="6" />

                    <Sender
                        className="org.apache.catalina.tribes.transport.ReplicationTransmitter">
                        <Transport
                            className="org.apache.catalina.tribes.transport.nio.PooledParallelSender" />
                    </Sender>
                    <Interceptor
                        className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector" />
                    <Interceptor
                        className="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor" />
                </Channel>

                <Valve className="org.apache.catalina.ha.tcp.ReplicationValve"
                    filter="" />
                <Valve className="org.apache.catalina.ha.session.JvmRouteBinderValve" />

                <Deployer className="org.apache.catalina.ha.deploy.FarmWarDeployer"
                    tempDir="/tmp/war-temp/" deployDir="/tmp/war-deploy/" watchDir="/tmp/war-listen/"
                    watchEnabled="false" />

                <ClusterListener
                    className="org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener" />
                <ClusterListener
                    className="org.apache.catalina.ha.session.ClusterSessionListener" />
            </Cluster>