others linux服务器运维 django3 监控 k8s golang 数据库 大数据 前端 devops 理论基础 java oracle 运维日志

centos7系统 lvs dr模式 配置

访问量:1305 创建时间:2023-03-20

vip 192.168.10.127

ds1 192.168.10.128 ds2 192.168.10.129 rs1 192.168.10.130 rs2 192.168.10.131

128\129机器,安装

yum -y install ipvsadm keepalived

128机器:

[root@localhost ~]# cat /etc/keepalived/keepalived.conf 
vrrp_instance VI_1 {
    state MASTER        \\初始状态
    interface ens33        \\VIP的网卡
    virtual_router_id 108
    priority 100        \\优先级
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.10.127     \\VIP地址
    }
}

virtual_server 192.168.10.127 80 {    \\虚拟服务器
    delay_loop 6
    lb_algo rr        \\算法
    lb_kind DR        \\模式
    nat_mask 255.255.255.0    \\子网掩码
    protocol TCP        \\虚拟服务器协议

    real_server 192.168.10.130 80 {    \\真实服务器web1的ip地址和端口
        weight 1            \\权重
        TCP_CHECK {            \\健康检查模块
            connect_timeout 3
            connect_port 80
            nb_get_retry 3
            delay_before_retry 3
        }
    }
    real_server 192.168.10.131 80 {    \\真实服务器web2的ip地址和端口
        weight 1            \\权重
        TCP_CHECK {            \\健康检查模块
            connect_timeout 3
            connect_port 80
            nb_get_retry 3
            delay_before_retry 3
        }
    }
}

129机器,安装

[root@localhost ~]# cat /etc/keepalived/keepalived.conf 
vrrp_instance VI_1 {
    state BACKUP        \\初始状态
    interface ens33        \\VIP的网卡
    virtual_router_id 108
    priority 90        \\优先级
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.10.127     \\VIP地址
    }
}

virtual_server 192.168.10.127 80 {    \\虚拟服务器
    delay_loop 6
    lb_algo rr        \\算法
    lb_kind DR        \\模式
    nat_mask 255.255.255.0    \\子网掩码
    protocol TCP        \\虚拟服务器协议

    real_server 192.168.10.130 80 {    \\真实服务器web1的ip地址和端口
        weight 1            \\权重
        TCP_CHECK {            \\健康检查模块
            connect_timeout 3
            connect_port 80
            nb_get_retry 3
            delay_before_retry 3
        }
    }
    real_server 192.168.10.131 80 {    \\真实服务器web2的ip地址和端口
        weight 1            \\权重
        TCP_CHECK {            \\健康检查模块
            connect_timeout 3
            connect_port 80
            nb_get_retry 3
            delay_before_retry 3
        }
    }
}

128\129机器

systemctl restart keepalived
systemctl enable keepalived
#128机器:

[root@localhost ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:24:7a:54 brd ff:ff:ff:ff:ff:ff
    inet 192.168.10.128/24 brd 192.168.10.255 scope global noprefixroute ens33
       valid_lft forever preferred_lft forever
    inet 192.168.10.127/32 scope global ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::fe13:dab5:a004:ed42/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
#129机器
[root@localhost ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:20:0b:4e brd ff:ff:ff:ff:ff:ff
    inet 192.168.10.129/24 brd 192.168.10.255 scope global noprefixroute ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::fe13:dab5:a004:ed42/64 scope link tentative noprefixroute dadfailed 
       valid_lft forever preferred_lft forever
    inet6 fe80::18b2:cb58:6cd1:931/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever

128、129机器,查看规则

[root@localhost ~]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  192.168.10.127:80 rr

130\131机器

[root@localhost network-scripts]# cp ifcfg-lo ifcfg-lo:0
[root@localhost network-scripts]# vim ifcfg-lo:0
DEVICE=lo:0
IPADDR=192.168.10.127
NETMASK=255.255.255.255
ONBOOT=yes
NAME=loopback:0
[root@localhost network-scripts]# ifup lo
[root@localhost network-scripts]# ifconfig lo:0
lo:0: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 192.168.10.127  netmask 255.255.255.255
        loop  txqueuelen 1000  (Local Loopback)
###添加到127的路由
[root@localhost network-scripts]# route add -host 192.168.10.127 dev lo:0

###内核配置

[root@localhost network-scripts]# vim /etc/sysctl.conf
net.ipv4.conf.all.arp_ignore  =  1
net.ipv4.conf.all.arp_announce  =  2
net.ipv4.conf.default.arp_ignore  =  1
net.ipv4.conf.default.arp_announce = 2
net.ipv4.conf.lo.arp_ignore  =  1
net.ipv4.conf.lo.arp_announce  = 2
[root@localhost network-scripts]# sysctl -p

130\131机器,测试结果

[root@localhost ~]# yum -y install httpd
#130机器
[root@localhost network-scripts]# echo "130" >> /var/www/html/index.html
#131机器
[root@localhost network-scripts]# echo "131" >> /var/www/html/index.html
###启动
systemctl start httpd

在ds机器上查看128\129

[root@localhost ~]# ipvsadm -ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  192.168.10.127:80 rr
  -> 192.168.10.130:80            Route   1      0          0         
  -> 192.168.10.131:80            Route   1      0          0       

访问页面http://192.168.10.127 刷新返回不同页面

登陆评论: 使用GITHUB登陆