一、 集群的分类
1、高可用集群 HA high availability
优点:避免单节点故障,另一节点能迅速顶替工作
软件:keepalived
2、负载均衡集群 LB load balance
优点:提高负载,提高并发量
软件:nginx反向代理,lvs
硬件:F5(BigIP)和redware
3、分布式存储集群
优点:极大提升存储容量,提高数据可用性,保证数据安全
软件:ceph
二、nginx proxy实现7层负载均衡
拓扑图
编写nginx配置文件vim /etc/nginx/nginx.conf
location / {
root /usr/share/nginx/html;
index index.html index.htm;
if ($request_uri ~* \.html$) {
proxy_pass http://htmlserver;
}
if ($request_uri ~* \.php$) {
proxy_pass http://phpserver;
}
}
配置upstream,在nginx主配置文件http内或者在conf.d目录下写一个.conf结尾的文件。
upstream htmlserver {
server 192.168.122.10;
server 192.168.122.20;
}
upstream phpserver {
server 192.168.122.30;
server 192.168.122.40;
}
重启nginx服务,修改后端服务器的index.html和index.php并测试。
用elinks –dump http:// 192.168.122.254测试
三、upstream支持的负载均衡算法
1、轮询(默认)rr round robin
可以通过weight指定轮询的权重,权重越大,被调度的次数越多
2、ip_hash
根据每个请求IP进行调度,可以解决session的问题,不能使用weight
3、fair
可以根据请求页面的大小和加载时间长短进行调度,使用第三方的upstream_fair模块
4、url_hash
按请求的url的hash进行调度,从而使每个url定向到同一服务器,使用第三方的hash模块
四、upstream支持的状态参数
down: 暂停对该服务器的调度
backup: 类似于LVS Sorry Server,当所有的非backup的服务器故障
max_fails: 请求失败的次数,默认为1
fail_timeout: 在经历max_fails次失败后,暂停服务的时间
upstream tianyun.com {
# ip_hash;
server 192.168.10.137 weight=1 max_fails=2 fail_timeout=2;
server 192.168.10.20 weight=2 max_fails=2 fail_timeout=2;
server 192.168.10.251 max_fails=2 fail_timeout=5 down;
server 192.168.10.253 backup;
}
注:当使用ip_hash时,服务器状态不可使用weight和backup
五、示例
1、调度到同一组上游服务器
配置示例
http {
upstream httpservers {
server 192.168.1.3:80 weight=1 max_fails=2 fail_timeout=2;
server 192.168.1.4:80 weight=2 max_fails=2 fail_timeout=2;
server 192.168.1.5:80 weight=2 max_fails=2 fail_timeout=2;
server 192.168.1.100:80 backup;
}
location / {
proxy_pass http://httpservers;
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
}
}
proxy_next_upstream:这个指令属于 http_proxy 模块的,指定后端返回什么样的异常响应时,使用另一个realserver
2、调度到不同组上游服务器
配置示例
http {
upstream news {
server 192.168.1.11:80 weight=1 max_fails=2 fail_timeout=2;
server 192.168.1.12:80 weight=2 max_fails=2 fail_timeout=2;
server 192.168.1.13:80 weight=2 max_fails=2 fail_timeout=2;
}
upstream milis {
server 192.168.1.21:80 weight=1 max_fails=2 fail_timeout=2;
server 192.168.1.22:80 weight=2 max_fails=2 fail_timeout=2;
server 192.168.1.23:80 weight=2 max_fails=2 fail_timeout=2;
}
upstream videos {
server 192.168.1.31:80 weight=1 max_fails=2 fail_timeout=2;
server 192.168.1.32:80 weight=2 max_fails=2 fail_timeout=2;
server 192.168.1.33:80 weight=2 max_fails=2 fail_timeout=2;
}
upstream images {
server 192.168.1.41:80 weight=1 max_fails=2 fail_timeout=2;
server 192.168.1.42:80 weight=2 max_fails=2 fail_timeout=2;
server 192.168.1.43:80 weight=2 max_fails=2 fail_timeout=2;
}
upstream others {
server 192.168.1.51:80 weight=1 max_fails=2 fail_timeout=2;
server 192.168.1.52:80 weight=2 max_fails=2 fail_timeout=2;
server 192.168.1.53:80 weight=2 max_fails=2 fail_timeout=2;
}
server {
location / {
proxy_pass http://others;
}
location /news {
proxy_pass http://news;
}
location /mili {
proxy_pass http://milis;
}
location ~* \.(wmv|mp4|rmvb)$ {
proxy_pass http://videos;
}
location ~* \.(png|gif|jpg)$ {
proxy_pass http://images;
}
}
动静分离示例
http {
upstream htmlservers {
server 192.168.1.3:80 weight=1 max_fails=2 fail_timeout=2;
server 192.168.1.4:80 weight=2 max_fails=2 fail_timeout=2;
}
upstream phpservers {
server 192.168.1.3:80 weight=1 max_fails=2 fail_timeout=2;
server 192.168.1.4:80 weight=2 max_fails=2 fail_timeout=2;
}
server {
location ~* \.html$ {
proxy_pass http://htmlservers;
}
location ~* \.php$ {
proxy_pass http://phpservers;
}
}
}