client | | LVS | | ------------------- | | | RS1 RS2 RS3
机器名称 | ip配置 | ip配置 | 备注信息 |
---|---|---|---|
LVS | 192.168.2.23 | 192.168.11.11 | 2块网卡 |
RS1 | 192.168.11.12 | ||
RS2 | 192.168.11.13 |
[root@lvs ~]# grep -i vs /boot/config-3.10.0-229.el7.x86_64 有IP_VS段说明支持
yum install -y ipvsadm
ipvsadm :
管理集群服务
添加:-A -t|u|f service-address [-s scheduler]
-t:TCP协议
-u:UDP协议
-f:FWM,防火墙标记
修改:-E
删除:-D -t|u|f service-address
# ipvsadm -A -t 192.168.2.23:80 -s rr
管理集群服务中的RS
添加:-a -t|u|f service-address -r server-address [-g|i|m] [-w weight]
-t|u|f service-address:事先定义好的某集群服务
-r server-address:某RS的地址,在NAT模型中,可以使用IP:PORT实现映射
[-g|i|m]:LVS类型
-g:DR模型
-i:TUN模型
-m:NAT模型
[-w weight]:定义服务器权重
修改:-e
删除:-d -t|u|f service-address -r server-address
# ipvsadm -a -t 192.168.2.23:80 -r 192.168.11.11 -m
# ipvsadm -a -t 192.168.2.23:80 -r 192.168.11.13 -m
查看:-L|l
-n:数字格式显示主机地址和端口
--stats:统计数据
--rate:速率
--timeout:显示tcp、tcpfin和udp的会话超时时长
-c:显示当前的ipvs链接情况
删除全部集群服务 html
-C:清空ipvs规则
保存规则
-S
# ipvsadm -S > /ath/to/somfefile
载入此前的规则:
-R
# ipvsadm -R < /path/from/somefile
前端
[root@lvs ~]# ipvsadm -A -t 192.168.2.23:80 -s rr [root@lvs ~]# ipvsadm -a -t 192.168.2.23:80 -r 192.168.11.12 -m [root@lvs ~]# ipvsadm -a -t 192.168.2.23:80 -r 192.168.11.13 -m
echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf
sysctl -p 使配置文件生效node
root@node2:~# curl http://192.168.2.23/index.html rs1 root@node2:~# curl http://192.168.2.23/index.html rs2 root@node2:~# curl http://192.168.2.23/index.html rs1 root@node2:~# curl http://192.168.2.23/index.html rs2 root@node2:~# curl http://192.168.2.23/index.html rs1 root@node2:~# curl http://192.168.2.23/index.html rs2 root@node2:~# curl http://192.168.2.23/index.html rs1 root@node2:~# curl http://192.168.2.23/index.html rs2
NAT模式:客户端-->分发路由器-->Real Server-->分发路由器-->客户机
DR模式:客户端-->分发路由器--Real Server-->客户机
TUN模式:客户机-->分发路由器--Real Server-->客户机linux
client | | Router | | ------------------------------------ | | | RS1 LVS RS3 Rip:192.168.2.72 Rip:192.168.2.23 Rip:192.168.2.104 Vip:192.168.2.200 Vip:192.168.2.200 Vip:192.168.2.200
ip addr add 192.168.2.200/32 dev eno16777736:1 ipvsadm -A -t 192.168.2.200:80 -s rr ipvsadm -a -t 192.168.2.200:80 -r 192.168.2.72 -g ipvsadm -a -t 192.168.2.200:80 -r 192.168.2.104:80 -g
arp_ignore参数的做用是控制系统在收到外部的ARP请求, 是否要回返ARP响应。nginx
8 - 不回应全部的ARP查询web
2 - IP数据包的源IP地址,选择该发送网卡上最合适的本地地址做为ARP请求的源IP地址缓存
ip addr add 192.168.2.200/32 dev lo:1 echo 1 > /proc/sys/net/ipv4/conf/eno16777736/arp_ignore echo 2 > /proc/sys/net/ipv4/conf/eno16777736/arp_announce
root@node2:~# curl http://192.168.2.200/index.html rs1 root@node2:~# curl http://192.168.2.200/index.html rs2 root@node2:~# curl http://192.168.2.200/index.html rs1 root@node2:~# curl http://192.168.2.200/index.html rs2 root@node2:~# curl http://192.168.2.200/index.html rs1 root@node2:~# curl http://192.168.2.200/index.html rs2
代理服务器,客户机在发送请求时,不会直接发送给目的主机,而是先发送给代理服务器,代理服务器接受客户请求以后,再向主机发出,并接收目的主机返回的数据,存放在代理服务器的磁盘中,再发送给客户机。安全
经过代理服务器访问不能访问的目标站点
互联网上有施工开发的代理服务器,客户机在访问受限时,可经过不受限的代理服务器访问目标站点服务器
反向代理有哪些主要应用
如今许多大型web网站都用到反向代理。除了能够防止网网对内网服务器的恶意攻击、缓存以减小服务器的压力和访问安全控制以外,还能够进行负载均衡,将用户请求分配给多个服务器。网络
新建配置文件/etc/nginx/conf.d/proxy.conf
upstream websrvs { server 192.168.2.72:80; server 192.168.2.104:80; } server { listen 80 default_server; server_name _; location / { proxy_pass http://websrvs; index index.html ; } }
root@node2:~# curl http://192.168.2.23/index.html rs2 root@node2:~# curl http://192.168.2.23/index.html rs1 root@node2:~# curl http://192.168.2.23/index.html rs2 root@node2:~# curl http://192.168.2.23/index.html rs1 root@node2:~# curl http://192.168.2.23/index.html rs2 root@node2:~# curl http://192.168.2.23/index.html rs2 root@node2:~# curl http://192.168.2.23/index.html rs1 root@node2:~# curl http://192.168.2.23/index.html rs2 root@node2:~# curl http://192.168.2.23/index.html rs1 root@node2:~# curl http://192.168.2.23/index.html rs2 root@node2:~# curl http://192.168.2.23/index.html
若是一台服务器有多个IP,可使用基于IP的虚拟主机配置,将不一样的服务绑定在不一样的IP上。
[root@lvs conf.d]# ip addr add 192.168.2.151/24 dev eno16777736:2 [root@lvs conf.d]# ip addr add 192.168.2.152/24 dev eno16777736:3 [root@lvs conf.d]# ip addr add 192.168.2.154/24 dev eno16777736:4
[root@lvs conf.d]# mkdir -pv /data/www/15{1..3} mkdir: 已建立目录 "/data" mkdir: 已建立目录 "/data/www" mkdir: 已建立目录 "/data/www/151" mkdir: 已建立目录 "/data/www/152" mkdir: 已建立目录 "/data/www/153" [root@lvs conf.d]# echo server151 > /data/www/151/index.html [root@lvs conf.d]# echo server152 > /data/www/152/index.html [root@lvs conf.d]# echo server153 > /data/www/153/index.html
/etc/nginx/conf.d/test.conf
server { listen 192.168.2.151:80; server_name www.test.com; location / { root /data/www/151; index index.html; } } server { listen 192.168.2.152:80; server_name www.test.com; location / { root /data/www/152; index index.html; } } server { listen 192.168.2.153:80; server_name www.test.com; location / { root /data/www/153; index index.html; } }
root@node2:~# curl http://192.168.2.153/index.html server153 root@node2:~# curl http://192.168.2.152/index.html server152 root@node2:~# curl http://192.168.2.151/index.html server151
若是一台服务器只有一个IP或须要经过不一样的端口访问不一样的虚拟主机,可使用基于端口的虚拟主机配置
[root@lvs conf.d]# mkdir /data/www/{7..9}081 [root@lvs conf.d]# echo "port 7081" > /data/www/7081/index.html [root@lvs conf.d]# echo "port 8081" > /data/www/8081/index.html [root@lvs conf.d]# echo "port 9081" > /data/www/9081/index.html
/etc/nginx/conf.d/port.conf
server { listen 192.168.2.155:7081; server_name www.port.com; location / { root /data/www/7081; index index.html; } } server { listen 192.168.2.155:8081; server_name www.port.com; location / { root /data/www/8081; index index.html; } } server { listen 192.168.2.155:9081; server_name www.port.com; location / { root /data/www/9081; index index.html; } }
测试结果:
root@node2:~# curl http://192.168.2.155:7081/index.html port 7081 root@node2:~# curl http://192.168.2.155:8081/index.html port 8081 root@node2:~# curl http://192.168.2.155:9081/index.html port 9081
使用基于域名的虚拟主机是比较流行的方式,能够在同一个IP上配置多个域名,而且都经过80端口访问
[root@lvs conf.d]# mkdir /data/www/www.oa.com [root@lvs conf.d]# mkdir /data/www/www.bbs.com [root@lvs conf.d]# mkdir /data/www/www.test.com [root@lvs conf.d]# echo www.oa.com > /data/www/www.oa.com/index.html [root@lvs conf.d]# echo www.bbs.com > /data/www/www.bbs.com/index.html [root@lvs conf.d]# echo www.test.com > /data/www/www.test.com/index.html
/etc/nginx/conf.d/vhost.conf
server { listen 192.168.2.155:80; server_name www.oa.com; location / { root /data/www/www.oa.com; index index.html; } } server { listen 192.168.2.155:80; server_name www.bbs.com; location / { root /data/www/www.bbs.com; index index.html; } } server { listen 192.168.2.155:80; server_name www.test.com; location / { root /data/www/www.test.com; index index.html; } }
root@node2:~# curl http://www.oa.com/index.html www.oa.com root@node2:~# curl http://www.bbs.com/index.html www.bbs.com root@node2:~# curl http://www.test.com/index.html www.test.com
参考文档: https://www.cnblogs.com/zhaojiedi1992/p/zhaojiedi_linux_021_lvsnat.html https://www.cnblogs.com/knowledgesea/p/6407018.html#undefined https://www.cnblogs.com/lipengxiang2009/p/7451050.html https://blog.csdn.net/Daybreak1209/article/details/51549031 https://blog.csdn.net/liupeifeng3514/article/details/79006998 https://blog.csdn.net/liupeifeng3514/article/details/79007035 https://blog.csdn.net/liupeifeng3514/article/details/79007051