双主模式使用两个VIP,前段有2台服务器,互为主从,两台服务器同时工作,不存在资源浪费情况。同时在前端的DNS服务器对网站做多条A记录,实现了Nginx的负载均衡,当一台服务器故障时候,资源会转移到另一台服务器,继续提供服务,在大型的网站中多数都使用此种架构。在此使用主主模式配置Nginx+keepalived的高可用性。

两台Nginx,两台Web,前端DNS由运营商提供。

IP地址

Nginx1:10.10.10.11

Nginx2:10.10.10.12

VIP1:10.10.10.21

VIP2:10.10.10.22

Real1:10.10.10.13

Real2:10.10.10.14

1,在2台Web主机上部署环境,安装Nginx+PHP+MySQL,参考我前面的文章

2,分别在二台Nginx负载均衡器上安装Nginx,配置

安装GCC编译器等工具:

yum install -y gcc gcc-c++ autoconf automake libtool make openssl openssl-devel

安装Nginx:

wget http://exim.mirror.fr/pcre/pcre-8.38.tar.gz

tar -zxvf pcre-8.38.tar.gz

cd pcre-8.38

./configure

make && make install

wget http://zlib.net/zlib-1.2.8.tar.gz

tar -zxvf zlib-1.2.8.tar.gz

cd zlib-1.2.8

./configure

make && make install

wget http://nginx.org/download/nginx-1.6.3.tar.gz

tar -zxvf nginx-1.6.3.tar.gz

cd nginx-1.6.3/

./configure --prefix=/usr/local/nginx --sbin-path=/usr/local/nginx/sbin/nginx --conf-path=/usr/local/nginx/conf/nginx.conf --pid-path=/usr/local/nginx/logs/nginx.pid --with-http_ssl_module --with-http_stub_status_module --with-http_gzip_static_module

make && make install

Nginx.conf配置文件,二个nginx负载均衡器的文件一样

user  nobody;

worker_processes  1;

error_log  /usr/local/nginx/logs/error.log notice;

pid        /usr/local/nginx/logs/nginx.pid;

worker_rlimit_nofile 51200;

events {

use epoll;

worker_connections  51200;

}

http {

include       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  logs/access.log  main;

server_names_hash_bucket_size 128;

client_header_buffer_size 32k;

large_client_header_buffers 4 32k;

client_max_body_size 8m;

sendfile        on;

tcp_nopush     on;

server_tokens off;

keepalive_timeout  60;

fastcgi_connect_timeout 300;

fastcgi_send_timeout 300;

fastcgi_read_timeout 300;

fastcgi_buffer_size 64k;

fastcgi_buffers 4 64k;

fastcgi_busy_buffers_size 128k;

fastcgi_temp_file_write_size 128k;

gzip  on;

upstream backend

{

server 10.10.10.13;

server 10.10.10.14;

}

server {

listen       80;

server_name  10.10.10.21;  #Nginx2改为10.10.10.22

location / {

root   html;

index  index.php index.html index.htm;

proxy_redirect off;

proxy_set_header Host $host;

proxy_set_header X-Real-IP $remote_addr;

#后端的Web服务器可以通过X-Forwarded-For获取用户真实IP

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

proxy_pass http://backend;

}

error_page   500 502 503 504  /50x.html;

location = /50x.html {

root   html;

}

location /nginx_status {

stub_status on;

auth_basic "NginxStatus";

auth_basic_user_file /usr/local/nginx/htpasswd;

#allow 127.0.0.1;

#deny all;

}

location ~* \.(ini|docx|txt|doc|pdf)$ {

#禁止访问文档性文件

root /usr/share/nginx/html;

deny all;

}

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|js|html|htm|css)$ {

root /home/image;

proxy_store on;

proxy_store_access user:rw group:rw all:rw;

proxy_temp_path /home/image;

if ( !-e $request_filename) {

proxy_pass  http://backend;

}

}

}

}

配置完成启动服务

[root@hd1 sbin]# ./nginx

./nginx: error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory

cd /lib

[root@hd1 lib]# ls

[root@hd1 lib]# ls *pcre*

libpcre.so.0  libpcre.so.0.0.1

[root@hd1 lib]# ln -s /lib/libpcre.so.0.0.1 /lib/libpcre.so.1

[root@hd1 lib]#

[root@hd1 sbin]# ./nginx

在二台Nginx上安装及配置keepalived:

wget http://www.keepalived.org/software/keepalived-1.2.15.tar.gz

tar -zxvf keepalived-1.2.15.tar.gz

cd keepalived-1.2.15

./configure --sysconf=/etc/  --with-kernel-dir=/usr/src/kernels/2.6.32-358.el6.i686

Keepalived configuration

------------------------

Keepalived version       : 1.2.15

Compiler                 : gcc

Compiler flags           : -g -O2

Extra Lib                : -lssl -lcrypto -lcrypt

Use IPVS Framework       : Yes

IPVS sync daemon support : Yes

IPVS use libnl           : No

