一、简单介绍
LVS是 Linux Virtual Server 的简称,也就是Linux虚拟服务器。这是一个由章文嵩博士发起的一个开源项目,它的官方网站是 http://www.linuxvirtualserver.org 现在 LVS 已经是 Linux 内核标准的一部分。使用 LVS 可以达到的技术目标是:通过 LVS 达到的负载均衡技术和 Linux 操作系统实现一个高性能高可用的 Linux 服务器集群,它具有良好的可靠性、可扩展性和可操作性。从而以低廉的成本实现最优的性能。LVS 是一个实现负载均衡集群的开源软件项目,LVS架构从逻辑上可分为调度层、Server集群层和共享存储。
二、配置实操
LVS VIP
--虚拟服务器配置子网卡,设置子网掩码为255.255.255.0
--删除一个子网卡 ifconfig eth0:8 down
ifconfig eth0:8 192.168.3.200/24
--0 表示禁止IP转发
--1 表示开启转发功能
-- 修改只能以重定向方式,不能vi/vim
-- 永久生效方式需要修改配置文件/etc/sysctl.conf中net.ipv4.ip_forward = 1
-- sysctl -p /etc/sysctl.conf
echo 1 > /proc/sys/net/ipv4/ip_forward
--安装ipvsadm(LVS只是一个技术名称,linux将此技术放到内核并成为ipvsadm功能模块)
yum install ipvsadm -y
--add-service -A add virtual service with option 在内核的虚拟服务器表中添加一条新的虚拟服务器记录。也就是增加一台新的虚拟服务器
--edit-service -E edit virtual service with options 编辑内核虚拟服务器表中的一条虚拟服务器记录
--delete-service -D delete virtual service 删除内核虚拟服务器表中的一条虚拟服务器记录
--clear -C clear the whole table 清除内核虚拟服务器表中的所有记录
--restore -R restore rules from stdin 恢复虚拟服务器规则
--save -S save rules to stdout 保存虚拟服务器规则,输出为-R 选项可读的格式
--add-server -a add real server with options 在内核虚拟服务器表的一条记录里添加一条新的真实服务器
--edit-server -e edit real server with options 编辑一条虚拟服务器记录中的某条真实服务器记录
--delete-server -d delete real server 删除一条虚拟服务器记录中的某条真实服务器记录
--list -L|-l list the table 显示内核虚拟服务器表
--zero -Z zero counters in a service or all services 虚拟服务表计数器清零
--set tcp tcpfin udp set connection timeout values 设置连接超时值
--start-daemon start connection sync daemon 启动同步守护进程。他后面可以是master 或backup,用来说明LVS Router 是master 或是backup。在这个功能上也可以采keepalived 的VRRP 功能。
--stop-daemon stop connection sync daemon 停止同步守护进程
--help -h display this help message 显示帮助信息
--tcp-service -t service-address service-address is host[:port] 说明虚拟服务器提供的是tcp 的服务[vip:port] or [real-server-ip:port]
--udp-service -u service-address service-address is host[:port] 说明虚拟服务器提供的是udp 的服务[vip:port] or [real-server-ip:port]
--fwmark-service -f fwmark fwmark is an integer greater than zero 说明是经过iptables 标记过的服务类型。
--scheduler -s scheduler one of rr|wrr|lc|wlc|lblc|lblcr|dh|sh|sed|nq,the default scheduler is wlc. 使用的调度算法,有这样几个选项,默认的调度算法是: wlc.
--persistent -p [timeout] persistent service 持久稳固的服务。这个选项的意思是来自同一个客户的多次请求,将被同一台真实的服务器处理。timeout 的默认值为300 秒
--netmask -M netmask persistent granularity mask
--real-server -r server-address server-address is host (and port) 真实的服务器[Real-Server:port]
--gatewaying -g gatewaying (direct routing) (default) 指定LVS 的工作模式为直接路由模式(也是LVS 默认的模式
--ipip -i ipip encapsulation (tunneling) 指定LVS 的工作模式为隧道模式
--masquerading -m masquerading (NAT) 指定LVS 的工作模式为NAT 模式
--weight -w weight capacity of real server 真实服务器的权值
--u-threshold -x uthreshold upper threshold of connections
--l-threshold -y lthreshold lower threshold of connections
--mcast-interface interface multicast interface for connection sync 指定组播的同步接口
--syncid sid syncid for connection sync (default=255)
--connection -c output of current IPVS connections 显示LVS 目前的连接 如:ipvsadm -L -c
--timeout output of timeout (tcp tcpfin udp) 显示tcp tcpfin udp 的timeout 值 如:ipvsadm -L
--timeout --daemon output of daemon information 显示同步守护进程状态
--stats output of statistics information 显示统计信息 --rate output of rate information 显示速率信息
--thresholds output of thresholds information --persistent-conn output of persistent connection info
--sort sorting output of service/server entries 对虚拟服务器和真实服务器排序输出
--ops -O one-packet scheduling --numeric -n numeric output of addresses and ports 输出IP 地址和端口的数字形式
--在内核的虚拟服务器表中添加一条新的虚拟服务器记录。也就是增加一台新的虚拟服务器
--rr 轮询算法
--wrr 加权轮训调度
--dh 目的地址哈希调度
--sh 源地址哈希调度
ipvsadm -A -t 192.168.3.200:80 -s rr
--在内核虚拟服务器表的一条记录里添加一条新的真实服务器
--[-g|i|m]: LVS类型
-- -g: DR
-- -i: TUN
-- -m: NAT
-- [-w weight]: 定义服务器权重
ipvsadm -a -t 192.168.3.200:80 -r 192.168.3.214 -g
ipvsadm -a -t 192.168.3.200:80 -r 192.168.3.215 -g
ipvsadm -lnc
-- 真实服务器配置
S-R1
--设置lo子网卡,掩码设置4个255
ifconfig lo:8 192.168.3.200 netmask 255.255.255.255
--arp_ignore参数的作用是控制系统在收到外部的arp请求时,是否要返回arp响应。
--0:响应任意网卡上接收到的对本机IP地址的arp请求(包括环回网卡上的地址),而不管该目的IP是否在接收网卡上。
--1:只响应目的IP地址为接收网卡上的本地地址的arp请求。
--2:只响应目的IP地址为接收网卡上的本地地址的arp请求,并且arp请求的源IP必须和接收网卡同网段。
--3:如果ARP请求数据包所请求的IP地址对应的本地地址其作用域(scope)为主机(host),则不回应ARP响应数据包,如果作用域为全局(global)或链路(link),则回应ARP响应数据包。
--4~7:保留未使用
--8:不回应所有的arp请求
--sysctl.conf中包含all和eth/lo(具体网卡)的arp_ignore参数,取其中较大的值生效。
echo 1 > /proc/sys/net/ipv4/conf/eth0/arp_ignore
echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
--arp_announce:定义将自己地址向外通告时的通告级别;
--0:允许使用任意网卡上的IP地址作为arp请求的源IP,通常就是使用数据包a的源IP。
--1:尽量避免使用不属于该发送网卡子网的本地地址作为发送arp请求的源IP地址。
--2:忽略IP数据包的源IP地址,选择该发送网卡上最合适的本地地址作为arp请求的源IP地址。
echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
echo 2 > /proc/sys/net/ipv4/conf/eth0/arp_announce
--安装httpd,启动服务,设置开机启动
yum install httpd -y
service httpd start
chkconfig httpd on
--添加主页
vi /var/www/html/index.html