用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)
nenenene
上一篇 2017-10-15
下一篇 2017-10-15

相关推荐

  • linux历史命令history详解

    history命令 history命令用于显示指定数目的指令命令,读取历史命令文件中的目录到历史命令缓冲区和将历史命令缓冲区中的目录写入命令文件。 该命令单独使用时,仅显示历史命令,在命令行中,可以使用符号!执行指定序号的历史命令。例如,要执行第2个历史命令,则输入!2。  128  ls  129  history[…

    Linux干货 2016-09-05
  • centos7 搭建SVN服务器

    运维常见的工作就是日常软件的安装和维护,SVN虽然被Git侵占了市场份额,但是仍然是高效简捷的源码管理工具。从日常软件的安装部署开始,熟悉一些Linux的常用命令。

    Linux干货 2018-03-26
  • 【N25第一周作业】Linux基础

    题目:   1、描述计算机的组成及其功能。   2、按系列罗列Linux的发行版,并描述不同发行版之间的联系与区别。   3、描述Linux的哲学思想,并按照自己的理解对其进行解释性描述。   4、说明Linux系统上命令的使用格式;详细介绍ifconfig、echo、tty、startx、export、pwd、his…

    Linux干货 2016-12-03
  • Linux启动过程及系统初始化

    一、前言     服务器在正常的运行过程中,很少有重启的机会。在正常的情况下,也很少有出现启动异常的情况,但是在遭到了人为或者误操作的情况后,可能会出现启动异常的情况。为了加深学习系统启动流程,能够更加深入理解Linux系统的启动流程。总结了这篇文字。 二、简述启动流程     …

    Linux干货 2016-11-23
  • Java中的Map List Set等集合类

    Map List Set等集合类: 一、概述 在JAVA的util包中有两个所有集合的父接口Collection和Map,它们的父子关系: +Collection 这个接口extends自 –java.lang.Iterable接口 ├+List(接口 代表有序,可重复的集合。列表) │├ ArreyList   &…

    Linux干货 2015-04-07
  • DNS服务器之配置

    一、安装DNS服务器     DNS服务的安装包为bind,使用yum直接安装即可:     安装完成后,查看DNS服务相关的文件: 二、配置正向区域     1、在配置正向区域之前,先来看下主配置文件中的内容:    &nbsp…

    Linux干货 2015-05-03