fwmark socket support    : Yes

Use VRRP Framework       : Yes

Use VRRP VMAC            : Yes

SNMP support             : No

SHA1 support             : No

Use Debug flags          : No

make && make install

ln -s /usr/local/sbin/keepalived  /sbin/

#这一步很重要,不执行ln -s会报错“Starting keepalived: /bin/bash: keepalived: command not found”

service keepalived start

二台Nginx上keepalived.conf配置如下,配置完成后分别service keepalived start启动,检测keepalived配置是否成功

Nginx1:

global_defs {

notification_email {

test@163.com

}

notification_email_from keepalived@localhost

smtp_server 127.0.0.1

smtp_connect_timeout 30

router_id NGINX_VIP1

}

vrrp_script chk_http_port {

script "/usr/local/src/check_nginx_pid.sh"

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

weight 2

}

vrrp_instance VI_1 {

state MASTER

interface eth1

virtual_router_id 51

priority 100

advert_int 1

authentication {

auth_type PASS

auth_pass 1111

}

track_script {

chk_http_port            #(调用检测脚本)

}

virtual_ipaddress {

10.10.10.21/24 broadcast 10.10.10.255 dev eth1 label eth1:1

}

}

vrrp_instance VI_2 {

state BACKUP

interface eth1

virtual_router_id 52

priority 99

advert_int 1

authentication {

auth_type PASS

auth_pass 1111

}

track_script {

chk_http_port            #(调用检测脚本)

}

virtual_ipaddress {

10.10.10.22/24 broadcast 10.10.10.255 dev eth1 label eth1:2

}

}

Nginx2:

global_defs {

notification_email {

test@163.com

}

notification_email_from keepalived@localhost

smtp_server 127.0.0.1

smtp_connect_timeout 30

router_id NGINX_VIP2

}

vrrp_script chk_http_port {

script "/usr/local/src/check_nginx_pid.sh"

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

weight 2

}

vrrp_instance VI_1 {

state BACKUP

interface eth1

virtual_router_id 51

priority 99

advert_int 1

authentication {

auth_type PASS

auth_pass 1111

}

track_script {

chk_http_port            #(调用检测脚本)

}

virtual_ipaddress {

10.10.10.21/24 broadcast 10.10.10.255 dev eth1 label eth1:1

}

}

vrrp_instance VI_2 {

state MASTER

interface eth1

virtual_router_id 52

priority 100

advert_int 1

authentication {

auth_type PASS

auth_pass 1111

}

track_script {

chk_http_port            #(调用检测脚本)

}

virtual_ipaddress {

10.10.10.22/24 broadcast 10.10.10.255 dev eth1 label eth1:2

}

}

以下是针对nginx状态进行检测的脚本

vim  /usr/local/src/check_nginx_pid.sh

#!/bin/bash

A=`ps -C nginx --no-header |wc -l`

if [ $A -eq 0 ];then

/usr/local/nginx/sbin/nginx

if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then

killall keepalived

fi

fi

脚本加上可执行权限

chmod +x /usr/local/keepalived/sbin/check_nginx.sh

服务开启后网卡状态

[root@hd2 keepalived-1.2.15]# ip addr

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN

link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00

inet 127.0.0.1/8 scope host lo

inet6 ::1/128 scope host

valid_lft forever preferred_lft forever

2: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNKNOWN qlen 1000

link/ether 00:50:56:22:04:b1 brd ff:ff:ff:ff:ff:ff

inet 10.10.10.12/24 brd 10.10.10.255 scope global eth1

inet 10.10.10.22/24 brd 10.10.10.255 scope global secondary eth1:2

inet6 fe80::250:56ff:fe22:4b1/64 scope link

valid_lft forever preferred_lft forever

3: pan0: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN

link/ether be:88:be:d6:81:a6 brd ff:ff:ff:ff:ff:ff

[root@hd2 keepalived-1.2.15]#

====================================测试============================

测试web主节点服务down掉之后,备用节点服务是否能正常运行,kill -9 xxxxx,web仍然能够访问

模拟keepalived节点出现故障,nginx服务器是否能自动转移

[root@hd1 sbin]# service keepalived stop

Stopping keepalived: [  OK  ]

结果访问vip 21无法访问,但是22仍然能够正常提供服务

