请借用上次NAT实验环境,同样是配置DR模式从头到尾
-
LVS DR模式,LVS主机和web服务器都是单网卡。它们连在同一网络中
-
修改实验环境
- client1:eth0-> 192.168.88.10
- lvs1:eth0->192.168.88.5,删除eth1的IP
- web1:eth0->192.168.88.100,删除eth1的IP
- web2:eth0->192.168.88.200,删除eth1的IP
# 删除lvs虚拟服务器配置
[root@lvs1 ~]# ipvsadm -D -t 192.168.88.5:80
[root@lvs1 ~]# ipvsadm -Ln
# 删除lvs1上eth1的配置
[root@lvs1 ~]# nmcli connection modify eth1 ipv4.method disabled ipv4.addresses ''
[root@lvs1 ~]# nmcli connection down eth1
# 修改web1的配置:停掉eth1的地址。配置eth0的地址为192.168.88.100
# 进入网卡配置文件目录
[root@web1 ~]# cd /etc/sysconfig/network-scripts/
# eth0网卡的配置文件叫ifcfg-eth0
[root@web1 network-scripts]# ls ifcfg-eth*
ifcfg-eth0 ifcfg-eth1
# 配置eth0地址
[root@web1 network-scripts]# vim ifcfg-eth0
TYPE=Ethernet # 网络类型为以太网
BOOTPROTO=none # IP地址是静态配置的,也可以用static
NAME=eth0 # 为设备重命名
DEVICE=eth0 # 网卡设备名
ONBOOT=yes # 开机激活网卡
IPADDR=192.168.88.100 # IP地址
PREFIX=24 # 子网掩码长度
GATEWAY=192.168.88.254 # 网关
[root@web1 ~]# systemctl restart NetworkManager # 重启网络服务
# 在web1上停掉eth1
[root@web1 ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth1
TYPE=Ethernet
BOOTPROTO=none
NAME=eth1
DEVICE=eth1
ONBOOT=no
[root@web1 ~]# nmcli connection down eth1 # 终端卡住,关掉它,在新终端重新连
# 修改web2的网络
[root@web2 ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0
TYPE=Ethernet
BOOTPROTO=none
NAME=eth0
DEVICE=eth0
ONBOOT=yes
IPADDR=192.168.88.200
PREFIX=24
GATEWAY=192.168.88.254
[root@web2 ~]# systemctl restart NetworkManager
[root@web2 ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth1
TYPE=Ethernet
BOOTPROTO=none
NAME=eth1
DEVICE=eth1
ONBOOT=no
[root@web2 ~]# nmcli connection down eth1
# 修改pubserver的主机清单文件
[root@pubserver cluster]# cp inventory inventory.bak
[root@pubserver cluster]# vim inventory
[clients]
client1 ansible_host=192.168.88.10
[webservers]
web1 ansible_host=192.168.88.100
web2 ansible_host=192.168.88.200
[lb]
lvs1 ansible_host=192.168.88.5
[all:vars]
ansible_ssh_user=root
ansible_ssh_pass=a
# 修改2台web服务器yum配置文件中的地址
[root@web1 ~]# sed -i 's/99/88/' /etc/yum.repos.d/local99.repo
[root@web1 ~]# cat /etc/yum.repos.d/local99.repo
[BaseOS]
name = BaseOS
baseurl = ftp://192.168.88.240/dvd/BaseOS
enabled = 1
gpgcheck = 0
[AppStream]
name = AppStream
baseurl = ftp://192.168.88.240/dvd/AppStream
enabled = 1
gpgcheck = 0
[rpms]
name = rpms
baseurl = ftp://192.168.88.240/rpms
enabled = 1
gpgcheck = 0
配置LVS DR模式
1.在lvs1的eth0上配置vip 192.168.88.15
[root@pubserver cluster]# vim 05-config-lvsvip.yml
---
- name: config lvs vip
hosts: lb
tasks:
- name: add vip
lineinfile: # 确保文件中有某一行内容
path: /etc/sysconfig/network-scripts/ifcfg-eth0
line: IPADDR2=192.168.88.15
notify: restart eth0 # 通知执行handlers中的任务
handlers: # 被通知执行的任务写到这里
- name: restart eth0
shell: nmcli connection down eth0; nmcli connection up eth0
[root@pubserver cluster]# ansible-playbook 05-config-lvsvip.yml
# 在lvs1查看添加的IP地址
[root@lvs1 ~]# ip a s eth0 | grep 88
inet 192.168.88.5/24 brd 192.168.88.255 scope global noprefixroute eth0
inet 192.168.88.15/24 brd 192.168.88.255 scope global secondary noprefixroute eth0
2.在2台web服务器的lo上配置vip 192.168.88.15。lo:0
网卡需要使用network-scripts
提供的配置文件进行配置
[root@pubserver cluster]# vim 06-config-webvip.yml
---
- name: config webservers vip
hosts: webservers
tasks:
- name: install network-scripts # 安装服务
yum:
name: network-scripts
state: present
- name: add lo:0 # 创建lo:0的配置文件
copy:
dest: /etc/sysconfig/network-scripts/ifcfg-lo:0
content: |
DEVICE=lo:0
NAME=lo:0
IPADDR=192.168.88.15
NETMASK=255.255.255.255
NETWORK=192.168.88.15
BROADCAST=192.168.88.15
ONBOOT=yes
notify: activate lo:0
handlers:
- name: activate lo:0 # 激活网卡
shell: ifup lo:0
[root@pubserver cluster]# ansible-playbook 06-config-webvip.yml
# 查看结果
[root@web1 ~]# cd /etc/sysconfig/network-scripts/
[root@web1 network-scripts]# cat ifcfg-lo:0
DEVICE=lo:0
NAME=lo:0
IPADDR=192.168.88.15
NETMASK=255.255.255.255
NETWORK=192.168.88.15
BROADCAST=192.168.88.15
ONBOOT=yes
[root@web1 network-scripts]# ifconfig # 可以查看到lo:0网卡信息
lo:0: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 192.168.88.15 netmask 255.255.255.255
loop txqueuelen 1000 (Local Loopback)
3.在2台web服务器上配置内核参数,使得它们不响应对192.168.88.15的请求
[root@web1 ~]# sysctl -a | grep arp_ignore
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.lo.arp_ignore = 0
[root@web1 ~]# sysctl -a | grep arp_announce
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.lo.arp_announce = 0
[root@web1 ~]# vim /etc/sysctl.conf
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.lo.arp_announce = 2
[root@web1 ~]# sysctl -p
[root@web2 ~]# vim /etc/sysctl.conf
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.lo.arp_announce = 2
[root@web2 ~]# sysctl -p
4.在lvs1上配置虚拟服务器
# 创建虚拟服务器
[root@lvs1 ~]# ipvsadm -A -t 192.168.88.15:80 -s wlc
# 向虚拟服务器中加真实服务器
[root@lvs1 ~]# ipvsadm -a -t 192.168.88.15:80 -r 192.168.88.100 -w 1 -g
[root@lvs1 ~]# ipvsadm -a -t 192.168.88.15:80 -r 192.168.88.200 -w 2 -g
# 查看配置
[root@lvs1 ~]# ipvsadm -Ln
# 客户验证
[root@client1 ~]# for i in {1..6}; do curl http://192.168.88.15/; done
Welcome from web2
Welcome from web1
Welcome from web2
Welcome from web2
Welcome from web1
Welcome from web2
附:出错时,排错步骤:
# 在lvs上可以访问到web服务器
[root@lvs1 ~]# curl http://192.168.88.100/
192.168.99.100
[root@lvs1 ~]# curl http://192.168.88.200/
apache web server2
# 查看vip
[root@lvs1 ~]# ip a s eth0 | grep 88
inet 192.168.88.5/24 brd 192.168.88.255 scope global noprefixroute eth0
inet 192.168.88.15/24 brd 192.168.88.255 scope global secondary noprefixroute eth0
[root@web1 ~]# ifconfig lo:0
lo:0: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 192.168.88.15 netmask 255.255.255.255
loop txqueuelen 1000 (Local Loopback)
# 查看内核参数
[root@web1 ~]# sysctl -p
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.lo.arp_announce = 2
# 查看规则
[root@lvs1 ~]# 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.88.15:80 wlc
-> 192.168.88.100:80 Route 1 0 12
-> 192.168.88.200:80 Route 2 0 18