一、ipvsadm命令用法
目标:
准备一台Linux服务器,安装ipvsadm软件包,练习使用ipvsadm命令,实现如下功能:
- 使用命令添加基于TCP一些的集群服务
- 在集群中添加若干台后端真实服务器
- 实现同一客户端访问,调度器分配固定服务器
- 会使用ipvsadm实现规则的增、删、改
- 保存ipvsadm规则
方案:
安装ipvsadm软件包,关于ipvsadm的用法可以参考man ipvsadm资料。常用ipvsadm命令语法格式如下表所示。
步骤:
步骤一:使用命令增、删、改LVS集群规则
1)创建LVS虚拟集群服务器
[[email protected] ~]# ipvsadm -A -t 192.168.0.254 -s wrr -p 30
2)为集群添加若干real server
[[email protected] ~]# ipvsadm –a -t 192.168.0.254 -r 192.168.0.1 -m -w 1
[[email protected] ~]# ipvsadm –a -t 192.168.0.254 -r 192.168.0.2 -m -w 2
[[email protected] ~]# ipvsadm –a -t 192.168.0.254 -r 192.168.0.3 -m -w 3
[[email protected] ~]# ipvsadm –a -t 192.168.0.254 -r 192.168.0.4 -m -w 4
3)修改集群服务器设置
[[email protected] ~]# ipvsadm -E -t 192.168.0.254 -s sh -p 30
4)修改read server
[[email protected] ~]# ipvsadm –a -t 192.168.0.254 -r 192.168.0.1 -m
5)查看LVS状态
[[email protected] ~]# ipvsadm –Ln
6)保存所有规则
[[email protected] ~]# service ipvsadm save
7)清空所有规则
[[email protected] ~]# ipvsadm –C
二、部署LVS-NAT集群
目标:
使用LVS实现NAT模式的集群调度服务器,为用户提供Web服务:
- 集群对外公网IP地址为202.114.106.20
- 调度器内网IP地址为192.168.0.254
- 真实Web服务器地址分别为192.168.0.1、192.168.0.2、192.168.0.3
- 使用加权轮询调度算法,真实服务器权重与其IP地址末尾数一致
方案:
使用4台虚拟机,1台作为Director调度器、3台作为Real Server、物理机作为客户端,拓扑结构如下图所示。
步骤:
步骤一:配置网络环境
1)设置Web服务器网络参数
[[email protected] ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
ONBOOT=yes
BOOTPROTO=static
IPADDR=192.168.0.1
NETMASK=255.255.255.0
GATEWAY=192.168.0.254
DNS1=202.106.0.20
[[email protected] ~]# systemctl restart NetworkManager
[[email protected] ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
ONBOOT=yes
BOOTPROTO=static
IPADDR=192.168.0.2
NETMASK=255.255.255.0
GATEWAY=192.168.0.254
DNS1=202.106.0.20
[[email protected] ~]# systemctl restart NetworkManager
[[email protected] ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
ONBOOT=yes
BOOTPROTO=static
IPADDR=192.168.0.3
NETMASK=255.255.255.0
GATEWAY=192.168.0.254
DNS1=202.106.0.20
[[email protected] ~]# systemctl restart NetworkManager
2)自定义Web页面
[[email protected] ~]# echo “192.168.0.1” > /var/www/html/index.html
[[email protected] ~]# echo “192.168.0.2” > /var/www/html/index.html
[[email protected] ~]# echo “192.168.0.3” > /var/www/html/index.html
3)启动Web服务器软件
[[email protected] ~]# service httpd start
[[email protected] ~]# service httpd start
[[email protected] ~]# service httpd start
4)设置LVS调度器网络参数
[[email protected] ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
ONBOOT=yes
BOOTPROTO=static
IPADDR=202.114.106.20
DNS1=202.106.0.20
[[email protected] ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth1
DEVICE=eth0
ONBOOT=yes
BOOTPROTO=static
IPADDR=192.168.0.254
NETMASK=255.255.255.0
DNS1=202.106.0.20
[[email protected] ~]# systemctl restart NetworkManager
[[email protected] ~]# sed -i ‘/ip_forward/s/0/1/‘ sysctl.conf //开启路由转发
[[email protected] ~]# sysctl -p
步骤二:调度器安装软件并启动服务
1)安装软件
[[email protected] Packages]# rpm –ihv ipvsadm-1.25-10.el6.x86_64.rpm
2)启动服务
[[email protected] Packages]# service ipvsadm start
[[email protected] Packages]# chkconfig ipvsadm on
步骤三:部署LVS-NAT模式调度器
1)创建集群服务器
[[email protected] ~]# ipvsadm -A -t 202.114.106.20:80 -s wrr
2)添加真实服务器
[[email protected] ~]# ipvsadm -a -t 202.114.106.20:80 -r 192.168.0.1 -w 1
[[email protected] ~]# ipvsadm -a -t 202.114.106.20:80 -r 192.168.0.2 -w 2
[[email protected] ~]# ipvsadm -a -t 202.114.106.20:80 -r 192.168.0.3 -w 3
3)查看规则列表,并保存规则
[[email protected] ~]# ipvsadm –Ln
[[email protected] ~]# ipvsadm-save -n > /etc/sysconfig/ipvsadm
步骤四:客户端测试
客户端使用curl命令反复连接http://202.114.106.20,查看访问的页面是否会轮询到不同的后端真实服务器。
三、部署LVS-DR集群
目标:
使用LVS实现DR模式的集群调度服务器,为用户提供Web服务:
- 路由器对外公网IP地址为202.114.106.20
- 路由器内网IP地址为192.168.0.254
- 路由是需要设置SNAT及DNAT功能
- LVS调度器真实IP地址为192.168.0.10
- LVS调度器VIP地址设置为192.168.0.253
- 真实Web服务器地址分别为192.168.0.1、192.168.0.2
- 使用加权轮询调度算法,真实服务器权重与其IP地址末尾数一致
方案:
使用4台虚拟机,1台作为Linux路由器、1台作为Director调度器、2台作为Real Server、物理机作为客户端,拓扑结构如下图所示。
步骤:
步骤一:配置网络环境
1)设置Web服务器网络参数
[[email protected] ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
ONBOOT=yes
BOOTPROTO=static
IPADDR=192.168.0.1
NETMASK=255.255.255.0
GATEWAY=192.168.0.254
DNS1=202.106.0.20
[[email protected] ~]# vim /etc/sysconfig/network-scripts/ifcfg-lo:0
DEVICE=lo:0
ONBOOT=yes
BOOTPROTO=static
IPADDR=192.168.0.253
NETMASK=255.255.255.255
[[email protected] ~]# vim /etc/sysctl.conf
.. ..
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
[[email protected] ~]# sysctl -p
[[email protected] ~]# systemctl restart NetworkManager
[[email protected] ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
ONBOOT=yes
BOOTPROTO=static
IPADDR=192.168.0.2
NETMASK=255.255.255.0
GATEWAY=192.168.0.254
DNS1=202.106.0.20
[[email protected] ~]# vim /etc/sysconfig/network-scripts/ifcfg-lo:0
DEVICE=lo:0
ONBOOT=yes
BOOTPROTO=static
IPADDR=192.168.0.253
NETMASK=255.255.255.255
[[email protected] ~]# vim /etc/sysctl.conf
.. ..
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
[[email protected] ~]# sysctl -p
[[email protected] ~]# systemctl restart NetworkManager
2)自定义Web页面
[[email protected] ~]# echo “192.168.0.1” > /var/www/html/index.html
[[email protected] ~]# echo “192.168.0.2” > /var/www/html/index.html
3)启动Web服务器软件
[[email protected] ~]# systemctl start httpd; systemctl enable httpd
[[email protected] ~]# systemctl start httpd; systemctl enable httpd
4)设置LVS调度器网络参数
[[email protected] ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
ONBOOT=yes
BOOTPROTO=static
IPADDR=192.168.0.10
NETMASK=255.255.255.0
GATEWAY=192.168.0.254
DNS1=202.106.0.20
[[email protected] ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0:0
DEVICE=eth0:0
ONBOOT=yes
BOOTPROTO=static
IPADDR=192.168.0.253
NETMASK=255.255.255.0
GATEWAY=192.168.0.254
DNS1=202.106.0.20
[[email protected] ~]# systemctl restart NetworkManager
5)设置Linux路由器网络参数
[[email protected] ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
ONBOOT=yes
BOOTPROTO=static
IPADDR=202.114.106.20
DNS1=202.106.0.20
[[email protected] ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth1
DEVICE=eth0
ONBOOT=yes
BOOTPROTO=static
IPADDR=192.168.0.254
NETMASK=255.255.255.0
DNS1=202.106.0.20
[[email protected] ~]# systemctl restart NetworkManager
6)设置Linux路由器的SNAT、DNAT功能
[[email protected] ~]# sed -i ‘/ip_forward/s/0/1/‘ sysctl.conf //开启路由转发
[[email protected] ~]# sysctl -p
[[email protected] ~]# iptables -t nat -A POSTROUTING -s 192.168.0.0/24 -p tcp –j SNAT --to-source 202.114.106.20
[[email protected] ~]# iptables -t nat -A PREROUTING -d 202.114.106.20 -p tcp --dport 80 –j DNAT --to-destination 192.168.0.253:80
[[email protected] ~]# service iptables save
步骤二:调度器安装软件并启动服务
1)安装软件
[[email protected] Packages]# rpm –ihv ipvsadm-1.25-10.el6.x86_64.rpm
2)启动服务
[[email protected] Packages]# systemctl start ipvsadm
[[email protected] Packages]# systemctl enable ipvsadm
步骤三:部署LVS-DR模式调度器
1)创建集群服务器
[[email protected] ~]# ipvsadm -A -t 192.168.0.253:80 -s wrr
2)添加真实服务器
[[email protected] ~]# ipvsadm -a -t 192.168.0.253:80 -r 192.168.0.1 -g -w 1
[[email protected] ~]# ipvsadm -a -t 192.168.0.253:80 -r 192.168.0.2 -g -w 2
3)查看规则列表,并保存规则
[[email protected] ~]# ipvsadm –Ln
[[email protected] ~]# ipvsadm-save -n > /etc/sysconfig/ipvsadm
步骤四:客户端测试
客户端使用curl命令反复连接http://202.114.106.20,查看访问的页面是否会轮询到不同的后端真实服务器。
原文地址:https://www.cnblogs.com/baichuanhuihai/p/8445959.html