第一臺 tar vxf apache-tomcat-7.0.54.tar.gz mv apache-tomcat-7.0.54 /usr/local/tomcat tar vxf jdk-7u65-linux-x64.tar.gz mv jdk1.7.0_65/ /usr/local/java 編輯 ...
- 準備好tomcat
第一臺
tar vxf apache-tomcat-7.0.54.tar.gz
mv apache-tomcat-7.0.54 /usr/local/tomcat
tar vxf jdk-7u65-linux-x64.tar.gz
mv jdk1.7.0_65/ /usr/local/java
編輯vim /etc/profile加
export JAVA_HOME=/usr/local/java #設置java根目錄
export PATH=$PATH:$JAVA_HOME/bin #在PATH環境變數中添加java跟目錄的bin子目錄
啟動tomcat服務
/usr/local/tomcat/bin/startup.sh
打開瀏覽器,ip:8080顯示tomcat主頁
關閉服務;
/usr/local/tomcat/bin/shutdown.sh
建立一個web 站點
cd webapps/
mkdir web
vim index.jsp
Server Info:
SessionID:<%=session.getId()%>
<br>
SessionIP:<%=request.getServerName()%>
<br>
SessionPort:<%=request.getServerPort()%>
<br>
<%
out.println("server one");
%>
修改Tomcat的server.xml文件
cp server.xml server.xml.bak
vim server.xml
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true">
<Context docBase="web" path="" reloadable="flase">
</Context>
</Host>
/usr/local/tomcat/bin/shutdown.sh
/usr/local/tomcat/bin/startup.sh
打開瀏覽器,ip:8080顯示tomcat主頁
第二台
與第一臺基本一致,更改測試頁的ip地址
scp [email protected]:/usr/local/tomcat/webapps/web/index.jsp .
scp [email protected]:/usr/local/tomcat/conf/server.xml .
啟動服務/usr/local/tomcat/bin/startup.sh
查看服務
netstat -anpult |grep java
瀏覽器訪問測試 http://ip:8080
顯示正常
2.準備nginx
第一臺
源碼安裝nginx,
修改配置文件,配置負載均衡
cp /usr/local/nginx/conf/nginx.conf{,.bak}
vim /usr/local/nginx/conf/nginx.conf
加upstream tomcat_server {
server 172.16.1.12:8080 weight=1;
server 172.16.1.2:8080 weight=1;
}
proxy_pass http://tomcat_server;
語法檢查
/usr/local/nginx/sbin/nginx –t
啟動服務/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
或者killadd -1 nginx
打開瀏覽器訪問: http://ip ,該ip為nginx 的ip,刷新瀏覽器,會顯示兩台
第二台
nginx-2伺服器配置:
配置方式與伺服器nginx-1一致
客戶端測試:
打開瀏覽器訪問: http://ip #不斷刷新可看到由於權重相同,頁面會反覆切換
3.安裝配置keepalived
yum -y install keepalived
修改配置文件vim keepalived.conf
配置一個實例名字為VI_2,狀態作為主,優先順序100
vrrp_instance VI_2 {
#備用伺服器上為 BACKUP
state MASTER
#綁定vip的網卡為ens33
interface ens33
virtual_router_id 51
#備用伺服器上為90
nopreempt #為不搶占模式
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
#track_script {
#nginx
#}
virtual_ipaddress { #配置虛擬ip地址
172.16.1.200
}
}
vrrp_instance VI_1 { #配置另一個實例 VI_1,狀態為 從,優先順序為90
state BACKUP
interface ens33
virtual_router_id 51
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress { #配置虛擬ip地址為254
192.168.200.254
}
}
配置第二台
Vim keepalive.conf
vrrp_instance VI_2 { #實例名字為VI_2,狀態作為從,優先順序90
#備用伺服器上為 BACKUP
state SLAVE
#綁定vip的網卡為ens33
interface ens33
virtual_router_id 51
#備用伺服器上為90
nopreempt
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.16.1.200 #配置虛擬ip地址
}
}
vrrp_instance VI_1 { #配置另一個實例 VI_1,狀態為主,優先順序為100
state MASTER
interface ens33
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.200.254
}
}
測試,關閉一臺keepalived,打開瀏覽器,刷新頁面,兩台機器網頁交替更新,測試正常
實現高可用。
4.安裝memcached,實現session會話保持。
yum -y install gcc openssl-devel pcre-devel zlib-devel
先安tar xf libevent-2.0.15-stable.tar.gz
cd libevent-2.0.15-stable
./configure --prefix=/usr/local/libevent && make && make install
再裝memcached
tar xf memcached-1.4.5.tar.gz
cd memcached-1.4.5
./configure --prefix=/usr/local/memcached --with-libevent=/usr/local/libevent/ && make && make install
配置
/usr/local/memcached/bin/memcached -u root -m 512M -n 10 -f 2 -d -vvv -c 512
vim /etc/ld.so.conf
include ld.so.conf.d/*.conf
/usr/local/libevent/lib/
Ldconfig
/usr/local/memcached/bin/memcached -u root -m 512M -n 10 -f 2 -d -vvv -c 512
netstat -antp| grep :11211
顯示埠開啟
安裝telnet測試yum -y install telnet
telnet 192.168.200.103 11211
顯示正常
最後執行讓Tomcat-1 Tomcat-2 通過(msm)連接到Memcached
將session包中的“*.jar複製到/usr/local/tomcat/lib/ 下麵
cp session/* /usr/local/tomcat/lib/使tomcat支持memcached,
tomcat-1 和 tomcat-2 配置文件一模一樣.
vim /usr/local/tomcat7/conf/context.xml
重啟服務tomcat
/usr/local/tomcat/bin/shutdown.sh
/usr/local/tomcat/bin/startup.sh
如果成功,tomcat與Memcached 埠會連在一起,前後有變化
Tomcat-1與Tomcat-2如下圖
netstat -antp|grep java
tcp 0 0 ::ffff:127.0.0.1:8005 :::* LISTEN 62496/java
tcp 0 0 :::8009 :::* LISTEN 62496/java
tcp 0 0 :::8080 :::* LISTEN 62496/java
tcp 0 0 ::ffff:192.168.200.10:28232 ::ffff:192.168.200.10:11211 ESTABLISHED 62496/java
tcp 0 0 ::ffff:192.168.200.10:28231 ::ffff:192.168.200.10:11211 ESTABLISHED 62496/java
tcp 0 0 ::ffff:192.168.200.10:28230 ::ffff:192.168.200.10:11211 ESTABLISHED 62496/java
tcp 0 0 ::ffff:192.168.200.10:28228 ::ffff:192.168.200.10:11211 ESTABLISHED 62496/java
tcp 0 0 ::ffff:192.168.200.10:28229 ::ffff:192.168.200.10:11211 ESTABLISHED 62496/java
[root@tomcat-1 ~]# netstat -antp|grep memcached
tcp 0 0 0.0.0.0:11211 0.0.0.0:* LISTEN 62402/memcached
tcp 0 0 192.168.200.103:11211 192.168.200.103:28230 ESTABLISHED 62402/memcached
tcp 45 0 192.168.200.103:11211 192.168.200.103:28228 ESTABLISHED 62402/memcached
tcp 0 0 192.168.200.103:11211 192.168.200.103:28232 ESTABLISHED 62402/memcached
tcp 0 0 192.168.200.103:11211 192.168.200.103:28229 ESTABLISHED 62402/memcached
tcp 0 0 192.168.200.103:11211 192.168.200.103:28231 ESTABLISHED 62402/memcached
tcp 0 0 :::11211 :::* LISTEN 62402/memcached