LVS负载均衡群集 Linux Vitual Server LVS的负载调度算法 配置LVS负载均衡

LVS负载均衡群集

L(Linux)v(Virtual“虚拟化”)s(Server“服务”)

结构

在这里插入图片描述

概述

在这里插入图片描述

负载均衡的结构

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

Linux Vitual Server

在这里插入图片描述

LVS的负载调度算法

在这里插入图片描述
环境:

开启四台centos
Centso04添加vmnet2

配置LVS负载均衡

1、安装LVS负载均衡

挂载光盘

[root@centos01 ~]# mount /dev/sr0 /mnt/

删除系统源

[root@centos01 ~]# rm -rf /etc/yum.repos.d/CentOS-*

安装http服务器

[root@centos01 ~]# yum -y install httpd

创建网站主页

[root@centos01 ~]# echo "www.benet.com" > /var/www/html/index.html

启动http服务-设置开机自动启动

[root@centos01 ~]# systemctl start httpd
[root@centos01 ~]# systemctl enable httpd

centos02
删除系统源

[root@centos02 ~]# rm -rf /etc/yum.repos.d/CentOS-*

挂载光盘

[root@centos02 ~]# mount /dev/sr0 /mnt/

安装http

[root@centos02 ~]# yum -y install httpd

创建网站主页

[root@centos02 ~]# echo "www.accp.com" > /var/www/html/index.html

启动httpd服务-设置开机自动启动

[root@centos02 ~]# systemctl start httpd
[root@centos02 ~]# systemctl enable httpd

Centos04
复制网卡配置文件

[root@centos04 ~]# cp /etc/sysconfig/network-scripts/ifcfg-ens32 /etc/sysconfig/network-scripts/ifcfg-ens34

编辑ens34网卡

