lvs模式配置

NAT模式配置

准备三台主机,两台rs,一台链路器

lvs配置

首先需要为lvs主机添加一块新网卡

两块网卡一块为nat模式另一块为仅主机模式

lvs模式配置_第1张图片

添加完网卡后重启虚拟机,即可看到多出一块网卡,并且网段与第一块网卡不同

[root@lvs ~]# ip a
1: lo:  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: ens160:  mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:1f:07:bc brd ff:ff:ff:ff:ff:ff
    inet 192.168.80.129/24 brd 192.168.80.255 scope global dynamic noprefixroute ens160
       valid_lft 1083sec preferred_lft 1083sec
    inet6 fe80::20c:29ff:fe1f:7bc/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
3: ens192:  mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:1f:07:c6 brd ff:ff:ff:ff:ff:ff
    inet 192.168.136.130/24 brd 192.168.136.255 scope global dynamic noprefixroute ens192
       valid_lft 1083sec preferred_lft 1083sec
    inet6 fe80::405a:7753:b874:c91e/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever

配置网卡

[root@lvs network-scripts]# cp ifcfg-ens160 ifcfg-ens192
[root@lvs network-scripts]# vim ifcfg-ens192 
TYPE=Ethernet
BOOTPROTO=static
NAME=ens192
DEVICE=ens192
ONBOOT=yes
IPADDR=192.168.136.130
NETMASK=255.255.255.0
//不用配置网关
//可ping通外网
[root@lvs network-scripts]# ping www.baidu.com
PING www.baidu.com (36.152.44.95) 56(84) bytes of data.
64 bytes from 36.152.44.95 (36.152.44.95): icmp_seq=1 ttl=128 time=22.5 ms
64 bytes from 36.152.44.95 (36.152.44.95): icmp_seq=2 ttl=128 time=22.4 ms
64 bytes from 36.152.44.95 (36.152.44.95): icmp_seq=3 ttl=128 time=23.1 ms
64 bytes from 36.152.44.95 (36.152.44.95): icmp_seq=4 ttl=128 time=22.8 ms

配置RS

//配置RS1
[root@RS1 ~]# yum -y install httpd
[root@RS1 ~]# apachectl start
[root@RS1 ~]# ss -antl
State      Recv-Q     Send-Q          Local Address:Port           Peer Address:Port     
LISTEN     0          128                   0.0.0.0:22                  0.0.0.0:*        
LISTEN     0          128                      [::]:22                     [::]:*        
LISTEN     0          80                          *:3306                      *:*        
LISTEN     0          128                         *:80                        *:* 

[root@RS1 ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens160 
TYPE=Ethernet
BOOTPROTO=static
NAME=ens160
DEVICE=ens160
ONBOOT=yes
IPADDR=192.168.80.128
NETMASK=255.255.255.0
GATEWAY=192.168.80.129   //网关指向lvsIP地址,并且重启网卡
DNS1=114.114.114.114

[root@RS1 ~]# ifdown ens160;ifup ens160
成功停用连接 "ens160"(D-Bus 活动路径:/org/freedesktop/NetworkManager/ActiveConnection/5)
连接已成功激活(D-Bus 活动路径:/org/freedesktop/NetworkManager/ActiveConnection/6)


//配置RS2
[root@RS2 ~]# systemctl start nginx
[root@RS2 ~]# vi /etc/sysconfig/network-scripts/ifcfg-ens160
TYPE=Ethernet
BOOTPROTO=static
NAME=ens160
DEVICE=ens160
ONBOOT=yes
IPADDR=192.168.80.130
NETMASK=255.255.255.0
GATEWAY=192.168.80.129
DNS1=114.114.114.114
[root@RS2 ~]# ifdown ens160;ifup ens160
成功停用连接 "ens160"(D-Bus 活动路径:/org/freedesktop/NetworkManager/ActiveConnection/3)
连接已成功激活(D-Bus 活动路径:/org/freedesktop/NetworkManager/ActiveConnection/4)

配置lvs

[root@lvs ~]# vi /etc/sysctl.conf
[root@lvs ~]# cat /etc/sysctl.conf
# sysctl settings are defined through files in
# /usr/lib/sysctl.d/, /run/sysctl.d/, and /etc/sysctl.d/.
#
# Vendors settings live in /usr/lib/sysctl.d/.
# To override a whole file, create a new file with the same in
# /etc/sysctl.d/ and put new settings there. To override
# only specific settings, add a file with a lexically later
# name in /etc/sysctl.d/ and put new settings there.
#
# For more information, see sysctl.conf(5) and sysctl.d(5).
net.ipv4.ip_forward = 1  //ip转发
[root@lvs ~]# sysctl -p
net.ipv4.ip_forward = 1

[root@lvs ~]# yum -y install ipvsadm
[root@lvs ~]# ipvsadm -A -t 192.168.136.130:80 -s rr
[root@lvs ~]# ipvsadm -a -t 192.168.136.130:80 -r 192.168.80.128 -m
[root@lvs ~]# ipvsadm -a -t 192.168.136.130:80 -r 192.168.80.130 -m
[root@lvs ~]# 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.136.130:80 rr
  -> 192.168.80.128:80            Masq    1      0          0         
  -> 192.168.80.130:80            Masq    1      0          0

使用终端访问lvs ip——192.168.136.130

观察到前后两次访问内容有变化即成功配置——访问到两个网站

[root@lvs ~]# curl 192.168.136.130



    
        Test Page for the Nginx HTTP Server on Red Hat Enterprise Linux
        
        
    

    
        

Welcome to nginx on Red Hat Enterprise Linux!

This page is used to test the proper operation of the nginx HTTP server after it has been installed. If you can read this page, it means that the web server installed at this site is working properly.

Website Administrator

This is the default index.html page that is distributed with nginx on Red Hat Enterprise Linux. It is located in /usr/share/nginx/html.

You should now put your content in a location of your choice and edit the root configuration directive in the nginx configuration file /etc/nginx/nginx.conf.

For information on Red Hat Enterprise Linux, please visit the Red Hat, Inc. website. The documentation for Red Hat Enterprise Linux is available on the Red Hat, Inc. website.

[ Powered by nginx ] [ Powered by Red Hat Enterprise Linux ]
[root@lvs ~]# curl 192.168.136.130 HTTP Server Test Page powered by CentOS