Linux安装keepalived

葛大爷 互联网 2015-09-16 1419
  1. keepalived安装
    [root@bogon java]# wget http://www.keepalived.org/software/keepalived-1.2.18.tar.gz
    [root@bogon java]# tar -zxvf keepalived-1.2.18.tar.gz
    [root@bogon java]# cd keepalived-1.2.18
    [root@bogon keepalived-1.2.18]# yum -y install ipvsadm
    [root@bogon keepalived-1.2.18]# ./configure
    configure: error:
    !!! OpenSSL is not properly installed on your system. !!!
    !!! Can not include OpenSSL headers files. !!!
    [root@bogon keepalived-1.2.18]# yum -y install openssl-devel
    [root@bogon keepalived-1.2.18]# make
    [root@bogon keepalived-1.2.18]# make install
  2. iptables添加VRRP通讯支持
    [root@bogon keepalived-1.2.18]# vi /etc/sysconfig/iptables
    -A INPUT -d 224.0.0.0/8 -j ACCEPT
    -A INPUT -p vrrp -j ACCEPT
    [root@bogon keepalived-1.2.18]# service iptables restart
    在iptables 中加入对vrrp报文的允许 ,vrrp广播报文使用的地址是224.0.0.18
  3. 将keepalived配置为服务
    [root@bogon keepalived-1.2.18]# cp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
    [root@bogon keepalived-1.2.18]# cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
    [root@bogon keepalived-1.2.18]# ln -s /usr/local/sbin/keepalived /usr/sbin/
    [root@bogon keepalived-1.2.18]# service keepalived start
    正在启动 keepalived: [确定]
    [root@bogon keepalived-1.2.18]# service keepalived stop
    停止 keepalived: [确定]
  4. 设为开机启动
    [root@bogon keepalived-1.2.18]# chkconfig keepalived on
    同上,安装另一服务器;两台服务器keepalived均ok时,继续以下操作;
  5. 修改主服务器配置文件
    ! Configuration File for keepalived  
    global_defs {
    notification_email {
    #admin@michael-demo.com
    }
    #notification_email_from admin@michael-demo.com
    #smtp_server 127.0.0.1
    #smtp_connect_timeout 30
    router_id LVS_DEVEL
    }
    vrrp_script chk_http_port {
    script "</dev/tcp/127.0.0.1/80"
    interval 1
    weight 2
    }
    vrrp_instance VI_1 {
    state MASTER #主MASTER,从BACKUP
    interface eth0 #设置实例绑定的网卡
    virtual_router_id 86 #主从服务器的值要保持一样
    priority 100    #主100从99
    advert_int 1
    authentication {
    auth_type PASS
    auth_pass 1111
    }
    virtual_ipaddress {
    192.168.86.100 #设置vip
    }
    track_script {
    chk_http_port
    }
    }
    #如果服务器端口为80,以下配置就不需要
    virtual_server 192.168.86.100 80 {
    delay_loop 3
    lb_algo rr
    lb_kind DR
    persistence_timeout 50
    protocol TCP
    real_server 192.168.86.130 80 {
    weight 3
    TCP_CHECK {
    connect_timeout 3
    nb_get_retry 3
    delay_before_retry 3
    connect_port 80
    }
    }
    }
  6. 修改从服务器配置文件
     ! Configuration File for keepalived  
    global_defs {
    notification_email {
    #admin@michael-demo.com
    } #notification_email_from admin@michael-demo.com
    #smtp_server 127.0.0.1
    #smtp_connect_timeout 30
    router_id LVS_DEVEL
    }
    vrrp_script chk_http_port {
    script "</dev/tcp/127.0.0.1/80"
    interval 1
    weight 2
    }
    vrrp_instance VI_1 {
    state BACKUP #主MASTER,从BACKUP
    interface eth0 #设置实例绑定的网卡
    virtual_router_id 86 #主从服务器的值要保持一样
    priority 99 #主100从99
    advert_int 1
    authentication {
    auth_type PASS
    auth_pass 1111
    }
    virtual_ipaddress {
    192.168.86.100 #设置vip
    }
    track_script {
    chk_http_port
    }
    }
    #如果服务器端口为80,以下配置就不需要
    virtual_server 192.168.86.100 80 {
    delay_loop 3
    lb_algo rr
    lb_kind DR
    persistence_timeout 50
    protocol TCP
    real_server 192.168.86.131 80 {
    weight 3
    TCP_CHECK {
    connect_timeout 3
    nb_get_retry 3
    delay_before_retry 3
    connect_port 80
    }
    }
    }
  7. 复制配置文件,启动keepalived
    [root@bogon keepalived-1.2.18]# mkdir /etc/keepalived
    [root@bogon keepalived-1.2.18]# cp /usr/local/java/keepalived-1.2.18/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
    [root@bogon keepalived-1.2.18]# service keepalived start
  8. 查看虚拟IP绑定
    [root@bogon keepalived-1.2.18]# ip addr