Nignx + Heartbeat 搭建HA(实验+详解)

参考阅读

LVS-DR + Keepalive搭建HA(实验+详解)
LVS的NAT模式搭建
Linux集群汇总

Nignx + Heartbeat(心跳检测)

Heartbeat是Linux-HA工程的一个组件,自1999年开始到现在,发布了众多版本,是目前开源Linux-HA项目最成功的一个例子,在行业内得到了广泛的应用

一、实验拓扑

在这里插入图片描述

二、nginx

首先,在主服务器上,即10.10.10.12上添加一个eth2的子接口,地址为10.10.10.100,作为客户端访问的地址

在这里插入图片描述

1、解压并安装其依赖关系

[root@localhost ~]# mount -t iso9660 /dev/cdrom /mnt/cdrom/
[root@localhost ~]# yum -y install pcre pcre-devel zlib zlib-devel

2、创建nginx用户及源码安装nginx

[root@localhost nginx-1.2.6]# useradd -s /sbin/nologin -M nginx

启动权限
-M 不添加家目录
[root@localhost nginx-1.2.6]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx
[root@localhost nginx-1.2.6]# make && makeinstall

3、修改网页文件后启动nginx

[root@localhost html]# vim index.html
[root@localhost html]# /usr/local/nginx/sbin/nginx 

注意:以上部署nginx网页在两台nginx服务器上都要做
在这里插入图片描述

三、heartbeat

在这里插入图片描述

3.1 安装解压其依赖关系

[root@localhost ~]# tar zxvf heartbeat.tar.gz 
heartbeat/
heartbeat/cluster-glue-1.0.5-6.el6.x86_64.rpm
heartbeat/heartbeat-3.0.4-2.el6.x86_64.rpm
heartbeat/resource-agents-3.9.5-46.el6.x86_64.rpm
heartbeat/cluster-glue-libs-1.0.5-6.el6.x86_64.rpm
heartbeat/PyXML-0.8.4-19.el6.x86_64.rpm
heartbeat/perl-TimeDate-1.16-13.el6.noarch.rpm
heartbeat/heartbeat-libs-3.0.4-2.el6.x86_64.rpm
[root@localhost ~]# ll
总用量 2528
-rw-------. 1 root root    1454 102 08:39 anaconda-ks.cfg
drwxr-xr-x  2 root root    4096 517 2017 heartbeat
-rw-r--r--  1 root root 1746728 416 2019 heartbeat.tar.gz
-rw-r--r--. 1 root root   46478 102 08:39 install.log
-rw-r--r--. 1 root root   10033 102 08:38 install.log.syslog
drwxr-xr-x  9 1001 1001    4096 222 05:32 nginx-1.2.6
-rw-r--r--  1 root root  726025 416 2019 nginx-1.2.6.tar.gz
drwxr-xr-x. 2 root root    4096 102 08:42 公共的
drwxr-xr-x. 2 root root    4096 102 08:42 模板
drwxr-xr-x. 2 root root    4096 102 08:42 视频
drwxr-xr-x. 2 root root    4096 102 08:42 图片
drwxr-xr-x. 2 root root    4096 102 08:42 文档
drwxr-xr-x. 2 root root    4096 102 08:42 下载
drwxr-xr-x. 2 root root    4096 102 08:42 音乐
drwxr-xr-x. 2 root root    4096 102 08:42 桌面
[root@localhost ~]# cd heartbeat
[root@localhost heartbeat]# ll
总用量 1948
-rw-r--r-- 1 root root  72744 625 2012 cluster-glue-1.0.5-6.el6.x86_64.rpm
-rw-r--r-- 1 root root 119096 625 2012 cluster-glue-libs-1.0.5-6.el6.x86_64.rpm
-rw-r--r-- 1 root root 165292 123 2013 heartbeat-3.0.4-2.el6.x86_64.rpm
-rw-r--r-- 1 root root 269468 123 2013 heartbeat-libs-3.0.4-2.el6.x86_64.rpm
-rw-r--r-- 1 root root  38264 1018 2014 perl-TimeDate-1.16-13.el6.noarch.rpm
-rw-r--r-- 1 root root 913840 525 2017 PyXML-0.8.4-19.el6.x86_64.rpm
-rw-r--r-- 1 root root 398016 324 2017 resource-agents-3.9.5-46.el6.x86_64.rpm
[root@localhost heartbeat]# yum -y install *

3.2 环境准备

1、安装时间同步服务,防止重放攻击

重放攻击:防止攻击者进行伪造攻击,伪造的报文时间会有延迟

[root@localhost heartbeat]# yum -y install ntp
[root@www ~]# vim /etc/ntp.conf 

在这里插入图片描述

restrict 10.10.10.0 mask 255.255.255.0 nomodify notrap
server 127.127.1.0
fudge 127.127.1.0 stratum 10

