首页 Keepalived教程实战案例:实现双主的,LVS-DR模式利用FWM绑定成一个双主集群服务

实战案例: 实现双主的LVS-DR模式

实战案例:实现其它应用的高可用性

运维派隶属马哥教育旗下专业运维社区,是国内成立最早的IT运维技术社区,欢迎关注公众号:yunweipai
领取学习更多免费Linux云计算、Python、Docker、K8s教程关注公众号:马哥linux运维

实战案例3:实现双主的LVS-DR模式,利用FWM绑定成一个双主集群服务
[root@ka1-centos8 ~]#vim  /etc/keepalived/keepalived.conf
! Configuration File for keepalived
    global_defs {
        notification_email {
        root@localhost
        }
    notification_email_from kaadmin@localhost
    smtp_server 127.0.0.1
    smtp_connect_timeout 30
    router_id ka1.magedu.org            #在另一个节点为ka2.magedu.org
    vrrp_mcast_group4 224.100.100.100
    }

vrrp_instance VI_1 {
    state MASTER                #在另一个节点为BACKUP
    interface eth0
    virtual_router_id 66
    priority 100                #在另一个节点为80
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 123456
    }
    virtual_ipaddress {
        10.0.0.10/24 dev eth0 label eth0:1
    }
    track_interface {
        eth0
    }
    notify_master "/etc/keepalived/notify.sh master"
    notify_backup "/etc/keepalived/notify.sh backup"
    notify_fault "/etc/keepalived/notify.sh fault"
}
vrrp_instance VI_2 {
    state BACKUP                    #在另一个节点为MASTER
    interface eth0
    virtual_router_id 88
    priority 80                     #在另一个节点为100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 123456
    }
    virtual_ipaddress {
        10.0.0.20/24 dev eth0 label eth0:2
    }
    track_interface {
        eth0
    }
    notify_master "/etc/keepalived/notify.sh master"
    notify_backup "/etc/keepalived/notify.sh backup"
    notify_fault "/etc/keepalived/notify.sh fault"
}

virtual_server fwmark 6 {   #指定FWM为6 
    delay_loop 2
    lb_algo rr
    lb_kind DR
    nat_mask 255.255.255.0
    protocol TCP
    sorry_server 127.0.0.1 80
    real_server 10.0.0.7 80 {
        weight 1
        HTTP_GET {
            url {
                path /
                status_code 200
            }
            connect_timeout 2
            nb_get_retry 3
            delay_before_retry 3
        }
    }
    real_server 10.0.0.17 80 {
        weight 1
        HTTP_GET {
            url {
                path /
                status_code 200
            }
            connect_timeout 2
            nb_get_retry 3
            delay_before_retry 3
        }
    }
}

#两个节点都执行以下操作
[root@ka1-centos8 ~]#iptables -t mangle -A PREROUTING -d 10.0.0.10,10.0.0.20  -p tcp --dport 80 -j MARK --set-mark 6
[root@ka2-centos8 ~]#iptables -t mangle -A PREROUTING -d 10.0.0.10,10.0.0.20  -p tcp --dport 80 -j MARK --set-mark 6

#在RS1和RS2运行下面脚本
[root@rs1 ~]#cat lvs_dr_rs.sh 
#!/bin/bash
#Author:wangxiaochun
#Date:2017-08-13
vip=10.0.0.10
vip2=10.0.0.20
mask='255.255.255.255'
dev=lo:1
dev2=lo:2
rpm -q httpd &> /dev/null || yum -y install httpd &>/dev/null
service httpd start &> /dev/null && echo "The httpd Server is Ready!"
echo "<h1>hostname</h1>" > /var/www/html/index.html

case $1 in
start)
    echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
    echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
    echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
    echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
    ifconfig $dev $vip netmask $mask #broadcast $vip up
    ifconfig $dev2 $vip2 netmask $mask #broadcast $vip up
    #route add -host $vip dev $dev
    echo "The RS Server is Ready!"
    ;;
stop)
    ifconfig $dev down
    ifconfig $dev2 down
    echo 0 > /proc/sys/net/ipv4/conf/all/arp_ignore
    echo 0 > /proc/sys/net/ipv4/conf/lo/arp_ignore
    echo 0 > /proc/sys/net/ipv4/conf/all/arp_announce
    echo 0 > /proc/sys/net/ipv4/conf/lo/arp_announce
    echo "The RS Server is Canceled!"
    ;;
*) 
    echo "Usage: $(basename $0) start|stop"
    exit 1
    ;;
esac
[root@rs1 ~]#bash lvs_dr_rs.sh  start
[root@rs2 ~]#bash lvs_dr_rs.sh  start

#访问测试
[root@centos6 ~]#curl 10.0.0.10;curl 10.0.0.20
<h1>rs1.magedu.org</h1>
<h1>rs2.magedu.org</h1>

同步组

LVS NAT 模型VIP和DIP需要同步,需要同步组

vrrp_sync_group VG_1 {
 group {
    VI_1  # name of vrrp_instance (below)
    VI_2  # One for each moveable IP
   }
  }
 vrrp_instance VI_1 {
    eth0
    vip
  }
vrrp_instance VI_2 {
    eth1
    dip
  }

本文链接:https://www.yunweipai.com/35384.html

实战案例: 实现双主的LVS-DR模式

实战案例:实现其它应用的高可用性

网友评论comments

发表回复

您的电子邮箱地址不会被公开。

暂无评论

Copyright © 2012-2022 YUNWEIPAI.COM - 运维派 京ICP备16064699号-6
扫二维码
扫二维码
返回顶部