负载均衡
2019-06-14 本文已影响0人
北疆07
负载均衡
用途
1.实现用户访问进行合理调度处理,最终分配给不同web节点
2.实现用户访问压力分担,将网站压力分配给每一个节点
让后端服务器 报错每台服务器工作平均负载
实现
硬件设备
F5 A10 Redware
开源软件
Nginx Haproxy Lvs
开源软件负载均衡区别
命名
负载均衡
用户请求的转发
lvs
反向代理
代替用户去找 再发给用户
nginx haproxy
功能
lvs
4层负载均衡
传输层tcp/udp 端口号
最大进行端口转发
nginx haproxy
4层和7层负载均衡
准备环境
web01上
[root@web01 ~]# echo 'this is PC website' >/app/www/lidao.html
web02上
[root@web02 ~]# echo 'this is Mobile website' >/app/www/lidao.html
db01设置
在db01 上安装nginx软件
[root@lb01 ~]# cat /etc/nginx/nginx.conf
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
upstream upload {
server 10.0.0.7:80 weight=1 max_fails=3 fail_timeout=10s;
}
upstream static {
server 10.0.0.8:80 weight=1 max_fails=3 fail_timeout=10s;
}
upstream default {
server 10.0.0.9:80 weight=1 max_fails=3 fail_timeout=10s;
}
# include /etc/nginx/conf.d/*.conf;
server {
listen 80;
server_name www.oldboy.com;
location /upload {
proxy_pass http://upload;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
location /static {
proxy_pass http://static;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
location / {
proxy_pass http://default;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
}
}
###设置完检查语法,重启一下nginx
配置upstream与location
定义upstream移动端与PC端
upstream default {
server 10.0.0.7:80 weight=1 max_fails=3 fail_timeout=10s;
}
upstream mobile {
server 10.0.0.8:80 weight=1 max_fails=3 fail_timeout=10s;
}
移动端的服务器池
location / {
if ($http_user_agent ~* "Android|IOS") {
proxy_pass http://mobile;
}
检查是否成功
curl一下查看结果
curl -A 可以指定系统
[root@lb01 nginx]# curl 10.0.0.5/lidao.html
this is PC website
[root@lb01 nginx]# curl -A ios 10.0.0.5/lidao.html
this is Mobile website
[root@lb01 nginx]# curl -A Android 10.0.0.5/lidao.html
this is Mobile website
####完成后下载火狐浏览器下载插件查看一下
user agent switcher
根据URI中的目录地址事项代理转发(动静分离)
添加一台测试web03节点—10.0.0.9
将web03的配置与web01和web02配置相同
(/app站点目录与nginx.conf配置文件)
准备环境
www.oldboy.com/upload/index.html
www.oldboy.com/static/index.html
www.oldboy.com/index.html
#web01:
mkdir -p /app/www/upload/index.html
echo this is upload >/app/www/index.html
[root@web01 ~]# cat /app/www/upload/index.html
this is upload
#web02:
mkdir -p /app/www/static/index.html
echo this is static >/app/www/index.html
[root@web02 ~]# cat /app/www/static/index.html
this is static
#web03:
mkdir -p /app/www/index.html #之前已经有首页文件,只需修改内容
echo this is default >/app/www/index.html
[root@web03 ~]# cat /app/www/index.html
this is default
配置upstream与location
定义upstream.
upstream upload {
server 10.0.0.7:80 weight=1 max_fails=3 fail_timeout=10s;
}
upstream static {
server 10.0.0.8:80 weight=1 max_fails=3 fail_timeout=10s;
}
upstream default {
server 10.0.0.9:80 weight=1 max_fails=3 fail_timeout=10s;
}
添加location
server {
listen 80;
server_name www.oldboy.com;
location /upload {
proxy_pass http://upload;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
location /static {
proxy_pass http://static;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
location /default {
proxy_pass http://default;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
}
完整配置
[root@lb01 nginx]# vim nginx.conf
....
upstream upload {
server 10.0.0.7:80 weight=1 max_fails=3 fail_timeout=10s;
}
upstream static {
server 10.0.0.8:80 weight=1 max_fails=3 fail_timeout=10s;
}
upstream default {
server 10.0.0.9:80 weight=1 max_fails=3 fail_timeout=10s;
}
# include /etc/nginx/conf.d/*.conf;
server {
listen 80;
server_name www.oldboy.com;
location /upload {
proxy_pass http://upload;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
location /static {
proxy_pass http://static;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
location / {
proxy_pass http://default;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
}
}
[root@lb01 nginx]# 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 nginx]# systemctl reload nginx
去浏览器测试一下
轮询算法
ip_hash
只要客户端ip地址相同就会被转发到同一台机器上
cookie与session会话区别
1.共同点
存放用户信息
key value类型 变量和变量内容
2.区别
cookie:
存放在浏览器
为保证安全性,存放简单信息、钥匙
开发设置
响应服务器给你设置
session:
存放在服务器
存放敏感信息
存放锁头
模块说明
weight=1 ###权重
max_fails=3 ####失败次数
fail_timeout=10s ####失败后没10秒尝试建立一次
upstream upload { ###池子模块
server 10.0.0.7:80 weight=1 max_fails=3 fail_timeout=10s;
}
location /upload { ###指向池子的模块
proxy_pass http://upload;
proxy_set_header Host $host; ####修改请求头,修改负载均衡向web服务器发出的请求头信息,$host nginx内置变量,记录着用户的请求的域名
proxy_set_header X-Forwarded-For $remote_addr; ####让用户访问日志记录用户的ip地址
image.png
image.png
image.png
image.png
image.png