###负载均衡概念
1、实现用户访问调度处理,最终分配给不同web节点上
2、实现用户访问的压力进行分担,将网站压力分配给每一个节点
####实现负载均衡的方式
硬件设备
F5 A10 Redware
开源软件
Nginx Haproxy Lvs
####lvs、haproxy、nginx区别
#####从命名分析
负载均衡:用户请求的转发 (lvs)
反向代理:代替用户去找,找到后再发给用户 (nginx haproxy)
#####从功能分析
1、lvs是4层负载均衡 传输层tap/udp 端口号
最多进行端口转发
2、nginx haproxy是4层和7层负载均衡 应用层 进行http协议和uri转发
###负载均衡与反向代理区别
准备环境工作
####配置nginx安装源然后安装
[oot@lb01 ~]# vim /etc/yum.repos.d/nginx.repo
▽
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1
####修改web01 web02配置文件
记得提前备份
[root@web01 /etc/nginx/conf.d]# cat 01-www.conf
server {
listen 80;
server_name www.oldboy.com;
access_log /var/log/nginx/access_www.log main ;
root /app/www;
location / {
index index.html index.htm;
}
}
[root@web01 /etc/nginx/conf.d]# cat 02-blog.conf
server {
listen 80;
server_name blog.oldboy.com;
access_log /var/log/nginx/access_blog.log main;
root /app/blog;
location / {
index index.php index.html index.htm;
}
location ~* \.(php|php5)$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_buffers 16 16k;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
重启nginx检查语法
####web01 web02 创建站点目录与首页文件
俩边都相同
[root@web01 /etc/nginx/conf.d]# mkdir -p /app/{www,blog}
[root@web01 /etc/nginx/conf.d]# for n in www blog ; do echo $n.oldboy.com >/app/$n/index.html ;done
[root@web01 /etc/nginx/conf.d]# tree /app/
/app/
├── blog
│ └── index.html
└── www
└── index.html
2 directories, 2 files
[root@web02 conf.d]# tree /app
/app
├── blog
│ └── index.html
└── www
└── index.html
2 directories, 2 files
####去db01上curl一下
curl -H Host:www.oldboy.com 10.0.0.[7-8]
##编写nginx反向代理服务配置文件(lb01)
ngx_http_upstream_module 负载均衡
ngx_http_proxy_module 反向代理
[root@lb01 ~]# vim /etc/nginx/nginx.conf
...
upstream web_pools {
server 10.0.0.7:80;
server 10.0.0.8:80;
}
# include /etc/nginx/conf.d/*.conf;
server {
listen 80;
server_name www.oldboy.com;
location / {
proxy_pass http://web_pools;
}
}
}
[root@lb01 ~]# 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 ~]# systemctl restart nginx
####为web01 web02首页文件追加内容让容易区分
for n in www blog ; do echo `hostname` $n.oldboy.com >/app/$n/index.html ;done
[root@web01 conf.d]# cat /app/www/index.html
web01 www.oldboy.com
[root@web02 conf.d]# cat /app/blog/index.html
web02 blog.oldboy.com
####在lb01上curl一下
[root@lb01 ~]# curl 10.0.0.7
web01 www.oldboy.com
[root@lb01 ~]# curl 10.0.0.8
web02 www.oldboy.com
[root@lb01 ~]#
[root@lb01 ~]#
[root@lb01 ~]# curl 10.0.0.5
web02 www.oldboy.com
[root@lb01 ~]# curl 10.0.0.5
web01 www.oldboy.com
[root@lb01 ~]# curl 10.0.0.5
web02 www.oldboy.com
[root@lb01 ~]# curl 10.0.0.5
web01 www.oldboy.com
##抓包
##upstream模块参数:
server —— RS配置,可以是ip或域名
weight ——权重
max_fails ——失败次数
fail_timeout =10s ——多久后在检查一遍
backup ——如果加上backup 会在池塘中其他机器都挂掉 才会启动
down 让服务器不可用