欢迎来到代码驿站!

Nginx

当前位置:首页 > 服务器 > Nginx

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

时间:2021-02-01 09:57:04|栏目: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

上一篇:Nginx配置SSL证书监听443端口

栏    目:Nginx

下一篇:CentOS Rsync服务端与Windows cwRsync客户端实现数据同步配置方法

本文标题:keepalived+nginx高可用实现方法示例

本文地址:http://www.codeinn.net/misctech/55074.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有