用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发展史

    前言     Linux属于类Unix中的一个当下比较流行的操作系统,占领了服务器大部分江山。作为一个专业复杂的操作系统,了解其发展过程是很有必要的。如果要讲linux的历史,肯定是从三个团体开发Multics系统说起,贝尔实验室离开Multics项目后,Tompson和他的同事一起创造了unix,而在unix的各种分支中BSD则是迅速发…

    Linux干货 2016-10-14
  • Btrfs文件系统

    btrfs文件系统特性: 1、多物理卷支持 Btrfs可由多个底层物理卷组成:支持raid,以联机添加,移除,修改 2、写时复制机制(cow)     复制,更新及替换指针,而非就地更新。在文件进行修改的时候,首先将文件复制一份出来,在复制的文件中进行修改,修改完成后,将指向原有文件的指针指向到修改完成的文件上,若修改完成的文件出现错误,则我们可以通过源文件…

    2016-04-10
  • 网络yum源的配置

    1.准备: 光盘 包 元数据 2.网络服务http(s)  ftp 查看是否安装  which  vsftp;  ls  /misc/cd/Packages | grep vsftpd 安装  rpm  -ivh  /misc/cd/Packages/vsftpd… …

    2017-06-13
  • Linux文件压缩、解压、归档总结

    一、简介   压缩对我们来说通常意味着减小文件体积,节省硬盘空间。在Windows平台上处理大文件或目录中的文件很多时我们都需要对这些文件进行压缩处理,压缩格式多种多样,如:zip、rar、7z等格式,压缩方式也比较多样话,支持按最大压缩比例或者最快时间进行压缩等多种方式,而生成的压缩包就可以让我们更方便的进行拷贝及归档整理。解压缩顾名思义就是回归…

    Linux干货 2015-09-26
  • N22-第五周博客作业

    1、显示当前系统上root、fedora或user1用户的默认shell; [root@bogon ~]# grep -E  "^(root|fedora|user1)" /etc/passwd  | cut -d: -f1,7r…

    Linux干货 2016-09-26
  • 压缩、解压缩和归档工具

    1  compress和uncompress 用法:     compress [-dfvcVr] [-b maxbits] [file…] 选项:     -d   解压缩     -c   结果…

    Linux干货 2016-08-18