注:环境介绍:VMware环境下4台虚拟机 两台做负载均衡服务器 两台做realserver (模拟物理web服务器)
1 配置keepalived实现nginx负载均衡高可用,keepalived 主要功能是是实现主备负载 均衡服务器之间的高可用,当一台宕机另一台会自动接管.实现热备。
2 在主备负载均衡服务器个配置nginx反向代理服务,并在本地(你的windows电 脑)/etc/hosts 做域名解析。
Nginx负载均衡+keepalived高可用_第1张图片

[root@lb01 ~]# cat /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 130
}
vrrp_instance VI_1 {
    state MASTER
    interface eth0
    lvs_sync_daemon_inteface eth2
    virtual_router_id 130
    priority 150
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.232.80/24 dev eth0 label eth0:1
    }
}
virtual_server 192.168.232.80 80 {
    delay_loop 6            
    lb_algo wrr                
    lb_kind DR                
    nat_mask 255.255.255.0
    persistence_timeout 50     
    protocol TCP                
        }
lvs01-nginx
[root@lb01 ~]# cat /application/nginx/conf/nginx.conf
#user  nobody;
worker_processes  1;
error_log  logs/error.log;
error_log  logs/error.log  notice;
error_log  logs/error.log  info;
#pid        logs/nginx.pid;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    log_format  commonlog  '$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;
    keepalive_timeout  65;
    include extra/upstream01.conf;
}
[root@lb01 ~]# cat /application/nginx/conf/nginx.conf
#user  nobody;
worker_processes  1;
error_log  logs/error.log;
error_log  logs/error.log  notice;
error_log  logs/error.log  info;

#pid        logs/nginx.pid;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    log_format  commonlog  '$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;
    keepalive_timeout  65;
    include extra/upstream01.conf;
}
[root@lb0~]#cat /application/nginx/conf/extra/upstream01.conf
#blog lb by cyt at 20180107
upstream server_pools {                                 server 192.168.232.132:80 weight=5;
        server 192.168.232.133:80 weight=5;
        }
    server {
        listen   80;
        server_name www.etiantian01.org;
        location  / {
        proxy_pass http://server_pools;
                     proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For     $remote_addr;
        }
    }
lvs02:
[root@lb02 ~]# cat /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 131
}

vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    lvs_sync_daemon_inteface eth2
    virtual_router_id 130
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.232.80/24 dev eth0 label eth0:1
    }
}
virtual_server 192.168.232.80 80 {
    delay_loop 6            
    lb_algo wrr                
    lb_kind DR                
    nat_mask 255.255.255.0
    persistence_timeout 50     
    protocol TCP                

    }
lvs02-nginx
[root@lb02 ~]# cat /application/nginx/conf/nginx.conf
#user  nobody;
worker_processes  1;

error_log  logs/error.log;
error_log  logs/error.log  notice;
error_log  logs/error.log  info;

#pid        logs/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    log_format  commonlog  '$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;
    keepalive_timeout  65;
    include extra/upstream01.conf;
}
[root@lb02~]#cat /application/nginx/conf/extra/upstream01.conf
#blog lb by cyt at 20180107
upstream server_pools {
        server 192.168.232.132:80 weight=5;
        server 192.168.232.133:80 weight=5;
        }
    server {
        listen   80;
        server_name www.etiantian01.org;
        location  / {
                proxy_pass http://server_pools;
                proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $remote_addr;
        }
    }
web01 和web02部署nginx和抑制ARP和绑定vip (后面两部写脚本执行)
[root@web01 html]# ps -ef | grep nginx
root       1342      1  0 11:20 ?        00:00:00 nginx: master process /application/nginx/sbin/nginx
nginx      1343   1342  0 11:20 ?        00:00:00 nginx: worker process        
root       1346   1298  0 11:29 pts/0    00:00:00 grep nginx
[root@web01 html]# netstat -lnt | grep 80
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      
[root@web01 html]# lsof -i tcp:80
COMMAND  PID  USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
nginx   1342  root    6u  IPv4  11119      0t0  TCP *:http (LISTEN)
nginx   1343 nginx    6u  IPv4  11119      0t0  TCP *:http (LISTEN)
[root@web01 html]#
然后抑制arp和绑定vip我们写如下脚本实现:
#!/bin/bash
# chkconfig: 3 52 23
#created by chenyant 11:43 2013/8/11
. /etc/init.d/functions
VIP=(
192.168.232.80
)
start(){

for ((i=0;i<${#VIP[@]};i++))
do
    ifconfig lo:$i ${VIP[$i]} netmask 255.255.255.255 up
    route add -host ${VIP[$i]} dev lo
done
echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore
echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce
echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore
echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce
}
start
服务启动后脚本执行了以后然后就检查:

可以模拟主备负载均衡服务器宕机 然后客户端持续访问看下是否会有影响。
拆除单台webserver 看下访问是否会受影响。
此文的只是模拟环境学习使用,生产环境相对复杂些需要结合实践情况学习。