负载均衡用途:
1.实现用户访问进行合理调度处理,最终分配给不同web节点
2.实现用户访问压力分担,将网站压力分配给每一个节点
集群概念说明:
1.完成相同工作的不同主机服务器(web服务器)
2.一般集群中多台服务器配置都是一致的
分类:
负载均衡集群(Load balancing clusters):lb
高可用集群(High-avilability—HA):clusters ---keepalived
高性能计算机群:
网格计算集群:
集群存在的价值:
1.高性能计算机群:
2.价格有效性
3.可伸缩性
4.高可用性
负载均衡预习:
准备4台机器
lb01 10.0.0.5
lb02 10.0.0.6
web01 10.0.0.7
web02 10.0.0.8
第一个历程:安装依赖软件与nginx
yum install nginx openssl openssl-devel pcre pcre-devel -y
检查
rpm -qa nginx openssl openssl-devel pcre pcre-devel
第二个历程:编写nginx反向代理服务配置文件(lb01)
ngx_http_upstream_module 负载均衡
Syntax: upstream name { ... }
Default: —
Context: http
upstream oldboy { #定义将用户的访问请求可以分配给那些web节点主机
server 10.0.0.7:80;
server 10.0.0.8:80;
server 10.0.0.9:80;
}
ngx_http_proxy_module 反向代理
Syntax: proxy_pass URL;
Default: —
Context: location, if in location, limit_except
location / {
proxy_pass http://oldboy;
}
最终格式:
vim /etc/nginx/conf.d/lb.conf
[root@lb01 conf.d]# vim lb.conf
upstream oldboy {
server 10.0.0.7:80;
server 10.0.0.8:80;
server 10.0.0.9:80;
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://oldboy;
}
}
[root@lb01 conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@lb01 conf.d]# systemctl restart nginx
第三个里程:配置web服务器节点
编写节点配置文件:
[root@web01 conf.d]# vim lb_www.conf
server {
listen 80;
server_name www.oldboy.com;
location / {
root /usr/share/nginx/html/www;
index index.html;
}
}
[root@web01 conf.d]# vim lb_blog.conf
server {
listen 80;
server_name blog.oldboy.com;
location / {
root /usr/share/nginx/html/blog;
index index.html;
}
}
[root@web01 conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 conf.d]# systemctl restart nginx
调整站点目录信息:
web01:
[root@web01 html]# cat {www,blog}/index.html
web01-10.0.0.7-www
web01-10.0.0.7-blog
web02:
[root@web01 html]# rsync -avz --delete /etc/nginx/conf.d/ 172.16.1.8:/etc/nginx/conf.d/
[root@web02 conf.d]# cat /etc/nginx/conf.d/lb_*
server {
listen 80;
server_name blog.oldboy.com;
location / {
root /usr/share/nginx/html/blog;
index index.html;
}
}
server {
listen 80;
server_name www.oldboy.com;
location / {
root /usr/share/nginx/html/www;
index index.html;
}
}
[root@web02 html]# cat {www,blog}/index.html
web02-10.0.0.8-www
web02-10.0.0.8-blog
[root@web02 html]# systemctl reload nginx
第四个历程:进行访问测试
需要修改hosts解析文件
10.0.0.5 www.oldboy.com blog.oldboy.com