keepalive+nginx 热备跟负载均衡
结构图

keepalived配置
master跟backup除了state跟优先级,其它一样,优先级master需大于backup
! Configuration File for keepalived
global_defs {
notification_email {
#acassen@firewall.loc
#failover@firewall.loc
#sysadmin@firewall.loc
}
#notification_email_from Alexandre.Cassen@firewall.loc
#smtp_server 192.168.200.1
#smtp_connect_timeout 30
router_id LVS_DEVEL
vrrp_skip_check_adv_addr
#vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_script chk_nginx {
script "/etc/keepalived/chk_nginx.sh"
interval 2
}
vrrp_instance VI_1 {
state BACKUP
interface ens192
virtual_router_id 51
priority 30
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.0.233
}
notify_master "/usr/bin/python2.7 /etc/keepalived/send.py XXX@qq.com 报!222成为主机! 报!222成为主机!"
notify_fault "/usr/bin/python2.7 /etc/keepalived/send.py XXX@qq.com 报!222已挂! 10.0.0.222故障!请尽快修复!"
track_script {
chk_nginx
}
}
其中检测nginx状态的脚本如下,当发现自身nginx没有运行,杀掉keepalived的进程
脚本运行不了可能是centos shell运行报语法错误: 未预期的文件结尾
#!/bin/bash
run=`ps -C nginx --no-header | wc -l`
if [ $run -eq 0 ]; then
/usr/local/nginx/sbin/nginx -s stop
/usr/local/nginx/sbin/nginx
sleep 3
if [ `ps -C nginx --no-header | wc -l` -eq 0 ]; then
systemctl stop keepalived
fi
fi
其中,调用python发送邮件的代码
# coding=utf-8
import smtplib,sys
from email.mime.text import MIMEText class Msmtp():
def __init__(self, target, subject, content):#收件人、标题、内容
self.msg_from = '发出邮件的邮箱'
self.password = '邮箱的授权码'
self.sender = smtplib.SMTP_SSL("smtp.qq.com", 465)
self.msg_to = target
self.subject = subject
self.content = content def _login(self):
self.sender.login(self.msg_from, self.password) def _msg(self):
self.msg = MIMEText(self.content)
self.msg['Subject'] = self.subject
self.msg['From'] = self.msg_from
self.msg['To'] = self.msg_to def send_mail(self):
self._login()
self._msg()
self.sender.sendmail(self.msg_from, self.msg_to, self.msg.as_string())
self.sender.quit() if __name__ == '__main__':
a = Msmtp(sys.argv[1], sys.argv[2], sys.argv[3])
a.send_mail()
nginx配置
#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 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; sendfile on;
#tcp_nopush on; #keepalive_timeout 0;
keepalive_timeout 65; #gzip on; upstream hello1 {#负载均衡
server 10.0.0.111:6666 max_fails=3 fail_timeout=3600s;#后台有问题3600s内不访问
server 10.0.0.222:6666 max_fails=3 fail_timeout=3600s;#后台性能瓶颈的话直接在此加后台
#server 10.0.0.18:80 max_fails=3 fail_timeout=3600s;#可直接转发到nginx或者tomcat等其他服务器上
} server {
listen 80;
server_name localhost; charset utf-8; location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 666;
server_name 10.0.0.233;#监听虚拟IP charset utf-8; location /{
root html;
index index.html index.htm;
}
location /hello{
proxy_pass http://hello1/hello;
}
}
}
测试后台代码
# encoding=utf8
from flask import Flask, request app = Flask(__name__)
app.app_context().push() @app.route('/hello')
def hello():
ip = request.remote_addr
return 'hello world<br/>我是10.0.0.222<br/>访问者IP为: '+ip def runFlask(port):
app.run(host='0.0.0.0',port=port)
CORS(app, supports_credentials=True) if '__main__' == __name__:
runFlask(6666)
检测数据包代码
tcpdump -nn -c -i any host 224.0.0.18
测试(我是xxx,是后台ip,访问者ip为nginx ip)
1. 10.0.0.111跟10.0.0.222均开启nginx及keepalived,虚拟ip在111上,随机访问后台


2. 10.0.0.111关掉nginx,关掉keepalived(模拟启动nginx失败场景),虚拟ip转到222上,随机访问后台,并且收到ip转移邮件
/usr/local/nginx/sbin/nginx -s stop
systemctl stop keepalived



3. 关掉10.0.0.111上的后台,只访问222的后台,速度正常



4.启动10.0.0.111上的nginx跟keepalived,虚拟ip重回111



