Nginx+Keepalived主从模式-负载均衡高可用(CentOS7.5)

简介

这种方案,使用一个虚拟VIP地址,前端使用2台机器,一台做主,一台做备,但同时只有一台机器工作,另一台备机在主机器不出现故障的时候,永远处于浪费状态,对于服务器不多的网站,该方案并不经济实惠,但能快速切换。

关于Nginx版本

Mainline version:开发版

Stable version:稳定版

Legacy versions:遗留的老版本

官方地址:http://nginx.org/,找到“news”中,最新的一个stable version

下载地址:http://nginx.org/download/,找到这个包的下载链接,右键复制链接地址

规划:

LB-01:16.155.199.223  nginx+keepalived-master

LB-02:16.155.197.42  nginx+keepalived-backup

VIP:16.155.197.100

OS:CentOS 7.5 

(tips:master,backup机器一定要在同一个网段内,vip也要设置在同一个网段内,不知道怎么在同一个网段内的小伙伴请自行百度一下)

Nginx+Keepalived主从架构

Tips:

在下面的部署过程中,为了节省篇幅,只显示了LB-01:16.155.199.223上的部署过程。新手请注意,按照部署过程,凡是在LB-01:16.155.199.223上做的所有配置(准备工作、部署Nginx、部署Keepalived),都需要在LB-02:16.155.197.42上,再部署一次,并保持两边的配置过程一样!

1.准备工作

查看centos版本命令:

cat /etc/centos-release

1.1 关闭SELinux

[root@example01 ~]# vim /etc/sysconfig/selinux

SELINUX=disabled

1.2 关闭IPTABLES防火墙

[root@example01 ~]# systemctl unmask firewalld

[root@example01 ~]# systemctl start firewalld

[root@example01 ~]# firewall-cmd --zone=public --add-port=80/tcp --permanent 开放80端口

[root@example01 ~]# firewall-cmd --direct --permanent --add-rule ipv4 filter INPUT 0 --in-interface em1 --destination 224.0.0.18 --protocol vrrp -j ACCEPT

'[root@example01 ~]# firewall-cmd --reload


1.3 安装wget

[root@example01 ~]# yum -y install wget

准备工作到此为止,reboot命令,重启两台服务器,使得SELinux配置生效

2.部署nginx

2.1 安装编译工具及库文件

[root@example01 ~]# yum -y install gcc gcc-c++ make automake autoconf libtool pcre pcre-devel zlib zlib-devel openssl openssl-devel

2.2 安装nginx

[root@example01 ~]# cd /usr/local/src/

[root@example01 src]# wget http://nginx.org/download/nginx-1.6.2.tar.gz

[root@example01 src]# tar -zxvf nginx-1.6.2.tar.gz

 [root@example01 src]# cd nginx-1.6.2

[root@example01 nginx-1.6.2]# ./configure --prefix=/usr/local/nginx \--with-http_stub_status_module \  --with-http_ssl_module 

 [root@example01 nginx-1.6.2]# make && make install

配置报错:

./configure: error: the HTTP rewrite module requires the PCRE library.

You can either disable the module by using --without-http_rewrite_module

option, or install the PCRE library into the system, or build the PCRE library

statically from the source with nginx by using --with-pcre= option.

解决办法:

[root@example01 nginx-1.6.2]# ./configure --prefix=/usr/local/nginx \> --without-http_rewrite_module

2.3 配置nginx

2.3.1 创建nginx运行使用的用户www

[root@example01 nginx-1.6.2]# /usr/sbin/groupadd www

[root@example01 nginx-1.6.2]# /usr/sbin/useradd -g www www

2.3.2 启动nginx

启动服务

[root@example01 nginx-1.6.2]# /usr/local/nginx/sbin/nginx

重载nginx配置

[root@example01 nginx-1.6.2]# /usr/local/nginx/sbin/nginx -s reload

开机启动

[root@example01 src]# vim /etc/rc.local # Nginx/usr/local/nginx/sbin/nginx

2.3.3 编辑index.html文件

编辑LB-01:16.155.199.223

[root@example01 nginx-1.6.2]# vim /usr/local/nginx/html/index.html    14

Welcome to nginx!Server01

编辑LB-02:16.155.197.42

[root@example02 nginx-1.6.2]# vim /usr/local/nginx/html/index.html    14

Welcome to nginx!Server02

