nginx+keepalived 双主负载均衡

      第一章  原理

 

 

 

原理:

VIP 是外网访问的IP地址,通过keepalived设置,以及VRRPVIP绑定到主机和备机上,通过权重实现控制。当主机挂掉后,keepalived释放对主机的控制,备机接管VIP

    Nginx+keepalived单主环境下,其中一台slave机器处于备份状态,有些浪费,其实可以利用DNS轮询来实现双机负载均衡。

 

          第二章  安装前准备

1.环境配置

1.Ip配置

操作系统:CentOS 6.5

Nginx+keepalived 1服务器 IP:192.168.1.244

Nginx+keepalived 2服务器 IP:192.168.1.245

访问VIP1:  192.168.1.248

访问VIP2:  192.168.1.249

2.需要软件包

zlib-static-1.2.3-29.el6.x86_64.rpm

keepalived-1.2.12.tar.gz

ngx_cache_purge-2.3.tar.gz

nginx-1.9.7.tar.gz

Nginx                                   nginx启动脚本

check_http.sh                            keepalived监控nginx进程的脚本,并自杀

 

 

 

 

2.安装前准备

1..关闭防火墙

[root@localhost ~]# service iptables stop

[root@localhost ~]#chkconfig iptables off

改完后确认

[root@localhost ~]# service iptables status

Iptables: 未运行防火墙

 

2..关闭selinux

 

修改/etc/selinux/config 文件,将SElINUX=enforcing改为SElINUX=disabled或SElINUX=permissive

# vi /etc/selinux/config

……

SElINUX=disabled

……

3.重启

[root@localhost ~]# reboot

 

4.配置本地yum

[root@localhost ~]# cd /etc/yum.repos.d/

[root@localhost yum.repos.d]# mkdir repo.bak

[root@localhost yum.repos.d]# cp CentOS-* repo.bak/

[root@localhost yum.repos.d]# rm -rf CentOS-*

[root@localhost yum.repos.d]# vi aa.repo

[aa]

name=aa

baseurl=file:///mnt

gpgcheck=1

 

[root@localhost ~]# rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-*

 

5.挂载

[root@localhost ~]# mount /dev/dvd /mnt

 

 

            第三章 安装

 

1.安装rpm

[root@localhost ~]# rpm -ivh zlib-static-1.2.3-29.el6.x86_64.rpm

 

2.yum安装包

[root@localhost ~]# yum -y install openssl openssl-devel pcre pcre-devel gcc gcc-c++ make wget zlib-static zlib zlib-devel

3.安装keepalived

tar xf keepalived-1.2.12.tar.gz

cd keepalived-1.2.12

./configure --prefix=/usr/local/keepalived

 

 

 

  make

  make install

4.拷贝文件

  cp /usr/local/keepalived/sbin/keepalived /usr/sbin

  cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/

  cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/

  mkdir /etc/keepalived

cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/

设置开机启动

chkconfig keepalived on

5.配置keepalived

1.配置244

[root@localhost ~]# vi /etc/keepalived/keepalived.conf

 

! Configuration File for keepalived

 

global_defs {

   notification_email {

     [email protected]

     [email protected]

     [email protected]

   }

   notification_email_from [email protected]

   smtp_server 192.168.200.1

   smtp_connect_timeout 30

   router_id NGINX_1        运行keepalived机器的一个标识

}

 vrrp_script chk_nginx {    

      script "/tmp/check_http.sh"

      interval 2

      weight 2

   }                       故障时执行的脚本

vrrp_instance VI_1 {          监控多个网段的实例

    state BACKUP           指定那个为master,那个为backup,主备考priority

    interface eth0           设置实例绑定的网卡

    virtual_router_id 51      VPID标记

    priority 99              优先级,高优先级竞选为master

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 1111

}

 track_script {

         chk_nginx

   }                   注意只有backup配置这三行

    virtual_ipaddress {    

   192.168.1.248 dev eth0 label eth0:1    访问vip1

    }

}

vrrp_instance VI_2 {

state MASTER               

   interface eth0              

    virtual_router_id 52     

    priority 100            

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 1111

    }

    virtual_ipaddress {

 192.168.1.249 dev eth0 label eth0:2    访问vip2

    }

}

2.配置245

[root@localhost ~]# vi /etc/keepalived/keepalived.conf

 

 

! Configuration File for keepalived

 

global_defs {

   notification_email {

     [email protected]

     [email protected]

     [email protected]

   }

   notification_email_from [email protected]

   smtp_server 192.168.200.1

   smtp_connect_timeout 30

   router_id NGIXN_2

}

 vrrp_script chk_nginx {

      script "/tmp/check_http.sh"

      interval 2

      weight 2

   }

