首页 > 代码库 > 02-keepalived实现对nginx服务的高可用(主备)

02-keepalived实现对nginx服务的高可用(主备)

实验环境:controller3,controller4为后端web服务器,controller1,controller2为nginx负载均衡服务器,用keepalived实现主备模式的高可用

controller1  IP:9.110.187.120  10.1.1.120

controller2  IP:9.110.187.121  10.1.1.121

controller3  IP:10.1.1.122

controller4  IP:10.1.1.123

1.controller3,controller4安装httpd,提供简单的测试页面

技术分享技术分享

2.controller1,controller2安装nginx,并配置后端服务器组

controller2配置和效果

技术分享

技术分享

controller1配置同controller2

3.在controller1上配置keepalived,实现对nginx的高可用

! Configuration File for keepalived

global_defs {
   notification_email {
        root@localhost    #通知邮箱
   }
   notification_email_from keepalived@localhost
   smtp_server 127.0.0.1    #邮箱服务器
   smtp_connect_timeout 30    #邮箱超时时间
   router_id controller1    #本机route ID
   vrrp_mcast_group4 224.0.32.18 #组播地址
}

vrrp_instance keepnginx {    #自定义虚拟路由实例
    state MASTER    #本机虚拟路由状态为master
    interface eno16777736    #心跳传递地址
    virtual_router_id 60    #虚拟路由id
    priority 100    #权重100
    advert_int 1    #1秒检测
    authentication {    #检测方式密码,123456
        auth_type PASS
        auth_pass 123456
    }
    virtual_ipaddress {
        9.110.187.60/24    dev    eno16777736      label   eno16777736:1 #虚拟ip,虚拟ip所在网卡(内网)
    }
}

track_interface {    #追踪检测网卡
        eno16777736  #外网网卡提供访问
        eno33554960  #内网网卡访问后端WEB服务器
}
        

controller2设置为备用

! Configuration File for keepalived

global_defs {
   notification_email {
        root@localhost
   }
   notification_email_from keepalived@localhost
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id controller2
}

vrrp_instance keepnginx {
    state BACKUP
    interface eno16777736
    virtual_router_id 60
    priority 99
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 123456
    }
    virtual_ipaddress {
        9.110.187.60/24 dev     eno16777736     label   eno16777736:1
    }
}

track_interface {
        eno16777736
        eno33554960
}

此时虚拟的Ip 9.110.187.60 因为controller1权重100运行在controller1上,停止controller1的keepalived服务后转移至controller2上,访问9.110.187.60 可轮询方式访问后端vipserver

 

02-keepalived实现对nginx服务的高可用(主备)