zoukankan      html  css  js  c++  java
  • keepalived实现nginx的高可用

    1.使用yum安装keepalived

    yum install keepalived -y

    2.修改配置文件keepalived.conf

    主服务器配置文件

    global_defs {
        router_id NodeA
    }
    vrrp_script check_run {
        script "/etc/keepalived/check_nginx.sh"  #自定义检查nginx的脚本
        interval 2
        weight 2
    }
    vrrp_instance VI_1 {
        state MASTER    #设置为主服务器
        interface eth0  #监测网络接口
        virtual_router_id 51  #虚拟路由标识,主、备必须一样
        priority 100   #(主、备机取不同的优先级,主机值较大,备份机值较小,值越大优先级越高)
        advert_int 1   #VRRP Multicast广播周期秒数
        nopreempt #恢复后也不抢占资源
    authentication { auth_type PASS #VRRP认证方式,主备必须一致 auth_pass 1111 #(密码) } track_script { check_run } virtual_ipaddress {   虚拟IP
    /掩码 #VRRP HA虚拟地址 } }

    检查nginx服务状态的脚本,check_nginx.sh 

    #!/bin/bash
    netstat -npl|grep -q nginx
    if [[ $? -ne 0 ]]; then
      /usr/local/nginx/sbin/nginx
      if [[ $? -ne 0 ]]; then
        service keepalived stop
      fi
    fi

    备用服务器配置文件

    global_defs {
        router_id NodeB
    }
    vrrp_script check_run{
        script "/etc/keepalived/check_nginx.sh"
        interval 2
        weight 2
    }
    vrrp_instance VI_1 {
        state BACKUP    #设置为备服务器
        interface eth0  #监测网络接口
        virtual_router_id 51  #虚拟路由标识,主、备必须一样
        priority 90   #(主、备机取不同的优先级,主机值较大,备份机值较小,值越大优先级越高)
        advert_int 1   #VRRP Multicast广播周期秒数
        nopreempt #恢复后也不抢占资源
    authentication { auth_type PASS #VRRP认证方式,主备必须一致 auth_pa
    ss 1111 #(密码) } track_script { check_run } virtual_ipaddress {   虚拟IP/掩码 #VRRP HA虚拟地址 } }

    3.主、备服务器启动keepalived和nginx服务

     /etc/init.d/keepalived start
     /etc/init.d/nginx start

    查看主服务器虚拟IP是否存在

    ip addr

    关闭主服务器的keepalived服务,测试虚拟IP是否切换到备用服务器上

    ps:

    当主备服务器都出现了VIP,可能是网络环境不支持广播方式,此时要使用单播方式

    unicast_src_ip 10.205.22.185    #本机IP
    unicast_peer {
             10.205.22.186  #备机IP
         }
  • 相关阅读:
    重新认识布局:html和body元素
    重新认识布局:3d空间中的css盒子
    重新认识布局:百分比单位
    重新认识布局:标准流,浮动,定位的关系
    Redis(1.7)Redis高可用架构与数据库交互(理论篇)
    C++: 模块定义文件声明(.def)的使用
    HttpListener supports SSL only for localhost? install certificate
    跨域请求引起的 OPTIONS request
    html 浏览器自动加上 标签的详解
    c# HttpServer 的使用
  • 原文地址:https://www.cnblogs.com/wsl222000/p/5506093.html
Copyright © 2011-2022 走看看