准备四台服务器

两台做主备,另外两台做访问

192.168.1.120 master

192.168.1.121 backup

192.168.1.122 nginx

192.168.1.123 nginx

安装keepalived

yum -y install gcc pcre-devel zlib-devel openssl-devel

yum -y install popt-devel

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

tar zxvf keepalived-1.2..tar.gz

cd keepalived-1.2.

./configure

make&&make install

漫长的等待后.....

cp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
mkdir /etc/keepalived
cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/
cp /usr/local/sbin/keepalived /usr/sbin/

加入启动服务 注册成服务

echo "/etc/init.d/keepalived start" >> /etc/rc.local

chkconfig --add keepalived

配置master nginx 192.168.1.110 和 192.168.1.121

#user  nobody;
worker_processes ; error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info; #pid logs/nginx.pid;
events {
worker_connections ;
}
http {
include mime.types;
default_type application/octet-stream;
access_log logs/access.log;
sendfile on;
upstream 120.com {
#ip_hash;
server 192.168.1.122:80;
server 192.168.1.123:80;
}
#tcp_nopush on; #keepalive_timeout ;
keepalive_timeout ; #gzip on; server {
listen ;
server_name localhost;
error_page /50x.html;
location = /50x.html {
root html;
}
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
proxy_pass http://120.com;
include fastcgi_params;
} } include vhost/*.conf; }

然后访问试试

实现了负载均衡调用另外两台服务器

配置master keepalived

vi /etc/keepalived/keepalived.conf

! Configuration File for keepalived

global_defs {
notification_email {
@qq.com
}
notification_email_from @qq.com
smtp_server 127.0.0.1
smtp_connect_timeout
router_id nginx_master
}
vrrp_script check_nginx_is_run {
script "/etc/keepalived/check_nginx_is_run.sh"
interval
weight
}
# 虚拟IP1, 本机作为Master
vrrp_instance VI_1 {
state MASTER
interface ens33 #你的网卡
virtual_router_id
priority
advert_int
track_interface {
ens33
}
authentication {
auth_type PASS
auth_pass
}
virtual_ipaddress {
192.168.1.51
}
track_script {
check_nginx_is_run
}
}

配置backup keepalived

! Configuration File for keepalived

global_defs {
notification_email {
@qq.com
}
notification_email_from @qq.com
smtp_server 127.0.0.1
smtp_connect_timeout
router_id nginx_backup
}
vrrp_script check_nginx_is_run {
script "/etc/keepalived/check_nginx_is_run.sh"
interval
weight
}
# 虚拟IP1, 本机作为Master
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id
priority
advert_int
track_interface {
ens33
}
authentication {
auth_type PASS
auth_pass
}
virtual_ipaddress {
192.168.1.51
}
track_script {
check_nginx_is_run
}
}

安装 killall 命令

yum install psmisc

编写 shell 脚本

#!/bin/bash
A=`ps -C nginx --no-header |wc -l`
if [ $A -eq ];then
/usr/local/nginx/sbin/nginx #重启nginx
sleep
if [ `ps -C nginx --no-header |wc -l` -eq ];then #nginx重启失败,则停掉keepalived服务,进行VIP转移
killall keepalived
fi
fi

设置可执行命令

chmod a+x check_nginx_is_run.sh

如果你是window下放进去的,会有

-bash: ./check_nginx_is_run.sh: /bin/bash^M: 坏的解释器: 没有那个文件或目录

解决办法

sed -i 's/\r$//' check_nginx_is_run.sh

查看master keepalived 日记 vim /var/log/messages

查看 backup

好了不说了 停止nginx ,再不行就关机 192.168.1.120 or 121

如果你service nginx stop 来测试,你会发现关不掉,因为脚本重启了

你会发现OK了~ 

nginx+keepalived实现负载均衡nginx的高可用的更多相关文章

  1. Nginx+keepalived实现负载均衡

    Nginx的优点是: 1.工作在网络的7层之上,可以针对http应用做一些分流的策略,比如针对域名.目录结构,它的正则规则比HAProxy更为强大和灵活,这也是它目前广泛流行的主要原因之一,Nginx ...

  2. Nginx+keepalived实现负载均衡高可用配置

    1. 什么是负载均衡高可用 nginx作为负载均衡器,所有请求都到了nginx,可见nginx处于非常重点的位置,如果nginx服务器宕机后端web服务将无法提供服务,影响严重. 为了屏蔽负载均衡服务 ...

  3. LVS负载均衡+动静分离+高可用(nginx+tomcat+keepalived)

    文章目录 [隐藏] 一.环境介绍 二.环境安装 1.安装JDK 2.两台服务器安装tomcat 3.nginx安装 4.keepalive安装 三.负载均衡 四.动静分离 五.keepalive高可用 ...

  4. keepalived+nginx负载均衡+ApacheWeb实现高可用

    1.Keepalived高可用软件 Keepalived软件起初是专为LVS负载均衡软件设计的,用来管理并监控LVS集群系统中各个服务节点的状态,后来又加入了可以实现高可用的VRRP功能.因此,kee ...

  5. nginx+keepalived+tomcat+memcache实现双VIP高可用及Session会话保持

    Nginx+Keepalived+Tomcat+Memcached 实现双VIP负载均衡及Session会话保持 IP 信息列表: 名称         IP                      ...

  6. 22、lnmp_nginx反向代理(负载均衡)、高可用

    负载均衡,根据ip和端口号找到相应的web服务器站点(即端口区分): 22.1.nginx的负载均衡: 1.介绍: 网站的访问量越来越大,服务器的服务模式也得进行相应的升级,比如分离出数据库服务器.分 ...

  7. nginx+keepalived实现 负载均衡 高可用

    Vip: 192.168.220.18 Rip1:192.168.220.2 Rip:192.168.220.3 Rip可以配置在一个服务器上通过ip做虚拟主机 1 rs上配置环境 2 配置应用 Yu ...

  8. nginx配置tomcat负载均衡,nginx.conf配置文件的配置

  9. nginx 七层负载均衡

    [tcp] nginx 七层负载均衡 nginx负载均衡概述 当我们的Web服务器直接面向用户,往往要承载大量并发请求,单台服务器难以负荷,我使用多台Web服务器组成集群,前端使用Nginx负载均衡, ...

随机推荐

  1. CMake Error: CMake was unable to find a build program corresponding to "Ninja".

    系统环境: $ lsb_release -a LSB Version: :base-4.0-amd64:base-4.0-noarch:core-4.0-amd64:core-4.0-noarch:g ...

  2. Codeforces 768B - Code For 1(分治思想)

    768B - Code For 1 思路:类似于线段树的区间查询. 代码: #include<bits/stdc++.h> using namespace std; #define ll ...

  3. 最长的文件路径 Longest Absolute File Path

    2018-07-30 22:05:52 问题描述: 问题求解: 本题个人感觉还是挺有意思的,题目要求的是最长的文件路径,其实是需要keep tracking路径长度,如果出现文件则需要进行比较,看是否 ...

  4. sql一些语句性能及开销优化

    1.应用程序中,保证在实现功能的基础上,尽量减少对数据库的访问次数:通过搜索参数,尽量减少对表的访问行数,最小化结果集,从而减轻网络负担:能够分开的操作尽量分开处理,提高每次的响应速度:在数据窗口使用 ...

  5. English trip -- VC(情景课)9 A Get ready

    She is doing homwork He is doing laundry He is drying the dishes She is making lunch She is making t ...

  6. English trip -- VC(情景课) 6 A Time

    Words eleven  十一 twelve  十二 thirteen  十三 fourteen fifteen sixteen seventeen eighteen nineteen twenty ...

  7. Cloud Commander

    一.Cloud Commander简介: Cloud Commander 是一个基于 web 的文件管理程序,它允许你通过任何计算机.移动端或平板电脑的浏览器查看.访问或管理系统文件或文件夹.它有两个 ...

  8. Leetcode 82

    有个错误就是member access within null pointer of type 'struct ListNode' 其实就是判断了指针是否异常了,比如NULL->next之类.要 ...

  9. C#图片转换成二进制流并且保存到sql server数据库

    注意:我要存储文件二进制流的列的类型是text,不是image类型. 我已经实现了从数据库中读取text类型的二进制流,,现在就是不知道怎么存进去. 我的部分关键代码: StreamReader sr ...

  10. 在主Android Activity中加载Fragment的一般简易方法 ,来模拟一个微信界面。

    在Fragment的生命周期中,需要重点关注onCreate.onCreateView.onViewCreated.Activity与Fragment生命周期在设计模式上大体一致. package c ...