hwl198212 发表于 2018-11-8 13:25:00

Keepalived+Nginx架构详解

#全局定义块global_defs {  
      notification_email {
  
   #指定keepalived在发生切换时需要发送email到的邮箱,一行一个
  
         admin@gmail.com
  
      }
  
   notification_email_fromlocalhost#指定发件人
  
   smtp_server 127.0.0.1      #指定smtp服务器地址
  
   smtp_connect_timeout 3   #指定smtp连接超时时间
  
   router_id LVS_DEVEL      #运行keepalived机器的一个标识}
  
#监控Nginx进程vrrp_scriptchk_nginx {
  
    script "/data/script/nginx.sh"   #监控服务脚本地址;
  
    interval 2                   #检测时间间隔(执行脚步间隔)
  
    weight 2}
  
#VRRP实例定义块
  
vrrp_sync_group VG_1{                #监控多个网段的实例
  
      group {
  
VI_1                     #实例名
  
VI_2
  
}
  
notify_master /data/sh/nginx.sh    #指定当切换到master时,执行的脚本
  
notify_backup /data/sh/nginx.sh    #指定当切换到backup时,执行的脚本
  
notify /data/sh/nginx.sh#发生任何切换,均执行的脚本
  
smtp_alert                     #使用global_defs中提供的邮件地址和smtp服务器发送邮件通知}
  
vrrp_instance VI_1 {
  
state BACKUP            #设置主机状态,MASTER|BACKUP
  
nopreempt          #设置为不抢占
  
interface eth0                  #对外提供服务的网络接口
  
lvs_sync_daemon_inteface eth0       #负载均衡器之间监控接口;
  
track_interface{   #设置额外的监控,网卡出现问题都会切换;
  
eth0
  
eth1
  
}
  
mcast_src_ip                #发送多播包的地址,如果不设置默认使用绑定网卡的primary ip
  
    garp_master_delay       #在切换到master状态后,延迟进行gratuitous ARP请求
  
    virtual_router_id 50      #VRID标记 ,路由ID,可通过#tcpdump vrrp查看
  
    priority 90               #优先级,高优先级竞选为master
  
    advert_int 1                #检查间隔,默认1秒
  
    preempt_delay         #抢占延时,默认5分钟
  
    debug                   #debug级别
  
    authentication {      #设置认证
  
      auth_type PASS      #认证方式
  
      auth_pass 22222   #认证密码
  
    }
  track_script {   #以脚本为监控chk_nginx;
  
      chk_nginx
  
    }
  
    virtual_ipaddress {         #设置vip
  
      192.168.111.188
  
    }
  
}
  
注意:使用了脚本监控Nginx或者MYSQL,不需要如下虚拟服务器设置块。
  
#虚拟服务器定义块
  
virtual_server 192.168.111.188 3306 {
  
    delay_loop 6                   #健康检查时间间隔
  
    lb_algo rr                     #调度算法rr|wrr|lc|wlc|lblc|sh|dh
  
    lb_kind DR                     #负载均衡转发规则NAT|DR|RUN
  
    persistence_timeout5      #会话保持时间
  
    protocol TCP                   #使用的协议
  
    real_server 192.168.1.12 3306 {
  
               weight 1            #默认为1,0为失效
  
               notify_up    |#在检测到server up后执行脚本;
  
               notify_down|#在检测到server down后执行脚本;
  
               TCP_CHECK {
  
               connect_timeout 3    #连接超时时间;
  
               nb_get_retry 3      #重连次数;
  
               delay_before_retry 3#重连间隔时间;
  
               connect_port 3306#健康检查的端口的端口;
  
               }
  
            HTTP_GET{
  
            url{                  #检查url,可以指定多个
  
            path /
  
            digest ATM       #检查后的摘要信息
  
            status_code 200       #检查的返回状态码
  
               }
  
      }
  
}


页: [1]
查看完整版本: Keepalived+Nginx架构详解