2.3.4 配置nginx.conf

    2 user  www www;

    3 worker_processes  1;

  35    upstream my Server {

  36        ip_hash;

  37        server 16.155.199.223;

  38        server 16.155.197.42;

  39    }

Tips:

负载均衡模块用于从”upstream”指令定义的后端主机列表中选取一台主机。nginx先使用负载均衡模块找到一台主机,再使用upstream模块实现与这台主机的交互。

从配置我们可以看出负载均衡模块的使用场景:

1.核心指令”ip_hash”只能在upstream {}中使用。这条指令用于通知nginx使用ip hash负载均衡算法。如果没加这条指令,nginx会使用默认的round robin负载均衡模块。

2.upstream {}中的指令可能出现在”server”指令前,可能出现在”server”指令后,也可能出现在两条”server”指令之间。

2.3.5 浏览器访问:

http://16.155.199.223/

LB-01:16.155.199.223

http://16.155.197.42

LB-02:16.155.197.42

nginx其它命令:

/usr/local/nginx/sbin/nginx -s reload# 重新载入配置文件

/usr/local/nginx/sbin/nginx -s reopen# 重启 Nginx

/usr/local/nginx/sbin/nginx -s stop# 停止 Nginx

3.部署keepalived

3.1 安装keepalived

[root@example01 src]# yum -y install keepalived

查看keepalived版本

[root@example01 src]# keepalived -vKeepalived v1.2.13(03/19,2015)

3.2 修改keepalived的配置文件

LB-01:16.155.199.223/的配置

[root@example01 src]# vim /etc/keepalived/keepalived.conf

  vrrp_script chk_nginx {

    script "/etc/keepalived/nginx_check.sh"    # 检测nginx状态的脚本路径

    interval 2                # 检测时间间隔2s

    weight -20                # 如果脚本的条件成立,权重-20

  }

  vrrp_instance VI_1 {

      state MASTER              # 服务状态;MASTER(工作状态)BACKUP(备用状态)

      interface ens192              # VIP绑定网卡,大家默认的一般是eth0,我这里个性化设置是ens192

      virtual_router_id 51      # 虚拟路由ID,主、备节点必须一致

      mcast_src_ip 16.155.199.223 # 本机IP

      nopreempt                # 优先级高的设置,解决异常回复后再次抢占的问题

      priority 100              # 优先级;取值范围:0~254;MASTER > BACKUP

      advert_int 1              # 组播信息发送间隔,主、备节点必须一致,默认1s

      authentication {          # 验证信息;主、备节点必须一致

          auth_type PASS          # VRRP验证类型,PASS、AH两种

          auth_pass 1111          # VRRP验证密码,在同一个vrrp_instance下,主、从必须使用相同的密码才能正常通信

      }

    track_script {          # 将track_script块加入instance配置块

          chk_nginx        # 执行Nginx监控的服务

      }

      virtual_ipaddress {        # 虚拟IP池,主、备节点必须一致,可以定义多个VIP

          16.155.197.100          # 虚拟IP

      }

  }


LB-02:16.155.197.42的配置

[root@example02 src]# vim /etc/keepalived/keepalived.conf

  vrrp_script chk_nginx {

    script "/etc/keepalived/nginx_check.sh"

    interval 2

    weight -20

  }

  vrrp_instance VI_1 {

      state BACKUP

      interface ens192

      virtual_router_id 51

      mcast_src_ip 16.155.197.42

      priority 90

      advert_int 1

      authentication {

          auth_type PASS

          auth_pass 1111

      }

      track_script {

          chk_nginx

      }

      virtual_ipaddress {

          16.155.197.100

      } 

  }

3.3 编写nginx状态监测脚本

[root@example01 keepalived]# vim /etc/keepalived/nginx_check.sh

  #!/bin/bash

  A=`ps -C nginx  --no-header |wc -l`

  if [ $A -eq 0 ];then

          /usr/local/nginx/sbin/nginx

          sleep 2

          if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then

                  killall keepalived

          fi

  fi

脚本要求:如果 nginx 停止运行,尝试启动,如果无法启动则杀死本机的 keepalived 进程, keepalied将虚拟 ip 绑定到 BACKUP 机器上。

3.4 保存脚本,赋予执行权限

[root@example01 keepalived]# chmod +x /etc/keepalived/nginx_check.sh

  [root@example01 keepalived]# ll

  total 8

  -rw-r--r--. 1 root root 3602 Mar 27 23:46 keepalived.conf

  -rwxr-xr-x. 1 root root  191 Mar 27 23:53 nginx_check.sh

