用keepalived高可用nginx

单主模型

环境四个虚拟机

upstream server:node1:172.16.100.6         CeotOS 6

upstream server:node3:172.16.100.69         CeotOS 7

节点1:172.16.100.67                        CeotOS 7

节点2:node2:172.16.100.68                       CeotOS 7

节1&2

# yum install nginx -y

# cd /etc/nginx

# vim nginx.conf

                      include /etc/nginx/conf.d/*.conf;
                      upstream webservers {
                                           server 172.16.100.6:80 weight=1;
                                           server 172.16.100.69:80 weight=1;
                       }

                     location   /   {

                                           proxy_pass   http://websrvers/;

                     }

# nginx   -t  

# systemctl start nginx.service
# netstat -tnlp
用浏览器访问172.16.100.67
# scp nginx.conf node2:/etc/nginx/

节2
# systemctl start nginx.service
# netstat -tnlp

用浏览器访问172.16.100.68看能否负载均衡

  • 用keepalived监控nginx服务
节1&节2
# vim keepalived.conf 
     vrrp_script chk_nginx {
             script "killall -0 nginx &> /dev/null"
             interval 1 
             weight -10
       }
       vrrp_instance VI_1 {
             track_script {
                   chk_nginx
             }
       }
节1

# systemctl status nginx.service

# systemctl start keepalived.service ssh node2:systemctl start keepalived.service

# systemctl status keepalived.service

# ip addr list

如果没启动起来,可能是组播地址和虚拟地址的问题,修改一下即可

宕掉nginx,肯是否会转到节点2上

# systemctl stop nginx.service

# systemctl status nginx

节2

# ip addr list

节1

# systemctl start nginx.service

浏览器访问172.16.100.88

节1(主节点)只要在线,节2(备节点)就抢不走,除非节1宕掉
# vim notify

 #!/bin/bash

 #


 vip=172.16.100.88

 contact='root@localhost' 


 notify() {

 mailsubject="`hostname` to be $1: $vip floating"    

 mailbody="`date '+%F %H:%M:%S'`: vrrp transition, `hostname` changed to be $1"   

echo $mailbody | mail -s "$mailsubject" $contact

 }


 case "$1" in

           master)

              notify master
              systemctl restart nginx.service

              exit 0

           ;;

           backup)

               notify backup
               systemctl restart nginx.service

               exit 0

           ;;

           fault)

               notify fault

                exit 0

           ;;

           *)

               echo 'Usage: `basename $0` {master|backup|fault}'

               exit 1

            ;;

  esac

# scp notify.sh node2:/etc/keepalived/

# systemctl status keepalived

# ip addr list

主停掉,备也停掉,会自动启动nginx

# systemctl stop httpd.server

节2

# systemctl stop nginx.service

# systemctl status keepalived

节1

# systemctl status nginx.service

# systemctl status keepalived

用keepalived高可用nginx 用keepalived高可用nginx

双主模型

节1
# vim keepalived.conf
            vrrp_instance VI_2 {

                        state BACKUP
                        interface ens33

                        virtual_router_id 61

                        priority 99

                        advert_int 1

                        authentication {

                                 auth_type PASS
                                 auth_pass 1111
                       }

                       virtual_ipaddress {

                                172.16.100.180/16 dev ens33 label ens33:2

                       }
                       track_script {

                                      chk_nginx

                       }

                        notify_master “/etc/keepalived/notify.sh master”   (不手动启动nginx,而让监控系统确保nginx在线)

                        notify_backup “/etc/keepalived/notify.sh backup”

                        notify_fault “/etc/keepalived/notify.sh fault”

             }
节1&节2
(不手动启动nginx,而让监控系统确保nginx在线)
# vim notify.sh
   注释掉systemctl restart nginx.service
节2
# vim keepalived.conf
        vrrp_instance VI_2 {

                        state MASTER
                        interface ens33

                        virtual_router_id 61

                        priority 100

                        advert_int 1
                        authentication {

                                 auth_type PASS
                                 auth_pass 1111
                       }

                       virtual_ipaddress {

                                172.16.100.180/16 dev ens33 label ens33:2

                       }
                       track_script {

                                      chk_nginx
                       }

                       notify_master “/etc/keepalived/notify.sh master” 

                       notify_backup “/etc/keepalived/notify.sh backup”

                       notify_fault “/etc/keepalived/notify.sh fault”

         }
节1

# sytemctl restart keepalived.service; ssh node2'sytemctl restart keepalived.service'
# systemctl status keepalived
# ifconfig ens33:1 down
# sytemctl restart keepalived.service; ssh node2'sytemctl restart keepalived.service'
# systemctl status keepalived
用keepalived高可用nginx
节2
# systemctl status keepalived.service
# ip addr list
用浏览器访问两个地址,看调度是否有问题
节1

# sytemctl restart keepalived.service; sytemctl start httpd.service
# systemctl status keepalived

用keepalived高可用nginx
节2
# ifconfig
节1
# sytemctl stop httpd.service; sytemctl restart nginx.service'
# systemctl status nginx.service
# systemctl status keepalived
用keepalived高可用nginx
# ifconfig     




原创文章,作者:nene,如若转载,请注明出处:http://www.178linux.com/88155

(0)
上一篇 2017-10-15 16:07
下一篇 2017-10-15 22:15

相关推荐

  • N21-天天-第九周课程练习

    1、写一个脚本,判断当前系统上所有用户的shell是否为可登录shell(即用户的shell不是/sbin/nologin);分别这两类用户的个数;通过字符串比较来实现; #!/bin/bash awk -F: '$NF~"/sbin/nologin" {shell++} $NF!~&qu…

    Linux干货 2016-09-01
  • Linux进程篇16.0

    Linux进程介绍

    Linux干货 2017-12-18
  • RAID简介

    RAID是什么? RAID英文名字叫Redundant Arrays of Independent Disks,也叫磁盘阵列?阵列有是什么意思?通俗的理解就是把很多的硬盘组织在一起来使用。       RAID可以根据性能来划分为两种:有无冗余(容错)能力     &n…

    Linux干货 2016-02-14
  • 01day-计算机与操作系统基础

    第1章 计算机基础 1.1 什么是计算机 计算机是个笼统的概念,泛指一切计算功能的机器。这样扯的话,以前的算盘也是计算机了。我们现在的计算机应该称作电子计算机,我天朝人民称为电脑。听说谷歌在研究量子计算机,完全不懂是什么玩意。 一不小心在百度百科看到还有光子计算机,生物计算机,吓我一跳,赶紧复制链接过来,自己慢慢看《百度百科—-计算机》 &nbs…

    Linux干货 2016-06-29
  • shell编程及小命令

    1. ping 10.1.252.25d2 -c1 -w1 &> /dev/null && echo "The host is up" || echo "The host is down" 2. 在vim命令中, p或则P可以实现复制。     p: 复制到下一行 &n…

    Linux干货 2016-08-12
  • vim快捷方式

    Vim是从 vi 发展出来的一个文本编辑器。代码补全、编译及错误跳转等方便编程的功能特别丰富,在程序员中被广泛使用,和Emacs并列成为类Unix系统用户最喜欢的文本编辑器

    Linux干货 2017-11-26