keepalived+nginx实现nginx高可用

前期准备:

1、实验拓扑图

2、地址规划

Master 10.10.0.224(VIP:10.10.0.220) keepalived、nginx
Backup 10.10.0.226(VIP:10.10.0.220) keepalived、nginx
Real Server 1 10.10.0.225 httpd
Real Server 2 10.10.0.221 httpd

一、安装keepalived和nginx

Master和Backup分别安装:

1、安装keepalived

[[email protected] ~]# yum install keepalived -y

2、配置repo源

[[email protected] ~]# vim /etc/yum.repos.d/nginx.repo

[nginx]

name=nginx repo

baseurl=http://nginx.org/packages/centos/6/$basearch/

gpgcheck=0

enabled=1

3、安装ngxin

[[email protected] ~]# yum install nginx -y

4、Master修改

(1).修改keepaliaved配置文件

[[email protected] ~]# cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak

[[email protected] ~]# vim /etc/keepalived/keepalived.conf

! Configuration File for keepalived

global_defs {

notification_email {

[email protected]

}

notification_email_from [email protected]

smtp_server 127.0.0.1

smtp_connect_timeout 30

router_id LVS1

}

vrrp_script chk_nginx{

script "killall -0 nginx"

interval 1

weight -2

fall 2

rise 2

}

vrrp_instance VI_1 {

state MASTER

interface eth0

virtual_router_id 51

priority 100

advert_int 1

authentication {

auth_type PASS

auth_pass 1111

}

virtual_ipaddress {

10.10.0.220 dev eth0 label eth0:0

}

track_script {

chk_nginx

}

}

(3).拷贝一份配置文件到Backup服

[[email protected] ~]# scp -r /etc/keepalived/keepalived.conf [email protected]:/etc/keepalived/

[[email protected] ~]# scp -r /etc/nginx/nginx.conf [email protected]:/etc/nginx/

(4).开启服务并开机自动启动

[[email protected] ~]# service keepalived restart

[[email protected] ~]# service nginx restart

[[email protected] ~]# chkconfig keepalived on

[[email protected] ~]# chkconfig nginx on

5、Backup修改

(1).修改keepalived配置文件

[[email protected] ~]# vim /etc/keepalived/keepalived.conf

! Configuration File for keepalived

global_defs {

notification_email {

[email protected]

}

notification_email_from [email protected]

smtp_server 127.0.0.1

smtp_connect_timeout 30

router_id LVS2     #修改唯一标识

}

vrrp_script chk_nginx{

script "killall -0 nginx"

interval 1

weight -2

fall 2

rise 2

}

vrrp_instance VI_1 {

state BACKUP

interface eth0

virtual_router_id 51

priority 90

advert_int 1

authentication {

auth_type PASS

auth_pass 1111

}

virtual_ipaddress {

10.10.0.220 dev eth0 label eth0:0

}

track_script {

chk_nginx

}

}

(2).开启服务并开机自动启动

[[email protected] ~]# service keepalived restart

[[email protected] ~]# service nginx restart

[[email protected] ~]# chkconfig keepalived on

[[email protected] ~]# chkconfig nginx on

6、Real Server 1

(1).安装httpd

[[email protected]]# yum install httpd -y

(2).建立测试页

[[email protected]]# vim /var/www/html/index.html

<h1>real server 1</h1>

(3).开启服务

[[email protected]]# service httpd restart

[[email protected]]# chkconfig httpd on

(4).测试

7、Real Server 2

(1).安装httpd

[[email protected]]# yum install httpd -y

(2).建立测试页

[[email protected]]# vim /var/www/html/index.html

<h1>real server 2</h1>

(3).开启服务

[[email protected]]# service httpd restart

[[email protected]]# chkconfig httpd on

(4).测试

8、nginx实现后端real server的负载均衡,在Master上配置

(1).修改nginx配置文件

[[email protected] ~]# cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak

[[email protected] ~]# vim /etc/nginx/nginx.conf

user  nginx;

worker_processes  1;

error_log  /var/log/nginx/error.log warn;

pid        /var/run/nginx.pid;

events {

worker_connections  1024;

}