Nginx+Keepalived双主轮询负载均衡的更多相关文章

  1. Nginx keepalived实现高可用负载均衡详细配置步骤

    Keepalived是一个免费开源的,用C编写的类似于layer3, 4 & 7交换机制软件,具备我们平时说的第3层.第4层和第7层交换机的功能.主要提供loadbalancing(负载均衡) ...

  2. Centos7.2下基于Nginx+Keepalived搭建高可用负载均衡(一.基于Keepalived搭建HA体系)

    说明 本文只为方便日后查阅,不对一些概念再做赘述,网上都有很多明确的解释,也请大家先了解相关概念. 两台搭建HA的服务器是华为云上的ECS(不要忘记开通VPC,保证我们的服务器都处在一个内网环境),由 ...

  3. Nginx+Keepalived+Tomcat高可用负载均衡,Zookeeper集群配置,Mysql(MariaDB)搭建,Redis安装,FTP配置

    JDK 安装步骤 下载 http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html rpm ...

  4. mysql主从之LVS+keepalived+双主MySQL 负载均衡

    LVS(Linux Virtual Server)即Linux 虚拟服务器,是一个的开源负载均衡项目,目前LVS 已经被集成到Linux 内核模块中.LVS 是四层负载均衡,也就是说建立在OSI 模型 ...

  5. nginx+keepalived高可用web负载均衡

    一:安装环境 准备2台虚拟机,都安装好环境 centos 7keepalived:vip: 192.168.1.112192.168.1.110 nginxip 192.168.1.109 maste ...

  6. Nginx+Keepalived搭建高可用负载均衡集群

    本文的重点是Keepalived的配置,Nginx的配置就简略带过.软件:CentOS 7.2 / Nginx 1.12.2 / Keepalived 1.3.9 ha-01:192.168.1.97 ...

  7. 搭建 Keepalived + Nginx + Tomcat 的高可用负载均衡架构

    1 概述 初期的互联网企业由于业务量较小,所以一般单机部署,实现单点访问即可满足业务的需求,这也是最简单的部署方式,但是随着业务的不断扩大,系统的访问量逐渐的上升,单机部署的模式已无法承载现有的业务量 ...

  8. Keepalived + Nginx + Tomcat 的高可用负载均衡架构搭建

    Keepalived + Nginx + Tomcat 的高可用负载均衡架构搭建 Nginx 是一个高性能的 HTTP反向代理服务器 Keepalived 是一个基于VRRP协议来实现的LVS服务高可 ...

  9. 搭建Keepalived + Nginx + Tomcat的高可用负载均衡架构

    1 概述 初期的互联网企业由于业务量较小,所以一般单机部署,实现单点访问即可满足业务的需求,这也是最简单的部署方式,但是随着业务的不断扩大,系统的访问量逐渐的上升,单机部署的模式已无法承载现有的业务量 ...

随机推荐

  1. 以后尽量不用cin、cout啦

    cout输出有问题(对于double,不同OJ处理的结果不一样),cin读入机制较scanf繁琐.慢!!!!!!!!

  2. C# 字符串转JSON

    一.简单小结 C# 中 String 转 JSON var items = JsonConvert.DeserializeObject<class>(stringJSON); 这里的 cl ...

  3. Spring第一天:Spring的概述、SpringIOC入门(XML)、Spring的Bean管理、Spring属性注入

    记得引入约束 上图路径. 此时 只需修改配置文件 便可以随意更换实现类 无需修改代码. 传统方法必须用实现类(不面向接口了)来调用方法设置属性. 而在Spring中:在创建类的过程中发现实现类有nam ...

  4. 浅谈JavaScript -- 正则表达式

    什么是正则表达式? 正则表达式是由一个字符序列形成的搜索模式.可用于文本搜索和文本替换. 语法:/正则表达式主体/修饰符(可选) var patt=new RegExp(pattern,modifie ...

  5. 洛谷P4762 [CERC2014]Virus synthesis(回文自动机+dp)

    传送门 回文自动机的好题啊 先建一个回文自动机,然后记$dp[i]$表示转移到$i$节点代表的回文串的最少的需要次数 首先肯定2操作越多越好,经过2操作之后的串必定是一个回文串,所以最后的答案肯定是由 ...

  6. linux笔记之基础 1

    内核命名规则: R.X.Y-Z   2.6.32-642 R: 内核有重大改变时才会更改,目前为止有四个大版本更新. X:基数为开发版,偶数为稳定版. Y.Z:修复bug,实现新功能,新特性的时候更改 ...

  7. Groovy xlsx

    如果在JMeter安装的“bin”文件夹下有Excel(xlsx)文件,则test.xlsx可以使用以下方法动态填充请求参数: 将tika-app.jar添加到JMeter Classpath 重新启 ...

  8. LeetCode初级算法(动态规划+设计问题篇)

    目录 爬楼梯 买卖股票的最佳时机 最大子序和 打家劫舍 动态规划小结 Shuffle an Array 最小栈 爬楼梯 第一想法自然是递归,而且爬楼梯很明显是一个斐波拉切数列,所以就有了以下代码: c ...

  9. 关于Markdown的一些学习笔记

    **关于Markdown的一些学习笔记** 一直利用markdown进行博客的文档编写,一方面是因为不需要特别注重排版,另一方面是十分的方便.最近突发奇想的认为,如果能运用到平时的作业或课程中,会不会 ...

  10. BZOJ 4919: [Lydsy1706月赛]大根堆 启发式合并

    我不会告诉你这是线段树合并的好题的... 好吧我们可以搞一个multiset在dfs时求出LIS(自带二分+排序)进行启发式合并,轻松加愉悦... #include<cstdio> #in ...