[root@centos04 ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens34
TYPE=Ethernet
BOOTPROTO=static
DEFROUTE=yes
NAME=ens34
DEVICE=ens34
ONBOOT=yes
IPADDR=192.168.200.10
NETMASK=255.255.255.0

重启网卡服务

[root@centos04 ~]# systemctl restart network
开启路由功能
[root@centos04 ~]# vim /etc/sysctl.conf
net.ipv4.ip_forward = 1

检查路由功能的配置文件是否有报错

[root@centos04 ~]# sysctl -p

Centos01
不同网络通信需要配置网关

[root@centos01 ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens32
GATEWAY=192.168.100.40

重启网卡服务

[root@centos01 ~]# systemctl restart network

centos02
不同网络通信需要配置网关

[root@centos02 ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens32
GATEWAY=192.168.100.40

重启网卡服务

[root@centos02 ~]# systemctl restart network

Centos03
不同网络通信需要配置网关

[root@centos03 ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens32
GATEWAY=192.168.100.40

重启网卡服务

[root@centos03 ~]# systemctl restart network

Centos04
删除系统源

[root@centos04 ~]# rm -rf /etc/yum.repos.d/CentOS-*

挂载光盘

[root@centos04 ~]# mount /dev/sr0 /mnt/

安装lvs服务器

[root@centos04 ~]# yum -y install ipvsadm

设置服务器开机自动启动

[root@centos04 ~]# systemctl enable ipvsadm

查看lvs规则

[root@centos04 ~]# ipvsadm -L

清空默认规则

[root@centos04 ~]# ipvsadm -C

保存

[root@centos04 ~]# ipvsadm-save
2、配置LVS的NAT

创建服务在均衡器指定IP地址和端口号,指定负载均衡调度算法

[root@centos04 ~]# ipvsadm -A -t 192.168.200.10:80 -s rr

添加服务器

[root@centos04 ~]# ipvsadm -a -t 192.168.200.10:80 -r 192.168.100.10:80 -m -w 1
[root@centos04 ~]# ipvsadm -a -t 192.168.200.10:80 -r 192.168.100.20:80 -m -w 1

查看LVS配置

[root@centos04 ~]# ipvsadm -L
保存配置
[root@centos04 ~]# ipvsadm-save
测试

启动win7
IP:192.168.200.20
GATEWAY:192.168.200.10

在这里插入图片描述
清除缓存
重新访问就访问到了benet
在这里插入图片描述

3、配置LVS的DR模式

清空规则

[root@centos04 ~]# ipvsadm -C

查看规则

[root@centos04 ~]# ipvsadm -L

保存规则

[root@centos04 ~]# ipvsadm-save

生成ens的虚拟网卡

[root@centos04 ~]# cp /etc/sysconfig/network-scripts/ifcfg-ens32 /etc/sysconfig/network-scripts/ifcfg-ens32:0

编辑虚拟网卡

[root@centos04 ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens32:0
TYPE=Ethernet
BOOTPROTO=static
DEFROUTE=yes
NAME=ens32:0
DEVICE=ens32:0
ONBOOT=yes
IPADDR=192.168.100.254
NETMASK=255.255.255.0

重启网卡服务

[root@centos04 ~]# systemctl restart network

查看虚拟网卡
在这里插入图片描述
Centos01
测试 Ping虚拟网卡

[root@centos01 ~]# ping 192.168.100.254

在这里插入图片描述
外网接口ens32、群及接口ens32:0

[root@centos01 ~]# cp /etc/sysconfig/network-scripts/ifcfg-lo /etc/sysconfig/network-scripts/ifcfg-lo:0

编辑虚拟网卡

[root@centos01 ~]# vim /etc/sysconfig/network-scripts/ifcfg-lo:0
DEVICE=lo:0
IPADDR=192.168.100.254
NETMASK=255.255.255.255
BROADCAST=127.255.255.255
ONBOOT=yes
NAME=loopback

重启网卡服务

[root@centos01 ~]# systemctl restart network

Centos02
做一个虚拟网卡

[root@centos2 ~]# cp /etc/sysconfig/network-scripts/ifcfg-lo /etc/sysconfig/network-scripts/ifcfg-lo:0

编辑虚拟网卡

[root@centos02 ~]# vim /etc/sysconfig/network-scripts/ifcfg-lo:0
DEVICE=lo:0
IPADDR=192.168.100.254
NETMASK=255.255.255.255
BROADCAST=127.255.255.255
ONBOOT=yes
NAME=loopback

重启网卡服务

[root@centos02 ~]# systemctl restart network

Centos04
修改LVS服务器内核参数关闭响应

	[root@centos04 ~]# vim /etc/sysctl.conf
	net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.ens32.send_redirects = 0

更新

[root@centos04 ~]# sysctl -p

Centos01
配置WEB服务器内核参数 配置ARP响应

[root@centos01 ~]# vim /etc/sysctl.conf
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.default.arp_ignore = 1
net.ipv4.conf.default.arp_announce = 2
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2

更新

[root@centos01 ~]# sysctl -p

Centos02
配置WEB服务器内核参数 配置ARP响应

root@centos02 ~]# vim /etc/sysctl.conf
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.default.arp_ignore = 1
net.ipv4.conf.default.arp_announce = 2
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2

更新

[root@centos02 ~]# sysctl -p

Centos04
配置lVS调度器的IP地址和端口号指定算法

[root@centos04 ~]# ipvsadm -A -t 192.168.100.254:80 -s rr
服务器池添加服务
[root@centos04 ~]# ipvsadm -a -t 192.168.100.254:80 -r 192.168.100.10:80 -g -w 1
[root@centos04 ~]# ipvsadm -a -t 192.168.100.254:80 -r 192.168.100.20:80 -g -w 1

保存查看规则

[root@centos04 ~]# ipvsadm-save
[root@centos04 ~]# ipvsadm -L
测试

Win7测试 IP地址:100.253

在这里插入图片描述
清空缓存切换ip再次访问
在这里插入图片描述

5、配置储存服务器

Centos03
挂载光盘

[root@centos03 ~]# mount /dev/sr0 /mnt/

删除系统源

[root@centos03 ~]# rm -rf /etc/yum.repos.d/CentOS-*

安装存储服务器

[root@centos03 ~]# yum -y install nfs-utils rpcbind
创建根目录和网站主页
[root@centos03 ~]# mkdir /www
[root@centos03 ~]# echo "www.nfs.com" > /www/index.html	

修改NFS储存主配置文件

[root@centos03 ~]# vim /etc/exports
/www 192.168.100.10(ro) 192.168.100.20(ro)
启动服务器-设置开机自动启动
[root@centos03 ~]# systemctl start nfs
[root@centos03 ~]# systemctl enable nfs
[root@centos03 ~]# systemctl start rpcbind
[root@centos03 ~]# systemctl enable rpcbind
查看共享存储
[root@centos03 ~]# showmount -e 192.168.10030

在这里插入图片描述

Centos01
配置网站服务器开机自动挂载存储

[root@centos01 ~]# vim /etc/fstab
192.168.100.40:/www                       /var/www/html           nfs     defaults,netdev 0 0

Centos02
配置网站服务器开机自动挂载存储

[root@centos02 ~]# vim /etc/fstab
192.168.100.40:/www                       /var/www/html           nfs     defaults,netdev 0 0

重新启动centos01和centos02
然后查看有没有自动挂载成功

在这里插入图片描述
在这里插入图片描述

测试

访问100.254
在这里插入图片描述
现在两台主机都是www.nfs.com
怎么测试他均衡了呢
我们去查看centos01和centos02的日志

刷新一下访问了centos01
在这里插入图片描述
再次刷新访问了centos02
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值