加入到开启自启

[root@localhost heartbeat]# service ntpd start
正在启动 ntpd:                                            [确定]
[root@localhost heartbeat]# chkconfig ntpd on

2、另外一个机器保证时间一致

[root@localhost ~]# yum -y install ntpdate
已加载插件:fastestmirror, refresh-packagekit, security
设置安装进程
Loading mirror speeds from cached hostfile
包 ntpdate-4.2.6p5-10.el6.centos.x86_64 已安装并且是最新版本
无须任何处理
[root@localhost ~]# ntpdate -u 10.10.10.12
22 Feb 07:01:33 ntpdate[29106]: adjust time server 10.10.10.12 offset -0.124058 sec

3、修改主机名

[root@localhost ~]# vim /etc/sysconfig/network

在这里插入图片描述

4、修改hosts文件

[root@www ~]# vim /etc/hosts

在这里插入图片描述

3.3 安装

1、修改heartbeat的配置文件

首先将配置文件拷贝到默认目录下

[root@localhost heartbeat-3.0.4]# cp -a ha.cf authkeys haresources /etc/ha.d/

2、认证服务,节点之间的认证配置,修改 /etc/ha.d/authkeys ,在主上修改

[root@localhost ha.d]# dd if=/dev/random bs=512 count=1 | openssl md5 #生成密钥随机数
记录了0+1 的读入
记录了0+1 的写出
44字节(44 B)已复制,7.0643e-05 秒,623 kB/(stdin)= bba049e9428b914976227d7e7d2a6212

在这里插入图片描述

修改权限,即修改authkeys 文件的权限,必须是600 要不不能识别

[root@localhost ha.d]# chmod 600 authkeys 
[root@localhost ha.d]# ll
总用量 44
-rw------- 1 root root   667 222 07:11 authkeys
-rw-r--r-- 1 root root 10502 123 2013 ha.cf
-rwxr-xr-x 1 root root   745 123 2013 harc
-rw-r--r-- 1 root root  5905 123 2013 haresources
drwxr-xr-x 2 root root  4096 222 06:54 rc.d
-rw-r--r-- 1 root root   692 123 2013 README.config
drwxr-xr-x 2 root root  4096 222 06:54 resource.d
-rw-r--r-- 1 root root  2082 324 2017 shellfuncs

3、修改ha.cf文件

a.使用哪块网卡进行心跳检测,本实验用的是eth2网卡

b.增加结点信息

在这里插入图片描述
在这里插入图片描述

4、修改haresources

在这里插入图片描述

www.centos1.com IPaddr::10.10.10.100/24/eth2:0

等同于

ifconfig eth2:0 10.10.10.100 netmask 255.255.255.0

5、使用scp将这些配置文件发送给备服务器

 scp ha.cf authkeys haresources root@10.10.10.13:/etc/ha.d/

四、测试

4.1 验证正常情况下的负载情况

启动heartbeat服务进行测试

[root@localhost ~]# /etc/init.d/heartbeat  start
Starting High-Availability services: INFO:  Resource is stopped
Done.

在这里插入图片描述

4.2 验证当主节点出现故障后的负载情况

当主服务器出现故障后,页面不能正常访问
在这里插入图片描述
在这里插入图片描述

过一段时间后,恢复正常,且页面切换至备服务器

在这里插入图片描述

4.3 当主节点恢复后的负载情况

当主服务器恢复后,过一段时间,页面跳转至主服务器

在这里插入图片描述

经如上测试,可见与预期相符


LVS+keepalive与Nginx+heartbeat比较

  • heartbeat检测的是网络的通讯能力,而不是服务的是否存活
  • 当nginx服务出现故障后(nginx不在Linux的内核里,而是独立的服务),heartbeat并不会切换服务器
  • LVS是内核级别的,出现故障后,keepalive直接切换
  • keepalive可以检测RS的down与否

比如此时在主服务器上关闭nginx服务

在这里插入图片描述

此时,客户端还是可以和10.10.10.100通,但是页面是不会跳转的

在这里插入图片描述

解决:

#!/bin/bash
PWD=/usr/local/script/jiankong
URL="http://10.10.10.11/index.html"
HTTP_CODE=`curl -o /dev/null -s -w "%{http_code}" "${URL}"` 
if [ $HTTP_CODE != 200 ]
    then
	service heartbeat stop
fi

使用脚本来判断http的返回状态码来确定nginx服务的正常与否,当监控出nginx服务不正常,直接关闭heartbeat,此时服务器直接切换

脚本测试

在这里插入图片描述

由于检测出http的状态码不正常,直接就将heartbeat服务关闭,速度是很快的,因为不用再去根据heartbeat的循环检测时间得到切换服务器命令


猜你喜欢

转载自blog.csdn.net/qq_41959899/article/details/113982035