Nginx负载均衡+keepalived高可用

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

[[email protected] ~]# 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
[[email protected] ~]# 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;
}
[[email protected] ~]# 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;
}
[[email protected]~]#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:
[[email protected] ~]# 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
[[email protected] ~]# 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;
}
[[email protected]~]#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 (后面两部写脚本执行)
[[email protected] 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
[[email protected] html]# netstat -lnt | grep 80
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN
[[email protected] 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)
[[email protected] 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 看下访问是否会受影响。
此文的只是模拟环境学习使用,生产环境相对复杂些需要结合实践情况学习。

原文地址:http://blog.51cto.com/11569838/2059998

时间: 2024-10-29 05:21:19

Nginx负载均衡+keepalived高可用的相关文章

nginx负载均衡+keepalived高可用完全配置小结

nginx做负载均衡(无高可用) 大致步骤. 1. 前端 nginx安装,pcre安装,具体步骤不解释. 2. 负载配置 A. 默认轮循 在nginx.conf  里加入一行 include upstream.conf,然后所有的负载均衡的配置直接在upstream.conf里配置. [[email protected] conf]# cat upstream.conf upstream httpservers { server 192.168.137.10:80 weight=5; serve

Nginx负载均衡+Keepalived高可用集群

一.搭建环境及软件版本 负载均衡:Nginx 高可用:Keepalived Linux:Centos 6.5 Nginx:nginx-1.6.2 Keepalived:keepalived-1.2.13 LB-00:Nginx+Keepalived 192.168.174.40 LB-01:Nginx+Keepalived 192.168.174.41 Web1:LNMP 192.168.174.20 Web2:LAMP 192.168.174.30 二.负载均衡搭建 ①解压编译安装Nginx

Keepalived+LVS+Nginx负载均衡之高可用

Keepalived+LVS+Nginx负载均衡之高可用 上一篇写了nginx负载均衡,此篇实现高可用(HA).系统整体设计是采用Nginx做负载均衡,若出现Nginx单机故障,则导致整个系统无法正常运行.针对系统架构设计的高可用要求,我们需要解决Nginx负载均衡出现单机故障时,系统正常运行的需求.所以系统架构引入Keepalived组件,实现系统高可用. 一.Keepalived介绍 Keepalived是分布式部署系统解决系统高可用的软件,结合LVS(Linux Virtual Serve

Nginx反向代理、负载均衡, keepalived高可用

Nginx反向代理.负载均衡,  keepalived高可用 Nginx反向代理.负载均衡,  keepalived高可用 一.Nginx反向代理.负载均衡 1.什么是反向代理.负载均衡 严格的说,Nginx仅仅是作为Nginx Proxv反向代理使用的,因为这个反向代理功能表现的效果是负载均衡集群的效果,所以本文称之为Nginx负载均衡.那么,反向代理和负载均衡有什么区别呢? 普通负载均衡软件,例如大名鼎鼎的LVS,其实现的功能只是对请求数据包的转发(也可能会改写数据包).传递,其中DR模式明

Net分布式系统之三:Keepalived+LVS+Nginx负载均衡之高可用

上一篇写了nginx负载均衡,此篇实现高可用(HA).系统整体设计是采用Nginx做负载均衡,若出现Nginx单机故障,则导致整个系统无法正常运行.针对系统架构设计的高可用要求,我们需要解决Nginx负载均衡出现单机故障时,系统正常运行的需求.所以系统架构引入Keepalived组件,实现系统高可用. 一.Keepalived介绍 Keepalived是分布式部署系统解决系统高可用的软件,结合LVS(Linux Virtual Server)使用,其功能类似于heartbeat,解决单机宕机的问

Nginx+Keepalived实现Nginx负载均衡及高可用WEB服务器集群

环境:操作系统Centos 6.5 X86_64(final)Nginx-Master:192.168.2.32Nginx-Backup:192.168.3.31VIP:192.168.2.33Web3:192.168.2.29Web4:192.168.2.301.分别在Nginx-Master和Nginx-Backup上安装nginx[[email protected] ~]# rpm -i http://nginx.org/packages/centos/6/noarch/RPMS/ngin

Haproxy负载均衡+Keepalived高可用web群集

一.HAProxy 1.haproxy简介 HAProxy 是一款提供高可用性.负载均衡以及基于TCP(第四层)和HTTP(第七层)应用的代理软件,支持虚拟主机,它是免费.快速并且可靠的一种解决方案. HAProxy是一个使用C语言编写的自由及开放源代码软件 HAProxy特别适用于那些负载特大的web站点,这些站点通常又需要会话保持或七层处理.HAProxy运行在当前的硬件上,完全可以支持数以万计的并发连接.并且它的运行模式使得它可以很简单安全的整合进您当前的架构中, 同时可以保护你的web服

搭建haproxy负载均衡keepalived高可用集群

自己的服务器到期,转移自己博客内容至此. haproxy官网:www.haproxy.orgkeepalived官网:www.keepalived.org 本站下载:haproxy-1.8.3.tar.gz本站下载:keepalived-1.4.0.tar.gz本站下载:keepalived启动服务文件 环境:web01: 10.8.8.51   centos7web02: 10.8.8.52   centos7haproxy_master:10.8.8.53   centos7    vip:

Nginx之二:负载均衡及高可用

一.Nginx负载均衡及高可用简介 负载均衡是为了解决单个节点压力过大,造成Web服务响应过慢,严重的情况下导致服务瘫痪,无法正常提供服务.负载均衡,将用户的所有HTTP请求均衡的分配到每一台机器上,充分发挥所有机器的性能,提高服务的质量和用户体验.负载均衡常用的Web服务器软件有Nginx.HAProxy.LVS.Apache. Nginx负载均衡是通过upstream模块来实现的,常见四种负载策略: 轮循(默认:将每个请求均匀分配到每台服务器 最少连接:将请求分配给连接数最少的服务器 IP