创建LVS集群

案例:练习使用ipvsadm
先配置yum源
[[email protected] ~]# yum-config-manager –add ftp://192.168.4.254/rhel7
已加载插件:langpacks, product-id
adding repo from: ftp://192.168.4.254/rhel7

[192.168.4.254_rhel7]
name=added from: ftp://192.168.4.254/rhel7
baseurl=ftp://192.168.4.254/rhel7
enabled=1
[[email protected] ~]# echo “gpgcheck=0” >> /etc/yum.repos.d/
192.168.4.254_rhel7.repo redhat.repo
[[email protected] ~]# echo “gpgcheck=0” >> /etc/yum.repos.d/192.168.4.254_rhel7.repo
开始装包ipvsadm
[[email protected] ~]# yum install -y ipvsadm
已加载插件:langpacks, product-id, search-disabled-repos, subscription-manager
This system is not registered with an entitlement server. You can use subscription-manager to register.
正在解决依赖关系
–> 正在检查事务
—> 软件包 ipvsadm.x86_64.0.1.27-7.el7 将被 安装
–> 解决依赖关系完成

依赖关系解决

==========================================================================================
Package 架构 版本 源 大小
==========================================================================================
正在安装:
ipvsadm x86_64 1.27-7.el7 192.168.4.254_rhel7 45 k

事务概要
==========================================================================================
安装 1 软件包

总下载量:45 k
安装大小:75 k
Downloading packages:
ipvsadm-1.27-7.el7.x86_64.rpm | 45 kB 00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
正在安装 : ipvsadm-1.27-7.el7.x86_64 1/1
192.168.4.254_rhel7/productid | 1.6 kB 00:00:00
验证中 : ipvsadm-1.27-7.el7.x86_64 1/1

已安装:
ipvsadm.x86_64 0:1.27-7.el7

完毕!
命令的基本格式和用户
-A -E -D 添加、修改、删除虚拟服务器
-a -e -d 添加、修改、删除真实服务器
-C 清空所有
-L 查看所有
-s (rr|wrr|lc|wlc)制定集群算法
-g(DR模式) -i(隧道模式) -m(NAT模式)

1、创建LVS虚拟集群服务器
[[email protected] ~]# ipvsadm -A -t 192.168.4.60:80 -s wrr
查看一下
[[email protected] ~]# 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.4.60:80 wrr

2、为集群添加2台真实服务器real server 61和62
[[email protected] ~]# ipvsadm -a -t 192.168.4.60:80 -r 192.168.4.61 -m -w 1
[[email protected] ~]# 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.4.60:80 wrr
-> 192.168.4.61:80 Masq 1 0 0
[[email protected] ~]# ipvsadm -a -t 192.168.4.60:80 -r 192.168.4.62 -m -w 2
查看一下
[[email protected] ~]# 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.4.60:80 wrr
-> 192.168.4.61:80 Masq 1 0 0
-> 192.168.4.62:80 Masq 2 0 0

3、修改集群的调度算法,刚才是加权轮询wrr,现在修改成rr轮询
(下面是错误的例子,如果虚拟IP写错了,直接报错Memory allocation problem)
[[email protected] ~]# ipvsadm -E -t 192.168.4.5:80 -s rr
Memory allocation problem
[[email protected] ~]# 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.4.60:80 wrr
-> 192.168.4.61:80 Masq 1 0 0
-> 192.168.4.62:80 Masq 2 0 0
(上面报错的原因就是虚拟IP写错了,改回正确的)
[[email protected] ~]# ipvsadm -E -t 192.168.4.60:80 -s rr
[[email protected] ~]# 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.4.60:80 rr
-> 192.168.4.61:80 Masq 1 0 0
-> 192.168.4.62:80 Masq 2 0 0

4、修改read server(将模式改为DR模式)
[[email protected] ~]# ipvsadm -e -t 192.168.4.60:80 -r 192.168.4.62 -g
[[email protected] ~]# 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.4.60:80 rr
-> 192.168.4.61:80 Masq 1 0 0
-> 192.168.4.62:80 Route 1 0 0

5、再创建一个集群
[[email protected] ~]# ipvsadm -A -t 192.168.4.5:3306 -s lc
[[email protected] ~]# ipvsadm -a -t 192.168.4.5:3306 -r 192.168.2.100 -m
[[email protected] ~]# ipvsadm -a -t 192.168.4.5:3306 -r 192.168.2.200 -m
[[email protected] ~]#
[[email protected] ~]# 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.4.5:3306 lc
-> 192.168.2.100:3306 Masq 1 0 0
-> 192.168.2.200:3306 Masq 1 0 0
TCP 192.168.4.60:80 rr
-> 192.168.4.61:80 Masq 1 0 0
-> 192.168.4.62:80 Route 1 0 0

