千家信息网

LVS负载均衡群集实战详细介绍

发表于:2024-10-19 作者:千家信息网编辑
千家信息网最后更新 2024年10月19日,下文给大家带来LVS负载均衡群集实战详细介绍,希望能够给大家在实际运用中带来一定的帮助,负载均衡涉及的东西比较多,理论也不多,网上有很多书籍,今天我们就用在行业内累计的经验来做一个解答。LVS负载均衡
千家信息网最后更新 2024年10月19日LVS负载均衡群集实战详细介绍

下文给大家带来LVS负载均衡群集实战详细介绍,希望能够给大家在实际运用中带来一定的帮助,负载均衡涉及的东西比较多,理论也不多,网上有很多书籍,今天我们就用在行业内累计的经验来做一个解答。

LVS负载均衡群集

环境准备:

CentOS 7-1:调度器,网关(需要两块网卡)外:12.0.0.1 内:192.168.200.1

CentOS 7-2:网站云服务器(Apache)192.168.200.110

CentOS 7-3:网站云服务器(Apache)192.168.200.120

CentOS 7-4:提供共享存储 192.168.200.130

win7-1:客户端 12.0.0.12

yum在线安装前置操作:

1.共享存储服务器CentOS 7-4的操作:

[root@localhost ~]# rpm -q nfs-utilsnfs-utils-1.3.0-0.48.el7.x86_64[root@localhost ~]# rpm -q rpcbind rpcbind-0.2.0-42.el7.x86_64

2.节点服务器CentOS 7-2和7-3两台节点服务器:

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

3.调度器网关CentOS 7-1操作:

