一.系统优化

1、修改/etc/sysctl.conf,优化tcp连接数

net.ipv4.tcp_fin_timeout = 30

net.ipv4.tcp_keepalive_time = 1200

net.ipv4.tcp_syncookies = 1

net.ipv4.tcp_tw_reuse = 1

net.ipv4.tcp_tw_recycle = 1

net.ipv4.ip_local_port_range = 1024 65000

net.ipv4.tcp_max_syn_backlog = 8192

net.ipv4.tcp_max_tw_buckets = 5000

2、修改/etc/security/limits.conf,修改最大文件打开数为65535

添加如下两行:

*      soft  nofile       65535

*      hard nofile       65535

*      soft  nproc       16384

*      hard nproc       16384

二.安装JDK

chmod +x jdk-6u38-linux-x64.bin

# ./jdk-6u38-linux-x64.bin

#添加环境变量

# vi /etc/profile

export JAVA_HOME=/kjt/java/jdk1.6.0_38

export CLASSPATH=.:$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar

export PATH=$JAVA_HOME/bin:$PATH

#使环境变量生效

# source /etc/profile

#查看java版本

# java -version

java version "1.6.0_38"

Java(TM) SE Runtime Environment (build 1.6.0_27-b07)

Java HotSpot(TM) 64-Bit Server VM (build 20.2-b06, mixed mode)

三.安装Tomcat

1、确保防火墙已关闭

#service iptables stop

#chkconfig iptables off

#vi /etc/selinux/config

selinux=disabled

2、安装tomcat

# tar zxvf apache-tomcat-6.0.36.tar.gz

# cd apache-tomcat-6.0.36

#修改/bin/catalina.sh,在cygwin=false前加路径

export JAVA_HOME=/kjt/java/jdk1.6.0_38/

export PATH=$JAVA_HOME/bin:$PATH

export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar

export TOMCAT_HOME=/kjt/java/apache-tomcat-6.0.36

export CATALINA_HOME=/kjt/java/apache-tomcat-6.0.36

export CATALINA_BASE=/kjt/java/apache-tomcat-6.0.36

3、修改/conf/server.xml,做tomcat线程池优化

<Connector port="8081" protocol="HTTP/1.1"

connectionTimeout="15000" maxThreads="500" minSpareThreads="50" maxSpareTheads="250" maxProcessors="600"

acceptCount="1000" enableLookups="false" compression="on"

redirectPort="8443" URIEncoding="utf-8" useBodyEncodingForURI="true"/>

<!-- A "Connector" using the shared thread pool-->

4、启动tomcat

cd /kjt/java/apache-tomcat-6.0.35/bin

./startup

5、查看tomcat进程

ps –ef|grep tomcat

四.Nginx配置

1.确定Nginx所需pcre库、openssl库等依赖包已经安装,若没有安装,安装nginx过程中将报错。

2.安装Nginx

tar zxvf nginx-1.0.15.tar.gz

cd nginx-1.0.15/

./configure  --prefix=/kjt/app/nginx --with-http_stub_status_module --with-http_ssl_module

make

make install

3.Nginx配置文件如下:

4.测试配置文件

/kjt/app/nginx/sbin/nginx -t

显示以下信息为正确的

the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok

configuration file /usr/local/nginx/conf/nginx.conf test is successful

5、启动nginx服务

/kjt/app/nginx/sbin/nginx

6、查看nginx进程

ps -ef|grep nginx

五.安装配置Keepalived

1、安装keepalived

tar -zxvf keepalived-1.2.7.tar.gz

cd keepalived-1.2.7

./configure --prefix=/kjt/app/keepalived

make

make install

2、将keepalived设置为开机启动

cp /kjt/app/keepalived/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/

cp/kjt/app/keepalived/etc/sysconfig/keepalived /etc/sysconfig/

cp /kjt/app/keepalived/sbin/keepalived /usr/sbin/

chkconfig --add keepalived

chkconfig keepalived on

3.修改配置文件为以下内容:

mkdir /etc/keepalived

cp /kjt/app/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/

vim  /etc/keepalived/keepalived.conf

! Configuration File for keepalived

global_defs {

router_id Nginx_Id_1

}

