nginx+keepalived实现高可用负载均衡方案

上传人:鲁** 文档编号:477073430 上传时间:2023-09-09 格式:DOCX 页数:10 大小:19.73KB
返回 下载 相关 举报
nginx+keepalived实现高可用负载均衡方案_第1页
第1页 / 共10页
nginx+keepalived实现高可用负载均衡方案_第2页
第2页 / 共10页
nginx+keepalived实现高可用负载均衡方案_第3页
第3页 / 共10页
nginx+keepalived实现高可用负载均衡方案_第4页
第4页 / 共10页
nginx+keepalived实现高可用负载均衡方案_第5页
第5页 / 共10页
点击查看更多>>
资源描述

《nginx+keepalived实现高可用负载均衡方案》由会员分享,可在线阅读,更多相关《nginx+keepalived实现高可用负载均衡方案(10页珍藏版)》请在金锄头文库上搜索。

1、学习文档目录1.引言 32.环境说明 33.Nginx 安装配置 34.Keepalived 安装配置 55.验证 91. 引言本学习文档主要介绍了采用 Nginx 负载均衡,通过 keepalived 实现 Nginx 双 机互备,保证实现的WEB服务高可用方案。2. 环境说明主 nginx 负载均衡器: 172.20.52.20 端口 81(CentOS release 5.8)副 nginx 负载均衡器: 172.20.52.21 端口 81(CentOS release 5.8)Tomcat1: 172.20.52.19 端口 3030Tomcat2: 172.20.52.20 端口

2、4040VIP:172.20.52.22软件:keepalived- 1.2.12 nginx-1.4.4说明:keepalived是一个基于VRRP协议来实现的WEB服务高可用方案, 可以利用其来避免单点故障。一个WEB服务至少会有2台服务器运行 Keepalived,一台为主服务器(MASTER),一台为备份服务器(BACKUP), 但是对外表现为一个虚拟IP,主服务器会发送特定的消息给备份服务器,当备份 服务器收不到这个消息的时候,即主服务器宕机的时候,备份服务器就会接管虚拟 IP,继续提供服务,从而保证了高可用性。3. Nginx 安装配置1. 安装 Nginx获取 Nginx 稳定版

3、,把 Nginx 安装到 /usr/local/nginx 目录下(两台机器都 安装)的详细步骤:yum - in stall gcc ope nssl-devel pcre-devel zlib-devel(安装相关组件) tar zxvf nginx-1.4.4.tar.gzcd nginx-1.4.4./configure-prefix=/usr/local/nginx-with-http_ssl_module-with-http_flv_module-with-http_gzip_static_module-with-http_stub_status_modulemake & make

4、 install2. 分别在两台服务器编写配置文件vim /usr/local/nginx/conf/nginx.conf#user nobody; worker_processes 1;#pid logs/nginx.pid;events worker_connections 1024;http include default_type sendfilemime.types;on;application/octet-stream;#tcp_nopush on; keepalive_timeout 65;upstream cart server 172.20.52.19:3030 weight

5、=1;server 172.20.52.20:4040 weight=1;#ip_hash;#在没有做共享 session 的情况下 ip_hash 可以解决 session 问题server listen 81;server_name 172.20.52.20; #另外一台填写另外 IP charset utf-8;location /cart root html;index index.html index.htm;proxy_next_upstream error timeout http_500 http_502 http_504; proxy_read_timeout 10s;pro

6、xy_pass http:/cart;proxy_set_headerHost$host:81; #没用默认 80 端口需要加入$remote_addr;proxy_set_header X-Real-IPproxy_set_headerX-Forwarded-For $proxy_add_x_forwarded_for;error_page 500 502 503 504 /50x.html;location = /50x.html root html;log_format access_log $remote_addr - $remote_user $time_local $request

7、 $status $body_bytes_sent $http_referer $http_user_agent $http_x_forwarded_for;access_log /usr/local/nginx/logs/access.log access_log;3. 验证配置文件正确性/usr/local/nginx/sbin/nginx -显示以下信息为正确的the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok configuration file /usr/local/nginx/conf/nginx

