系统性能扩展方式:
LB: Load Balancing,一定高可用能力,但不是高可用集群,是以提高服务的并发处理能力为根本着眼点
HA:High Availiablity,高可用集群(增加服务可用性),高可用集群,是以提升服务的始终在线能力为着眼点,不会因为宕机而导致服务不能用
SLA:服务等级协议(简称: SLA,全称: service level agreement)。是在一定开销下为保障服务的性能和可用性,服务提供商与用户间定义的一种双方认可的协定。通常这个开销是驱动提供服务质量的主要因素。在常规的领域中,总是设定所谓的三个9,四个9来进行表示,当没有达到这种水平的时候,就会有一些列的惩罚措施,而运维,最主要的目标就是达成这种服务水平。
1年 = 365天 = 8760小时
99.9 = 8760 * 0.1% = 8760 * 0.001 = 8.76小时
99.99 = 876 * 0.0001 = 0.876小时 = 0.876 * 60 = 52.6分钟
99.999 = 8760 * 0.00001 = 0.0876小时 = 0.0876 * 60 = 5.26分钟
停机时间又分为两种,一种是计划内停机时间,一种是计划外停机时间,而运维则主要关注计划外停机时间。
传输层(通用): DNAT和DPORT
LVS:
nginx: stream
haproxy: mode tcp
应用层(专用):针对特定协议,常称为proxy server
http: nginx, httpd, haproxy(mode http), …
fastcgi: nginx,httpd, …
mysql: mysql-proxy,…
工作原理
注意:在NAT模式中,Real Server的网关必须指向LVS,否则报文无法送达客户端
优点:节约IP地址,网络隔离更安全
缺点:LVS很可能成为系统性能瓶颈,所有的请求都需要应答
使用五台主机 CentOS Linux release 8.3.2011
Client: 桥接本地 192.168.2.181
LVS: 桥接本地 192.168.2.151
VMnet2 192.168.1.151
Web1: VMnet2 192.168.1.161
Web2: VMnet2 192.168.1.162
Web2: VMnet2 192.168.1.163
[root@localhost ~]# yum install nginx -y
[root@localhost ~]# systemctl enable nginx --now && systemctl stop firewalld
[root@localhost ~]# cat /etc/selinux/config
SELINUX=disabled
[root@localhost ~]# nmcli con mo ens33 ipv4.gateway 192.168.1.151 && nmcli con up ens33
[root@localhost ~]# echo "192.168.1.161" > /usr/share/nginx/html/index.html
这里注意,启动服务之前需要保存负载配置,不然启动会报错,查看日志可以看到,没有哪个文件或目录
[root@localhost ~]# yum install ipvsadm -y
[root@localhost ~]# systemctl start ipvsadm
Job for ipvsadm.service failed because the control process exited with error code.
See "systemctl status ipvsadm.service" and "journalctl -xe" for details.
[root@localhost ~]# ipvsadm-save > /etc/sysconfig/ipvsadm
[root@localhost ~]# systemctl start ipvsadm
[root@localhost ~]# echo "net.ipv4.ip_forward=1" >> /etc/sysctl.conf
[root@localhost ~]# sysctl -p
net.ipv4.ip_forward = 1
[root@localhost ~]# iptables -t nat -F
[root@localhost ~]# iptables -F
[root@localhost ~]# iptables -t nat -A POSTROUTING -s 192.168.2.0/24 -o ens37 -j SNAT --to-source 192.168.2.151
root@localhost ~]# modprobe ip_Vs #加载ip_vs模块
[root@localhost ~]# cat /proc/net/ip_vs #查看ip vs版本信息
[root@localhost ~]# for i in $(ls /usr/1ib/modules/$(uname -r)/kernel/net/netfilter/ipvslgrep -o "^[^.]*");do echo $i; /sbin/modinfo -F filename $i >/dev/null 2>&1 && /sbin/modprobe $i;done
[root@localhost ~]# systemctl start ipvsadm
[root@localhost ~]# ipvsadm -C
[root@localhost ~]# ipvsadm -A -t 192.168.2.151:80 -s rr #-A:对外提供的地址 -t:tcp -s:策略 rr:轮巡
[root@localhost ~]# ipvsadm -a -t 192.168.2.151:80 -r 192.168.1.161 -m #-a:对内真实服务器 -r:真实 -m:地址伪装
[root@localhost ~]# ipvsadm -a -t 192.168.2.151:80 -r 192.168.1.162 -m
[root@localhost ~]# ipvsadm -a -t 192.168.2.151:80 -r 192.168.1.163 -m
[root@localhost ~]# ipvsadm #启用策略
[root@localhost ~]# ipvsadm -ln #查看节点状态,Masq代表NAT模式
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.2.151:80 rr
-> 192.168.1.161:80 Masq 1 0 0
-> 192.168.1.162:80 Masq 1 0 0
-> 192.168.1.163:80 Masq 1 0 0
[root@localhost ~]# curl 192.168.2.151
192.168.1.163
[root@localhost ~]# curl 192.168.2.151
192.168.1.162
[root@localhost ~]# curl 192.168.2.151
192.168.1.161
[root@localhost ~]# curl 192.168.2.151
192.168.1.163
[root@localhost ~]# curl 192.168.2.151
192.168.1.162
[root@localhost ~]# tail -f /var/log/nginx/access.log
192.168.2.181 - - [05/May/2021:01:39:42 -0400] "GET / HTTP/1.1" 200 14 "-" "curl/7.61.1" "-"
[root@localhost nginx]# tail -f /var/log/nginx/access.log
192.168.2.181 - - [05/May/2021:01:39:43 -0400] "GET / HTTP/1.1" 200 14 "-" "curl/7.61.1" "-"
192.168.2.181 - - [05/May/2021:01:39:48 -0400] "GET / HTTP/1.1" 200 14 "-" "curl/7.61.1" "-"```
[root@localhost nginx]# tail -f /var/log/nginx/access.log
192.168.2.181 - - [05/May/2021:01:16:27 -0400] "GET / HTTP/1.1" 200 14 "-" "curl/7.61.1" "-"
192.168.2.181 - - [05/May/2021:01:16:33 -0400] "GET / HTTP/1.1" 200 14 "-" "curl/7.61.1" "-"
[root@localhost network-scripts]# ipvsadm -ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.2.151:80 rr
-> 192.168.1.161:80 Masq 1 0 1
-> 192.168.1.162:80 Masq 1 0 2
-> 192.168.1.163:80 Masq 1 0 2
[root@localhost ~]# ipvsadm-save > /etc/sysconfig/ipvsadm #备份LVS策略
[root@localhost ~]# ipvsadm -C
[root@localhost ~]# ipvsadm -ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
[root@localhost ~]# ipvsadm-restore < /etc/sysconfig/ipvsadm #恢复LVS策略
[root@localhost ~]# ipvsadm -ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 127.0.0.1:80 rr
-> 127.0.0.1:80 Masq 1 0 0
-> 192.168.1.162:80 Masq 1 0 0
-> 192.168.1.163:80 Masq 1 0 0