vrrp_script Monitor_Nginx {

script "/kjt/app/keepalived/etc/keepalived/samples/monitor_nginx.sh
"  #脚本存放位置

interval 2     #检查间隔为2秒

weight 2

}

vrrp_instance VI_1 {

state
BACKUP  #设置nopreempt,表示之前挂掉的master重新恢复后不再切换为master,直接成为BACKUP,nopreempt只能配置在BACKUP状态下

nopreempt

interface eth0

virtual_router_id 51

priority 100    #优先级较高的成为MASTER

advert_int 5   #健康检查间隔为5秒

authentication {

auth_type PASS

auth_pass kjt2013

}

virtual_ipaddress {

10.46.86.60

}

track_script {

Monitor_Nginx

}

}

注:monitor_nginx.sh为监控nginx进程的脚本,内容如下:当检测到nginx主进程挂掉之后,系统将自动重启nginx,若nginx主进程重启失败,延迟5秒后杀掉keepalived进程,进行切换。

#!/bin/bash

if [ "$(ps -ef | grep "nginx:
master process"| grep -v grep )" == "" ]

then

/kjt/app/nginx/sbin/nginx

sleep 5

if [ "$(ps -ef | grep "nginx: master process"| grep -v
grep )" == "" ]

then

killall keepalived

fi

fi

5.启动keepalived

service keepalived start

注:备机的keepalived的安装和上面一样,只要把配置文件中的priority改为小于100的数即可

6、配置Keepalived监控脚本,每分钟执行一次,当检测到原先MASTER上的Keepalived进程挂掉之后,将重启Keepalived进程

vi  /root/shell/keepalived.sh

touch  /root/shell/keepalived.log

chmod 755 *

新增计划任务

crontab –e

* * * * * (sh /root/shell/keepalived.sh
>> /root/shell/keepalived.log)

六.测试步骤

1.访问VIP看是否能够正常访问后端的tomcat

2.停止其中一个tomcat看是否能将访问转到另一台上

3.停止两台nginx上任何一个nginx进程看监控进程脚本是否会自动启动nginx

VRRP_Script(Monitor_Nginx) timed out

VRRP_Script(Monitor_Nginx) succeeded

4.停止任何一台nginx上的keepalived进程看另一台是否接管vip

比如停止Master上的keepalived,例如如下killall keepalived,查看BACKUP机器是否已经接管,如果BACKUP接管后,BACKUP机器日志会是出下情况

tail 
-f  /var/log/messages

Keepalived_vrrp: VRRP_Instance(VI_1)
Transition to MASTER STATE

Keepalived_vrrp: VRRP_Instance(VI_1)
Entering MASTER STATE

Keepalived_vrrp: VRRP_Instance(VI_1)
setting protocol VIPs.

Keepalived_vrrp: VRRP_Instance(VI_1)
Sending gratuitous ARPs on eth0 for 10.46.86.60

MASTER机器上日志会显示

Keepalived_vrrp: Terminating VRRP child
process on signal

Keepalived_vrrp: VRRP_Instance(VI_1)
removing protocol VIPs.

现在把原先MASTER上的Keepalived重新启动,会看到原先MASTER直接成为BACKUP,不再做一次切换。

现在的BACKUP日志如下:

Keepalived_vrrp: VRRP_Instance(VI_1)
Received higher prio advert

Keepalived_vrrp: VRRP_Instance(VI_1)
Entering BACKUP STATE

Keepalived_vrrp: VRRP_Instance(VI_1)
removing protocol VIPs.

Master日志如下:

Keepalived_vrrp: VRRP_Script(Monitor_Nginx)
succeeded

Keepalived_vrrp: VRRP_Instance(VI_1)
Transition to MASTER STATE

Keepalived_vrrp: VRRP_Instance(VI_1)
Entering MASTER STATE

Keepalived_vrrp: VRRP_Instance(VI_1)
setting protocol VIPs.

Keepalived_vrrp: VRRP_Instance(VI_1)
Sending gratuitous ARPs on eth0 for 10.46.86.60

一.系统优化

1、修改/etc/sysctl.conf,优化tcp连接数

net.ipv4.tcp_fin_timeout = 30

net.ipv4.tcp_keepalive_time = 1200

net.ipv4.tcp_syncookies = 1

net.ipv4.tcp_tw_reuse = 1

net.ipv4.tcp_tw_recycle = 1

