nginx + keepalived 双机热备

双机热备是指两台机器都在运行,但并非两台机器同时在提供服务。
当提供服务的一台出现故障的时候,另外一台会马上自动接管并且提供服务,且切换的时间非常短。

keepalived的工作原理是VRRP——虚拟路由冗余协议。

测试环境如下:


ip


vip


master


192.168.174.135


192.168.174.140


backup


192.168.174.137


192.168.174.140

nginx

安装

sudo apt-get install nginx 

查找配置文件位置

sudo find / -name nginx.conf
/etc/nginx/nginx.conf

修改配置文件(nginx.conf)

user www-data;
worker_processes 4;
pid /run/nginx.pid;

events {
    worker_connections 1024;
}

http {
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    server {
        listen 80 default_server;
        server_name test;
        charset utf-8;

        location / {
        root html;
        index index.html index.htm;
        proxy_set_header X-Real_IP $remote_addr;
        client_max_body_size 100m;
        }
    }
}

文件/usr/share/nginx/html/index.html

在192.168.174.135上加上  <h1>Welcome to nginx!  135  </h1>

在192.168.174.137上加上  <h1>Welcome to nginx!   ***137***   </h1>

启动

sudo service nginx start  

关闭

sudo service nginx stop

keepalived

安装

下载keepalived-1.2.19.tar.gz

tar –zxvf keepalived-1.2.19.tar.gz
cd keepalived-1.2.19
./configure --prefix=/usr/local/keepalived
make
sudo make install

期间可能出现问题:

!!! OpenSSL is not properly installed on your system. !!!
!!! Can not include OpenSSL headers files. !!!

解决

sudo apt-get install libssl.dev

建立软链接

sudo ln -s /usr/local/keepalived/sbin/keepalived /sbin/
sudo ln -s /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
sudo ln -s /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/

启动

sudo keepalived -D -f /usr/local/keepalived/etc/keepalived/keepalived.conf

关闭

sudo killall keepalived

配置(keepalived.conf):

global_defs {
    router_id NODEA
}

vrrp_instance VI_1 {
    state MASTER
    interface eth0        #监测网络接口
    virtual_router_id 50  #主、备必须一样
    priority 100          #优先级:主>备
    advert_int 1
    authentication {
        auth_type PASS #VRRP认证,主备一致
        auth_pass 1111  #密码
}

virtual_ipaddress {
        192.168.174.140/24 #VRRP HA虚拟地址
    }
}

备用节点的配置

global_defs {
   router_id NODEB
}

vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 50
    priority 90
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }

 virtual_ipaddress {
    192.168.174.140/24
    }
}

测试

双击热备

两台机子均启动nginx和keepalived,浏览器各自访问

浏览器访问:http://192.168.174.140/,显示的是MASTER的页面。

同样用ip appr可以验证:

135机器:

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default

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

inet 127.0.0.1/8 scope host lo

valid_lft forever preferred_lft forever

inet6 ::1/128 scope host

valid_lft forever preferred_lft forever

2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000

link/ether 00:0c:29:39:d4:88 brd ff:ff:ff:ff:ff:ff

inet 192.168.174.135/24 brd 192.168.174.255 scope global eth0

valid_lft forever preferred_lft forever

inet 192.168.174.140/24 scope global secondary eth0

valid_lft forever preferred_lft forever

inet6 fe80::20c:29ff:fe39:d488/64 scope link

valid_lft forever preferred_lft forever

137机器:

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default

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

inet 127.0.0.1/8 scope host lo

valid_lft forever preferred_lft forever

inet6 ::1/128 scope host

valid_lft forever preferred_lft forever

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

link/ether 00:0c:29:cf:23:62 brd ff:ff:ff:ff:ff:ff

inet 192.168.174.137/24 brd 192.168.174.255 scope global eth0

valid_lft forever preferred_lft forever

inet6 fe80::20c:29ff:fecf:2362/64 scope link

valid_lft forever preferred_lft forever

现在关闭135机器的keepalived。

但当nginx宕掉或整个机子宕机后,这种情况不行了——通过浏览器访问192.168.174.140访问不到资源。

nginx宕掉/机器宕掉热备

为了解决上一问题,可以利用脚本,当检测到nginx进程宕掉后,自动关闭keepalived进程,从而实现热备份。

主节点的配置

global_defs {
    router_id NODEA
}

vrrp_script chk_http_port {
    script "/home/jimite/keepalived/chk_nginx_pid.sh"
    interval 2
    weight 2
}

vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 50
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    track_script {
        chk_http_port
    }
    virtual_ipaddress {
        192.168.174.140/24
    }
}

备用节点的配置

global_defs {
   router_id NODEB
}

vrrp_script chk_http_port {
    script "/home/jihite/keepalived/chk_nginx_pid.sh"
    interval 2
    weight 2
}

vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 50
    priority 90
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
   track_script {
       chk_http_port
   }

    virtual_ipaddress {
    192.168.174.140/24
    }
}

其中/home/jimite/keepalived/chk_nginx_pid.sh为

#!/bin/bash
A=`ps -C nginx --no-header |wc -l`
if [ $A -eq 0 ]
then
    echo ‘nginx server is died‘
    sudo killall keepalived
