简 介:html
集群就是一组独立的计算机,经过网络链接组合成一个组合来共同完一个任务。nginx
集群优势:web
集群种类:算法
Nginx复杂概念说明:后端
Nginx反向代理说明:缓存
Nginx反向代理所使用的模块:bash
upstream模块配置服务器
参数:网络
调度算法session
####################### proxy upstream server_pools { server 192.168.10.249:80 weight=3 max_fails=3 fail_timeout=10; server 192.168.10.253:80 weight=5 max_fails=3 fail_timeout=10; server 192.168.10.252:80 weight=5 max_fails=3 fail_timeout=10 backup; }
ip_hash算法(不能有backup标识和weighe标识)
upstream server_pools { ip_hash; server 192.168.10.249:80 max_fails=3 fail_timeout=10; server 192.168.10.253:80 max_fails=3 fail_tim eout=10; server 192.168.10.252:80 max_fails=3 fail_timeout=10; }
动态调度算法
upstream server_pools { server 192.168.10.249:80 max_fails=3 fail_timeout=10; server 192.168.10.253:80 max_fails=3 fail_timeout=10; server 192.168.10.252:80 max_fails=3 fail_timeout=10; fair; }
参数详解:
server { listen 80; server_name web.yan.com; location / { proxy_pass http://server_pools; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $remote_addr; proxy_connect_timeout 60; proxy_send_timeout 60; proxy_read_timeout 60; proxy_buffer_size 4k; proxy_buffers 4 32k; proxy_busy_buffers_size 64k; proxy_temp_file_write_size 64k;
proxy_next_upstream error timeout invalid_header http_500 http_502 http_504;
}
}
服务器规划:
/upload 10.0.0.8:80 html/www/upload upload服务器
/static 10.0.0.7:80 html/www/static static静态服务器
/ 10.0.0.9:80 html/www 默认
建立upstream负载信息:
upstream upload_pools { server 192.168.10.56:80; } upstream static_pools { server 192.168.10.57:80; } upstream default_pools { server 192.168.10.58:80; }
调用upstream信息
server { listen 80; server_name www.yan.com; location /static/ { proxy_pass http://static_pools; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $remote_addr; } location /upload/ { proxy_pass http://upload_pools; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $remote_addr; } location / { proxy_pass http://default_pools; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $remote_addr; } access_log logs/access_www.log main; } }
手机用户电脑用户访问不一样网站实例配置:
upstream upload_pools { server 10.0.0.8:80; } upstream static_pools { server 10.0.0.7:80; } upstream default_pools { server 10.0.0.9:80; } server { listen 80; server_name www.yan.com; location / { if ($http_user_agent ~* "Iphome") { proxy_pass http://static_pools; } if ($http_user_agent ~* "Android") { proxy_pass http://upload_pools; } proxy_pass http://default_pools; } access_log logs/access_www.log main; } }
Nginx配置
配置listen 192.168.10.244:80; (域名解析到VIP)内核须要开启容许绑定非本地的IP
echo 'net.ipv4.ip_nonlocal_bind = 1' >>/etc/sysctl.conf ##/etc/sysctl.conf 加上 sysctl -p
配置文件
upstream server_pools { server 192.168.10.56; server 192.168.10.57; server 192.168.10.58; } server { listen 192.168.10.244:80; server_name www.yan.com; location / { proxy_pass http://server_pools; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $remote_addr; } access_log logs/access_www.log main; } server { listen 192.168.10.245:80; server_name blog.yan.com; location / { proxy_pass http://server_pools; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $remote_addr; } access_log logs/access_blog.log main; }
keepalived配置
keepalived脑裂说明:
因为某种缘由,致使两台高可用服务器对在指定的时间内,没法检测到对方心跳信息,各自取得资源服务权,而此时的两台高可用服务器都在正常运行,会致使同一个IP或者服务器在两端同时存在而发生冲突,严重占用同一个VIP使用户写入数据丢失。
心跳线老化,
高可用集群同一个交换机故障
防火墙规则
网卡等信息配置不正确等
解决办法,监控备用服务器需IP信息,若是有则,主服务器出现问题。
#!/bin/bash if [ `ip a s ens33|grep 192.168.10.244|wc -l` -ne 0 ] then echo "keepalived is error!!!" else echo "keepalived is ok!!!" fi
NGinx宕机实现,keepalived主备切换(配置文件标红处就是执行脚本切换操做)
#!/bin/bash #name: check_web.sh #desc: check nginx and kill keepalived if [ `ps -ef |grep nginx |grep -v grep |wc -l` -lt 2 ];then /etc/init.d/keepalived stop fi
chmod +x /server/scripts/check_web.sh
配置文件
#lb01 global_defs { router_id LVS_01 #惟一标示 }
vrrp_script check_web {
script "/server/scripts/check_web.sh" #执行的脚本路径
interval 2 #两秒检查一次
weight 2 #触发脚本优先级减去定义的weight下降优先级改成备服务
}
vrrp_instance VI_1 { # state MASTER interface ens33 virtual_router_id 51 priority 150 advert_int 1 authentication { auth_type PASS auth_pass admin } virtual_ipaddress { 192.168.10.244/24 dev ens33 label ens33:1 }
track_script { #执行脚本
check_web
} } vrrp_instance VI_2 { state BACKUP interface ens33 virtual_router_id 52 priority 100 advert_int 1 authentication { auth_type PASS auth_pass root } virtual_ipaddress { 192.168.10.245/24 dev ens33 label ens33:2 }
} #lb02 global_defs { router_id LVS_02 } vrrp_instance VI_1 { state BACKUP interface ens33 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass admin } virtual_ipaddress { 192.168.10.244/24 dev ens33 label ens33:1 } } vrrp_instance VI_2 { state MASTER interface ens33 virtual_router_id 52 priority 150 advert_int 1 authentication { auth_type PASS auth_pass root } virtual_ipaddress { 192.168.10.245/24 dev ens33 label ens33:2 } }