net.ipv4.ip_local_port_range = 1024 65000

net.ipv4.tcp_max_syn_backlog = 8192

net.ipv4.tcp_max_tw_buckets = 5000

2、修改/etc/security/limits.conf,修改最大文件打开数为65535

添加如下两行:

*      soft  nofile       65535

*      hard nofile       65535

*      soft  nproc       16384

*      hard nproc       16384

二.安装JDK

chmod +x jdk-6u38-linux-x64.bin

# ./jdk-6u38-linux-x64.bin

#添加环境变量

# vi /etc/profile

export JAVA_HOME=/kjt/java/jdk1.6.0_38

export
CLASSPATH=.:$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar

export PATH=$JAVA_HOME/bin:$PATH

#使环境变量生效

# source /etc/profile

#查看java版本

# java -version

java version "1.6.0_38"

Java(TM) SE Runtime Environment (build
1.6.0_27-b07)

Java HotSpot(TM) 64-Bit Server VM (build
20.2-b06, mixed mode)

三.安装Tomcat

1、确保防火墙已关闭

#service iptables stop

#chkconfig iptables off

#vi /etc/selinux/config

selinux=disabled

2、安装tomcat

# tar zxvf apache-tomcat-6.0.36.tar.gz

# cd apache-tomcat-6.0.36

#修改/bin/catalina.sh,在cygwin=false前加路径

export JAVA_HOME=/kjt/java/jdk1.6.0_38/

export PATH=$JAVA_HOME/bin:$PATH

export
CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar

export
TOMCAT_HOME=/kjt/java/apache-tomcat-6.0.36

export
CATALINA_HOME=/kjt/java/apache-tomcat-6.0.36

export
CATALINA_BASE=/kjt/java/apache-tomcat-6.0.36

3、修改/conf/server.xml,做tomcat线程池优化

<Connector port="8081" protocol="HTTP/1.1"

connectionTimeout="15000" maxThreads="500"
minSpareThreads="50" maxSpareTheads="250"
maxProcessors="600"

acceptCount="1000"
enableLookups="false" compression="on"

redirectPort="8443"
URIEncoding="utf-8" useBodyEncodingForURI="true"/>

<!-- A
"Connector" using the shared thread pool-->

4、启动tomcat

cd /kjt/java/apache-tomcat-6.0.35/bin

./startup

5、查看tomcat进程

ps –ef|grep tomcat

四.Nginx配置

1.确定Nginx所需pcre库、openssl库等依赖包已经安装,若没有安装,安装nginx过程中将报错。

2.安装Nginx

tar zxvf nginx-1.0.15.tar.gz

cd nginx-1.0.15/

./configure  --prefix=/kjt/app/nginx --with-http_stub_status_module
--with-http_ssl_module

make

make install

3.Nginx配置文件如下:

4.测试配置文件

/kjt/app/nginx/sbin/nginx -t

显示以下信息为正确的

the configuration file
/usr/local/nginx/conf/nginx.conf syntax is ok

configuration file
/usr/local/nginx/conf/nginx.conf test is successful

5、启动nginx服务

/kjt/app/nginx/sbin/nginx

6、查看nginx进程

ps -ef|grep nginx

五.安装配置Keepalived

1、安装keepalived

tar -zxvf keepalived-1.2.7.tar.gz

cd keepalived-1.2.7

./configure --prefix=/kjt/app/keepalived

make

make install

2、将keepalived设置为开机启动

cp /kjt/app/keepalived/etc/rc.d/init.d/keepalived
/etc/rc.d/init.d/

cp/kjt/app/keepalived/etc/sysconfig/keepalived
/etc/sysconfig/

cp /kjt/app/keepalived/sbin/keepalived
/usr/sbin/

chkconfig --add keepalived

chkconfig keepalived on

3.修改配置文件为以下内容:

mkdir /etc/keepalived

cp /kjt/app/keepalived/etc/keepalived/keepalived.conf
/etc/keepalived/

vim  /etc/keepalived/keepalived.conf

! Configuration File for keepalived

global_defs {

router_id Nginx_Id_1

}

vrrp_script Monitor_Nginx {

script "/kjt/app/keepalived/etc/keepalived/samples/monitor_nginx.sh
"  #脚本存放位置

interval 2     #检查间隔为2秒

weight 2

}

