keepalived初步使用

简介

keepalived是一款用c写的,为linux集群提供负载均衡和高可用的软件.其中负载均衡功能封装了lvs模块,而高可用功能则是采用了vrrp协议


安装

在centos7上,keepalived的安装可以采用rpm包和源码包两种方式

  • rpm包安装
    keepavlived的rpm包直接被收录进了base源,因此可以通过系统光盘安装
]# yum install keepalived
  • 源码包安装
    keepalived的源码包可以在官网keepalived.org上得到
]# yum install curl gcc openssl-devel libnl3-devel net-snmp-devel//编译所需工具
]# wget http://keepalived.org/software/keepalived-1.4.2.tar.gz
]# tar -zxvf keepalived-1.4.2.tar.gz
]# cd keepalived-1.4.2/
]# ./configure --prefix=/usr/local/keepalived
]# make && make install

配置

keepalived的配置文件是keepalived.conf,默认位于/etc/keepalived目录下(yum安装)

]# cat /etc/keepalived/keepalived.conf
    ! Configuration File for keepalived

    global_defs {
       notification_email {//vrrp消息通知的mail地址
            root@localhost
       }
       notification_email_from kaadmin@localhost//发送mail所用的地址
       smtp_server 127.0.0.1//发送mail所用的email服务器

       smtp_connect_timeout 30
       router_id node1//本机id号,同vrrp组唯一
       vrrp_skip_check_adv_addr
       vrrp_garp_interval 0
       vrrp_gna_interval 0
       #vrrp_strict//默认启用,会添加iptables规则导致vip无法访问,因此关闭
       vrrp_mcast_group4 224.10.0.200//vrrp组播地址,同vrrp组需一致(也可不写)
    }

    vrrp_script chk_schedown {//创建vrrp脚本,注意脚本应写在脚本调用语句之前
            script "[[ -f /etc/keepalived/down ]] && exit 1 || exit 0"
            interval 1//脚本检查时间
            weight -2//exit 1后将权重值降低2
    }

    vrrp_instance VI_1 {
        state MASTER//初始化为MASTER服务器,可选值为BACKUP
        interface ens33//默认vip所用网卡
        virtual_router_id 51//虚拟路由id号,同vrrp组需一致
        priority 100//优先级,在抢占模式下优先级大者成为MASTER
        advert_int 1//心跳信息间隔时间,单位为秒
        authentication {//vrrp服务器之间的认证块
            auth_type PASS//认证方式
            auth_pass 1111//认证密码
        }
        virtual_ipaddress {//高可用的ip,即vip
            192.168.10.200/32 label ens33:0
        }
        track_script {//启用脚本
            chk_schedown
    }
        notify_master "/etc/keepalived/notify.sh master"//变为MASTER状态触发
        notify_backup "/etc/keepalived/notify.sh backup"//变为BACKUP状态触发
        notify_fault "/etc/keepalived/notify.sh fault"//默认触发
    }
]# cat notify.sh
#!/bin/bash

vip=192.168.10.200
contact='root@localhost'

notify() {
    mailsubject="`hostname` to be $1: $vip floating"
    mailbody="`date '+%F %H:%M:%S'`: vrrp transition, `hostname` changed to be $1"
    echo $mailbody | mail -s "$mailsubject" $contact
}

case "$1" in
    master)
        notify master
        exit 0
    ;;
    backup)
        notify backup
        exit 0
    ;;
    fault)
        notify fault
        exit 0
    ;;
    *)
        echo 'Usage: `basename $0` {master|backup|fault}'
        exit 1
    ;;
esac

你可能感兴趣的:(学习总结,linux)