6、把刚才的规则保存一下
ipvsadm ipvsadm-restore ipvsadm-save
[[email protected] ~]# ipvsadm-save -n > /etc/sysconfig/ipvsadm
[[email protected] ~]# cat /etc/sysconfig/ipvsadm
-A -t 192.168.4.5:3306 -s lc
-a -t 192.168.4.5:3306 -r 192.168.2.100:3306 -m -w 1
-a -t 192.168.4.5:3306 -r 192.168.2.200:3306 -m -w 1
-A -t 192.168.4.60:80 -s rr
-a -t 192.168.4.60:80 -r 192.168.4.61:80 -m -w 1
-a -t 192.168.4.60:80 -r 192.168.4.62:80 -g -w 1

7、把刚才的规则清空
[[email protected] ~]# ipvsadm -C
[[email protected] ~]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
(注意下面的,虽然清空了规则,但是刚才的配置文件是不会删除了,所以为了避免混乱,如果清楚规则就必须删除配置文件)
[[email protected] ~]# cat /etc/sysconfig/ipvsadm
-A -t 192.168.4.5:3306 -s lc
-a -t 192.168.4.5:3306 -r 192.168.2.100:3306 -m -w 1
-a -t 192.168.4.5:3306 -r 192.168.2.200:3306 -m -w 1
-A -t 192.168.4.60:80 -s rr
-a -t 192.168.4.60:80 -r 192.168.4.61:80 -m -w 1
-a -t 192.168.4.60:80 -r 192.168.4.62:80 -g -w 1
[[email protected] ~]#

案例
部署LVS-DR集群
使用4台虚拟机,1台作为客户端、1台作为Director调度器、2台作为Real Server、,拓扑结构如下
PC 客户端 eth0 192.168.4.100/24
LVS调度器 eth0 192.168.4.5/24
Web服务器1 eth0:192.168.4.10/24 VIP(lo:0):192.168.4.5/32
Web服务器1 eth0:192.168.4.20/24 VIP(lo:0):192.168.4.5/32
说明:VIP是对客户端提供服务的IP地址,RIP是后端服务器的真实IP地址,DIP是调度器与后端服务器通信的IP地址(DIP必须配置在虚拟接口)。

步骤一:配置实验网络环境
1)设置Proxy代理服务器的VIP和DIP
注意:为了防止冲突,VIP必须要配置在网卡的虚拟接口!!!
调度器使用DIP与RIP通信,否则会出现192.168.4.5与192.168.4.5通信。
[[email protected] ~]# cd /etc/sysconfig/network-scripts/
[[email protected] ~]# cp ifcfg-eth0{,:0}
[[email protected] ~]# vim ifcfg-eth0
TYPE=Ethernet
BOOTPROTO=none
NAME=eth0
DEVICE=eth0
ONBOOT=yes
IPADDR=192.168.4.15
PREFIX=24
[[email protected] ~]# vim ifcfg-eth0:0
TYPE=Ethernet
BOOTPROTO=none
DEFROUTE=yes
NAME=eth0:0
DEVICE=eth0:0
ONBOOT=yes
IPADDR=192.168.4.5
PREFIX=24
[[email protected] ~]# systemctl restart network

2)设置Web1服务器网络参数
[[email protected] ~]# nmcli connection modify eth0 ipv4.method manual ipv4.addresses 192.168.4.10/24 connection.autoconnect yes
[[email protected] ~]# nmcli connection up eth0
接下来给web1配置VIP地址
注意:这里的子网掩码必须是32(也就是全255),网络地址与IP地址一样,广播地址与IP地址也一样。
[[email protected] ~]# cd /etc/sysconfig/network-scripts/
[[email protected] ~]# cp ifcfg-lo{,:0}
[[email protected] ~]# vim ifcfg-lo:0
DEVICE=lo:0
IPADDR=192.168.4.5
NETMASK=255.255.255.255
NETWORK=192.168.4.5
BROADCAST=192.168.4.5
ONBOOT=yes
NAME=lo:0
注意:这里因为web1也配置与代理一样的VIP地址,默认肯定会出现地址冲突。
写入这四行的主要目的就是访问192.168.4.5的数据包,只有调度器会响应,其他主机都不做任何响应。
[[email protected] ~]# vim /etc/sysctl.conf
#手动写入如下4行内容
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
net.ipv4.conf.all.arp_announce = 2
#当有arp广播问谁是192.168.4.5时,本机忽略该ARP广播,不做任何回应
#本机不要向外宣告自己的lo回环地址是192.168.4.5
重启网络服务,设置防火墙与SELinux
[[email protected] ~]# systemctl restart network
[[email protected] ~]# ifdown eth1
[[email protected] ~]# ifconfig
[[email protected] ~]# systemctl stop firewalld
[[email protected] ~]# setenforce 0