vrrp_instance VI_1 {

state
BACKUP  #设置nopreempt,表示之前挂掉的master重新恢复后不再切换为master,直接成为BACKUP,nopreempt只能配置在BACKUP状态下

nopreempt

interface eth0

virtual_router_id 51

priority 100    #优先级较高的成为MASTER

advert_int 5   #健康检查间隔为5秒

authentication {

auth_type PASS

auth_pass kjt2013

}

virtual_ipaddress {

10.46.86.60

}

track_script {

Monitor_Nginx

}

}

注:monitor_nginx.sh为监控nginx进程的脚本,内容如下:当检测到nginx主进程挂掉之后,系统将自动重启nginx,若nginx主进程重启失败,延迟5秒后杀掉keepalived进程,进行切换。

#!/bin/bash

if [ "$(ps -ef | grep "nginx:
master process"| grep -v grep )" == "" ]

then

/kjt/app/nginx/sbin/nginx

sleep 5

if [ "$(ps -ef | grep "nginx: master process"| grep -v
grep )" == "" ]

then

killall keepalived

fi

fi

5.启动keepalived

service keepalived start

注:备机的keepalived的安装和上面一样,只要把配置文件中的priority改为小于100的数即可

6、配置Keepalived监控脚本,每分钟执行一次,当检测到原先MASTER上的Keepalived进程挂掉之后,将重启Keepalived进程

vi  /root/shell/keepalived.sh

touch  /root/shell/keepalived.log

chmod 755 *

新增计划任务

crontab –e

* * * * * (sh /root/shell/keepalived.sh
>> /root/shell/keepalived.log)

六.测试步骤

1.访问VIP看是否能够正常访问后端的tomcat

2.停止其中一个tomcat看是否能将访问转到另一台上

3.停止两台nginx上任何一个nginx进程看监控进程脚本是否会自动启动nginx

VRRP_Script(Monitor_Nginx) timed out

VRRP_Script(Monitor_Nginx) succeeded

4.停止任何一台nginx上的keepalived进程看另一台是否接管vip

比如停止Master上的keepalived,例如如下killall keepalived,查看BACKUP机器是否已经接管,如果BACKUP接管后,BACKUP机器日志会是出下情况

tail 
-f  /var/log/messages

Keepalived_vrrp: VRRP_Instance(VI_1)
Transition to MASTER STATE

Keepalived_vrrp: VRRP_Instance(VI_1)
Entering MASTER STATE

Keepalived_vrrp: VRRP_Instance(VI_1)
setting protocol VIPs.

Keepalived_vrrp: VRRP_Instance(VI_1)
Sending gratuitous ARPs on eth0 for 10.46.86.60

MASTER机器上日志会显示

Keepalived_vrrp: Terminating VRRP child
process on signal

Keepalived_vrrp: VRRP_Instance(VI_1)
removing protocol VIPs.

现在把原先MASTER上的Keepalived重新启动,会看到原先MASTER直接成为BACKUP,不再做一次切换。

现在的BACKUP日志如下:

Keepalived_vrrp: VRRP_Instance(VI_1)
Received higher prio advert

Keepalived_vrrp: VRRP_Instance(VI_1)
Entering BACKUP STATE

Keepalived_vrrp: VRRP_Instance(VI_1)
removing protocol VIPs.

Master日志如下:

Keepalived_vrrp: VRRP_Script(Monitor_Nginx)
succeeded

Keepalived_vrrp: VRRP_Instance(VI_1)
Transition to MASTER STATE

Keepalived_vrrp: VRRP_Instance(VI_1)
Entering MASTER STATE

Keepalived_vrrp: VRRP_Instance(VI_1)
setting protocol VIPs.

Keepalived_vrrp: VRRP_Instance(VI_1)
Sending gratuitous ARPs on eth0 for 10.46.86.60