//要先添加一个网络适配器,成为两个网卡[root@localhost ~]# yum install ipvsadm -y
共享存储服务器CentOS 7-4:
//先将网卡更改为仅主机模式[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33BOOTPROTO="static"      //将dhcp改为staticDEVICE="ens33"ONBOOT="yes"IPADDR=192.168.200.130      //在末行下插入:IP,子网,网关NETMASK=255.255.255.0GATEWAY=192.168.200.1修改完成后输入:wq保存退出[root@localhost ~]# service network restart Restarting network (via systemctl):                        [  确定  ][root@localhost ~]# systemctl stop firewalld.service [root@localhost ~]# setenforce 0[root@localhost ~]# systemctl start nfs.service[root@localhost ~]# systemctl status nfs.service● nfs-server.service - NFS server and services   Loaded: loaded (/usr/lib/systemd/system/nfs-server.service; disabled; vendor preset: disabled)   Active: active (exited) since 二 2019-11-26 17:42:05 CST; 11s ago......省略多行,状态为Active说明正常[root@localhost ~]# systemctl start rpcbind.service[root@localhost ~]# systemctl status rpcbind.service● rpcbind.service - RPC bind service   Loaded: loaded (/usr/lib/systemd/system/rpcbind.service; indirect; vendor preset: enabled)   Active: active (running) since 二 2019-11-26 17:40:23 CST; 4min 26s ago......省略多行,状态为Active说明正常[root@localhost ~]# vim /etc/exports/usr/share *(ro,sync)/opt/accp 192.168.200.0/24(rw,sync)/opt/benet 192.168.200.0/24(rw,sync)//加入以上内容之后输入:wq保存退出[root@localhost ~]# cd /opt/[root@localhost opt]# mkdir benet accp[root@localhost opt]# chmod 777 accp/ benet/        //提升权限[root@localhost opt]# exportfs -rv      //进行发布exporting 192.168.200.0/24:/opt/benetexporting 192.168.200.0/24:/opt/accpexporting *:/usr/share
节点服务器(CentOS 7-2)上的操作:
[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33BOOTPROTO="static"      //将dhcp改为staticDEVICE="ens33"ONBOOT="yes"IPADDR=192.168.200.110      //在末行下插入:IP,子网,网关NETMASK=255.255.255.0GATEWAY=192.168.200.1修改完成后输入:wq保存退出[root@localhost ~]# service network restart Restarting network (via systemctl):                        [  确定  ][root@localhost ~]# systemctl stop firewalld.service [root@localhost ~]# setenforce 0[root@localhost ~]# systemctl start httpd.service[root@localhost ~]# netstat -ntap | grep 80 tcp6       0      0 :::80               :::*            LISTEN          7315/httpd          //设置网络适配器为仅主机[root@localhost ~]# ping 192.168.200.130PING 192.168.200.130 (192.168.200.130) 56(84) bytes of data.64 bytes from 192.168.200.130: icmp_seq=1 ttl=64 time=0.754 ms64 bytes from 192.168.200.130: icmp_seq=2 ttl=64 time=0.368 ms64 bytes from 192.168.200.130: icmp_seq=3 ttl=64 time=0.398 ms[root@localhost ~]# showmount -e 192.168.200.130Export list for 192.168.200.130:/usr/share */opt/benet 192.168.200.0/24/opt/accp  192.168.200.0/24[root@localhost ~]# mount.nfs 192.168.200.130:/opt/accp /var/www/html/       //挂载[root@localhost ~]# cd /var/www/html/[root@localhost html]# echo "this is accp web" > index.html       //写首页内容

此时回到存储服务器CentOS 7-4上看是否有此文件:

[root@localhost ~]# cd /opt/[root@localhost opt]# lsaccp  benet  rh[root@localhost opt]# cd accp/[root@localhost accp]# lsindex.html[root@localhost accp]# cat index.html this is accp web//此时有我们新建的index.html文件

此时,在CentOS 7-2节点服务器上使用火狐浏览器输入:127.0.0.1看是否可以显示我们写入的网页内容:

节点服务器(CentOS 7-3)上的操作:
[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33BOOTPROTO="static"      //将dhcp改为staticDEVICE="ens33"ONBOOT="yes"IPADDR=192.168.200.120      //在末行下插入:IP,子网,网关NETMASK=255.255.255.0GATEWAY=192.168.200.1//修改完成后输入:wq保存退出[root@localhost ~]# service network restart Restarting network (via systemctl):                        [  确定  ][root@localhost ~]# systemctl stop firewalld.service [root@localhost ~]# setenforce 0[root@localhost ~]# systemctl start httpd.service[root@localhost ~]# netstat -ntap | grep 80 tcp6       0      0 :::80               :::*            LISTEN          7315/httpd   //设置网络适配器为仅主机[root@localhost ~]# ping 192.168.200.130PING 192.168.200.130 (192.168.200.130) 56(84) bytes of data.64 bytes from 192.168.200.130: icmp_seq=1 ttl=64 time=0.532 ms64 bytes from 192.168.200.130: icmp_seq=2 ttl=64 time=1.01 ms64 bytes from 192.168.200.130: icmp_seq=3 ttl=64 time=0.940 ms[root@localhost ~]# showmount -e 192.168.200.130Export list for 192.168.200.130:/usr/share */opt/benet 192.168.200.0/24/opt/accp  192.168.200.0/24[root@localhost ~]# mount.nfs 192.168.200.130:/opt/benet /var/www/html/[root@localhost ~]# cd /var/www/html/[root@localhost html]# echo "this is benet web" > index.html

此时回到存储服务器CentOS 7-4上看是否有此文件:

[root@localhost ~]# cd /opt/[root@localhost opt]# lsaccp  benet  rh[root@localhost opt]# cd accp/[root@localhost accp]# lsindex.html[root@localhost accp]# cat index.html this is benet web//此时有我们新建的index.html文件
验证:在CentOS 7-3节点服务器上使用火狐浏览器输入:127.0.0.1看是否可以显示我们写入的网页内容

调度,网关服务器CentOS 7-1的操作:

[root@localhost ~]# cd /etc/sysconfig/network-scripts/[root@localhost network-scripts]# cp ifcfg-ens33 ifcfg-ens36[root@localhost network-scripts]# vim ifcfg-ens36BOOTPROTO="static"      //将dhcp改为staticNAME="ens36"        //把名称改为ens36UUID号删除DEVICE="ens36"      //把名称改为ens36ONBOOT="yes"IPADDR=12.0.0.1     //在末行下插入:IP,子网NETMASK=255.255.255.0//修改完成后输入:wq保存退出[root@localhost network-scripts]# vim ifcfg-ens33BOOTPROTO="static"      //将dhcp改为staticDEVICE="ens33"ONBOOT="yes"        //在末行下插入:IP,子网IPADDR=192.168.200.1NETMASK=255.255.255.0//修改完成后输入:wq保存退出[root@localhost network-scripts]# service network restart Restarting network (via systemctl):                        [  确定  ][root@localhost network-scripts]# vim /etc/sysctl.conf //在最后一行按o转下行插入net.ipv4.ip_forward=1//修改完成后输入:wq保存退出[root@localhost network-scripts]# sysctl -pnet.ipv4.ip_forward = 1[root@localhost network-scripts]# iptables -t nat -F[root@localhost network-scripts]# iptables -F[root@localhost network-scripts]# iptables -t nat -A POSTROUTING -o ens36 -s 192.168.200.0/24 -j SNAT --to-source 12.0.0.1
win7-1中验证转发:
先把网卡设置为仅主机模式,网卡配置为静态地址,同时关闭防火墙:

加载LVS内核模块:
[root@localhost network-scripts]# modprobe ip_vs[root@localhost network-scripts]# cat /proc/net/ip_vsIP Virtual Server version 1.2.1 (size=4096)Prot LocalAddress:Port Scheduler Flags  -> RemoteAddress:Port Forward Weight ActiveConn InActConn
启动服务:(注意:在CentOS 7中必须要先保存在启动服务,否则会报错!!!)
[root@localhost network-scripts]# ipvsadm --save > /etc/sysconfig/ipvsadm[root@localhost network-scripts]# systemctl start ipvsadm.service 
编写配置规则:
[root@localhost network-scripts]# cd /opt/[root@localhost opt]# vim nat.sh#!/bin/bashipvsadm -C          //清除内核虚拟服务器表中的所有记录ipvsadm -A -t 12.0.0.1:80 -s rr         //添加新的虚拟的服务器ipvsadm -a -t 12.0.0.1:80 -r 192.168.200.110:80 -mipvsadm -a -t 12.0.0.1:80 -r 192.168.200.120:80 -mipvsadm//输入完成后输入:wq保存退出[root@localhost opt]# source nat.shIP Virtual Server version 1.2.1 (size=4096)Prot LocalAddress:Port Scheduler Flags  -> RemoteAddress:Port           Forward Weight ActiveConn InActConnTCP  localhost.localdomain:http rr  -> 129.168.200.110:http         Masq    1      0          0           -> 129.168.200.120:http         Masq    1      0          0       
验证:使用win7-1客户终端访问12.0.0.1,看是否可以成功:

看了以上关于LVS负载均衡群集实战详细介绍,如果大家还有什么地方需要了解的可以在行业资讯里查找自己感兴趣的或者找我们的专业技术工程师解答的,技术工程师在行业内拥有十几年的经验了。


0