Keepalived+Nginx+Apache主备及双活搭建测试
思韵闪耀
2020-11-10
0

keepalived+nginx高可用有主备和双活两种方式。主备方式下对外提供一个vip,同时只有一台服务器工作,另一台作备机;双活方式下对外提供两个vip,两台机器互为备份,下面详细说明搭建测试步骤。

 

主备模式

架构图:

配置:

主机 ip 操作系统 软件 备注
nginx01 172.27.9.91 Centos7.3.1611

nginx 端口82 

keepalived

关闭防火墙和selinu
nginx02 172.27.9.92 Centos7.3.1611

nginx 端口82

keepalived

关闭防火墙和selinu
web01 172.27.9.125 Centos7.3.1611 apache 端口1180 关闭防火墙和selinu
web02 172.27.9.126 Centos7.3.1611 apache 端口1180 关闭防火墙和selinu

 

1.nginx安装

nginx01和nginx02安装nginx参见Centos7安装nginx

2.nginx配置

两台nginx服务器配置相同,如下:

[root@nginx01 ~]# more /usr/local/nginx/conf/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;
    upstream webser{
             server 172.27.9.125:1180;
             server 172.27.9.126:1180;
           }
    server {
        listen       82;
        server_name  localhost;
        #charset koi8-r;
        #access_log  logs/host.access.log  main;
        location / {
            proxy_pass http://webser;
            #root   html;
            #index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}
[root@nginx01 ~]#

  

3.keepalived安装

分别在nginx01和nginx02上安装keepalived:

[root@nginx01 ~]# yum -y install keepalived

 

4.keepalived配置

nginx01上keepalived配置:

[root@nginx01 keepalived]# more keepalived.conf
! Configuration File for keepalived
global_defs {
   router_id proxy1 
}
vrrp_script chk_nginx {
  script "/etc/keepalived/check_nginx.sh"
  interval 2    #健康检查周期
  weight 20     #优先级变化幅度
  fall 3         #判定服务异常的检查次数
  rise 2         #判定服务正常的检查次数
}
vrrp_instance VI_1 {
    state MASTER 
    interface ens33
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        172.27.9.200
    }
    track_script {
        chk_nginx
    }
}

nginx02上的keepalived配置:

[root@nginx02 keepalived]# more keepalived.conf
! Configuration File for keepalived
global_defs {
   router_id proxy2
}
vrrp_script chk_nginx {
  script "/etc/keepalived/check_nginx.sh"
  interval 2
  weight 20 
  fall 3
  rise 2
}
vrrp_instance VI_1 {
    state BACKUP 
    interface ens33
    virtual_router_id 51
    priority 90
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        172.27.9.200
    }
    track_script {
        chk_nginx
    }
}

check_nginx.sh脚本:

[root@nginx02 keepalived]# more check_nginx.sh 
#!/bin/bash
A=`ps -C nginx --no-header |wc -l`
if [ $A -eq 0 ];then
      /usr/local/nginx/sbin/nginx
      if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
      pkill keep
      fi
fi

该脚本用户检测nginx进程是否存在,若不存在则重启,若重启失败则直接杀掉keepalived进程,触发切换。(若没有pkill命令请先安装)

 

5.apache安装

在web01和web02上分别安装配置apache:

[root@web01 ~]# yum -y install httpd
[root@web01 ~]# systemctl start httpd
[root@web01 ~]# systemctl enable httpd
[root@web01 /]# echo web01-172.27.9.125 >/var/www/html/index.html

[root@web02 /]# echo web02-172.27.9.126 >/var/www/html/index.html

分别修改apache默认端口,有80更改为1180:

[root@web01 /]# view /etc/httpd/conf/httpd.conf
Listen 1180

 

 

6.启动服务

启动两台服务器nginx和keepalived服务。

[root@nginx01 ~]# nginx
[root@nginx01 ~]# service keepalived start
Redirecting to /bin/systemctl start  keepalived.service

 

7.高可用测试

页面访问http://172.27.9.200:82/

vip查看:

发现vip在nginx01上,此时对外提供服务的为nginx01,nginx02用作备份。停止nginx01上的keepalived服务,触发切换。

[root@nginx01 ~]# pkill keep
[root@nginx01 ~]# ps -ef|grep keep
root      11389   2172  0 16:56 pts/0    00:00:00 grep --color=auto keep

此时vip切换至nginx02,刷新页面访问http://172.27.9.200:82/

发现访问vip还是以轮询方式访问后端web服务器,此时对外提供服务器的为nginx02,nginx01相当于宕机状态。

 

双活模式

架构图:

配置:

主机 ip 操作系统 软件 vip
nginx01 172.27.9.91 Centos7.3.1611

nginx 端口82 

keepalived

172.27.9.200
nginx02 172.27.9.92 Centos7.3.1611

nginx 端口82

keepalived

172.27.9.210
web01 172.27.9.125 Centos7.3.1611 apache 端口1180 /
web02 172.27.9.126 Centos7.3.1611 apache 端口1180 /

 