3.5 启动keepalived

开机启动

[root@example02 src]# chkconfig keepalived on

启动服务

[root@example01 keepalived]# service keepalived startStartingkeepalived:[  OK  ]

4.keepalived+nginx的高可用测试

4.1 查看服务器上的地址

查看MASTER的地址:

[root@example01 keepalived]# ip add 

1: lo:mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever2: ens192: mtu 1500 qdisc mq state UP group default qlen 1000

    link/ether 00:50:56:b0:2e:17 brd ff:ff:ff:ff:ff:ff

    inet 16.155.199.223/21 brd 16.155.199.255 scope global noprefixroute dynamic ens192

      valid_lft 113604sec preferred_lft 113604sec

    inet 16.155.197.100/32 scope global ens192      # 注意,此时MASTER上存在一个VIP

      valid_lft forever preferred_lft forever

    inet6 fe80::c453:9e3c:8efd:1d73/64 scope link noprefixroute

      valid_lft forever preferred_lft forever


查看BACKUP的地址:

[root@example02 src]# ip add 

1: lo:mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever2: ens192: mtu 1500 qdisc mq state UP group default qlen 1000

    link/ether 00:50:56:b0:3b:c4 brd ff:ff:ff:ff:ff:ff

    inet 16.155.197.42/21 brd 16.155.199.255 scope global noprefixroute dynamic ens192

      valid_lft 110123sec preferred_lft 110123sec

    inet6 fe80::c5d2:fa65:bf3c:e8a8/64 scope link tentative dadfailed

      valid_lft forever preferred_lft forever

    inet6 fe80::4440:cea8:b176:daa2/64 scope link tentative dadfailed

      valid_lft forever preferred_lft forever

    inet6 fe80::c865:c307:7968:5daf/64 scope link tentative dadfailed

      valid_lft forever preferred_lft forever

浏览器访问:http://16.155.197.100。



4.2 关闭MASTER上的nginx,keepalived会将它重新启动

[root@example01 keepalived]# /usr/local/nginx/sbin/nginx -s stop

4.3 关闭MASTER上的keepalived,VIP会切换到BACKUP上

[root@example01 keepalived]# service keepalived stopStoppingkeepalived:[  OK  ]

4.4 验证VIP的漂移

验证方法1:通过ip add查看VIP的漂移

[root@example01 keepalived]# ip add

1: lo:mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever2: ens192: mtu 1500 qdisc mq state UP group default qlen 1000

    link/ether 00:50:56:b0:2e:17 brd ff:ff:ff:ff:ff:ff

    inet 16.155.199.223/21 brd 16.155.199.255 scope global noprefixroute dynamic ens192

      valid_lft 113216sec preferred_lft 113216sec

    inet6 fe80::c453:9e3c:8efd:1d73/64 scope link noprefixroute

      valid_lft forever preferred_lft forever

 [root@example02 src]# ip add 

1: lo:mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever2: ens192: mtu 1500 qdisc mq state UP group default qlen 1000

    link/ether 00:50:56:b0:3b:c4 brd ff:ff:ff:ff:ff:ff

    inet 16.155.197.42/21 brd 16.155.199.255 scope global noprefixroute dynamic ens192

      valid_lft 110002sec preferred_lft 110002sec

    inet 16.155.197.100/32 scope global ens192    #注意此时vip漂浮到backup机器上了

      valid_lft forever preferred_lft forever

    inet6 fe80::c5d2:fa65:bf3c:e8a8/64 scope link tentative dadfailed

      valid_lft forever preferred_lft forever

    inet6 fe80::4440:cea8:b176:daa2/64 scope link tentative dadfailed

      valid_lft forever preferred_lft forever

    inet6 fe80::c865:c307:7968:5daf/64 scope link tentative dadfailed

      valid_lft forever preferred_lft forever


验证方法2:浏览器访问:http://16.155.197.100。

刷新页面,显示“Welcome to nginx!Server02”,表示已经VIP已经漂移到了BACKUP服务器上

高可用

到这里,整个部署就已经完成了!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,242评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,769评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,484评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,133评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,007评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,080评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,496评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,190评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,464评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,549评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,330评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,205评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,567评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,889评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,160评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,475评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,650评论 2 335

推荐阅读更多精彩内容