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

服务器
Keepalived软件起初是专为LVS负载均衡软件设计的,用来管理并监控LVS集群系统中各个服务节点的状态,后来又加入了可以实现高可用的VRRP功能。

 1、Nginx

业务背景

现公司需求快速搭建web服务器,对外提供给用户web服务。

需求拆分

需要基于http协议的软件,搭建服务实现

介绍

常见用法:

1) web服务器软件 httpd http协议

同类的web服务器软件:apache(老牌) nginx(俄罗斯) IIS(微软)

2)代理服务器 反向代理

3)邮箱代理服务器 IMAP POP3 SMTP

4)负载均衡功能 LB loadblance

Nginx架构的特点:

  • ①高可靠:稳定性 master进程 管理调度请求分发到哪一个worker=> worker进程 响应请求 一master多worker②热部署 :(1)平滑升级 (2)可以快速重载配置③高并发:可以同时响应更多的请求 事件 epoll模型 几万④响应快:尤其在处理静态文件上,响应速度很快 sendfile⑤低消耗:cpu和内存 1w个请求 内存2-3MB⑥分布式支持 :反向代理 七层负载均衡

官方网址:http://nginx.org/

[[272214]]

1.2、安装

常见安装方式:

①yum安装配置,需使用Nginx官方源或者EPEL源②源码编译

  1. #添加运行用户 
  2. shell > useradd -s/sbin/nologin -M www 
  3. #安装依赖 
  4. shell > yum -y install pcre-devel zlib-devel openssl-devel 
  5. #编译安装 
  6. shell > cd /root/soft 
  7. shell > tar xvf nginx-1.14.2.tar.gz 
  8. shell > cd nginx-1.14.2 
  9. shell > ./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module && make && make install 

编译参数说明

 

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

 

1.3、目录介绍

查看安装目录/usr/local/nginx

 

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

 

1.4、软件操作参数

查看nginx的二进制可执行文件的相关参数

  1. shell > cd /usr/local/nginx/sbin 
  2.  shell > ./nginx -h 

执行后显示

  1. nginx version: nginx/1.14.2 
  2.  Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives] 
  3.  ​ 
  4.  Options: 
  5.  #查看帮助 
  6.  -?,-h : this help 
  7.  #查看版本并退出 
  8.  -v : show version and exit 
  9.  #查看版本和配置选项并退出 
  10.  -V : show version and configure options then exit 
  11.  #检测配置文件语法并退出 
  12.  -t : test configuration and exit 
  13.  #检测配置文件语法打印它并退出 
  14.  -T : test configuration, dump it and exit 
  15.  #在配置测试期间禁止显示非错误信息 
  16.  -q : suppress non-error messages during configuration testing 
  17.  #发送信号给主进程 stop强制退出 quit优雅的退出 reopen重开日志 reload重载配置 
  18.  -s signal : send signal to a master process: stop, quit, reopen, reload 
  19.  #设置nginx目录 $prefix路径 
  20.  -p prefix : set prefix path (default: /usr/local/nginx/) 
  21.  #指定启动使用的配置文件 
  22.  -c filename : set configuration file (default: conf/nginx.conf) 
  23.  #在配置文件之外设置全局指令 
  24.  -g directives : set global directives out of configuration file 

一般主要使用:

  • -s参数控制管理nginx服务-V参数查看nginx开启的模块和编译参数-t参数检测配置文件是否有错误

2、Keepalived实现高可用

业务背景

单例web服务器能够满足业务基本需求,提供web服务。但是,存在单点故障的问题,即当服务器宕机后,用户将无法获取到服务响应。

为了能够提高用户体验度,能够持续得给用户提供优质的服务,当web服务器不可用时,可以有备用服务器接替web服务器的工作,继续为用户提供响应。其中,还要解决一个问题,需要备用服务器能够快速自动切换过来。

一般将以上业务需求,称为实现服务的高可用HA。

需求拆分

也就是高可用的实现核心:

①冗余服务器(备份服务器)

②自动切换 可以通过绑定虚拟IP的方式 用户通过VIP访问服务

 

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

 

2.1、介绍

Keepalived软件起初是专为LVS负载均衡软件设计的,用来管理并监控LVS集群系统中各个服务节点的状态,后来又加入了可以实现高可用的VRRP功能。因此,Keepalived除了能够管理LVS软件外,还可以作为其他服务(例如:Nginx、Haproxy、MySQL等)的高可用解决方案软件。

 

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

 

keepalived主要使用三个模块,分别是core、check和vrrp。

core模块为keepalived的核心,负责主进程的启动、维护以及全局配置文件的加载和解析。

check负责健康检查,包括常见的各种检查方式。

vrrp模块是来实现VRRP协议的。

2.2、安装

master和backup都需要进行安装,也就是server01和server03机器

#安装keepalived shell > yum -y install keepalived

 

 

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

 

keepalived需要使用的目录和文件:

 

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

 

2.3、配置

