keepalived配置
2019-03-06 本文已影响2人
二手三流科学家
给出的虚拟IP为10.0.0.110
-
给出MASTER的配置文件:
! Configuration File for keepalived #这里给出keepalived -v的版本信息 #Keepalived v1.3.5 (03/19,2017), git commit v1.3.5-6-g6fa32f2 global_defs { router_id nginx_load_balance_01 enable_script_security on script_user keepalived_script #不对,原因未知 #default_interface ens33 } #注意,这里一定要给成双引号,单引号不行 #好的策略是BACKUP的priority不变 #MASTER OK的话,优先级不变 #MASTER ERR的话,优先级减小 #再联系下面的check_nginx脚本 #推出weight为负数 vrrp_script check_nginx { script "/home/keepalived_script/check_nginx" weight -7 } vrrp_instance nginx_load_balance { state MASTER interface ens33 virtual_router_id 51 priority 10 advert_int 1 #nopreempt好像得配置在backup机器上 #nopreempt authentication { auth_type PASS auth_pass 111111 } #不好使__start__ #指定要track哪些网卡,一旦这些网卡down了,那么,这个vrrp_instance就进入fault状态 #track_interface { # ens33 # ens34 #} #不好使__end__ #因为只是在网络层面track网卡是不够的,我们还要进行监控某些应用层的东西 track_script { check_nginx } #注意,这里一定要给成双引号,单引号不行 notify_master "/home/keepalived_script/change_to_master" notify_backup "/home/keepalived_script/change_to_backup" notify_fault "/home/keepalived_script/change_to_fault" virtual_ipaddress { 10.0.0.110/8 } }
-
然后是
/home/keepalived_script/check_nginx
脚本:#!/bin/env sh ret=$(ss -tunlp | grep 80 | wc -l) if [ $ret -lt 1 ]; then exit 1 else exit 0 fi
-
然后是BACKUP的配置:
! Configuration File for keepalived #这里给出keepalived -v的版本信息 #Keepalived v1.3.5 (03/19,2017), git commit v1.3.5-6-g6fa32f2 global_defs { router_id nginx_load_balance_02 enable_script_security on script_user keepalived_script } #注意,这里一定要使用双引号 #vrrp_script check_nginx { # script "/home/keepalived_script/check_nginx" # weight -7 #} vrrp_instance nginx_load_balance { state BACKUP interface ens33 virtual_router_id 51 priority 5 advert_int 1 #这个不能打开。这个选项的意思是当MASTER down了以后, #BACKUP不抢占虚拟IP。 #启动时,日志输出中有Preempt disabled #nopreempt on authentication { auth_type PASS auth_pass 111111 } #不好使__start__ #指定要track哪些网卡,一旦这些网卡down了,那么,这个vrrp_instance就进入fault状态 #track_interface { # ens33 # ens34 #} #不好使__end__ #注意,这里一定要给成双引号,单引号不行 notify_master "/home/keepalived_script/change_to_master" notify_backup "/home/keepalived_script/change_to_backup" notify_fault "/home/keepalived_script/change_to_fault" #因为只是在网络层面track网卡是不够的,我们还要进行监控某些应用层的东西 #track_script { # check_nginx #} virtual_ipaddress { 10.0.0.110/8 } }
- 遗留问题:
- global_defs中的default_interface什么意思,会在哪个模块中用到,VRRP,LVS还是SNMP?
- 启动时,日志文件(/var/log/messages)中会有这么一个报错:“PID file /var/run/keepalived.pid not readable (yet?) after start.”,这是为什么?这里给出/usr/lib/systemd/system/keepalived.service中的配置信息:
[Unit] Description=LVS and VRRP High Availability Monitor After=syslog.target network-online.target [Service] Type=forking PIDFile=/var/run/keepalived.pid KillMode=process EnvironmentFile=-/etc/sysconfig/keepalived ExecStart=/usr/sbin/keepalived $KEEPALIVED_OPTIONS ExecReload=/bin/kill -HUP $MAINPID [Install] WantedBy=multi-user.target