Squid+LVS练习

实验环境

角色SquidLVS 1LVS 2Web 1Web 2Client
IP地址192.168.1.10192.168.1.11192.168.1.12192.168.1.21192.168.1.22192.168.1.88
虚拟IP192.168.1.100192.168.1.200

squid

安装squid

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

修改squid配置文件

[root@localhost ~]# vim /etc/squid/squid.conf

# cache_peer 指定后端服务器地址,80 为后端服务端口,0 为 ICP 端口号(多个 Squid 时用),originserver 指定资源服务器,round_robin开启轮询并指定轮询算法,name 可以指定一个别名

初始化squid缓存目录

[root@localhost ~]# squid -z

启动squid服务

[root@localhost ~]# systemctl start squid.service

LVS 1 /  LVS 2

添加虚拟IP

[root@localhost ~]# cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# cp ifcfg-ens33 ifcfg-ens33:1
[root@localhost network-scripts]# vim ifcfg-ens33:1
# 修改以下参数即可
NAME=ens33:1
DEVICE=ens33:1
IPADDR=192.168.1.100
# 重启网卡
[root@localhost network-scripts]# systemctl restart network

加载ip_vs模块

[root@localhost ~]# modprobe ip_vs

安装LVS命令行工具

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

创建集群

# LVS 1
[root@localhost ~]# ipvsadm -A -t 192.168.1.100:80 -s rr

# LVS 2
[root@localhost ~]# ipvsadm -A -t 192.168.1.200:80 -s rr

将节点加入集群

向虚拟服务器中添加web服务器的真实IP,这样客户端在访问虚拟IP的时候就能访问到web服务器 -g表示使用DR模式

# LVS 1
[root@localhost ~]# ipvsadm -a -t 192.168.1.100:80 -r 192.168.1.22 -g
[root@localhost ~]# ipvsadm -a -t 192.168.1.100:80 -r 192.168.1.33 -g

# LVS 2
[root@localhost ~]# ipvsadm -a -t 192.168.1.200:80 -r 192.168.1.21 -g
[root@localhost ~]# ipvsadm -a -t 192.168.1.200:80 -r 192.168.1.22 -g

保存节点状态

[root@localhost ~]# ipvsadm -S

Web1 / Web2

添加虚拟IP

[root@localhost ~]# cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# cp ifcfg-lo ifcfg-lo:1
[root@localhost network-scripts]# vim ifcfg-lo:1
# 修改为以下参数
DEVICE=lo:1
IPADDR=192.168.1.100
NETMASK=255.255.255.255
# If you're having problems with gated making 127.0.0.0/8 a martian,
# you can change this to something else (255.255.255.255, for example)
ONBOOT=yes
NAME=web1    # web2的名称为web2
[root@localhost network-scripts]# cp ifcfg-lo ifcfg-lo:2
[root@localhost network-scripts]# vim ifcfg-lo:2
DEVICE=lo:2
IPADDR=192.168.1.200
NETMASK=255.255.255.255
# If you're having problems with gated making 127.0.0.0/8 a martian,
# you can change this to something else (255.255.255.255, for example)
ONBOOT=yes
NAME=web1    # web2的名称为web2
# 重启网卡
[root@localhost network-scripts]# systemctl restart network

添加内核参数

[root@localhost ~]# vim /etc/sysctl.conf 
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.default.arp_ignore = 1      # 关闭本地循环的arp请求
net.ipv4.conf.lo.arp_announce = 2
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.default.arp_announce = 2    # 开启ens33网卡的arp请求

[root@localhost ~]# sysctl -p     # 立即生效

安装nginx

修改nginx配置文件

# 修改此参数等于0即可
  keepalive_timeout 0;

重载nginx配置文件

[root@localhost ~]# nginx -s reload

修改nginx前端页面(web1)

[root@localhost ~]# echo web111111111 > /usr/local/nginx/html/index.html

修改nginx前端页面(web2)

[root@localhost ~]# echo web222222222 > /usr/local/nginx/html/index.html

Client

测试访问

[root@localhost ~]# firefox 192.168.1.10
web111111111
[root@localhost ~]# firefox 192.168.1.10
web2222222222
[root@localhost ~]# firefox 192.168.1.10
web111111111
[root@localhost ~]# firefox 192.168.1.10
web222222222
Last modification:July 23rd, 2020 at 09:35 pm
如果觉得我的文章对你有用,请随意赞赏