1、负载均衡:配置实列
实现效果:浏览器地址栏输入地址http://192.168.xxx.xxx/www/a.html, 负载均衡效果,平均到8081和8082端口中
准备工作:
1. 准备两台tomcat服务器,一 台8081, 一台8082
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
802047bd7530 nginx "/docker-entrypoin..." 17 hours ago Up 27 minutes 0.0.0.0:80->80/tcp mynginx
afb277b2006e mytomcat "catalina.sh run" 21 hours ago Up 16 minutes 0.0.0.0:8081->8080/tcp mytomcat1
ba962911646b mytomcat "catalina.sh run" 21 hours ago Up 11 minutes 0.0.0.0:8082->8080/tcp mytomcat2
2.创建文件夹和测试的页面
由于会从webapps下的ROOT文件下进行查找,所以在ROOT目录下进行创建
访问路径:http://192.168.3.3:8081/www/a.html
[root@localhost ~]# docker exec -it afb277b2006e bash
root@ba962911646b:/usr/local/tomcat# cd webapps/ROOT/www/
root@ba962911646b:/usr/local/tomcat/webapps/ROOT/www# ls
a.html
8081端口展示界面
访问路径:http://192.168.3.3:8082/www/a.html
[root@localhost ~]# docker exec -it ba962911646b bash
root@ba962911646b:/usr/local/tomcat# cd webapps/ROOT/www/
root@ba962911646b:/usr/local/tomcat/webapps/ROOT/www# ls
a.html
8082端口展示界面
3.在nginx中进行配置
3.1、文件在root@802047bd7530:/# cd 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;
#添加myserver,用来进行负载均衡
upstream myserver {
server 192.168.3.3:8081;
server 192.168.3.3:8082;
}
include /etc/nginx/conf.d/*.conf;
}
3.2、文件在root@802047bd7530:/# cd etc/nginx/conf.d/下的default.conf
server {
listen 80;
listen [::]:80;
#将localhost更改为ip地址
server_name 192.168.3.3;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html;
# proxy_pass http://192.168.3.3:8080;
#将配置的myserver载入,实现负载均衡
proxy_pass http://myserver;
index index.html index.htm;
}
# location ~ /yee/ {
# proxy_pass http://192.168.3.3:8081;
# }
# location ~ /yew/ {
# proxy_pass http://192.168.3.3:8082;
# }
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
nginx.监听端口默认80,访问测试:http://192.168.3.3/www/a.html
2、均衡策略
2.1 轮循(默认)
每个请求按时间顺序逐一分配到不同的应用服务器,如果应用服务器down掉,能自动剔除。
2.2 权重
通过配置权重,指定轮询几率,权重和访问比率成正比,用于应用服务器性能不均的情况。默认:weight=1
2.3 ip_哈希算法
每个请求按访问ip的hash结果分配,这样每个访客固定访问一个应用服务器,可以解决session共享的问题
2.4 least_conn 最少连接
把请求转发给连接数较少的后端服务器。轮询算法是把请求平均的转发给各个后端,使它们的负载大致相同;但是,有些请求占用的时间很长,会导致其所在的后端负载较高。这种情况下,least_conn这种方式就可以达到更好的负载均衡效果
upstream dynamic_zuoyu {
least_conn; #把请求转发给连接数较少的后端服务器
server localhost:8080 weight=2; #tomcat 7.0
server localhost:8001; #tomcat 8.0
server localhost:8082 backup; #tomcat 8.5
server localhost:8083 max_fails=3 fail_timeout=20s; #tomcat 9.0
}
2.5 fair(第三方)
按后端服务器的响应时间来分配请求,响应时间短的优先分配
2.6 url_hash(第三方)
按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,后端服务器为缓存时比较有效
fail_timeout | max_fails结合使用 |
---|---|
max_fails | 设置在fail_timeout参数设置的时间内最大失败次数,如果在这个时间内,所有针对该服务器的请求都失败了,那么认为该服务器会被认为是停机了 |
fail_time | 服务器会被认为停机的时间长度,默认为10s |
backup | 标记该服务器为备用服务器。当主服务器停止时,请求会被发送到它这里 |
down | 标记服务器永久停机了 |