8、.conf test is successful4. 启动/usr/local/nginx/sbin/nginx4.Keepalived 安装配置1安装(两台nginx机器都安装)#安装 poptyum -y install popt popt-develtar zxvf keepalived-1.2.12.tar.gzcd keepa l i ved- 1 . 2 . 1 2./configure -prefix=/usr/local/keepalived -sysconf=/etcmake & make installcp /usr/local/keepalived/sbin/keepal

9、ived /bin/chkconfig -add keepalived#设置开机启动chkconfig keepalived on#启动 keepalive 服务/etc/init.d/keepalived startservice keepalived restart2.配置cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf_bakMASTERvim /etc/keepalived/keepalived.conf! Configuration File for keepalivedglobal_defs not

10、ification_email notification_email_from smtp_server 127.0.0.1smtp_connect_timeout 30router_id LVS_DEVELvrrp_script Monitor_Nginx script /root/monitor/monitor_nginx.shinterval 2weight 2 vrrp_instance VI_1 state MASTER #(主机为 MASTER,备用机为 BACKUP)in terface ethO #(HA 监测网络接口)virtual_router_id 51 #(主、备机的 v

11、irtual_router_id 必须相同)priority 1OO #(主、备机取不同的优先级,主机值较大,备份机值较小,值越大优先级越高)advert_i nt 1 #(VRRP Multicast 广播周期秒数)authentication auth_type PASS #(VRRP 认证方式)auth_pass 1111 #(密码)track_script Monitor_Nginx #(调用 nginx 进程检测脚本)virtual_ipaddress 172.20.52.22 #(VRRP HA 虚拟地址)BACKUP方面只需要修改state为BACKUP , priority比M

12、ASTER稍低即可3.监控脚本vim /root/monitor_nginx.sh当检测到nginx进程不存在的时候,就干掉所有的keepalived,这时候,请求将会由 keepalived 的 backup 接管!vim /opt/nginx_pid.sh#!/bin/bash# varsion 0.0.2# 根据一网友说这样做不科学,如果 nginx 服务起来了,但是我把 keepalived 杀掉 了,我的理由是,如果nginx死掉了,我觉得就很难在起来,再有就是nagios当然要 给你报警了啊。不过这位同学说的有道理,所以就稍加改了一下脚本#查看是否有nginx进程 把值赋给变量AA

13、=ps -C nginx -no-header |wc -Iif $A -eq 0 ;then# 如果没有进程值得为 零/usr/local/nginx/sbin/nginxsleep 3if ps -C nginx -no-header |wc -l -eq 0 ;thenkillall keepalived# 则结束 keepalived 进程fifi运行 chmod +x /root/monitor_nginx.sh 赋权限注意:运行 mon itor_ngin x.sh 脚本时出现了这错误 /bin/bash人M: bad in terpreter:没有那个文件或目录。原因:linux

14、和windows之间的不完全兼容。具体细节不管,如果验证:vim XXX.sh :set ff?如果出现fileforma=dos那么就基本可以确定是这个问题了。:set fileformat=unix :wqOK 了。4.启动172.20.52.20 172.20.52.21 都重新启动 keepalived:service keepalived restart这里请注意,当 keepalived 启动后,我们可以用命令:ip add show ethO来看我们的ethO网卡确实被添加了虚拟IP,如图rootterracotta2 keepalived# ip add show ethO2: etho: mtu 1500 qdisc pfifo fast qlen 1000 link/ether 00:50:56:8b:5b:02 brd ff:ff:ff:ff:ff:ffFnet 172.20.52.20/24 Jard 172.20.52.255 scope global ethOlinet 172.20.52.22/32 scope global ethOinet 丄72.20.52.22/24 brd 丄72.20.52.255 scope global secondary ethO:0 inet6 fe80:25

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 机械/制造/汽车 > 综合/其它

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号