LVS-Nat模式+Keepalived

四台机器:

​ 两台做lvs-nat模式的keepalived集群

​ 两台做RS(Nginx提供静态页面即可)

lvs-nat模式的keepalived集群,两台
1、首先添加网络适配器,在虚拟机可以看到虚拟机ens37以及其ip地址,必须保证ens37网卡的虚拟ip与真实ip在同一网段
2、lvs的master节点配置文件
[root@lvs-master ~]# yum -y install ipvsadm keepalived
[root@lvs-master keepalived]# cat keepalived.conf
! Configuration File for keepalived
global_defs {
    router_id lvs-keepalived-master
}

vrrp_instance VI_1 {
    state MASTER
    interface ens37
    virtual_router_id 81
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
    10.8.156.111/24
    }
    }

virtual_server 10.8.156.111 80 {
    delay_loop 3
    lb_algo rr
    lb_kind NAT
    nat_mask 255.255.255.0
    protocol TCP
real_server 192.168.245.134 80 {
    weight 1
    inhibit_on_failure
    TCP_CHECK {
        connect_port 80
        connect_timeout 3
    }
}
real_server 192.168.245.135 80 {
    weight 1
    inhibit_on_failure
    TCP_CHECK {
    connect_timeout 3
    connect_port 80
    }
    }
}
vrrp_instance VI_2 {
    state MASTER
    interface ens33
    virtual_router_id 82
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 2222
        }
    virtual_ipaddress {
        192.168.245.112/24
    }
}

3、slave节点
[root@lvs-slave ~]# yum -y install ipvsadm keepalived
[root@lvs-slave keepalived]# cat keepalived.conf
! Configuration File for keepalived
global_defs {
    router_id lvs-keepalived-master
}

vrrp_instance VI_1 {
    state BACKUP
    interface ens37
    virtual_router_id 81
    priority 80
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
    10.8.156.111/24
    }
    }

virtual_server 10.8.156.111 80 {
    delay_loop 3
    lb_algo rr
    lb_kind NAT
    nat_mask 255.255.255.0
    protocol TCP
real_server 192.168.245.134 80 {
    weight 1
    inhibit_on_failure
    TCP_CHECK {
        connect_port 80
        connect_timeout 3
    }
}
real_server 192.168.245.135 80 {
    weight 1
    inhibit_on_failure
    TCP_CHECK {
    connect_timeout 3
    connect_port 80
    }
    }
}
vrrp_instance VI_2 {
    state BACKUP
    interface ens33
    virtual_router_id 82
    priority 80
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 2222
        }
    virtual_ipaddress {
        192.168.245.112/24
    }
}

4、注意开启路由转发
[root@lvs-master ~]# vim /etc/sysctl.conf
net.ipv4.ip_forward = 1
[root@lvs-master ~]# sysctl -p

[root@lvs-slave ~]# vim /etc/sysctl.conf
net.ipv4.ip_forward = 1
[root@lvs-slave ~]# sysctl -p
====================================================================================

两台RS(Nginx服务器)操作

安装nginx服务器

默认网关改为ens33的虚拟ip

[root@test-nginx1 ~]# route add default gw 192.168.245.112  dev ens33
[root@test-nginx2 ~]# route add default gw 192.168.245.112  dev ens33
提示:由于临时添加的方式,网关有可能会消失,消失就添加即可。

访问测试

当两个vip在master节点上时,访问浏览器

总结:如果ip漂移之后,访问不到,

1.查看backup节点是否开启了路由转发

2.查看RS上面的网关有没有消失

你可能感兴趣的:(lvs,运维,nginx)