软件版本:
pcre8.36 ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.36.tar.gz
keepalived1.2.19
http://www.keepalived.org/software/keepalived-1.2.19.tar.gz
nginx1.8.0
http://nginx.org/download/nginx-1.8.0.tar.gz<pre name="code" class="html"><pre name="code" class="html">安装步骤
安装pcre
tar -zxvf pcre-8.36.tar.gz
cd pcre-8.36
./configure --prefix=/usr/local/pcre
make && make install
安装keepalived
tar -zxvf keepalived-1.2.19.tar.gz
cd keepalived-1.2.19
./configure --prefix=/usr/local/keepalived
make && make install
cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
mkdir –pv /etc/keepalived
cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
ln -s /usr/local/keepalived/sbin/keepalived /sbin/
chkconfig keepalived on
安装nginx
tar -zxvf nginx-1.8.0.tar.gz
cd nginx-1.8.0
./configure --prefix=/usr/local/nginx --with-pcre=/usr/local/pcre/
make && make install
<strong><span style="font-size:18px;">启动和停止</span></strong>
Nginx:
启动:进入到安装之后${nginx}的sbin目录,执行./nginx
停止:./nginx –s stop
检查是否安装成功:进入到安装之后${nginx}的sbin目录,执行./nginx -t
Keepalived
启动:service keepalived start
停止:service keepalived stop
<strong><span style="font-size:18px;">配置</span></strong>
配置Keepalived
主备keepalived的配置大致相同,不同之处在于state和priority。如下所示:
! Configuration File for keepalived
#配置报警邮件
global_defs {
notification_email {
acassen@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
#配置keepalived服务器实例
vrrp_instance VI_1 {#VI_1为名称
state MASTER #MASTER为主服务器,BACKUP为备用服务器
interface p2p1 #p2p1为网卡标志
virtual_router_id 51 #51为默认值
priority 100 #主服务器的优先级要大于备服务器
advert_int 1 #1为默认值
authentication {#认证,采用默认值即可
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {#对外提供的虚拟IP,不能与现有IP冲突
200.31.157.243
}
}
配置Nginx
Nginx的基本配置如下所示:
#user nobody; #用户名称
worker_processes auto; #处理进程个数,一般为自动分配
error_log logs/error.log; #错误日志记录位置
#error_log logs/error.log notice; #notice/info等为记录错误的级别
#error_log logs/error.log info;
pid logs/nginx.pid; #进程记录文件
events {
worker_connections 5120; #可处理的连接数,最大处理能力为processes×connections
}
http {
include mime.types;
default_type application/octet-stream;
#配置日志格式(main为自定义格式名称)
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 logs/access.log main; #日志文件位置
keepalive_timeout 65; #连接超时时间
proxy_connect_timeout 10; #后台服务器响应超时时间
#配置反向代理
upstream tomcat{ #tomcat为名称
server 200.31.157.116:8090 weight=1; #后台服务器的地址以及端口号,weight为权重
server 200.31.157.117:8090 weight=1;
}
#配置负载均衡,Server为nginx服务器
upstream nginx{
server 200.31.157.116:8084 weight=1;
server 200.31.157.117:8084 weight=1;
}
#配置处理请求Server
server{
listen 8084; #监听的端口号
server_name 200.31.157.243; #自定义服务名称,不能与其他Server有冲突
#charset koi8-r; #定义字符集
#access_log logs/host.access.log main; #定义日志名称与日志格式(main)
#设定访问处理规则,如果路径以/cwap开头,则通过以下规则进行处理
location /cwap {
proxy_pass http://tomcat; #反向代理到tomcat服务器该处的tomcat为upstream名称
proxy_redirect off;
proxy_set_header Host $host; #以下是读取访问IP
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location / {
root html; #根目录
index index.html index.htm;
}
#error_page 404 /404.html; #配置错误页面
# redirect server error pages to the static page /50x.html
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server{
listen 80;
server_name localhost;
access_log logs/access.log main;
location /cwap {
proxy_pass http://nginx;
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;
}
location / {
root html;
index index.html index.htm;
#expires 1d; #页面缓存时间
}
error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
<strong><span style="font-size:18px;">负载均衡</span></strong>
nginx 的 upstream目前支持 4 种方式的分配
轮询(默认)
每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。
weight
定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。
ip_hash
每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。
fair(第三方)
按后端服务器的响应时间来分配请求,响应时间短的优先分配。
在http中upstream配置中,clouder是起的负载均衡服务器或者反向代理的名称。
upstream clouder {
#ip_hash;
#least_conn;
server 200.31.157.116:8090;
server 200.31.157.116:9090 down; #表示当前server暂时不参与负载
server 200.31.157.117:8090 weight=2; #默认为1,weight越大,负载的权重越大
#其它所有非backup机器down或忙时,请求backup机器。所以这台机器压力会最轻
server 200.31.157.117:9090 backup;
<span style="white-space:pre"> </span>server 200.31.157.117:8084 fail_timeout=10s; #失败后的暂停时间
<span style="white-space:pre"> </span>#最大失败次数为2,失败后暂停时间为10
server 200.31.157.117:8083 max_fails=2 fail_timeout=10s;
}