keealived安装
依赖
yum install -y libnl* yum install -y libnfnetlink-devel zlib zlib-devel gcc gcc-c++ openssl openssl-devel openssh1.上官网下载 .tar.gz任意版本压缩包
2.上传linux ,解压tar xvf keepalived-1.3.5.tar.gz 3.进入解压目录,./configure --prefix=/usr/local/keepalived 指定安装目录。然后make && make install 4. cp /解压目录/keepalived/etc/init.d/keepalived /etc/init.d/keepalived 赋值执行问脚本到service服务 5.mkdir /etc/keepalived 然后赋值配置文件到指定目录 cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf6.cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/keepalived
7.chkconfig keepalived on 设置开机启动
8.service keepalived start 启动
ps: 使用service keepalived start命令启动服务时, 默认会将/etc/sysconfig/keepalived文件中KEEPALIVED_OPTIONS参数作为keepalived服务启动时的参数, 并从/etc/keepalived/目录下加载keepalived.conf配置文件,或用-f参数指定配置文件的位置。或者修改启动脚本里的 keepalived=/usr/local/keepalived/sbin/keepalived 启动脚本 keepalived_config=/usr/local/keepalived/etc/keepalived/keepalived.conf 配置文件地址 keepalived_pid=/usr/local/keepalived/run/keepalived.pid pid没的说配置如下:具体含义可以自己查
! Configuration File for keepalivedglobal_defs {
#notification_email { # acassen@firewall.loc #failover@firewall.loc #sysadmin@firewall.loc #} 不需要邮件发送 #notification_email_from Alexandre.Cassen@firewall.loc #smtp_server 192.168.200.1 #smtp_connect_timeout 30 router_id LVS_DEVEL #vrrp_skip_check_adv_addr #vrrp_strict 暂不需要这些参数 #vrrp_garp_interval 0 #vrrp_gna_interval 0 } vrrp_script check_nginx { script "/etc/keepalived/check_nginx.sh" ##监控脚本 interval 2 ##时间间隔,2秒 weight -10 ##权重weight 为正时,脚本检测成功时此weight会加到priority上 ,weight为负时,脚本检测成功时此weight不影响priority,检测失败时priority – abs(weight) fall 2 rise 1 }vrrp_instance VI_1 {
state MASTER interface em1 mcast_src_ip 192.168.17.32 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } track_script { check_nginx #监控脚本 } virtual_ipaddress { 192.168.18.200 } }备用节点修改mcast_src_ip为自己ip ,priority值小于主节点 99即可,注意跟权重的配合。
修改state=BACKUPcheck_nginx.sh 最靠谱的方式应该检查某个url是否返回值
#!/bin/bash # 如果进程中没有nginx则将keepalived进程kill掉 A=`ps -C nginx --no-header |wc -l` ## 查看是否有 nginx进程 把值赋给变量A if [ $A -eq 0 ];then ## 如果没有进程值得为 零 service keepalived stop ## 则结束 keepalived 进程 fi 双主模式只需要再建一个vrrp_instance 将slave 变成master master里配置 slave即可。注意ip不能重复。