当前位置: 首页 > 编程笔记 >

keepalived+nginx高可用实现方法示例

支铭晨
2023-03-14
本文向大家介绍keepalived+nginx高可用实现方法示例,包括了keepalived+nginx高可用实现方法示例的使用技巧和注意事项,需要的朋友参考一下

1.keepalived介绍

keepalived最初是专为LVS负载均衡软件设计的,用来管理并监控LVS集群系统中各个服务节点的状态,后来又加入了实现高可用的VRRP功能。keepalived除了能够管理LVS软件外,还能支持其他服务的高可用解决方案。

keepalived通过VRRP协议实现高可用功能的。VRRP(Virtual Router Redundancy Protocol)虚拟路由冗余协议。VRRP出现的目的就是为了解决静态路由单点故障问题,它能保证当个别节点宕机时,整个网络可以不间断地运行。

2.keepalived高可用故障转移原理

keepalived高可用服务之间的故障转移,是通过VRRP来实现的。在keepalived服务工作时,主Master节点会不断地向备节点发送(多播的方式)心跳消息,用来告诉备Backup节点自己还活着。

  当主节点发生故障时,无法给备节点发送心跳消息,如果备节点无法继续检测到来自主节点的心跳。就会调用自身的接管程序,接管主节点的IP资源和服务。当主节点恢复时,备节点又会释放主节点故障时自身接管的IP资源和服务,恢复到原来的备用角色

3.安装nginx

3.1.主节点(192.168.80.22)

3.1.1.安装编译工具和库文件

yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel

3.1.2.安装pcre

#进入目录
cd /usr/local/develop/anginx

#上传安装文件并解压
tar -zxvf pcre-8.38.tar.gz

#进入安装目录
cd pcre-8.38

#检查配置
./configure

#编译、安装
make && make install

#查看pcre版本
pcre-config --version

3.1.3.安装nginx

#进入目录
cd /usr/local/develop/anginx
​
#上传安装文件,并解压
tar -zxvf nginx-1.8.1.tar.gz
​
#进入安装目录
cd nginx-1.8.1
​
#检查配置
./configure --prefix=/usr/local/develop/anginx/webserver/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre=/usr/local/develop/anginx/pcre-8.38
​
#编译安装
make && make install
​
#查看nginx版本
 /usr/local/develop/anginx/webserver/nginx/sbin/nginx -v
--------------------------------------------------------
[root@hadoop02 webserver]# /usr/local/develop/anginx/webserver/nginx/sbin/nginx -v
nginx version: nginx/1.8.1
​
#配置nginx(检查)
/usr/local/develop/anginx/webserver/nginx/sbin/nginx -t
​
#nginx管理命令
/usr/local/develop/anginx/webserver/nginx/sbin/nginx       # 启动 Nginx
/usr/local/develop/anginx/webserver/nginx/sbin/nginx -s stop       # 停止 Nginx
/usr/local/develop/anginx/webserver/nginx/sbin/nginx -s reload      # 重新载入配置文件
/usr/local/develop/anginx/webserver/nginx/sbin/nginx -s reopen      # 重启 Nginx

3.1.4.nginx基础配置

vi nginx.conf

