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;

server 192.168.137.20:80 weight=5;

}

server {

listen 80;

server_name 192.168.137.100;

location / {

proxy_pass http://httpservers;

}

}

A. ip_hash 每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题

[[email protected] conf]# cat upstream.conf

upstream httpservers {

ip_hash;

server 192.168.137.10:80 weight=5;

server 192.168.137.20:80 weight=5;

}

server {

listen 80;

server_name 192.168.137.100;

location / {

proxy_pass http://httpservers;

}

}

nginx做负载均衡(keepalived高可用)

拓扑图



1. nginx配置(在主备服务器上配置)

upstream httpservers {

ip_hash;

server 192.168.137.10:80 weight=5;

server 192.168.137.20:80 weight=5;

}

server {

listen 80;

server_name 192.168.137.201;

location / {

proxy_pass http://httpservers;

}

}

2. keepalived安装及配置(在主备服务器上配置)

下载:wget http://www.keepalived.org/software/keepalived-1.2.8.tar.gz

解压:tar zxvf keepalived-1.2.8.tar.gz

编译安装: ./configure --prefix=/usr/local/keepalived

make

make install

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

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

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

mkdir -p /etc/keepalived/

cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/   (配置文件一定要在这里,让我费了好大的劲)

修改/etc/keepalived/keepalived.conf ,  以下分别列出master和backup的配置

+++++++++++++++++++++++++++++++++++++master+++++++++++++++++++++++++++++++++++++++++++

! Configuration File for keepalived

global_defs {

router_id nginx-proxy-ha

}

vrrp_script chk_http_port {

script "/usr/bin/check_nginx.sh"

interval 2

weight 2

}

vrrp_instance VI_1 {

state MASTER

interface eth0

virtual_router_id 51

priority 200

advert_int 1

authentication {

auth_type PASS

auth_pass 1234

}

track_interface {

eth0

}

track_script {

chk_http_port

}

virtual_ipaddress {

192.168.137.201

}

}

++++++++++++++++++++++++backup++++++++++++++++++++++++++++++++++++++++++++++++++

! Configuration File for keepalived

global_defs {

router_id nginx-proxy-ha

}

vrrp_script chk_http_port {

script "/usr/bin/check_nginx.sh"

interval 2

weight 2

}

vrrp_instance VI_1 {

state BACKUP

interface eth0

virtual_router_id 51

priority 180

advert_int 1

authentication {

auth_type PASS

auth_pass 1234

}

track_interface {

eth0

}

track_script {

chk_http_port

}

virtual_ipaddress {

192.168.137.201

}

}

以上配置完成之后 启动nginx和keepalived,一定要先启动nginx,然后在启动keepalived。

测试效果:master和backup都启动keepalived和nginx后,默认的虚拟ip是在master上的,在master上kill nginx后,浮动ip会绑定在backup上。

各配置解释:

global_defs {

notification_email {

[email protected]     (这里可以定义多个报警邮箱)

}

notification_email_from [email protected] (报警人)

smtp_server 127.0.0.1

smtp_connect_timeout 30

router_id LVS_DEVEL

}

vrrp_script chk_http_port {

script "/opt/tools/bin/check_ng.sh"

interval 2                           (检测脚本执行的间隔)

weight 2

}

vrrp_instance VI_1 {

state BACKUP           (显示定义为从服务器)

interface eth1            (绑定的网口,该网口即上面提到的两个IP的接口)

virtual_router_id 51    (定义的ID,官方的是 51,主从服务器必须一直)

mcast_src_ip 211.151.138.3  (从服务器的IP)

priority 50                (优先级,任意定义,但是一定要比主服务器低)

advert_int 1

authentication {

auth_type PASS

auth_pass 1111         (默认即可)

}

track_script {

chk_http_port            (调用检测脚本)

}

virtual_ipaddress {

211.151.137.5         (绑定的虚IP)

}

}

++++++++++++++++++++++check_nginx.sh+++++++++++++++++++++++++++++++++++++++++++++++++

该脚本用来检测nginx状态,一旦nginx退出,则关闭master的 keepalived,此时的backup收到消息会自动切换为master并绑定虚拟ip。该脚本放在/usr/bin下

#!/bin/bash

if [ "$(ps -ef | grep "nginx: master process"| grep -v grep )" == "" ]

then

killall -9  keepalived

fi

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

时间: 2024-12-24 18:49:06

nginx负载均衡+keepalived高可用完全配置小结的相关文章

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

Nginx负载均衡+keepalived高可用

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

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