项目笔记:1.nginx的七层的负载均衡集群

时间:2021-11-07 05:52:12

项目笔记:1.nginx的七层的负载均衡集群

项目笔记:1.nginx的七层的负载均衡集群

1、在后端网站节点192.168.0.2部署网站

2、将部署好的网站同步至192.168.0.3

3、安装和使用的七层负载均衡器(nginx)

4、测试七层负载均衡器

注意ip的分配不要冲突。


1、在后端网站节点192.168.0.2部署网站
1)下载、安装、启动软件

# tar xf jdk-7u15-linux-x64.tar.gz -C /opt 
# tar xf apache-tomcat-7.0.37.tar.gz -C /opt
# cp tomcat-init.sh /etc/rc.d/init.d/tomcat --复制启动脚本到启动的目录init.d
# chmod +x !$ -增加可执行权限
# cd /opt
# mv apache-tomcat-7.0.37/ tomcat
# mv jdk1.7.0_15/ jdk
# cd /opt/tomcat/bin/
# tar xf commons-daemon-native.tar.gz
# cd commons-daemon-1.0.13-native-src/unix/
# ./configure --with-java=/opt/jdk && make
# cp jsvc /opt/tomcat/bin/ --安装jsvc以服务的方式启动Java程序
# service tomcat start
# chkconfig tomcat on

2)配置bbs1.kong.com  bbs2.kong.com两个网站

# mkdir /var/log/tomcat
# mkdir /var/www/html/bbs{1,2}.kong.com -p
# cd /root/
# unzip ejforum-2.3.zip
# unzip JavaCenter_Home_2.0_Source_UTF8.zip
# mv JavaCenter_Home_2.0_Source_UTF8/source/Var/www/html/* /var/www/html/bbs1.kong.com/
# mv ejforum-2.3/ejforum/* /var/www/html/bbs2.kong.com/
# service tomcat restart

修改tomcat配置文件

#vim /opt/tomcat/conf/server.xml 
<?xml version='1.0' encoding='utf-8'?>
<Server port="8005" shutdown="SHUTDOWN">
<Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
<Listener className="org.apache.catalina.core.JasperListener" />
<Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
<Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
<Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />
<GlobalNamingResources>
<Resource name="UserDatabase" auth="Container" type="org.apache.catalina.UserDatabase" description="User database that can be updated and saved" factory="org.apache.catalina.users.MemoryUserDatabaseFactory" pathname="conf/tomcat-users.xml" />
</GlobalNamingResources>
<Service name="Catalina">
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
<span style="color:#ff0000;"><Engine name="Catalina" defaultHost="bbs1.kong.com"> --覆盖默认页面</span>
<Realm className="org.apache.catalina.realm.LockOutRealm">
<Realm className="org.apache.catalina.realm.UserDatabaseRealm"
resourceName="UserDatabase"/>
</Realm>
<span style="color:#ff0000;"><Host name="bbs1.kong.com" appBase="webapps"
unpackWARs="true" autoDeploy="true">
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="/var/log/tomcat"
prefix="bbs1.kong.com_access" suffix=".log"
pattern="%h %l %u %t "%r" %s %b" />
<Context path="" docBase="/var/www/html/bbs1.kong.com" />
</Host></span>

<span style="color:#ff0000;"><Host name="bbs2.kong.com" appBase="webapps"
unpackWARs="true" autoDeploy="true">
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="/var/log/tomcat"
prefix="bbs2.kong.com_access" suffix=".log"
pattern="%h %l %u %t "%r" %s %b" />
<Context path="" docBase="/var/www/html/bbs2.kong.com" /></span>
</Host>
</Engine>
</Service>
</Server>


3)在192.168.0.254上安装数据库,为两个网站创建用户和数据库


# yum -y install mysql-server
# service mysqld start
# chkconfig mysqld on
mysql> create database bbs1 default charset utf8;
Query OK, 1 row affected (0.00 sec)

mysql> create database bbs2 default charset utf8;
Query OK, 1 row affected (0.00 sec)

mysql> grant all on bbs1.* to runbbs1@'192.168.0.0/255.255.255.0' identified by '123';
Query OK, 0 rows affected (0.00 sec)

mysql> grant all on bbs2.* to runbbs2@'192.168.0.0/255.255.255.0' identified by '123';
Query OK, 0 rows affected (0.00 sec)


修改/etc/hosts文件,把bbs1和bbs2的网站地址专向192.168.0.2!

bbs1可以通过页面自的安装向导

访问http://bbs1.kong.com:8080/install 填入在0.254上面的的runbbs1的用户名,密码和表名称bbs1

bbs2需要手工配置连接至数据库

