LVS群集 nginx+tomcat
项目拓扑图:
一.安装nginx + tomcat
192.168.1.248和192.168.1.249服务器上如下配置:
1. JDK的安装
将下载好的安装程序放到/soft目录下
cd /soft
chmod a+x jdk-6u23-linux-i586.bin
./jdk-6u23-linux-i586.bin
mkdir -p /data/conf
mv jdk1.6.0_23/ /data/conf/jdk
删除系统中旧的版本
rm -rf /usr/bin/java
rm -rf /usr/bin/javac
创建链接,设置新的版本
ln -s /data/conf/jdk/bin/java /usr/bin/java
ln -s /data/conf/jdk/bin/javac /usr/bin/javac
查看新的版本信息
java -version
javac -version
查看jdk版本是否是1.6.到此JDK已经安装完成
2. Tomcat安装
解压压缩文件
cd /soft
tar xzvf apache-tomcat-6.0.32.tar.gz
mv apache-tomcat-6.0.32 /data/conf/tomcat
cd /data/conf/tomcat/bin/
添加用户
useradd webuser -s /sbin/nologin
chown -R webuser:webuser /data/
tomcat优化选项
(1)添加tomcat管理员
# vi /usr/local/www/tomcat/conf/tomcat-users.xml
在<tomcat-users> </tomcat-users>中间添加
<role rolename="manager"/>
<user username="tomcat" password="li147258369" roles="manager"/>
其中username="tomcat"为用户名password="li147258369"为密码
(2)修改JVM
JAVA_OPTS="-Xms1024m -Xmx1024m -Xmn256m -Djava.awt.headless=true"
(3)server.xml 参数修改
<Connector port="8080" protocol="HTTP/1.1"
maxHttpHeaderSize="8192" useBodyEncodingForURI="true"
maxThreads="600" 最大连接数
redirectPort="8443"
enableLookups="false" 禁用DNS查询
compression="on"
compressionMinSize="2048" 压缩,压缩大小
compressableMimeType="text/html,text/xml,text/javascript,text/css,text/plain"
connectionTimeout="20000"
disableUploadTimeout="true"
/>
设置环境目录
vi /etc/profile
TOMCAT_HOME=/data/conf/tomcat
JAVA_HOME=/data/conf/jdk
JRE_HOME=/data/conf/jdk/jre
export JAVA_HOME JRE_HOME TOMCAT_HOME
备份tomcat配置文件
cd /data/conf/tomcat/conf
mv server.xml server.xml.bak
设置tomcat配置文件,配置虚拟主机
vi server.xml
在</Host>后面添加
<Host name="www.benet.com" unpackWARs="true" autoDeploy="true" appBase="webapps">
<Context path="" docBase="/data/web/www.benet.com" />
</Host>
<Host name="www.accp.com" unpackWARs="true" autoDeploy="true" appBase="webapps">
<Context path="" docBase="/data/web/www.accp.com" />
</Host>
<Host name="www.apache.com" unpackWARs="true" autoDeploy="true" appBase="webapps">
<Context path="" docBase="/data/web/www.apache.com" />
</Host>
启动tomcat
/data/conf/tomcat/bin/startup.sh
查看启动进程
ps -ef |grep tomcat
访问tomcat测试页,出现猫头网页,则安装成功!
links http://localhost:8080
3. 安装nginx
解压压缩文件
tar zxvf pcre-8.02.tar.gz
cd pcre-8.02/
编译安装
./configure
make && make install
tar zxvf nginx-0.8.50.tar.gz
cd nginx-0.8.50/
编译安装
./configure --prefix=/data/conf/nginx --with-http_stub_status_module
make && make install
配置nginx配置文件
cd /data/conf/nginx
备份nginx.conf配置文件
mv nginx.conf nginx.conf.bak
vi nginx.conf
user nobody nobody;
worker_processes 4;
pid /data/conf/nginx/logs/nginx.pid;
worker_rlimit_nofile 51200;
events
{
use epoll;
worker_connections 51200;
}
http{
include mime.types;
default_type application/octet-stream;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 8m;
sendfile on;
tcp_nopush on;
keepalive_timeout 60;
tcp_nodelay on;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
upstream www
{
server 192.168.1.248:8080;
server 192.168.1.249:8080;
}
server {
listen 80;
server_name www.benet.com;
location / {
root /data/web/www.benet.com ;
index index.jsp index.htm index.html;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://www;
}
access_log /data/logs/benet.com/www.benet.access.log;
error_log /data/logs/benet.com/error-www.benet.com;
}
server {
listen 80;
server_name www.accp.com;
location / {
root /var/www ;
index index.jsp index.htm index.html;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://www;
}
access_log /data/logs/accp.com/www.accp.access.log;
error_log /data/logs/accp.com/error-www.accp.com;
}
server {
listen 80;
server_name www.apache.com;
location / {
root /data/web/www.apache.com ;
index index.jsp index.htm index.html;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://www;
}
access_log /data/logs/apache.com/www.apache.access.log;
error_log /data/logs/apache.com/error-www.apache.com;
}
}
检测配置文件是否存在错误
./nginx -t
启动nginx
创建项目目录
mkdir /data/web/www.benet.com
mkdir /data/web/www.accp.com
mkdir /data/web/www.apache.com
mkdir -p /data/logs/benet.com
mkdir -p /data/logs/accp.com
mkdir -p /data/logs/apache.com
建立测试网页
echo "This is benet home page !!!" > /data/web/www.benet.com/index.html
echo "This is accp home page !!! " > /data/web/www.accp.com/index.html
echo "This is apache home page !!!" > /data/web/www.apache.com/index.html
添加hosts记录
echo "192.168.1.248 www.benet.com" >> /etc/hosts
echo "192.168.1.248 www.accp.com" >> /etc/hosts
echo "192.168.1.248 www.apache.com" >> /etc/hosts
浏览测试网页,出现相应的网页,则配置完成。
links http://www.benet.com
links http://www.accp.com
links http://www.apache.com
2011年04月07日修改完成
4. sersync 同步配置,使得网站项目文件一致。
在192.168.1.248服务器上安装rsync
tar xzvf rsync-3.0.7.tar.gz
cd rsync-3.0.7
./configure --prefix=/data/conf/rsync
make && make install
rpm -qa |grep rsync
rpm -qf /usr/bin/rsync
rpm -e rsync-2.6.8-3.1
ln -s /data/conf/rsync/bin/rsync /usr/bin/rsync
查看rsync版本
rsync -version
配置rsync配置文件
vi rsyncd.conf
port = 873
max connections = 4
pid file = /data/conf/rsync/rsync.pid
lock file = /data/conf/rsync/rsync.lock
log file = /data/conf/rsync/rsync.log
[dataweb]
path = /data/web
auth users = aa
secrets file = /data/conf/rsync/conf/rsync.passwd
uid = webuser
gid = webuser
read only = no
创建认证用户名和密码文件
vi /data/conf/rsync/conf/rsync.passwd
aa:123456
启动rsync守护进程
rsync --daemon
配置sersync配置文件
vi confxml.xml
修改为<localpath watch="/data/web">
<remote ip="192.168.1.248" name="dataweb"/>
<!--<remote ip="192.168.8.39" name="tongbu"/>-->
<!--<remote ip="192.168.8.40" name="tongbu"/>-->
</localpath>
<rsync>
<commonParams params="-artuz"/>
<auth start="true" users="aa" passwordfile="/data/conf/rsync/conf/rsync.passwd"/>
<userDefinedPort start="false" port="873"/><!-- port=874 -->
<timeout start="true" time="100"/><!-- timeout=100 -->
<ssh start="false"/>
开启sersync守护进程
./sersync2 -d
测试文件是否同步
在192.168.1.249服务器上/data/web/目录下创建些文件
在192.168.1.248服务器上/data/web/目录下看是否同步有相应的文件
LVS群集配置
1.客户端 就是真实服务器
在192.168.1.248和249服务器环回接口上绑定 虚拟ip
vi /data/conf/realserver
#!/bin/bash
#description : start realserver
VIP=192.168.1.100
/etc/rc.d/init.d/functions
case "$1" in
start)
echo " start LVS of REALServer"
/sbin/ifconfig lo:0 $VIP broadcast $VIP netmask 255.255.255.255 up
echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore
echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce
echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore
echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce
;;
stop)
/sbin/ifconfig lo:0 down
echo "close LVS Directorserver"
echo "0" >/proc/sys/net/ipv4/conf/lo/arp_ignore
echo "0" >/proc/sys/net/ipv4/conf/lo/arp_announce
echo "0" >/proc/sys/net/ipv4/conf/all/arp_ignore
echo "0" >/proc/sys/net/ipv4/conf/all/arp_announce
;;
*)
echo "Usage: $0 {start|stop}"
exit 1
esac
开启虚拟IP
cd /data/conf
./realserve start
查看虚拟IP
ip add 或者 ifconfig
192.168.1.246 lvs主机------------192.168.1.247 lvs备份机
1. 在1.246和247上安装 ipvsadm
yum -y install ipvsadm
2. 192.168.1.246 lvs主机安装keepalived
安装keepalived
cd /soft
tar xzvf keepalived-1.1.19_.tar.gz
./configure --prefix=/data/conf/keepalived
make
make install
创建keepalived目录
mkdir -p /etc/keepalived
创建keepalived.conf配置文件
! Configuration File for keepalived
global_defs {
notification_email {
[email protected]
}
notification_email_from [email protected]
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
# 20081013 written by :netseek
# VIP1
vrrp_instance VI_1 {
state MASTER #备份服务器上将MASTER改为BACKUP
interface eth0
virtual_router_id 51
priority 100 # 备份服务上将100改为99
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.100 #(如果有多个VIP,继续换行填写.)
}
}
virtual_server 192.168.1.100 80 {
delay_loop 6 #(每隔10秒查询realserver状态)
lb_algo wrr #(lvs 算法)
lb_kind DR #(Direct Route)
persistence_timeout 60 #(同一IP的连接60秒内被分配到同一台realserver)
inhibit_on_failure #当web挂掉的时候,前面请求的用户,可以继续打开页面,但是后面的请求不会调度到挂掉的web上面。
protocol TCP #(用TCP协议检查realserver状态)
real_server 192.168.1.248 80 {
weight 3 #(权重)
TCP_CHECK {
connect_timeout 10 #(10秒无响应超时)
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.1.249 80 {
weight 1
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
}
}
}
启动keepalived
cd /data/conf/keepalived/sbin
./keepalived -D
查看keepalived进程,是否有3个进程
ps -ef |grep keepalived
查看ipvsadm
ipvsadm -L -c
LVS群集测试:
设置hosts文件
192.168.1.100 www.benet.com
192.168.1.100 www.accp.com
192.168.1.100 www.apache.com
访问测试网页
http://www.benet.com
关闭LVS master主机,看是否还能访问测试网页不,如果能访问,则LVS配置成功了,若不行,则进行相应的排错啦.