keepalive+nginx 热备跟负载均衡的更多相关文章
- keepalived+LVS 实现双机热备、负载均衡、失效转移 高性能 高可用 高伸缩性 服务器集群
本章笔者亲自动手,使用LVS技术实现实现一个可以支持庞大访问量.高可用性.高伸缩性的服务器集群 在读本章之前,可能有不少读者尚未使用该技术,或者部分读者使用Nginx实现应用层的负载均衡.这里大家都可 ...
- nginx负载均衡三:keepalive+nginx双机热备 和负载均衡
环境 centos7.0 nginx:1.15 1.主备四台服务器 f1:负载均衡 192.168.70.169 f2:web站点 192.168.70.170 f3:web站点 192.168 ...
- Nginx+Keepalived 实现双击热备及负载均衡
Nginx master : 10.1.58.191 Nginx负载均衡主机 Nginx slave : 10.1.58.181 Nginx负载均衡备机Nginx_VIP_TP: 10 ...
- Mysql 如何做双机热备和负载均衡 (方法二)
先简要介绍一下mysql双向热备:mysql从3.23.15版本以后提供数据库复制功能.利用该功能可以实现两个数据库同步,主从模式(A->B),互相备份模式(A<=>B)的功能. m ...
- Mysql 如何做双机热备和负载均衡
MySQL数据库没有增量备份的机制,但它提供了一种主从备份的机制,就是把主数据库的所有的数据同时写到备份数据库中.实现MySQL数据库的热备份. 下面是具体的主从热备份的步骤:假设主服务器A(mast ...
- Mysql 如何做双机热备和负载均衡 (方法一)
MySQL数据库没有增量备份的机制,但它提供了一种主从备份的机制,就是把主数据库的所有的数据同时写到备份数据库中.实现MySQL数据库的热备份. 下面是具体的主从热备份的步骤:假设主服务器A(mast ...
- Keepalive+nginx实现高可用负载均衡方案
Keepalive+nginx实现高可用负载均衡方案 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.nginx做高可用工作在第几层? 上个月我发表了一篇keepalive+lv ...
- Nginx+keepalived做双机热备加tomcat负载均衡
Nginx+keepalived做双机热备加tomcat负载均衡 环境说明: nginx1:192.168.2.47 nginx2:192.168.2.48 tomcat1:192.168.2.49 ...
- Nginx代理功能与负载均衡详解
序言 Nginx的代理功能与负载均衡功能是最常被用到的,关于nginx的基本语法常识与配置已在上篇文章中有说明,这篇就开门见山,先描述一些关于代理功能的配置,再说明负载均衡详细. Nginx代理服务的 ...
随机推荐
- java中的随机数Random
java中一般有两种随机数,一个是Math中random()方法,一个是Random类. 一.Math.random() : 随即生成0<x<1的小数 实例:如何写,生成随机生成 ...
- Win10 1803 Spring Creators update Consumer edition的版本记录
安装时可选择的版本列表 安装完之后的版本: 3. 时间线更新 4. Focus assistant
- 基于C#.NET的高端智能化网络爬虫(一)(反爬虫哥必看)
前两天朋友发给我了一篇文章,是携程网反爬虫组的技术经理写的,大概讲的是如何用他的超高智商通过(挑衅.怜悯.嘲讽.猥琐)的方式来完美碾压爬虫开发者.今天我就先带大家开发一个最简单低端的爬虫,突破携程网超 ...
- matplotlib之随机漫步
# 随机漫步类 from random import choice from matplotlib import pyplot as plt from pylab import mpl from ma ...
- AtCoder WTF 2019 C2. Triangular Lamps Hard
题目链接 感觉这样的题真的称得上是鬼斧神工啊,\(\text{OI}\)中能多一些这样的题目就太好了. 题意: 有一个二维的三角坐标系,大概如图所示(图是从atcoder里偷下来的): 坐标系上的每个 ...
- scrapy 简单爬虫实验
利用python的模块requests来爬取百度搜索出来的url 使用环境为python3 #!/use/bin/env python # -*- coding:utf-8 -*- import re ...
- BZOJ2120&2453数颜色——线段树套平衡树(treap)+set/带修改莫队
题目描述 墨墨购买了一套N支彩色画笔(其中有些颜色可能相同),摆成一排,你需要回答墨墨的提问.墨墨会像你发布如下指令: 1. Q L R代表询问你从第L支画笔到第R支画笔中共有几种不同颜色的画笔. 2 ...
- ZOJ2836-Number Puzzle-容斥原理
依次考虑一个数的倍数,两个数的倍数(lcm),三个数的倍数(lcm)... 会发现有这么一个规律,奇数个数时要加上情况数,偶数个数时要减去情况数. 一种只有10个数,用二进制枚举所有情况即可. #in ...
- HDU-1686-KMP-水题
纯KMP #include <cstdio> #include <algorithm> #include <cstring> #include <ctype. ...
- MT【229】最小值函数
已知定义域为$R$的函数,$f(x),g(x)$满足:$f(x)+g(x)=e^{-x^2+1}$,则$min\{f(x),g(x)\}$的最大值为______ 解答:$min\{f(x),g(x)\ ...