1.keepalived配置

nginx01上keepalived配置:

[root@nginx01 ~]# more /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
   router_id proxy1 
}
vrrp_script chk_nginx {
  script "/etc/keepalived/check_nginx.sh"
  interval 2
  weight 20
  fall 3
  rise 2
}
vrrp_instance VI_1 {
    state MASTER
    interface ens33
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        172.27.9.200
    }
    track_script {
        chk_nginx
    }
}
vrrp_instance VI_2 {
    state BACKUP 
    interface ens33
    virtual_router_id 52
    priority 90
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        172.27.9.210
    }
    track_script {
        chk_nginx
    }
}

nginx02上keepalived配置:

[root@nginx02 ~]# more /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
   router_id proxy2
}
vrrp_script chk_nginx {
  script "/etc/keepalived/check_nginx.sh"
  interval 2
  weight 20
  fall 3
  rise 2
}
vrrp_instance VI_1 {
    state BACKUP 
    interface ens33
    virtual_router_id 51
    priority 90
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        172.27.9.200
    }
    track_script {
        chk_nginx
    }
}
vrrp_instance VI_2 {
    state MASTER
    interface ens33
    virtual_router_id 52
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        172.27.9.210
    }
    track_script {
        chk_nginx
    }
}

修改keepalived配置后分别重启keepalived服务:

[root@nginx01 ~]# service keepalived restart

 

 

2.vip查看

nginx01:

nginx02:

 

3.页面访问:

vip1:http://172.27.9.200:82/

vip2:http://172.27.9.210:82/ 

发现vip1和vip2分别以轮询方式访问web服务器

 

4.高可用测试

此时vip1在nginx01上,vip2在nginx02上,两台服务器互为主备一期对外提供服务。现在停止nginx01上的keepalived服务,模拟宕机,触发切换。

[root@nginx01 ~]# pkill keep
[root@nginx01 ~]# ps -ef|grep keep
root      13688   2172  0 17:22 pts/0    00:00:00 grep --color=auto keep

页面访问:

vip1:http://172.27.9.200:82/

vip2:http://172.27.9.210:82/

发现vip1和vip2访问web服务正常。vip查看:

nginx01:

nginx02:

发现vip1漂移至vip2,nginx02接管nginx01的vip1,此时nginx02单独对外提供服务。

 

      总结:

      1.主备模式对外只提供一个vip,访问便捷,但同时只有一台服务器对外提供服务;

      2.双活模式对外提供两个vip,访问比较麻烦,但同时又两台服务器对外提供服务;

      3.不管主备模式还是双活模式都能高可用运行。

【版权声明】
本站部分内容来源于互联网,本站不拥有所有权,不承担相关法律责任。如果发现本站有侵权的内容,欢迎发送邮件至masing@13sy.com 举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。

相关内容

将IIS设置克隆到新服务器...
将IIS设置克隆到新服务器的最佳方法是使用IIS配置文件。以下是详...
2025-06-06
网页保护、网页图片保护
1、禁止另存网页,把如下代码加入到网页body/body中 程序代...
2025-05-30
搭建Git服务器及本机克隆...
Git是什么?Git是目前世界上最先进的分布式版本控制系统。SVN...
2025-03-17
ubuntu环境下搭建gi...
操作环境:服务器:Ubuntu 24.04.2 LTS+git 2...
2025-03-17
Ubuntu防火墙ufw的...
一、不使用IPv6打开UFW配置。sudo vim /e...
2024-12-28
ubuntu防火墙命令介绍
ubuntu在开启ufw防火墙前,为了避免与iptables现有规...
2024-07-21

热门资讯

SIOCADDRT: Netw... SIOCADDRT: Network is unreachable SIOCADDRT: 网络不可达...
centos7下创建新用户和组 linux下添加,删除,修改,查看用户和用户组 1 .增加一个test组 groupadd test...
linux通过sed 修改文件... 13sy.txt里面内容如下 A; B; C; write by luohao199621; 1.要...
关于cannot remove... 关于cannot remove directory: Directory not empty的解决办...
mount: unknown ... mount: unknown filesystem type LVM2_member解决方案 系统启...
OpenMediaVault安... OpenMediaVault 是一个基于Debian的专用 Linux 发行版,用于构建网络连接存储...
centos7 双网卡双网关的... 最近公司需要在一台服务器上同时使用内网和外网,并且都需要跨网段访问,因此需要双网关,但是一台机子上只...
ethtool 命令详解 1 概述 ethtool 是用于查询及设置网卡参数的命令。 2 命令详解 2.1 命令格式 (1) ...
CentOS7单网卡设置双IP... linux系统CentOS7单网卡设置双IP的方法,centos7.5、7.6、7.7设置双IP教程...
Linux进程状态D,S,Z的... Linux进程状态top,ps中看到进程状态D,S,Z的含义 在top和ps命令中有一列显示进程状态...