01. 介绍
首先说明一下关于集群中的几个关键词,比如:高可用,负载均衡,主备,主主,主从等。那么,使用一个世界杯决赛的布局看一看,长宽比例大概是105:68的。
举几个例子说明一下:前端可以使用lvs、nginx等,负载可以使用lvs、proxy、nginx等,中间件可以使用nginx、tomcat、weblogic等,后端可以使用tomcat、Apache等部署,数据区域一般是数据库和存储,教练可以使用keepalive、heartbeat等来调用人员保证高可用,
如图:
02. 网络拓扑图
03.部署配置
说明:将lvs+keepalived部署在同一台服务上,部署了两台;tomcat+nginx部署在同一台机器上,部署了两台。一共部署了四台机器。
IP分配如上网络结构图所示。
3.1 主机keepalive+lvs的大体配置
keepalive的master部署配置:
$ cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
}
notification_email_from [email protected]
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state MASTER #标记为SLAVE
interface eth0
virtual_router_id 51
priority 110 #slave改为100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.116.199/24
}
}
virtual_server 192.168.116.199 8001 { #工程1
delay_loop 6
lb_algo rr
lb_kind DR
nat_mask 255.255.255.0
protocol TCP
real_server 192.168.116.195 8001 {
TCP_CHECK {
connect_timeout 10
}
}
real_server 192.168.116.196 8001 {
TCP_CHECK {
connect_timeout 10
}
}
}
virtual_server 192.168.116.199 8002 { #工程2
delay_loop 6
lb_algo rr
lb_kind DR
nat_mask 255.255.255.0
protocol TCP
real_server 192.168.116.195 8002 {
TCP_CHECK {
connect_timeout 10
}
}
real_server 192.168.116.196 8002 {
TCP_CHECK {
connect_timeout 10
}
}
}
lvs配置,这个已在keepalive配置文件配置了
$ sudo 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.116.199:8001 rr
-> 192.168.116.195:8001 Route 1 0 0
-> 192.168.116.196:8001 Route 1 0 0
TCP 192.168.116.199:8002 rr
-> 192.168.116.195:8002 Route 1 0 0
-> 192.168.116.196:8002 Route 1 0 0
开启路由转发功能:
$ sudo sysctl -p|grep forwar
net.ipv4.ip_forward = 1
查看漂移VIP:
$ ip addr |grep "global secondary"
inet 192.168.116.199/24 scope global secondary eth0
3.2 nginx+tomcat配置
nginx的配置文件:(另外一台也差不多:只需更改servername即可)
在这里负载了端口为8001和8002的工程
$ cat /etc/nginx/conf.d/nowtest.conf
upstream openapi {
server 192.168.116.195:8131 weight=1;
server 192.168.116.196:8131 weight=2;
}
upstream qr {
server 192.168.116.195:8132 weight=2;
server 192.168.116.196:8132 weight=1;
}
server {
listen 8001;
server_name 192.168.116.195; #另外一台更改为192.168.116.196
location ~ / {
index index.html index.htm index.jsp;
proxy_pass http://openapi;
proxy_set_header Host $host;
proxy_set_header X-Rerl-IP $remote_addr;
proxy_http_version 1.1;
client_max_body_size 100m;
}
}
server {
listen 8002;
server_name 192.168.116.195; #另外一台更改为192.168.116.196
location ~ / {
index index.html index.htm index.jsp;
proxy_pass http://qr;
proxy_set_header Host $host;
proxy_set_header X-Rerl-IP $remote_addr;
proxy_http_version 1.1;
client_max_body_size 100m;
}
}
tomcat配置就不贴出来了:
$ cat /opt/mai/apache-tomcat-8.5.23/conf/server.xml --就是配置了两个工程
可参照:tomcat不同端口部署多个工程配置
apr解析:
$ cat lvs_dr.sh
#!/bin/sh
VIP=192.168.116.199
/sbin/ifconfig lo:0 $VIP broadcast $VIP netmask 255.255.255.255 up
/sbin/route add -host $VIP dev lo:0
echo "1" > /proc/sys/net/ipv4/conf/lo/arp_ignore
echo "2" > /proc/sys/net/ipv4/conf/lo/arp_announce
echo "1" > /proc/sys/net/ipv4/conf/all/arp_ignore
echo "2" > /proc/sys/net/ipv4/conf/all/arp_announce
端口查看:
$ sudo netstat -tnlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 986/sshd
tcp 0 0 0.0.0.0:8001 0.0.0.0:* LISTEN 1379/nginx
tcp 0 0 0.0.0.0:8002 0.0.0.0:* LISTEN 1379/nginx
tcp 0 0 :::22 :::* LISTEN 986/sshd
tcp 0 0 :::8131 :::* LISTEN 1050/java
tcp 0 0 :::8132 :::* LISTEN 1050/java
tcp 0 0 ::ffff:127.0.0.1:8005 :::* LISTEN 1050/java
tcp 0 0 :::8009 :::* LISTEN 1050/java
04. 测试
做了2:1的权重,结果如下
$ curl http://192.168.116.199:8001
webapps-openapi6
$ curl http://192.168.116.199:8001
webapps-openapi6
$ curl http://192.168.116.199:8001
webapps-openapi5
$ curl http://192.168.116.199:8001
webapps-openapi5
$ curl http://192.168.116.199:8002
webapps-qr6
$ curl http://192.168.116.199:8002
webapps-qr5
$ curl http://192.168.116.199:8002
webapps-qr5
$ curl http://192.168.116.199:8002
webapps-qr5
05. 大型网站架构目标
有那么一些特点:高性能、高可用、可伸缩、可扩展、安全性等。
存在那么一些模式:分层、分割、分布、集群、缓存、异步、冗余、安全、自动等。