ngin+keepalived实现高可用负载均衡方案_第1页
ngin+keepalived实现高可用负载均衡方案_第2页
ngin+keepalived实现高可用负载均衡方案_第3页
ngin+keepalived实现高可用负载均衡方案_第4页
ngin+keepalived实现高可用负载均衡方案_第5页
已阅读5页,还剩5页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

学习文档目录TOC\o"1-5"\h\z\o"CurrentDocument"1. 引言 32. 环境说明 3\o"CurrentDocument"3. Nginx安装配置 3\o"CurrentDocument"4. Keepalived安装配置 5\o"CurrentDocument"5. 验证 91.引言本学习文档主要介绍了采用Nginx负载均衡,通过keepalived实现Nginx双机互备,保证实现的WEB服务高可用方案。2.环境说明主nginx负载均衡器:0端口81(CentOSrelease5.8)副nginx负载均衡器:1端口81(CentOSrelease5.8)Tomcat1:9端口3030Tomcat2:0端口4040VIP:2软件:keepalived-1.2.12nginx-1.4.4说明:keepalived是一个基于VRRP协议来实现的WEB服务高可用方案,可以利用其来避免单点故障。一个WEB服务至少会有2台服务器运行Keepalived,一台为主服务器(MASTER),一台为备份服务器(BACKUP),但是对外表现为一个虚拟IP,主服务器会发送特定的消息给备份服务器,当备份服务器收不到这个消息的时候,即主服务器宕机的时候,备份服务器就会接管虚拟IP,继续提供服务,从而保证了高可用性。3.Nginx安装配置1.安装Nginx获取Nginx稳定版,把Nginx安装到/usr/local/nginx目录下(两台机器都安装)的详细步骤:yum-installgccopenssl-develpcre-develzlib-devel(安装相关组件)tarzxvfnginx-1.4.4.tar.gzcdnginx-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&&makeinstall2.分别在两台服务器编写配置文件vim/usr/local/nginx/conf/nginx.conf#usernobody;worker_processes1;#pidlogs/nginx.pid;events{worker_connections1024;http{includedefault_typesendfilemime.types;includedefault_typesendfileon;application/octet-stream;on;#tcp_nopushon;keepalive_timeout65;upstreamcart{server9:3030weight=1;server0:4040weight=1;#ip_hash; #在没有做共享session的情况下ip_hash可以解决session问题}server{listen81;server_name0;#另外一台填写另外IPcharsetutf-8;location/cart{roothtml;indexindex.htmlindex.htm;proxy_next_upstreamerrortimeouthttp_500http_502http_504;proxy_read_timeout10s;proxy_passhttp://cart;