①备份主备服务器的配置文件

  1. shell > cd /etc/keepalived 
  2.  shell > cp keepalived.conf keepalived.conf_bak 

②分别修改主备服务器配置文件

  1. shell > vim keepalived.conf 

示例配置文件说明

  1. ! Configuration File for keepalived 
  2. #发送邮件的配置 
  3. global_defs { 
  4. notification_email { 
  5. acassen@firewall.loc 
  6. failover@firewall.loc 
  7. sysadmin@firewall.loc 
  8. notification_email_from Alexandre.Cassen@firewall.loc 
  9. smtp_server 192.168.200.1 
  10. smtp_connect_timeout 30 
  11. router_id LVS_DEVEL 
  12. #vrrp协议的配置 
  13. vrrp_instance VI_1 { 
  14. #工作模式 
  15. state MASTER 
  16. #监听的网卡 
  17. interface eth0 
  18. #虚拟路由id 需要和备服务器一致 
  19. virtual_router_id 51 
  20. #权重 优先级 
  21. priority 100 
  22. #vrrp包的发送周期 1s 
  23. advert_int 1 
  24. #权限验证 
  25. authentication { 
  26. auth_type PASS 
  27. auth_pass 1111 
  28. #需要绑定切换的VIP 
  29. virtual_ipaddress { 
  30. 192.168.200.16 
  31. 192.168.200.17 
  32. 192.168.200.18 

主服务器

  1. ! Configuration File for keepalived 
  2.  global_defs { 
  3.  notification_email { 
  4.  acassen@firewall.loc 
  5.  failover@firewall.loc 
  6.  sysadmin@firewall.loc 
  7.  } 
  8.  notification_email_from Alexandre.Cassen@firewall.loc 
  9.  smtp_server 192.168.200.1 
  10.  smtp_connect_timeout 30 
  11.  router_id LVS_DEVEL 
  12.  } 
  13.  vrrp_instance VI_1 { 
  14.  state MASTER 
  15.  interface eth0 
  16.  virtual_router_id 51 
  17.  priority 100 
  18.  advert_int 1 
  19.  authentication { 
  20.  auth_type PASS 
  21.  auth_pass 1111 
  22.  } 
  23.  #master默认只需要修改使用VIP即可 
  24.  virtual_ipaddress { 
  25.  192.168.17.200 
  26.  } 
  27.  } 

备服务器

  1. ! Configuration File for keepalived 
  2. global_defs { 
  3. notification_email { 
  4. acassen@firewall.loc 
  5. failover@firewall.loc 
  6. sysadmin@firewall.loc 
  7. notification_email_from Alexandre.Cassen@firewall.loc 
  8. smtp_server 192.168.200.1 
  9. smtp_connect_timeout 30 
  10. router_id LVS_DEVEL 
  11. vrrp_instance VI_1 { 
  12. #修改工作模式为备 
  13. state BACKUP 
  14. interface eth0 
  15. virtual_router_id 51 
  16. priority 100 
  17. advert_int 1 
  18. authentication { 
  19. auth_type PASS 
  20. auth_pass 1111 
  21. #注意修改VIP 
  22. virtual_ipaddress { 
  23. 192.168.17.200 

③分别按照顺序启动主服务器和备服务器的keepalived

  1. shell > service keepalived start 

④查看主备服务器的网卡信息

#需要通过ip a命令查看 分别在server01和server03查看 shell > ip a

2.4、模拟故障

模拟服务器故障宕机,查看是否可以切换服务到备用机器。

模拟宕机,关闭server01 master服务器,VIP自动切换到server03 backup服务器

  1. #抓包vrrp 
  2. shell > yum -y install tcpdump 
  3. shell > tcpdump vrrp -n 

 

责任编辑:武晓燕 来源: 今日头条
相关推荐

2019-09-26 09:04:22

NginxKeepalived服务器

2011-11-28 21:01:03

NginxKeepalived高可用

2015-07-08 10:24:15

NginxKeepalived

2023-05-15 08:20:56

2021-09-17 07:51:24

Keepalived服务高可用

2019-10-09 16:02:16

NginxKeepalivedLvs

2010-06-04 13:18:22

2018-03-14 11:13:35

Web服务器Nginx

2017-12-27 10:18:09

ApacheNginx服务器

2020-04-15 20:57:57

NginxWeb服务器

2019-12-24 14:28:00

KeepalivedNginxTomcat

2012-02-13 23:46:22

keepalived高可用

2011-09-08 10:42:38

Web服务器Nginx

2010-03-30 18:26:07

Nginx Web服务

2021-11-24 08:00:00

服务器Web系统

2011-09-30 13:02:10

51CTO博客一周热门Web架构

2021-08-23 10:59:01

Nginx高可用Linux

2010-03-24 17:10:10

Nginx Web服务

2010-03-12 10:48:03

2014-07-15 09:44:07

Linux Web服务Nginx
点赞
收藏

51CTO技术栈公众号