nginx配置负载的更多相关文章

  1. 通过http、https域名访问静态网页、nginx配置负载均衡(nginx配置)

    很多场景下需要可以通过浏览器访问静态网页,不想把服务器ip地址直接暴露出来,通过nginx可以解决这个问题. 实现http域名访问静态网页 1.域名解析配置(本文都是以阿里云为例,其他平台,操作步骤类 ...

  2. 解决nginx配置负载均衡时invalid host in upstream报错

    当前平台: windows nginx版本: 1.11.5 前言: 在配置负载均衡时,同时也需要设置反向代理,当修改了nginx.conf时,发现nginx服务无法开启. 1. 打开"ngi ...

  3. nginx配置 负载均衡

    配置nginx #配置虚拟主机 server { listen 80; server_name www.testaaa.com; location / { #root /usr/local/nginx ...

  4. NginX——配置负载均衡

    A.            在http模块加上upstream配置 upstream www.myweb.com { server  127.0.0.1:9100 weight=3; server  ...

  5. 图解nginx配置负载均衡

    1. 在Linux上准备两份tomcat 2. 修改两份tomcat的端口号 修改的端口如图所示: 3. 启动两个tomcat服务器 4. 修改两个服务器上的主页方便测试区分 5. 在nginx配置文 ...

  6. nginx集群:nginx配置负载均衡集群(nginx1.18.0)

    一,nginx的负载均衡集群的特点: 1,nginx集群和lvs的不同? lvs集群:工作在第4层(传输层) nginx集群:工作在第7层(应用层) lvs集群:性能更强 nginx集群:功能更强:可 ...

  7. nginx配置负载均衡与反向代理

    #给文件夹授权   1 chown -R www:www /usr/local/nginx #修改配置文件vim nginx.conf   1 2 3 4 5 6 7 8 9 10 11 12 13 ...

  8. nginx配置负载均衡

    本教程不讲解nginx的安装,若安装请看博客 http://www.cnblogs.com/hqjy/p/8092983.html 本教程不讲解tomcat的安装,若安装请看博客 http://www ...

  9. Nginx 配置负载均衡

    nginx负载均衡配置,主要是proxy_pass,upstream的使用. 注意问题,多台机器间session的共享问题. 不用session,用户cookie.或者用redis替代session. ...

随机推荐

  1. SQL 分组去重

    select * from (select p.province_name, p.province_code, c.city_name, c.city_code, c.city_id, ROW_NUM ...

  2. bootstrap-dropdown

    功能:实现点击时下拉框显示 插件:dropdown.js 要点:dropdown功能往往用在导航栏.导航条上,用作标题显示.dropdown与<ul><li>标签搭配用. 以d ...

  3. 元素间距属性(scrollLeft,scrollWidth,clientWidth,offsetWidth,padding,margin)

    scrollHeight: 获取对象的滚动高度.scrollLeft:设置或获取位于对象左边界和窗口中目前可见内容的最左端之间的距离scrollTop:设置或获取位于对象最顶端和窗口中可见内容的最顶端 ...

  4. DAY2 Python 标准库 -> Getpass 模块 -> 命令行下输入密码的方法.

    getpass 模块 getpass 模块提供了平台无关的在命令行下输入密码的方法. getpass(prompt) 会显示提示字符串, 关闭键盘的屏幕反馈, 然后读取密码. 如果提示参数省略, 那么 ...

  5. 用jQuery编的一个分页小代码

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  6. [HDU2089]不要62

    [HDU2089]不要62 试题描述 杭州人称那些傻乎乎粘嗒嗒的人为62(音:laoer).杭州交通管理局经常会扩充一些的士车牌照,新近出来一个好消息,以后上牌照,不再含有不吉利的数字了,这样一来,就 ...

  7. Linux下ffmpeg的各种编解码器的安装

    首先要安装各种解码器 1.lame  tar -zxvf lame- cd lame- ./configure --enable-shared make make install 2.libogg  ...

  8. BZOJ 4579: [Usaco2016 Open]Closing the Farm

    Description 依次删去一个点和它的边,问当前图是否连通. Sol 并查集. 倒着做就可以了. 每次将一个点及其的边加入,如果当前集合个数大于 1,那么就不连通. Code /******** ...

  9. SNMP简单网络管理协议(转载)

    SNMP SNMP 网络管理的历史 美国国防部设计了世界上头几个包交换网之一的ARPANET,在70年代,TCP/IP协议族正式被定为军方通信标准,随着此协议的广泛使用,网络管理成了一件大事.在80年 ...

  10. putty如何使用

    使用putty连接管理centos 1 双击putty.exe2 和linux命令行一样了 使用psftp上传和下载 cd d:/psftppsftp open 10.0.0.9 输入用户密码root ...