proxy_set_headerHost$host:81;#没用默认proxy_set_headerHost$host:81;#没用默认80端口需要加入$remote_addr;proxy_set_headerX-Forwarded-For$proxy_add_x_forwarded_for;proxy_set_headererror_page500502503504/50x.html;location=/50x.html{roothtml;}log_formataccess_log'$remote_addr-$remote_user[$time_local]$request'"$status"$body_bytes_sent"$http_referer"''"$http_user_agent""$http_x_forwarded_for"';access_log/usr/local/nginx/logs/access.logaccess_log;}}3.验证配置文件正确性/usr/local/nginx/sbin/nginx-显示以下信息为正确的theconfigurationfile/usr/local/nginx/conf/nginx.confsyntaxisokconfigurationfile/usr/local/nginx/conf/nginx.conftestissuccessful4.启动/usr/local/nginx/sbin/nginx4.Keepalived安装配置1.安装(两台nginx机器都安装)#安装poptyum-yinstallpoptpopt-develtarzxvfkeepalived-1.2.12.tar.gzcdkeepalived-1.2.12./configure--prefix=/usr/local/keepalived--sysconf=/etcmake&&makeinstallcp/usr/local/keepalived/sbin/keepalived/bin/chkconfig--addkeepalived#设置开机启动chkconfigkeepalivedon#启动keepalive服务/etc/init.d/keepalivedstartservicekeepalivedrestart2.配置cp/etc/keepalived/keepalived.conf/etc/keepalived/keepalived.conf_bakMASTERvim/etc/keepalived/keepalived.conf!ConfigurationFileforkeepalivedglobal_defs{notification_email{jiyulong@gufanbiao@}notification_email_fromjiyulong@smtp_serversmtp_connect_timeout30router_idLVS_DEVEL}vrrp_scriptMonitor_Nginx{script"/root/monitor/monitor_nginx.sh"interval2weight2}vrrp_instanceVI_1{stateMASTER#(主机为MASTER,备用机为BACKUP)interfaceethO#(HA监测网络接口)virtual_router_id51#(主、备机的virtual_router_id必须相同)priority1OO#(主、备机取不同的优先级,主机值较大,备份机值较小,值越大优先级越高)advert_int1#(VRRPMulticast广播周期秒数)authentication{auth_typePASS#(VRRP认证方式)auth_pass1111#(密码)}track_script{Monitor_Nginx#(调用nginx进程检测脚本)}virtual_ipaddress{2#(VRRPHA虚拟地址)}}BACKUP方面只需要修改state为BACKUP,priority比MASTER稍低即可3.监控脚本vim/root/monitor_nginx.sh当检测到nginx进程不存在的时候,就干掉所有的keepalived,这时候,请求将会由keepalived的backup接管!!vim/opt/nginx_pid.sh#!/bin/bashvarsion0.0.2根据一网友说这样做不科学,如果nginx服务起来了,但是我把keepalived杀掉了,我的理由是,如果nginx死掉了,我觉得就很难在起来,再有就是nagios当然要给你报警了啊。不过这位同学说的有道理,所以就稍加改了一下脚本##查看是否有nginx进程把值赋给变量AA='ps-Cnginx--no-header|wc-I'if[$A-eq0];then ##如果没有进程值得为零/usr/local/nginx/sbin/nginxsleep3if['ps-Cnginx--no-header|wc-l'-eq0];thenkillallkeepalived ##则结束keepalived进程fifi运行chmod+x/root/monitor_nginx.sh赋权限注意:运行monitor_nginx.sh脚本时出现了这错误/bin/bash人M:badinterpreter:没有那个文件或目录。原因:linux和windows之间的不完全兼容。。。具体细节不管,如果验证:vimXXX.sh:setff?如果出现fileforma=dos那么就基本可以确定是这个问题了。:setfileformat=unix:wqOK了。。。。。。。4.启动01都重新启动keepalived:servicekeepalivedrestart这里请注意,当keepalived启动后,我们可以用命令:ipaddshowethO来看我们的ethO网卡确实被添加了虚拟IP,如图[root@terracotta2keepalived]#ipaddshowethO2:etho:<BROADCA^TfMULTICAST,UP,LOWER_UP>mtu1500qdiscpfifofastqlen1000link/ether00:50:56:8b:5b:02brdff:ff:ff:ff:ff:ffFnet0/24Jard55scopeglobalethOlinet2/32scopeglobalethOinet丄2/24brd丄55scopeglobalsecondaryethO:0inet6fe80::250:56ff:fe8b:5b02/64scopelinkvalidlftforeverpzeferredlftforever注:给大家提供加虚IP的方法egifconfigethO:OOOnetmask255.255.255.OupipaddshowethO来看我们的ethO网卡确实被添加了虚拟IP5.验证1访问VIP看是否能够正常访问后端的tomcat2•停止其中一个tomcat看是否能将访问转到另一台上停止两台nginx上任何一个nginx进程看监控进程脚本是否会自动启动nginx停止任何一台nginx上的keepalived进程看另一台是否接管vip比如停止Master上的keepalived,例如如下killallkeepalived,查看BACKUP机器是否已经接管,如果BACKUP接管后,BACKUP机器日志会是出下情况日志路径:tail/var/log/messagesKeepalived_vrrp:VRRP_Instance(VI_1)TransitiontoMASTERSTATEKeepalived_vrrp:VRRP_Instance(VI_1)EnteringMASTERSTATEKeepalived_vrrp:VRRP_Instance(VI_1)settingprotocolVIPs.Keepalived_vrrp:VRRP_Instance(VI_1)SendinggratuitousARPsoneth0for2MASTER/

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论