3)设置Web2服务器网络参数
[[email protected] ~]# nmcli connection modify eth0 ipv4.method manual \
ipv4.addresses 192.168.4.20/24 connection.autoconnect yes
[[email protected] ~]# nmcli connection up eth0
接下来给web2配置VIP地址
注意:这里的子网掩码必须是32(也就是全255),网络地址与IP地址一样,广播地址与IP地址也一样。
[[email protected] ~]# cd /etc/sysconfig/network-scripts/
[[email protected] ~]# cp ifcfg-lo{,:0}
[[email protected] ~]# vim ifcfg-lo:0
DEVICE=lo:0
IPADDR=192.168.4.5
NETMASK=255.255.255.255
NETWORK=192.168.4.5
BROADCAST=192.168.4.5
ONBOOT=yes
NAME=lo:0
注意:这里因为web2也配置与代理一样的VIP地址,默认肯定会出现地址冲突。
写入这四行的主要目的就是访问192.168.4.5的数据包,只有调度器会响应,其他主机都不做任何响应。
[[email protected] ~]# vim /etc/sysctl.conf
#手动写入如下4行内容
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
net.ipv4.conf.all.arp_announce = 2
#当有arp广播问谁是192.168.4.5时,本机忽略该ARP广播,不做任何回应
#本机不要向外宣告自己的lo回环地址是192.168.4.5
重启网络服务,设置防火墙与SELinux
[[email protected] ~]# systemctl restart network
[[email protected] ~]# ifdown eth1
[[email protected] ~]# ifconfig
[[email protected] ~]# systemctl stop firewalld
[[email protected] ~]# setenforce 0

步骤二:配置实验网络环境

1)自定义Web页面
[[email protected] ~]# yum -y install httpd
[[email protected] ~]# echo “192.168.4.10” > /var/www/html/index.html
[[email protected] ~]# yum -y install httpd
[[email protected] ~]# echo “192.168.4.20” > /var/www/html/index.html
2)启动Web服务器软件
[[email protected] ~]# systemctl start httpd; systemctl enable httpd
[[email protected] ~]# systemctl start httpd; systemctl enable httpd

步骤三:proxy调度器安装软件并部署LVS-DR模式调度器
1)安装软件(如果已经安装,此步骤可以忽略)
[[email protected] Packages]# yum -y install ipvsadm
2)清理之前实验的规则,创建新的集群服务器规则
[[email protected] ~]# ipvsadm -C #清空所有规则
[[email protected] ~]# ipvsadm -A -t 192.168.4.5:80 -s wrr
3)添加真实服务器(-g参数设置LVS工作模式为DR模式)
[[email protected] ~]# ipvsadm -a -t 192.168.4.5:80 -r 192.168.4.10 -g -w 1
[[email protected] ~]# ipvsadm -a -t 192.168.4.5:80 -r 192.168.4.20 -g -w 1
4)查看规则列表,并保存规则
[[email protected] ~]# ipvsadm –Ln
[[email protected] ~]# ipvsadm-save -n > /etc/sysconfig/ipvsadm

步骤四:客户端测试
客户端使用curl命令反复连接http://192.168.4.5,查看访问的页面是否会轮询到不同的后端真实服务器。
[[email protected] ~]# curl 192.168.4.5
192.168.45.20
[[email protected] ~]# curl 192.168.4.5
192.168.4.10
[[email protected] ~]# curl 192.168.4.5
192.168.45.20
[[email protected] ~]# curl 192.168.4.5
192.168.4.10
[[email protected] ~]#

扩展知识:默认LVS不带健康检查功能,需要自己手动编写动态检测脚本,实现该功能:(参考脚本如下,仅供参考)
[[email protected] ~]# vim check.sh
#!/bin/bash
VIP=192.168.4.5:80
RIP1=192.168.4.10
RIP2=192.168.4.20
while :
do
for IP in $RIP1 $RIP2
do
curl -s http://$IP &>/dev/vnull
web_stat=$?
ipvsadm -Ln | grep -q $IP
web_in_lvs=$?
if [ $web_stat -ne 0 -a $web_in_lvs -eq 0 ];then
ipvsadm -d -t $VIP -r $IP
elif [ $web_stat -eq 0 -a $web_in_lvs -ne 0 ];then
ipvsadm -a -t $VIP -r $IP
fi
done
sleep 1
done

此条目发表在ceph分类目录,贴了标签。将固定链接加入收藏夹。

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注