集群的分类:
负载均衡的有点:
负载均衡集群实现方式:
lvs的三种模式:
lvs arch
Client IP:CIP(客户端IP)
Director Virutal IP:VIP(调度器虚拟IP)
Director IP:DIP(调度器IP)
Real Server IP:RIP(真实服务器IP)
工作原理
lvs-nat模式通过修改请求报文的目标IP地址(同时可能会修改目标端口)至挑选出来的某RS的RIP地址实现转发
lvs-nat的特点:
lvs-dr(direct routing):lvs的默认模式,gateway
工作原理
lvs-tun模式不修改请求报文的ip首部,而是通过在原有的ip首部(CIP<–>VIP)之外再封装一个ip首部(DIP<–>RIP)
lvs-tun的特点:
lvs-dr(direct routing):lvs的默认模式,gateway
工作原理
lvs-dr模式通过修改请求报文的目标MAC地址进行转发
Director:调度器要配置VIP,DIP
RSs:所有的Real Server都要配置RIP,VIP
lvs-dr的特点:
lvs scheduler:lvs调度器,即lvs挑选RS的算法
ipvs的集群服务:
管理集群服务:
//管理集群服务(调度器DR) ipvsadm -A|E -t|u|f service-address [-s scheduler] [-p [timeout]] [-M netmask] //新增或编辑 ipvsadm -D -t|u|f service-address //删除 常见的service-address: tcp:-t ip:port udp:-u ip:port fwm:-f mark -s scheduler:默认为wlc -p [timeout]:定义持久连接,timeout不指定时默认为300秒 //管理集群服务中的RS ipvsadm -a|e -t|u|f service-address -r server-address [-g|i|m] [-w weight] [-x upper] [-y lower] ipvsadm -d -t|u|f service-address -r server-address ipvsadm -L|l [options] server-address:ip[:port] lvs-type: -g:gateway,dr模式 -i:ipip,tun模式 -m:masquerade,nat模式 -w:设定权重,可为0到正无穷数值,设为0时不会被调度,默认为1 权重值越大则表示性能越好,被调度的资源也会更多 //清空和查看: ipvsadm -C 清空 ipvsadm -L|l [options] 查看 options: -n:numeric,基于数字格式显示地址和端口 -c:connection,显示当前ipvs连接 --stats:统计数据 --rate:输出速率信息 --exact:显示精确值,不做单位换算 //保存和重载: ipvsadm -R 重载 ipvsadm -S [-n] 保存
主机名 | IP |
---|---|
DR | 192.168.218.130 |
RS1 | 192.168.218.132 |
RS2 | 192.168.218.133 |
[root@DR ~]# systemctl disable --now firewalld [root@DR ~]# vi /etc/selinux/config SELINUX=disabled [root@DR ~]# reboot [root@RS1 ~]# systemctl disable --now firewalld [root@RS1 ~]# vi /etc/selinux/config SELINUX=disabled [root@RS1 ~]# reboot [root@RS2 ~]# systemctl disable --now firewalld [root@RS2 ~]# vi /etc/selinux/config SELINUX=disabled [root@RS2 ~]# reboot
//安装服务 [root@RS1 ~]# yum -y install httpd [root@RS2 ~]# yum -y install httpd //配置页面 [root@RS1 ~]# cd /var/www/html/ [root@RS1 html]# ls [root@RS1 html]# echo 'test rs1' > index.html [root@RS2 ~]# cd /var/www/html/ [root@RS2 html]# ls [root@RS2 html]# echo 'test rs2' > index.html //启动服务 [root@SR1 ~]# systemctl enable --now httpd //修改配置文件 [root@SR1 ~]# cd /etc/httpd/conf [root@SR1conf]# vi httpd.conf 取消前面的#号 ServerName www.example.com:80 //重启服务 [root@SR1 conf]# systemctl restart httpd //SR2和SR1一样的操作
网页查看
//查看IP [root@DR ~]# ip a 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000 link/ether 00:0c:29:9e:92:e1 brd ff:ff:ff:ff:ff:ff inet 192.168.218.130/24 brd 192.168.218.255 scope global dynamic noprefixroute ens160 valid_lft 1626sec preferred_lft 1626sec inet6 fe80::d68f:1be3:2ad4:4605/64 scope link noprefixroute valid_lft forever preferred_lft forever //安装ipvsadm包 [root@DR ~]# yum -y install ipvsadm //添加网卡到lo [root@DR ~]# ip addr add 192.168.218.250/32 dev lo [root@DR ~]# ip a 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet 192.168.218.250/32 scope global lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000 link/ether 00:0c:29:9e:92:e1 brd ff:ff:ff:ff:ff:ff inet 192.168.218.130/24 brd 192.168.218.255 scope global dynamic noprefixroute ens160 valid_lft 1575sec preferred_lft 1575sec inet6 fe80::d68f:1be3:2ad4:4605/64 scope link noprefixroute valid_lft forever preferred_lft forever //测试能否ping通 [root@DR ~]# ping 192.168.218.250 PING 192.168.218.250 (192.168.218.250) 56(84) bytes of data. 64 bytes from 192.168.218.250: icmp_seq=1 ttl=64 time=0.015 ms 64 bytes from 192.168.218.250: icmp_seq=2 ttl=64 time=0.074 ms 64 bytes from 192.168.218.250: icmp_seq=3 ttl=64 time=0.039 ms ^C --- 192.168.218.250 ping statistics --- 3 packets transmitted, 3 received, 0% packet loss, time 43ms rtt min/avg/max/mdev = 0.015/0.042/0.074/0.025 ms
//修改RS上的网卡内核参数 [root@SR1 ~]# vi /etc/sysctl.conf net.ipv4.conf.all.arp_ignore = 1 //添加此行 net.ipv4.conf.all.arp_announce = 2 //添加此行 [root@RS1 ~]# sysctl -p net.ipv4.conf.all.arp_ignore = 1 net.ipv4.conf.all.arp_announce = 2 //RS2相同操作 [root@SR2 ~]# vi /etc/sysctl.conf net.ipv4.conf.all.arp_ignore = 1 //添加此行 net.ipv4.conf.all.arp_announce = 2 //添加此行 [root@SR2 ~]# sysctl -p net.ipv4.conf.all.arp_ignore = 1 net.ipv4.conf.all.arp_announce = 2
//RS1添加vip [root@RS1 ~]# ip addr add 192.168.218.250 dev lo [root@RS1 ~]# ip a 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet 192.168.218.250/32 scope global lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000 link/ether 00:0c:29:bf:07:4a brd ff:ff:ff:ff:ff:ff inet 192.168.218.132/24 brd 192.168.218.255 scope global dynamic noprefixroute ens160 valid_lft 1346sec preferred_lft 1346sec inet6 fe80::de23:cbad:4ba:e181/64 scope link noprefixroute valid_lft forever preferred_lft forever //RS2添加vip [root@SR2 ~]# ip a 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet 192.168.218.250/32 scope global lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000 link/ether 00:0c:29:03:ca:ed brd ff:ff:ff:ff:ff:ff inet 192.168.218.133/24 brd 192.168.218.255 scope global dynamic noprefixroute ens160 valid_lft 1489sec preferred_lft 1489sec inet6 fe80::6faa:c998:4390:2ee4/64 scope link noprefixroute valid_lft forever preferred_lft forever
[root@DR ~]# route add -host 192.168.218.250 dev lo [root@RS1 ~]# route add -host 192.168.218.250 dev lo [root@SR2 ~]# route add -host 192.168.218.250 dev lo
[root@DR ~]# ipvsadm -A -t 192.168.218.250:80 -s wrr [root@DR ~]# ipvsadm -a -t 192.168.218.250:80 -r 192.168.218.132:80 -g [root@DR ~]# ipvsadm -a -t 192.168.218.250:80 -r 192.168.218.133:80 -g
PS C:\WINDOWS\system32> curl http://192.168.218.132 Content : test rs1 PS C:\WINDOWS\system32> curl http://192.168.218.133 Content : test rs2