- LVS-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
- web2:eth0->192.168.88.200
Ansible配置
## 调整Ansible配置
# 调整主机清单文件,将webservers主机组内主机IP地址为88段
[root@pubserver ~]# cd cluster/
[root@pubserver cluster]# vim inventory
[clients]
client ansible_ssh_host=192.168.88.10
[webservers]
web1 ansible_ssh_host=192.168.88.100
web2 ansible_ssh_host=192.168.88.200
[lbs]
lvs1 ansible_ssh_host=192.168.88.5
[all:vars]
ansible_ssh_port=22
ansible_ssh_user=root
ansible_ssh_pass=a
[root@pubserver cluster]# ansible all -m ping
[root@pubserver cluster]#
# 配置各主机yum源
[root@pubserver cluster]# vim 05_config_yum.yml
[root@pubserver cluster]# cat 05_config_yum.yml
---
- name: config yum
hosts: all
tasks:
- name: rm dir #删除目录
file:
path: /etc/yum.repos.d/
state: absent
- name: create dir #创建目录
file:
path: /etc/yum.repos.d/
state: directory
mode: '0755'
- name: copy repo #发送repo文件
copy:
src: files/local88.repo
dest: /etc/yum.repos.d/
[root@pubserver cluster]# ansible-playbook 05_config_yum.yml
# 配置webservers主机内主机web服务
[root@pubserver cluster]# ansible-playbook 02_config_web.yml
[root@pubserver cluster]# curl http://192.168.88.100
Welcome to web1
[root@pubserver cluster]# curl http://192.168.88.200
Welcome to web2
[root@pubserver cluster]#
配置测试DR模式
## 配置DR模式
# 配置虚拟IP地址(VIP)
[root@pubserver cluster]# vim 06_config_dr_vip.yml
---
- name: install soft #安装network-scripts软件
hosts: lbs,webservers
tasks:
- name: install network-scripts #安装软件
yum:
name: network-scripts
state: present
- name: config lvs vip #配置lvs1主机eth0网卡虚拟接口IP地址
hosts: lbs
tasks:
- name: config file #生成ifcfg-eth0:0配置文件
copy:
content: |
TYPE=Ethernet
DEVICE=eth0:0
NAME=eth0:0
IPADDR=192.168.88.15
PREFIX=24
BROADCAST=192.168.88.255
ONBOOT=yes
dest: /etc/sysconfig/network-scripts/ifcfg-eth0:0
notify: active vip
handlers:
- name: active vip #激活eth0:0
shell: ifup eth0:0
- name: config web vip #配置webservers主机lo虚拟接口IP地址
hosts: webservers
tasks:
- name: config file #生成ifcfg-lo:0配置文件
copy:
content: |
DEVICE=lo:0
NAME=lo:0
IPADDR=192.168.88.15
PREFIX=32
NETWORK=192.168.88.15
BROADCAST=192.168.88.15
ONBOOT=yes
dest: /etc/sysconfig/network-scripts/ifcfg-lo:0
notify: active vip
handlers:
- name: active vip #激活lo:0
shell: ifup lo:0
[root@pubserver cluster]# ansible-playbook 06_config_dr_vip.yml
[root@pubserver cluster]# ansible lbs,webservers -m shell -a "ip a s | grep 192.168"
lvs1 | CHANGED | rc=0 >>
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 eth0:0
web2 | CHANGED | rc=0 >>
inet 192.168.88.15/32 brd 192.168.88.15 scope global lo:0
inet 192.168.88.200/24 brd 192.168.88.255 scope global noprefixroute eth0
web1 | CHANGED | rc=0 >>
inet 192.168.88.15/32 brd 192.168.88.15 scope global lo:0
inet 192.168.88.100/24 brd 192.168.88.255 scope global noprefixroute eth0
[root@pubserver cluster]#
# 配置web服务器内核参数,两种方法二选一即可
##参数解析
net.ipv4.conf.all.arp_ignore
Linux 系统中的一个内核参数,用于控制是否忽略来自所有网络接口的 ARP(地址解析协议)请求
0:表示不忽略来自所有网络接口的 ARP 请求,即对所有接口发来的 ARP 请求都会进行响应
1:表示忽略来自所有网络接口的 ARP 请求,即不会对任何接口发来的 ARP 请求进行响应
net.ipv4.conf.lo.arp_ignore
Linux 系统中的一个内核参数,用于控制是否忽略本地回环接口(lo)上的 ARP(地址解析协议)请求
0:表示不忽略本地回环接口上的 ARP 请求,即对本地回环接口上的 ARP 请求进行响应
1:表示忽略本地回环接口上的 ARP 请求,即不对本地回环接口上的 ARP 请求进行响应
net.ipv4.conf.all.arp_announce
Linux 内核参数之一,用于控制源 IP 地址在 ARP 请求中的发布方式
0:系统使用物理接口上的 IP 地址作为 ARP 请求中的源 IP 地址。这是默认设置,适用于大多数情况。
1: 系统使用主机上的所有 IP 地址中与目标 IP 地址在同一子网上的 IP 地址作为 ARP 请求中的源 IP 地址。这个选项适用于多地址绑定的服务器,确保 ARP 请求使用与目标 IP 在同一子网上的 IP 地址,以提高 ARP 的准确性和性能。
2: 系统使用主机上配置的主 IP 地址作为 ARP 请求中的源 IP 地址。这个选项适用于某些特殊网络环境,例如负载均衡的集群中,确保 ARP 请求使用主 IP 地址,以避免 ARP 缓存中的旧条目导致流量被错误地发送到主机的其他地址
net.ipv4.conf.lo.arp_announce
Linux 内核参数之一,用于控制本地回环接口(lo)上 ARP 请求中的源 IP 地址的发布方式
0: 表示系统将使用物理接口上的 IP 地址作为 ARP 请求中的源 IP 地址。这是默认设置,适用于大多数情况。在本地回环接口上,通常情况下使用此设置,因为本地回环接口用于本地通信,不需要进行 ARP 请求。
1: 表示系统将使用主机上的所有 IP 地址中与目标 IP 地址在同一子网上的 IP 地址作为 ARP 请求中的源 IP 地址。这个选项适用于多地址绑定的服务器,确保 ARP 请求使用与目标 IP 在同一子网上的 IP 地址,以提高 ARP 的准确性和性能
2: 表示系统将使用主机上配置的主 IP 地址作为 ARP 请求中的源 IP 地址。这个选项适用于某些特殊网络环境,例如负载均衡的集群中,确保 ARP 请求使用主 IP 地址,以避免 ARP 缓存中的旧条目导致流量被错误地发送到主机的其他地址
[root@pubserver cluster]# vim 07_config_dr_sysctl.yml
---
- name: config kernel args
hosts: webservers
tasks:
- name: config arp_ignore #配置忽略arp广播
sysctl:
name: "{{ item }}"
value: "1"
sysctl_set: true
sysctl_file: /etc/sysctl.conf
loop:
- net.ipv4.conf.all.arp_ignore
- net.ipv4.conf.lo.arp_ignore
notify: flush args
- name: config arp_announce #配置禁止arp宣告
sysctl:
name: "{{ item }}"
value: "2"
sysctl_set: true
sysctl_file: /etc/sysctl.conf
loop:
- net.ipv4.conf.all.arp_announce
- net.ipv4.conf.lo.arp_announce
notify: flush args
handlers:
- name: flush args #刷新sysctl.conf文件配置
shell: "sysctl -p"
[root@pubserver cluster]# ansible-playbook 07_config_dr_sysctl.yml
[root@pubserver cluster]# vim 07_config_dr_sysctl2.yml
---
- name: config kernel args
hosts: webservers
tasks:
- name: modify kernel args
blockinfile:
path: /etc/sysctl.conf
block: |
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
notify: flush args
handlers:
- name: flush args
shell: "sysctl -p"
[root@pubserver cluster]# ansible-playbook 07_config_dr_sysctl2.yml
[root@pubserver cluster]# ansible webservers -m shell -a "tail -6 /etc/sysctl.conf"
web1 | CHANGED | rc=0 >>
# BEGIN ANSIBLE MANAGED BLOCK
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
# END ANSIBLE MANAGED BLOCK
web2 | CHANGED | rc=0 >>
# BEGIN ANSIBLE MANAGED BLOCK
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
# END ANSIBLE MANAGED BLOCK
[root@pubserver cluster]#
# 配置LVS-DR模式规则
[root@lvs1 ~]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
[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:80 -w 1 -g
[root@lvs1 ~]# ipvsadm -a -t 192.168.88.15:80 -r 192.168.88.200:80 -w 2 -g
[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 0
-> 192.168.88.200:80 Route 2 0 0
[root@lvs1 ~]#
# 访问测试,必须使用LVS集群之外的主机作为客户端
[root@client ~]# for i in {1..6}
> do
> curl http://192.168.88.15
> done
Welcome to web2
Welcome to web1
Welcome to web2
Welcome to web2
Welcome to web1
Welcome to web2