vrrp_instance VI_1 {

    state MASTER

    interface eth0

    virtual_router_id 51

    priority 100

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 1111

    }

    virtual_ipaddress {

192.168.1.248 dev eth0 label eth0:1

    }

}

 

vrrp_instance VI_2 {

    state BACKUP

    interface eth0

    virtual_router_id 52

    priority 99

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 1111

    }

 track_script {

        chk_nginx

    }

    virtual_ipaddress {

192.168.1.249 dev eth0 label eth0:2

    }

}

 

 

6.上传check_http.sh

 

check_http.sh脚本通过ftp传输到到文件夹/tmp

赋予脚本执check_http.sh行权限

[root@localhost tmp]# chmod +x check_http.sh

 

 

 

     第四章 nginx安装及缓存配置

1.Nginx安装

上传软件包到/root    

[root@localhost~]#mkdir -p /usr/local/nginx

[root@localhost~]#tar zxvf ngx_cache_purge-2.3.tar.gz

[root@localhost~]#tar zxvf nginx-1.9.7.tar.gz

[root@localhost~]#cd nginx-1.9.7

[root@localhost nginx-1.9.7]# ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_sub_module --add-module=../ngx_cache_purge-2.3加入缓存模块

[root@localhost nginx-1.9.7]#make

[root@localhost nginx-1.9.7]#make install

[root@localhost nginx-1.9.7]#cd /usr/local/nginx

启动nginx

[root@loaclhost nginx]#./sbin/nginx

查看nginx启动是否成功

[root@localhost nginx]#./sbin/nginx-t

 

重新载入

[root@localhost nginx]#./sbin/nginx -s reload

查看nginx进程

[root@localhost nginx]#ps -ef | grep nginx

 

2.Nginx配置文件

cd /usr/local/nginx/

vi conf/nginx.conf

 

user nobody;

#nginx 运行的用户

worker_processes auto;

#启动进程,通常设置成和cpu总核心数量相等,auto自动检测

#error_log logs/error.log;

#error_log logs/error.log notice;

error_loglogs/error.log info;

#打开定义错误日志的路径及其日志级别

pid logs/nginx.pid;

#nginx 的进程

worker_rlimit_nofile 65535;

#文件句柄数,和系统单进程打开的文件数相同,不必理会进程个数

events{

worker_connections 65535;定义的是单个进程的连接数,该值受系统进程打开文件数限制,需要修改打开的文件句柄数,但是max_client = worker_proxesses X work_connextions

use epoll;   #linux最常用支持大并发的事件触发机制

}

 

#设定http服务器,利用它的反向代理功能提供负载均衡支持