#user nobody;
worker_processes 1;
​
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
​
pid    logs/nginx.pid;
​
​
events {
  worker_connections 1024;
}
​
​
http {
  include    mime.types;
  default_type application/octet-stream;
​
  log_format main '$remote_addr - $remote_user [$time_local] "$request" '
           '$status $body_bytes_sent "$http_referer" '
           '"$http_user_agent" "$http_x_forwarded_for"';
​
  access_log logs/access.log main;
​
  sendfile    on;
  #tcp_nopush   on;
​
  #keepalive_timeout 0;
  keepalive_timeout 65;
​
  #gzip on;
  
  #添加tomcat列表,真实应用服务器都放在这
  upstream tomcat_pool{
    #server tomcat地址:端口号 weight表示权值,权值越大,被分配的几率越大;
    server 192.168.80.22:8080 weight=4 max_fails=2 fail_timeout=30s;
    server 192.168.80.22:8081 weight=4 max_fails=2 fail_timeout=30s;
    
  }
​
  server {
    listen    80;
    server_name tomcat_pool;
​
    #charset koi8-r;
​
    #access_log logs/host.access.log main;
​
    location / {
      #root  html;
      #index index.html index.htm;
      proxy_pass http://tomcat_pool;  #转向tomcat处理
      proxy_set_header  Host       $host;
      proxy_set_header  X-Real-IP    $remote_addr;
      proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
    }
​
    #error_page 404       /404.html;
​
    # redirect server error pages to the static page /50x.html
    #
    error_page  500 502 503 504 /50x.html;
    location = /50x.html {
      root  html;
    }
​
​
}

3.2.备节点(192.168.80.21)

说明:安装方式同nginx主节点。

4.安装keepalived

4.1.主节点(192.168.80.22)

#安装keepalived
yum install keepalived -y
​
#启动keepalived服务
/etc/init.d/keepalived start
-------------------------------------------
[root@hadoop02 anginx]# /etc/init.d/keepalived start
正在启动 keepalived:                   [确定]
[root@hadoop02 anginx]# ps -ef |grep keepalived
root   15723   1 0 00:59 ?    00:00:00 /usr/sbin/keepalived -D
root   15724 15723 0 00:59 ?    00:00:00 /usr/sbin/keepalived -D
root   15725 15723 0 00:59 ?    00:00:00 /usr/sbin/keepalived -D
root   15731 15622 0 00:59 pts/1  00:00:00 grep keepalived
[root@hadoop02 anginx]#
​
#设置开机自启动
echo "/etc/init.d/keepalived start" >>/etc/rc.local
​
#关闭keepalived服务
/etc/init.d/keepalived stop
​
#编辑keepalived配置文件
vi /etc/keepalived/keepalived.conf
​
-----------------------------------------------------------
! Configuration File for keepalived
​
global_defs {
  notification_email {
   acassen@firewall.loc
   failover@firewall.loc
   sysadmin@firewall.loc
  }
  notification_email_from Alexandre.Cassen@firewall.loc
  smtp_server 192.168.200.1
  smtp_connect_timeout 30
  router_id lb01
}
​
vrrp_instance VI_1 {
  state MASTER
  interface eth1
  virtual_router_id 55
  priority 150
  advert_int 1
  authentication {
    auth_type PASS
    auth_pass server123
  }
  virtual_ipaddress {
    192.168.80.100 dev eth1 label eth1:1
  }
}
...........................................................

关于配置说明:

  •  【router_id】 是路由标识,在一个局域网里面应该是唯一的
    • 【vrrp_instance VI_1】{...}这是一个VRRP实例,里面定义了keepalived的主备状态、接口、优先级、认证和IP信息
    • 【state】 定义了VRRP的角色
    • 【interface】定义使用的接口,这里我的服务器用的网卡都是eth1
    • 【virtual_router_id】是虚拟路由ID标识,一组的keepalived配置中主备都是设置一致
    • 【priority】是优先级,数字越大,优先级越大,
    • 【auth_type】是认证方式
    • 【auth_pass】是认证的密码
  • 【virtual_ipaddress】 {...}定义虚拟IP地址,可以配置多个IP地址,这里我定义为192.168.80.100,绑定了eth1的网络接口,虚拟接口eth1:1

4.2.备节点(192.168.80.21)

#安装keepalived
yum install keepalived -y
​
#启动keepalived服务
/etc/init.d/keepalived start
-------------------------------------------
[root@hadoop02 anginx]# /etc/init.d/keepalived start
正在启动 keepalived:                   [确定]
[root@hadoop02 anginx]# ps -ef |grep keepalived
root   15723   1 0 00:59 ?    00:00:00 /usr/sbin/keepalived -D
root   15724 15723 0 00:59 ?    00:00:00 /usr/sbin/keepalived -D
root   15725 15723 0 00:59 ?    00:00:00 /usr/sbin/keepalived -D
root   15731 15622 0 00:59 pts/1  00:00:00 grep keepalived
[root@hadoop02 anginx]#
​
#设置开机自启动
echo "/etc/init.d/keepalived start" >>/etc/rc.local
​
#关闭keepalived服务
/etc/init.d/keepalived stop
​
#编辑keepalived配置文件
vi /etc/keepalived/keepalived.conf
​
-----------------------------------------------------------------
! Configuration File for keepalived
​
global_defs {
  notification_email {
   acassen@firewall.loc
   failover@firewall.loc
   sysadmin@firewall.loc
  }
  notification_email_from Alexandre.Cassen@firewall.loc
  smtp_server 192.168.200.1
  smtp_connect_timeout 30
  router_id lb02
}
​
vrrp_instance VI_1 {
  state BACKUP
  interface eth1
  virtual_router_id 55
  priority 100
  advert_int 1
  authentication {
    auth_type PASS
    auth_pass server123
  }
  virtual_ipaddress {
    192.168.80.100 dev eth1 label eth1:1
  }
}
.............................................................
​
​

5.测试

5.1.启动主备节点的keepalived服务

#在节点一执行(192.168.80.22)
/etc/init.d/keepalived start
-------------------------------------
[root@hadoop02 anginx]# ps -ef |grep keepalived
root   15788   1 0 01:09 ?    00:00:00 /usr/sbin/keepalived -D
root   15790 15788 0 01:09 ?    00:00:00 /usr/sbin/keepalived -D
root   15791 15788 0 01:09 ?    00:00:00 /usr/sbin/keepalived -D
root   15807 15622 0 01:33 pts/1  00:00:00 grep keepalived
[root@hadoop02 anginx]#
​
​
#在节点二执行(192.168.80.21)
/etc/init.d/keepalived start
---------------------------------------
[root@hadoop01 ~]# ps -ef |grep keepalived
root   11542   1 0 01:30 ?    00:00:00 /usr/sbin/keepalived -D
root   11544 11542 0 01:30 ?    00:00:00 /usr/sbin/keepalived -D
root   11545 11542 0 01:30 ?    00:00:00 /usr/sbin/keepalived -D
root   11550 11512 0 01:33 pts/1  00:00:00 grep keepalived
[root@hadoop01 ~]#

5.2.通过虚ip访问服务

http://192.168.80.100/session-redis-demo/

5.3.停止主节点keepalived服务

#在节点一执行(192.168.80.22)
/etc/init.d/keepalived stop
​
#观察备节点变化
ip addr
-------------------------------------------
[root@hadoop01 ~]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN 
  link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
  inet 127.0.0.1/8 scope host lo
  inet6 ::1/128 scope host 
    valid_lft forever preferred_lft forever
2: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
  link/ether 00:50:56:38:e5:46 brd ff:ff:ff:ff:ff:ff
  inet 192.168.80.21/24 brd 192.168.80.255 scope global eth1
  inet 192.168.80.100/32 scope global eth1:1
  inet6 fe80::250:56ff:fe38:e546/64 scope link 
    valid_lft forever preferred_lft forever
[root@hadoop01 ~]#

5.4.继续通过虚ip访问服务

http://192.168.80.100/session-redis-demo/

6.keepalived+nginx整合

说明:编写nginx守护脚本,如果nginx服务出现故障,则停止当前节点的keepalived服务。自动切换到备用节点。

6.1.编写nginx守护脚本

vi nginx_check.sh
​
--------------------------------------
#!/bin/bash
while true
do
if [ $(netstat -tlnp|grep nginx|wc -l) -ne 1 ]
then
  /etc/init.d/keepalived stop
fi
sleep 2
done
​
#给脚本授权
chmod u+x nginx_check.sh
​
#执行脚本
nohup /usr/local/develop/anginx/shell/nginx_check.sh &

6.2.停止主节点nginx服务

#停止主节点nginx服务
/usr/local/develop/anginx/webserver/nginx/sbin/nginx -s stop
​
#查找进程
[root@hadoop02 ~]# ps -ef |grep nginx
root   15915   1 0 01:51 ?    00:00:00 /bin/bash /usr/local/develop/anginx/shell/nginx_check.sh
root   16516 15753 0 01:54 pts/5  00:00:00 grep nginx
[root@hadoop02 ~]#
​
#观察备用节点变化【服务正常】
ip addr
--------------------------------------
[root@hadoop01 shell]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN 
  link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
  inet 127.0.0.1/8 scope host lo
  inet6 ::1/128 scope host 
    valid_lft forever preferred_lft forever
2: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
  link/ether 00:50:56:38:e5:46 brd ff:ff:ff:ff:ff:ff
  inet 192.168.80.21/24 brd 192.168.80.255 scope global eth1
  inet 192.168.80.100/32 scope global eth1:1
  inet6 fe80::250:56ff:fe38:e546/64 scope link 
    valid_lft forever preferred_lft forever
[root@hadoop01 shell]#
​
#再次重新启动主节点nginx和keepalived服务
/usr/local/develop/anginx/webserver/nginx/sbin/nginx
​
/etc/init.d/keepalived start

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持小牛知识库。

 类似资料:
  • 本文向大家介绍keepalived实现nginx高可用,包括了keepalived实现nginx高可用的使用技巧和注意事项,需要的朋友参考一下 keepalived直译就是保持存活,在网络里面就是保持在线了,也就是所谓的高可用或热备,用来防止单点故障(单点故障是指一旦某一点出现故障就会导致整个系统架构的不可用)的发生,keepalived实现的基础是vrrp,至于vrrp是什么请直接看这里vrrp

  • 本文向大家介绍keepalived结合nginx实现nginx高可用的方法,包括了keepalived结合nginx实现nginx高可用的方法的使用技巧和注意事项,需要的朋友参考一下 1.简介 Keepalived 是一个基于VRRP协议来实现的LVS服务高可用方案,可以利用其来避免单点故障。一个LVS服务会有2台服务器运行Keepalived,一台为主服务器(MASTER),一台为备份服务器(B

  • 说明 高可用 HA(High Availability),简单讲就是:我某个应用挂了,自动有另外应用起来接着扛着,致使整个服务对外来看是没有中断过的。这里的重点就是不中断,致使公司整个业务能不断进行中,把影响减到最小,赚得更多。 因为要不中断,所以我们就需要用到了 Keepalived。Keepalived 一般不会单独使用,基本都是跟负载均衡软件(LVS、HAProxy、Nginx)一起工作来达

  • 本文向大家介绍详解Keepalived+Nginx实现高可用(HA),包括了详解Keepalived+Nginx实现高可用(HA)的使用技巧和注意事项,需要的朋友参考一下 keepalived的HA分为抢占模式和非抢占模式,抢占模式即MASTER从故障中恢复后,会将VIP从BACKUP节点中抢占过来。非抢占模式即MASTER恢复后不抢占BACKUP升级为MASTER后的VIP。下面分别介绍Cent

  • 本文向大家介绍Keepalived+Nginx+Tomcat 实现高可用Web集群的示例代码,包括了Keepalived+Nginx+Tomcat 实现高可用Web集群的示例代码的使用技巧和注意事项,需要的朋友参考一下 Keepalived+Nginx+Tomcat 实现高可用Web集群 一、Nginx的安装过程 1.下载Nginx安装包,安装依赖环境包 (1)安装 C++编译环境 (2)安装pc

  • 本文向大家介绍Docker+keepalived+nginx实现主从热备的方法示例,包括了Docker+keepalived+nginx实现主从热备的方法示例的使用技巧和注意事项,需要的朋友参考一下 前言 为解决单点故障,我们需要配置主从热备方案,服务器数量有限,故使用Docker模拟安装配置。 本次配置默认已经安装了Docker。 配置环境:centos7 64位 docker版本:Docker