http {

include       /etc/nginx/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  /var/log/nginx/access.log  main;

sendfile        on;

#tcp_nopush     on;

keepalive_timeout  65;

#gzip  on;

upstream apacheweb {

server 10.10.0.225:80 max_fails=3 fail_timeout=2s;

server 10.10.0.221:80 max_fails=3 fail_timeout=2s;

}

server {

listen       80;

server_name  localhost;

#charset koi8-r;

#access_log  /var/log/nginx/log/host.access.log  main;

location / {

root   /usr/share/nginx/html;

index  index.html index.htm;

}

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

root        /var/www/html;  #此处定义后端服务器网页存放路径

proxy_pass   http://apacheweb;

}

#error_page  404              /404.html;

# redirect server error pages to the static page /50x.html

error_page   500 502 503 504  /50x.html;

location = /50x.html {

root   /usr/share/nginx/html;

}

(2).拷贝一份配置文件到Backup

[[email protected] ~]# scp -r /etc/nginx/nginx.conf [email protected]:/etc/nginx/

(3).重启nginx服务

[[email protected] ~]# service nginx restart

[[email protected] ~]# service nginx restart

9、测试

(1).在浏览器中输入vip地址

刷新浏览器之后

(2).模拟故障:

[[email protected]]# service httpd stop

停掉real server 1的apache服务,刷新浏览器发现访问的内容一直是real server2的内容

启用之后real server 1会被加入会话。反之停掉real server 2,访问的内容就一直是real server 1啦

时间: 2024-10-14 01:57:48

keepalived+nginx实现nginx高可用的相关文章

初识keepalived(三)——keepalived与nginx代理实现高可用

背景介绍 nginx除了可以作为web服务器外,同时也是一个反向代理软件,nginx不能完全取代apache,所以很多场景下nginx作为apache的反向代理服务器.nginx运行在用户空间,代替用户向服务器发起请求,请求返回时再经由nginx服务器返回给用户.不同于LVS的NAT模型,web服务器的网关不用必须指向nginx服务器,只要2者之间可以通信就可以,由于nginx作为代理服务器,所以支持端口转换. 网络拓扑 实验拓扑结构如下: 实验系统采用CentOS6.8,nginx采用1.12

利用keepalived实现nginx调度器高可用(一)

利用keepalived实现nginx调度器高可用 声明:提供四台主机,其中两台nginx做前端调度器(一台做主调度器,一台做备用调度器), 另外两台主机做web服务器向外提供http服务: 框架如图: 1.在两台nginx上配置nginx反代服务 # vim /etc/nginx/nginx.conf 在http上下文中添加下文: upstream webser {             server 172.16.1.12:80 weight=1;             server 1

Keepalived+Nginx+Tomcat 实现高可用Web集群(转)

Keepalived+Nginx+Tomcat 实现高可用Web集群 溯水心生 关注 2018.01.08 20:28* 字数 1382 阅读 965评论 1喜欢 9 集群规划图片 一.Nginx的安装过程 1.下载Nginx安装包,安装依赖环境包 (1)安装 C++编译环境 yum -y install gcc #C++ (2)安装pcre yum -y install pcre-devel (3)安装zlib yum -y install zlib-devel (4)安装Nginx 定位到n

Nginx负载均衡高可用

1.   Nginx负载均衡高可用 要实现nginx的高可用,需要实现备份机. 1.1. 什么是负载均衡高可用 nginx作为负载均衡器,所有请求都到了nginx(对外服务的唯一入口,唯一公网IP),可见nginx处于非常重点的位置,如果nginx服务器宕机后端web服务将无法提供服务,影响严重. 为了屏蔽负载均衡服务器的宕机,需要建立一个备份机.主服务器和备份机上都运行高可用(High Availability)监控程序,通过传送诸如“I am alive”这样的信息来监控对方的运行状况.当备

实例:LVS+Keepalived配置LVS的高可用

LVS+Keepalived配置LVS的高可用 我们这里LVS-DR模型的高可用集群: 实验环境:     vm1 LVS-DR1:              eth0 172.16.3.2/16              VIP :eth0:0 172.16.3.88              vm2 LVS-DR2:             eth0 172.16.3.3/16     vm3 Server-web1             RS1: eth0 172.16.3.1/16  

Lvs+keepalived+httpd+NFS搭建高可用

Lvs+keepalived+httpd+NFS搭建高可用 自己捯饬的模型图 NAT模型图 注意事项:RealServer需要把网关指向Director,并且Director要打开转发功能命令如下: echo "1" > /proc/sys/net/ipv4/ip_foreward DR模型图 注意事项:需要在RealServer配置如下信息: echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore echo 1 > /proc

LVS基础及LVS+Keepalived实现双主高可用负载均衡

LVS基础及LVS+Keepalived实现双主高可用负载均衡 LB集群: (Load  Balancing)即负载均衡集群,其目的是为了提高访问的并发量及提升服务器的性能,其    实现方式分为硬件方式和软件方式. 硬件实现方式: 常用的有 F5公司的BIG-IP系列.A10公司的AX系列.Citrix公司的 NetScaler系列等 软件实现方式: LVS工作于传输层.nginx工作于应用层.haproxy工作于传输层和应用层 本文主要讲解LVS. 一.什么是LVS ? 1. LVS:(Li

基于keepalived实现mariadb的高可用

提示: 上一篇博文己经介绍过了keepalived是什么,有那些参数,也介绍过基于corosync+pacemaker实现mairadb高可用,这次我将介绍一下如何利用keepalived对mariadb实现高可用. ----本文大纲 前言 主机环境 配置过程 测试 ----------- 一.前言 说到对mariadb实现高可用,也就是就说,当有任何一个mariadb挂掉之后在还有其它mariadb主机接管业务,完全不会影响到线上的业务,当挂掉的主机修复后重新上线,周而复始的工作,这就要对ma

Keepalived+Haproxy双主高可用负载均衡web和mysql综合实验

日期及版本:2014.5.4v1.0 架构图 实验目的: 1.Haproxy+Keepalived双主双机高可用模型,keepalived为Haproxy主从提供高可用保证haproxy-master若挂掉haproxy-backup能无缝接管,haproxy为后端Web提供负载均衡,缓解并发压力,实现WEB站点负载均衡+高可用性: 2. Haproxy反代web做动静分离: 3. Haproxy反代mysql 算法leastconn和roundrobin的不同效果: 系统环境: OS:cent

基于keepalived搭建MySQL的高可用集群

http://www.cnblogs.com/ivictor/p/5522383.html 基于keepalived搭建MySQL的高可用集群 MySQL的高可用方案一般有如下几种: keepalived+双主,MHA,MMM,Heartbeat+DRBD,PXC,Galera Cluster 比较常用的是keepalived+双主,MHA和PXC. 对于小公司,一般推荐使用keepalived+双主,简单. 下面来部署一下 配置环境: 角色