fi
时间: 2024-10-14 06:54:34

nginx + keepalived 双机热备的相关文章

Nginx+keepalived双机热备+负载均衡 ???待续

keepalived+nginx双机热备+负载均衡 最近因业务扩展,需要将当前的apache 转为nginx(web), 再在web前端放置nginx(负载均衡).同时结合keepalived 对前端nginx实现HA.nginx进程基于于Master+Slave(worker)多进程模型,自身具有非常稳定的子进程管理功能.在Master进程分配模式下,Master进程永远不进行业务处理,只是进行任务分发,从而达到Master进程的存活高可靠性,Slave(worker)进程所有的业务信号都 由

Nginx + keepalived 双机热备(主从模式)

双机高可用一般是通过虚拟IP(飘移IP)方法来实现的,基于Linux/Unix的IP别名技术. 双机高可用方法目前分为两种: 1)双机主从模式:即前端使用两台服务器,一台主服务器和一台热备服务器,正常情况下,主服务器绑定一个公网虚拟IP,提供负载均衡服务,热备服务器处于空闲状态:当主服务器发生故障时,热备服务器接管主服务器的公网虚拟IP,提供负载均衡服务:但是热备服务器在主机器不出现故障的时候,永远处于浪费状态,对于服务器不多的网站,该方案不经济实惠. 2)双机主主模式:即前端使用两台负载均衡服

Nginx+Keepalived(双机热备)搭建高可用负载均衡环境(HA)

原文:https://my.oschina.net/xshuai/blog/917097 摘要: Nginx+Keepalived搭建高可用负载均衡环境(HA) http://blog.csdn.net/xyang81/article/details/52554398可以看更多介绍 Keepalived的介绍可以百度一堆一堆的资料.一定要看看哦. 1.基于上一篇博客总结,再次安装一个虚拟机当backup服务器,这个服务器只安装Keepalived+Nginx即可 2.Master还是上一篇博文的

Nginx+keepalived双机热备

一:环境介绍: Nginx Master: 122.112.X.XNginx Backup: 211.101.X.XVIP:Keepalived 1.2.13Nginx安装见nginx安装配置文档Master与Backup的Nginx配置一致.在MASTER跟BACKUP上安装Keepalived: 二:安 装:\\ 两台接入服务器分别安装NginX和keepalived: #tar zxvf keepalived-1.2.13.tar.gz   #cd /usr/local/keepalive

Nginx+keepalived双机热备(主主模式)

IP说明: master机器(master-node):10.0.0.5/172.16.1.5   VIP1:10.0.0.3slave机器(slave-node): 10.0.0.6/172.16.1.6   VIP2:10.0.0.4 注意事项: 双主配置:MASTER-BACKUP和BACKUP-MASTER; 如果是三主,就是MATER-BACKUP-BACKUP.BACKUP-MASTER-BACKUP和BACKUP-BACKUP-MASTER; 配置中的虚拟路由标识virtual_r

keepalived双机热备nginx

nginx目前是我最常用的反向代理服务,线上环境为了能更好的应对突发情况,一般会使用keepalived双机热备nginx或者使用docker跑nginx集群,keepalived是比较传统的方式,虽然用docker跑nginx集群更方便,但传统的方式总是有他的可取之处,并且多学一些东西也很好.以后也会写如何使用docker跑nginx集群. 环境准备: 2台centos: 192.168.0.105 和192.168.0.118, 虚拟IP(VIP)为192.168.0.119 配置keepa

&#8203;Keepalived双机热备

Keepalived双机热备 Keepalived是专门针对LVS设计的一款强大的辅助工具,主要用来提供故障切换和健检查功能,如判断LVS负载调度器.节点服务器的可用性,及时隔离并替换为新的服务器,当故障主机恢复后将其重新加入群集.在非LVS群集环境中使用时Keepalived也可以作为热备软件使用. Keepalived采用VRRP(virtual  router  redundancy protocol,虚拟路由冗余协议)热备份协议,以软件的方式实现linux服务器的多机热备功能.VRRP是

Centos 7 keepalived双机热备

Centos 7 keepalived双机热备 操作环境:keepalived热备份(双机热备)Centos 7系统ip地址: 192.168.80.100 --主服务器Centos 7系统ip地址: 192.168.80.101 --从服务器windows系统ip地址: 192.168.80.20 --测试客户端 Centos 7系统ip地址: 192.168.80.100操作如下:1. keepalived软件安装yum install keepalived -y 2.修改配置文件vi /e

centos 7之keepalived双机热备理论+配置文件详解

一.keepalived工作原理及作用: keepalived最初是专门针对LVS设计的一款强大的辅助工具,主要用来提供故障切换和健康检 查功能--判断LVS负载调度器.节点服务器的可用性,及时隔离并替换为新的服务器,当故 障主机回复后将其重新加入群集.单独部署LVS环境的话,调度器发生宕机的话,整个群集就 失效了,某一个web节点宕机后,客户端在访问时,总会碰上访问不到网页的情况,所以,将 keepalived和LVS结合起来,才可形成一个真正的高可用群集,当然,后端的共享存储也必须 搭建一个