Nginx负载均衡和Keepalived的安装设置

一、Nginx设置负载均衡  

(1)upstream的配置

http {

    upstream backend {                         #这里设置后台分发的服务器族群,有多少个可以添加,同时设置查询策略
        server 192.168.3.148:80;
        server 192.168.3.148:8080;
    }

    include       /etc/nginx/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  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}

upstream的配置方式:

nginx 的upstream目前支持4种方式的分配

1、轮询(默认)

每个请求按时间顺序逐一分配到不同的后端服务器 ,如果后端服务器down掉,能自动剔除。

2、weight
指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。  
例如:
    upstream bakend {
         server 192.168.0.14 weight=10;
         server 192.168.0.15 weight=10;
    }

3、ip_hash 
每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session 的问题。
例如:
    upstream bakend {
         ip_hash;
         server 192.168.0.14:88;
         server 192.168.0.15:80;
    }

4、fair(第三方)
按后端服务器的响应时间来分配请求,响应时间短的优先分配。
upstream backend {
    server server1;
    server server2;
    fair;
}

5、url_hash(第三方)

按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,后端服务器为缓存时比较有效。

例:在upstream中加入hash语句,server语句中不能写入weight等其他的参数,hash_method是使用的hash算法

upstream backend {
    server squid1:3128;
    server squid2:3128;
    hash   $request_uri;
    hash_method crc32;
}

细节:

upstream bakend{        #定义负载均衡 设备的Ip及设备状态
 ip_hash;
    server 127.0.0.1:9090 down;
    server 127.0.0.1:8080 weight=2;
    server 127.0.0.1:6060;
    server 127.0.0.1:7070 backup;
}

每个设备的状态设置为:
1.down 表示单前的server暂时不参与负载
2.weight 默认为1.weight越大,负载的权重就越大。
3.max_fails :允许请求失败的次数默认为1.当超过最大次数时,返回proxy_next_upstream 模块定义的错误
4.fail_timeout:max_fails次失败后,暂停的时间。
5.backup: 其它所有的非backup机器down或者忙的时候,请求backup机器。所以这台机器压力会最轻。

nginx支持同时设置多组的负载均衡,用来给不用的server来使用。

client_body_in_file_only 设置为On,可以将client post过来的数据记录到文件中用来做debug
client_body_temp_path 设置记录文件的目录,可以设置最多3层目录

location 对URL进行匹配.可以进行重定向或者进行新的代理 负载均衡

 

(2)server的配置

server {
    listen       8001;
    server_name  localhost;
    set $root_path '/var/www/html';

    #charset koi8-r;
    #access_log  /var/log/nginx/log/host.access.log  main;

    location / {
        proxy_set_header host              $host;
        proxy_set_header X-forwarded-for   $proxy_add_x_forwarded_for;
        proxy_set_header X-real-ip         $remote_addr;           #获取经过负载均衡转发后实际的客户端IP
        proxy_pass       http://backend;                           #这里的proxy_pass设置成http
        #root   $root_path;
        #index  index.html index.htm index.php;
    }

    #location /API/public {
    #    try_files $uri $uri/ /index.php?$query_string;
    #}

    #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   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ .php$ {
    #    proxy_pass   http://backend;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ .php$ {
    #    root           $root_path;
    #    fastcgi_pass   unix:/var/run/php5-fpm.sock;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME   $document_root$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /.ht {
    #    deny  all;
    #}
}

 

二、Keepalived的设置  

(1)安装

  sudo apt-get install keepalived

(2)配置

  sudo vim /etc/keepalived/keepalived.conf  (keepalived.conf不存在,则创建)

Master机:

 

global_defs  {

notification_email  {

       123@163.com                    #警告邮箱

     }

notification_email_from  123@163.com

smtp_server 127.0.0.1

smtp_connect_timeout 30

router_id  LVS_Twioo

}


  vrrp_instance VI_1  {
          state MASTER                         ### 设置为主机
          interface eth0                        ### 监控网卡    
          virtual_router_id 51              ### 这个两台服务器必须一样
          priority 101                      ### 权重值 MASTRE 一定要高于 BAUCKUP
有的地方说是至少要比BACKUP50
          authentication  {
                  auth_type PASS             ### 加密
                  auth_pass eric              ### 加密的密码,两台服务器一定要一样, 不然会出错
          }
          virtual_ipaddress  {
                  192.168.0.222             ### VIP 地址
          }
  }

Backup机(基本和Master一样,有些地方需要更改即可):

 

  global_defs  {

notification_email  {

       123@163.com                    #警告邮箱

}

notification_email_from  123@163.com

     smtp_server 127.0.0.1

     smtp_connect_timeout 30

        router_id  LVS_Twioo

}


  vrrp_instance VI_1  {
          state BACKUP                        ### 设置为主机
          interface eth0                        ### 监控网卡    
          virtual_router_id 51              ### 这个两台服务器必须一样
          priority 11                        ### 权重值 MASTRE 一定要高于 BAUCKUP有的地方说是至少要比BACKUP50
          authentication  {
                  auth_type PASS             ### 加密
                  auth_pass eric              ### 加密的密码,两台服务器一定要一样, 不然会出错
          }
          virtual_ipaddress  {
                  192.168.0.222             ### VIP 地址
          }
  }

  自己可以设置监控脚本

原文地址:https://www.cnblogs.com/linguoguo/p/5409019.html