当前位置: 首页 > 科技观察

Nginx+Keepalived实现web服务器高可用

时间:2023-03-12 01:29:45 科技观察

1.Nginx业务背景现在公司需要快速搭建一个web服务器,对外提供web服务给用户使用。需求拆分需要基于http协议的软件,构建服务实现常用用法:1)web服务器软件httpdhttp协议类似web服务器软件:apache(老牌子)nginx(俄罗斯)IIS(微软)2)代理服务器反向代理3)邮箱代理服务器IMAPPOP3SMTP4)负载均衡功能LBloadblanceNginx架构特点:①高可靠性:稳定master进程管理调度请求哪个worker分配=>worker进程响应请求一master多worker②热部署:(1)平滑升级(2)快速重载配置③高并发:可以同时响应更多的请求事件数万种epoll模型④快速响应:尤其是在处理静态文件时,响应速度非常快sendfile⑤低消耗:1wCPU和内存请求内存2-3MB⑥分布式支持:反向代理七层负载均衡官网:http://nginx.org/1.2,安装Commoninstallati关于方法:①yum安装配置,需要使用Nginx官方源或者EPEL源②源码编译#添加并运行用户shell>useradd-s/sbin/nologin-Mwww#安装依赖shell>yum-yinstallpcre-develzlib-developenssl-devel#编译安装shell>cd/root/softshell>tarxvfnginx-1.14.2.tar.gzshell>cdnginx-1.14.2shell>./configure--prefix=/usr/local/nginx--user=www--group=www--with-http_ssl_module--with-http_stub_status_module--with-http_realip_module&&make&&makeinstall编译参数说明1.3、目录介绍查看安装目录/usr/local/nginx1.4、软件运行参数查看nginx二进制可执行文件shell的相关参数>cd/usr/local/nginx/sbinshell>./nginx-h执行后显示nginxversion:nginx/1.14.2Usage:nginx[-?hvVtTq][-ssignal][-cfilename][-pprefix][-gdirectives]Options:#查看帮助-?,-h:thishelp#查看版本并退出-v:showversionandexit#查看版本和配置选项并退出-V:showversionandconfigureoptionsthenexit#检测配置文件语法并退出-t:testconfigurationandexit#检测配置文件语法打印并退出-T:testconfiguration,dumpitandexit#在配置测试期间抑制非错误信息-q:suppressnon-errormessagesduringconfigurationtesting#发送信号给The主进程stop强制退出quitgracefullyexitreopenreopenlogreload重载配置-ssignal:sendsignaltoamasterprocess:stop,quit,reopen,reload#设置nginx目录$prefix路径-pprefix:setprefixpath(default:/usr/local/nginx/)#指定启动使用的配置文件-cfilename:setconfigurationfile(default:conf/nginx.conf)#在配置文件外设置全局指令启用模块和编译参数-t参数检测是否有配置文件出错之后,用户将无法获得服务响应。为了提升用户体验,持续为用户提供优质的服务,当Web服务器不可用时,备份服务器可以接管Web服务器的工作,继续为用户提供响应。其中,有一个问题需要解决,即要求备份服务器能够快速自动切换。以上业务需求统称为高可用HA来实现服务。需求拆分是高可用的核心:①冗余服务器(备份服务器)②自动切换。用户可以通过绑定虚拟IP2.1通过VIP访问服务。简介Keepalived软件最初是为LVS设计的负载均衡软件,用于管理和监控LVS集群系统中各个服务节点的状态,后来加入了可以实现高可用的VRRP功能。因此,Keepalived除了可以管理LVS软件外,还可以作为其他服务(如:Nginx、Haproxy、MySQL等)的高可用解决软件。keepalived主要使用三个模块,分别是core、check和vrrp。核心模块是keepalived的核心,负责主进程的启动和维护,以及全局配置文件的加载和解析。check负责健康检查,包括各种常见的检查方法。vrrp模块是实现VRRP协议的。2.2.master和backup都需要安装,即server01和server03机器#安装keepalivedshell>yum-yinstallkeepalivedkeepalived需要用到的目录和文件:2.3.配置①备份主备服务器的配置文件shell>cd/etc/keepalivedshell>cpkeepalived.confkeepalived.conf_bak②分别修改主备服务器的配置文件shell>vimkeepalived.conf示例配置文件说明!ConfigurationFileforkeepalived#发送邮件的配置global_defs{notification_email{acassen@firewall.locfailover@firewall.locsysadmin@firewall.loc}notificationAlex_andemail_from.Cassen@firewall.locsmtp_server192.168.200.1smtp_connect_timeout30router_idLVS_DEVEL}#vrrp协议配置vrrp_instanceVI_1{#工作模式stateMASTER#监控网卡接口beconsistentwithstandbyservervirtual_router_id51#weightpriority100#intvrrpadcycle1#sendingofverrrpadpackets权限验证authentication{auth_typePASSauth_pass1111}#需要绑定切换的VIPvirtual_ipaddress{192.168.200.16192.168.200.17192.168.200.18}}主服务器!ConfigurationFileforkeepalivedglobal_defs{notification_email{阿卡斯n@firewall.locfailover@firewall.locsysadmin@firewall.loc}notification_email_fromAlexandre.Cassen@firewall.locsmtp_server192.168.200.1smtp_connect_timeout30router_idLVS_DEVEL}vrrp_instanceVI_1{stateMASTERinterfaceeth0virtual_router_id51priority100advert_int1authentication{auth_typePASSauth_pass1111}#master默认只需要修改使用VIP即可virtual_ipaddress{192.168.17.200}}备服务器!ConfigurationFileforkeepalivedglobal_defs{notification_email{acassen@firewall.locfailover@firewall.locsysadmin@firewall.loc}notification_email_fromAlexandre.Cassen@firewall.locsmtp_server192.168.200.1smtp_connect_timeout30router_idLVS_DEVEL}vrrp_instanceVI_1{#修改工作模式为备stateBACKUPinterfaceeth0virtual_router_id51priority100advert_int1authentication{auth_typePASSauth_pass1111}#注意修改VIPvirtual_ipaddress{192.168.17.200}}③分别按照顺序启动主服务器和备服务器的keepalivedshell>servicekeepalivedstart④查看主备Thenetworkcardinformationoftheserver#Needtocheckthroughtheipacommand,checktheshell>ipa2.4inserver01andserver03respectively,simulatethefailureofthesimulatedserverfailure,checkwhethertheservicecanbeswitchedtothestandbymachinetosimulatethedowntime,shutdowntheserver01masterserver,VIPAutomaticallyswitchtoserver03backupserver#capturevrrpshell>yum-yinstalltcpdumpshell>tcpdumpvrrp-n