http{

include mime.types;

default_type application/octet-stream;

 

#服务器名字的hash表大小

server_names_hash_bucket_size 128;
#指定来自客户端请求头的headerbuffer大小,设置为32KB
client_header_buffer_size 32k;
#指定客户端请求中较大的消息头的缓存最大数量和大小,这里是432KB
large_client_header_buffers 4 32k;
#用来设置允许客户端请求的最大的单个文件字节数
client_max_body_size 356m;

 

#客户端请求主体读取缓存

client_body_buffer_size 32k;

#缓冲区代理缓冲用户端请求的最大字节数

proxy_connect_timeout 10;

#nginx跟后端服务器连接超时时间(代理连接超时)

proxy_read_timeout 180;

#连接成功后,后端服务器响应时间(代理接收超时)

proxy_send_timeout 5;

#后端服务器数据回传时间(代理发送超时)

proxy_buffer_size 16k;

#设置代理服务器(nginx)保存用户头信息的缓冲区大小

proxy_buffers 4 64k;

#proxy_buffers缓冲区,网页平均在32k以下的设置

proxy_temp_file_write_size 256k;

#设定缓存文件夹大小,大于这个值,将从upstream服务器传

proxy_busy_buffers_size 128k;

#高负荷下缓冲大小(proxy_buffers*2

open_file_cache max=102400 inactive=20s;

#这个将为打开文件指定缓存,默认是没有启用的,max指定缓存数量,建议和打开文件数一致,inactive是指经过多长时间文件没被请求后删除缓存。

 

open_file_cache_valid 30s;

#这个是指多长时间检查一次缓存的有效信息。

open_file_cache_min_uses 1;

#open_file_cache指令中的inactive参数时间内文件的最少使用次数,如果超过这个数字,文件描述符一直是在缓存中打开的,如上例,如果有一个文件在inactive时间内一次没被使用,它将被移除。

 

#配置缓存目录

proxy_temp_path /usr/local/nginx/temp;

#proxy_temp_pathproxy_cache_path指定的路径必须在同一分区

proxy_cache_path /usr/local/nginx/cache levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=30g;

#设置内存缓存空间大小为200MB1天没有被访问的内容自动清除,硬盘缓存空间大小为30GB

#日志的格式

 log_format access.log '$remote_addr-[$time_local]-"$request"-"$http_user_agent"'    ;

#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;

sendfile on;

tcp_nopush on;

tcp_nodelay on;

#开启高效模式文件传输模式,将tcp_nopushtcp_nodely两个指另设置为on,用于防止网络阻塞

keepalive_timeout 60;给客户端分配keep-alive链接超时时间。服务器将在这个超时时间过后关闭链接。我们将它设置低些可以让ngnix持续工作的时间更长

server_tokens off;

关闭错误时的nginx 的版本显示

#gzip on;

 

配置负载均衡服务器列表

upstream heren-master{

ip_hash;

server 192.168.1.240:8051 weight=1 max_fails=2 fail_timeout=30s;

server 192.168.1.241:8051 weight=1 max_fails=2 fail_timeout=30s;

}

#weight设置的权重max_fails设置的是健康监测失败次数,timeout设置超时时间

upstream heren-message{

ip_hash;

server 192.168.1.240:9999 weight=1 max_fails=2 fail_timeout=30s;

server 192.168.1.241:8051 weight=1 max_fails=2 fail_timeout=30s;

}

upstream heren-report{

ip_hash;

server 192.168.1.240:8081 weight=1 max_fails=2 fail_timeout=30s;

server 192.168.1.241:8051 weight=1 max_fails=2 fail_timeout=30s;

}

upstream heren-schedule{

ip_hash;

server 192.168.1.240:8061 weight=1 max_fails=2 fail_timeout=30s;

server 192.168.1.241:8051 weight=1 max_fails=2 fail_timeout=30s;

}

 

配置主机

server{

listen 80;

 

server_name localhost;

 

#charset koi8-r;

 

#access_log logs/host.access.log main;

 

配置业务访问url


location /{

root html;

#index index.html index.htm;      增加#

}

 

配置缓存规则

location ~.*\.(gif|jpg|jpeg|png|bmp|sqf|js|css)$

#设置扩展名以gif  .jpg .css 等结尾的静态文件缓存

{

proxy_next_upstream http_502 http_504 error timeout invalid_header;

#如果后端的服务器返回502 504 执行超时等错误,自动将请求转发到upstream 负责均衡池中的另外一台服务器,实现故障转移

    proxy_cache cache_one;

#进行缓存,使用web缓存去cache_one

proxy_cache_valid 200 304 12h;

proxy_cache_valid any 1m;

proxy_set_header Host $host;

proxy_set_header X-Real-IP $remote_addr;

#对不同的HTTP 状态码设置不同的缓存时间

proxy_set_header Accept-Encoding "none";

# proxy_set_header Accept-Encoding ""; 同上面一项相同

proxy_ignore_headers "Cache-Control" "Expires";

#设定proxy_set_header  Accept-Encoding 或者是后台服务器关闭gzip,该台机器才不会缓存被压缩的文件,造成乱码#设置proxy_cache 支持后台设定的expires , 即使支持HTTP头信息定义的缓存规则

    

proxy_pass http://heren-master;

expires 1h;

#设置过去的时间

}

 

#设置js|css在浏览器中过期的时间

location ~.*\.(js|css)

{

expires 1h;

}

#设置清除所有的cache

location ~/purge(/.*)

{

allow 127.0.0.1;

allow 192.168.244.0/24;

deny all;

proxy_cache_purge cache_one $host$1$is_args$args;

}

}

}

3.nginx配置静态页面

cd  /usr/local/html

mkdir heren

上传4jar包到主节点和备节点heren文件夹下,解压


4.上传nginx脚本.

nginx启动脚本通过ftp传输到到文件夹/etc/init.d/

赋予nginx脚本执行权限

[root@localhost~]# cd /etc/init.d

[root@localhost init.d]# ll

 

如若出现上面情况,执行下面命令

 

[root@localhost init.d]# cp  nginx?ű? nginx

[root@localhost init.d]# rm -rf  nginx?ű?

[root@localhost init.d]# chmod +x nginx

[root@localhost init.d]# ll

 

 

设置nginx开机启动  

chkconfig nginx on

           

          第五章 测试                       

1.启动keepalivednginx

查看244

[root@localhost ~]# service nginx start

[root@localhost ~]# /etc/init.d/keepalived start

[root@localhost ~]# ip a

 

 

查看245

[root@localhost ~]# service nginx start

[root@localhost ~]# /etc/init.d/keepalived start

[root@localhost ~]# ip a

 

2.停掉244nginx

查看244

 

查看245

 

3.启动244nginxkeepalived

查看244

 

查看245

 

 

4.245nginx的测试请参照244

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章