# yum -y install mysql 
# mysql -urunbbs2 -p123 -h192.168.0.254 bbs2 < /root/ejforum-2.3/install/script/easyjforum_mysql.sql
# vim /var/www/html/bbs2.kong.com/WEB-INF/conf/config.xml
<?xml version="1.0" encoding="UTF-8"?>
<config>
<database maxActive="10" maxIdle="10" minIdle="2" maxWait="10000"
username="runbbs2" password="123"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:<span style="color:#ff0000;">mysql://192.168.0.254:3306/bbs2</span>?characterEncoding=gbk&autoReconnect=true&autoReconnectForPools=true&zeroDateTimeBehavior=convertToNull"
sqlAdapter="sql.MysqlAdapter"/>
<system adminUser="admin"/>
<misc>
<maxMemberPages>20</maxMemberPages>
<maxSessionPosts>10</maxSessionPosts>
<maxFavorites>50</maxFavorites>
<maxShortMsgs>50</maxShortMsgs>
<maxAvatarPixels>150</maxAvatarPixels>
</misc>
</config>
# service tomcat restart
访问即可 http://bbs2.kong.com:8080/


4)安装nginx实现页面动静分离


# vim /etc/nginx/nginx.conf
user nobody nobody;
worker_processes 4;
error_log /var/log/nginx/error.log info;
pid /var/run/nginx.pid;


events {
worker_connections 15000;
}


http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
keepalive_timeout 30;
gzip on;
upstream tomcat_upstream { --动态的调给tomcat
server 127.0.0.1:8080;
}
server {
listen 80;
server_name bbs1.kong.com;
charset utf8;
access_log /var/log/nginx/bbs1.kong.com.access.log main;

location / {
root /var/www/html/bbs1.kong.com;
index index.html index.htm index.jsp;
}

location ~ \.jsp$ {
proxy_pass http://tomcat_upstream;
proxy_set_header Host $host;
proxy_set_header X-Forward-For $remote_addr;
}
}

server {
listen 80;
server_name bbs2.kong.com;
charset utf8;
access_log /var/log/nginx/bbs2.kong.com.access.log main;

location / {
root /var/www/html/bbs2.kong.com;
index index.html index.htm index.jsp;
}

location ~ \.jsp$ {
proxy_pass http://tomcat_upstream;
proxy_set_header Host $host;
proxy_set_header X-Forward-For $remote_addr;
}

location ~ ^/forum-[0-9]-[0-9]-[0-9]\.html$ {
proxy_pass http://tomcat_upstream;
proxy_set_header Host $host;
proxy_set_header X-Forward-For $remote_addr;
}
}
}
# mkdir /var/log/nginx
# ulimit -HSn 65535
# vim /etc/security/limits.conf
root - nproc 65535
root - nofile 65535
# service nginx start
# chkconfig nginx on

5) 将部署好的网站在192.168.0.2同步至192.168.0.3


1。先在192.168.0.3上安装nginx tomcat mysql


2。再从192.168.0.2上同步至192.168.0.3
# rsync -avzR /opt/ /etc/rc.d/init.d/nginx /etc/rc.d/init.d/tomcat /var/www/html/* /etc/nginx/nginx.conf root@192.168.0.3:/

3。在192.168.0.3上启动nginx和tomcat
# service tomcat start
# service nginx start
# chkconfig tomcat on
# chkconfig nginx on

也可以在0.3再做一次项目笔记:1.nginx的七层的负载均衡集群


6)在后端节点上置两个不同的页面

测试回话的session

192.168.0.2
# vim /var/www/html/bbs1.kong.com/test.jsp
<html>
<body bgcolor="red">
<center>
<%out.print(request.getSession().getId()) ;%>
<h1>Tomcat1</h1>
</body>
</html>

192.168.0.3
# vim /var/www/html/bbs1.kong.com/test.jsp
<html>
<body bgcolor="blue">
<center>
<%out.print(request.getSession().getId()) ;%>
<h1>Tomcat2</h1>
</body>
</html>


10)在192.168.0.1安装和使用的七层负载均衡器(nginx)

修改其配置文件

# vim /etc/nginx/conf/nginx.conf
user nobody nobody;
worker_processes 4;
error_log /var/log/nginx/error.log info;
pid /var/run/nginx.pid;


events {
worker_connections 15000;
}


http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
keepalive_timeout 30;
gzip on;
upstream nginx_upstream {
server 192.168.0.2:80;
server 192.168.0.3:80;
}

upstream tomcat_upstream {
server 192.168.0.2:8080 weight=10;
server 192.168.0.3:8080 weight=10;
}


server {
listen 80;
server_name localhost;
charset utf8;

location / {
index index.html index.htm index.jsp;
proxy_pass http://nginx_upstream;
proxy_set_header Host $host;
proxy_set_header X-Forward-For $remote_addr;
}

location ~ \.jsp$ {
proxy_pass http://tomcat_upstream;
proxy_set_header Host $host;
proxy_set_header X-Forward-For $remote_addr;
}
location ~ ^/forum-[0-9]-[0-9]-[0-9]\.html$ {
proxy_pass http://tomcat_upstream;
proxy_set_header Host $host;
proxy_set_header X-Forward-For $remote_addr;
}


}
}

11)测试七层负载均衡器

修改/etc/hosts 文件把bbs1,bbs2转向0.1!
1)关掉后端nginx
# firefox http://bbs1.kong.com/index.jsp & --结果看不到静态元素


2)关掉是后端的tomcat
# firefox http://bbs1.kong.com/template/default/image/guest_bg.jpg & --可以看到图片

网站的样子

项目笔记:1.nginx的七层的负载均衡集群