Nginx + Keepalived achieve high availability web server

1、Nginx

Business Background

Now the company needs to quickly set up a web server, external web services available to users.

Demand Split

The need software-based http protocol, set up the service implementation

Introduction

 

 

Common usage:

1) web server software httpd http protocol

Similar web server software: apache (old) nginx (Russia) IIS (Microsoft)

2) Proxy Reverse Proxy

3) proxy server IMAP POP3 SMTP mailbox

4) load balancing LB loadblance

Nginx architecture features:

  • ① high reliability: stability management master process where a scheduling request distribution worker => worker process a response request multiple master worker
  • ② The deployment: (1) a smooth upgrade (2) can be configured quickly reload
  • ③ high concurrency: can respond to more requests at the same time tens of thousands of events epoll model
  • ④ quick response: especially in the static files, fast response sendfile
  • ⑤ consumption: cpu and memory requests 1w memory 2-3MB
  • ⑥ Distributed Support: seven reverse proxy load balancing

Official Website:

1.2 Installation

Common Installation:

  • ①yum installation configuration, use Nginx official sources or source EPEL
  • ② compile source code
 #添加运行用户
 shell > useradd -s/sbin/nologin -M www
 #安装依赖
 shell > yum -y install pcre-devel zlib-devel openssl-devel
 #编译安装
 shell > cd /root/soft
 shell > tar xvf nginx-1.14.2.tar.gz
 shell > cd nginx-1.14.2
 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

Compile Parameter Description

1.3 Contents Introduction

Review the installation directory / usr / local / nginx

1.4, the software operating parameters

View parameters nginx binary executable files

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

After performing display

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

The main general use:

  • nginx -s parameter control management service
  • -V parameter to view and compile nginx open module parameters
  • -T parameters to detect whether the configuration file for errors

2, Keepalived achieve high availability

Business Background

Singleton web server is able to meet the basic needs of the business, to provide web services. However, there is a single point of failure problem that when the server goes down, users will not be able to get service response.

In order to improve the user experience, users can continue to have to provide quality service, when the web server is unavailable, the standby server can take over web servers work, and continue to provide users with a response. Among them, we have to solve a problem and needs to quickly backup server automatically switched over.

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

需求拆分

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

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

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

 

 

2.1、介绍

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

 

 

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

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

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

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

2.2、安装

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

 #安装keepalived
 shell > yum -y install keepalived

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

2.3、配置

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

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

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

 shell > vim keepalived.conf

示例配置文件说明

 ! Configuration File for keepalived
 #发送邮件的配置
 global_defs {
    notification_email {
      [email protected]
      [email protected]
      [email protected]
    }
    notification_email_from [email protected]
    smtp_server 192.168.200.1
    smtp_connect_timeout 30
    router_id LVS_DEVEL
 }
 #vrrp协议的配置
 vrrp_instance VI_1 {
     #工作模式
     state MASTER
     #监听的网卡
     interface eth0
     #虚拟路由id 需要和备服务器一致
     virtual_router_id 51
     #权重 优先级
     priority 100
     #vrrp包的发送周期  1s
     advert_int 1
     #权限验证
     authentication {
         auth_type PASS
         auth_pass 1111
     }
     #需要绑定切换的VIP
     virtual_ipaddress {
         192.168.200.16
         192.168.200.17
         192.168.200.18
     }
 }

主服务器

 ! Configuration File for keepalived
 global_defs {
    notification_email {
      [email protected]
      [email protected]
      [email protected]
    }
    notification_email_from [email protected]
    smtp_server 192.168.200.1
    smtp_connect_timeout 30
    router_id LVS_DEVEL
 }
 vrrp_instance VI_1 {
     state MASTER
     interface eth0
     virtual_router_id 51
     priority 100
     advert_int 1
     authentication {
         auth_type PASS
         auth_pass 1111
     }
     #master默认只需要修改使用VIP即可
     virtual_ipaddress {
         192.168.17.200
     }
 }

备服务器

 ! Configuration File for keepalived
 global_defs {
    notification_email {
      [email protected]
      [email protected]
      [email protected]
    }
    notification_email_from [email protected]
    smtp_server 192.168.200.1
    smtp_connect_timeout 30
    router_id LVS_DEVEL
 }
 vrrp_instance VI_1 {
     #修改工作模式为备
     state BACKUP
     interface eth0
     virtual_router_id 51
     priority 100
     advert_int 1
     authentication {
         auth_type PASS
         auth_pass 1111
     }
     #注意修改VIP
     virtual_ipaddress {
         192.168.17.200
     }
 }

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

 shell > service keepalived start

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

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

2.4、模拟故障

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

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

 #抓包vrrp
 shell > yum -y install tcpdump
 shell > tcpdump vrrp -n
转自:https://zhuanlan.zhihu.com/p/74784743

Guess you like

Origin www.cnblogs.com/silentdoer/p/11909628.html