“lnmap实战之负载均衡架构(无高可用)”之新增keepalived高可用

“lnmap实战之负载均衡架构(无高可用)”之新增keepalived高可用

我之前有一篇”lnmap实战之负载均衡架构(无高可用)“博客是专门部署了lanmap,之前没有做高可用,那么我们现在就把高可用补上去吧

这样我们照着之前的文档从新部署一下 1.机器结构如下:

192.168.42.150 node0 [负载均衡器]
192.168.42.151 node1 [负载均衡器 新增]
192.168.42.152 node2 [web1]
192.168.42.153 node3 [web2]
192.168.42.154 node4 [nfs 共享存储]
192.168.42.155 node5 [memcached session存储]
192.168.42.156 node6 [mariadb]

我们在此基础之上新增一台nginx[负载均衡器]
我们此次是针对这两台nginx负载均衡器做高可用

2.我们依照上面的机器结构和之前的部署文档,把”lnmap实战之负载均衡架构(无高可用)”部署出来,部署出来,测试没问题之后我们在node1节点上安装nignx
安装方法和node0一样
node1操作:
(1).安装nginx

yum install nginx -y

(2).备份nginx.conf

cp /etc/nginx/nginx.conf{,.back}

(3).将node0的nginx配置文件cp一份到node1 ->[记得是在node0上操作]

scp /etc/nginx/nginx.conf  192.168.42.151:/etc/nginx/
cd /etc/nginx/conf.d/
scp *.conf  192.168.42.151:/etc/nginx/conf.d/

(4).更改node1上的hosts文件

echo "192.168.42.151 www.test.com">>/etc/hosts
echo "192.168.42.151  test.discuz.com">>/etc/hosts
cat /etc/hosts
nginx -t
nginx -s reload

3.nginx+keepalived高可用

node0,node1 关闭nignx

node0:
(1).安装keepalived

yum install keepalived -y

(2).配置keepalived
配置如下:

! Configuration File for keepalived

global_defs {
   notification_email {
     root@localhost
   }
   notification_email_from keepalived@localhost
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id node0
   vrrp_mcast_group4 224.1.101.23

}

#存在文件时,检测成功,即执行降级;否则不存在,全部退出;实现服务器切换
vrrp_script chk_down{
    script "[[ -f /etc/keepalived/down ]] && exit 1 || exit 0"
    interval 1
    weight -10
    fall 1
    rize 1
}


#脚本,健康状态检测,检测nginx是否存活
vrrp_script chk_nginx {   
    script "killall -0 nginx && exit 0 || exit 1"
    interval 1
    weight -10
    fall 1
    rise 1
}

vrrp_instance sr1 {
    state MASTER
    interface ens33
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass rEiszbuO
    }
    virtual_ipaddress {
        192.168.42.182/24 dev ens33 label ens33:0
    }

    #脚本调用
    track_script {
        chk_down
        chk_nginx
    }

    notify_master "/etc/keepalived/notify.sh master"
    notify_backup "/etc/keepalived/notify.sh backup"
    notify_fault "/etc/keepalived/notify.sh fault"

}

node1:

(1).安装keepalived

yum install keepalived -y

(2).配置keepalived

配置如下:

! Configuration File for keepalived

global_defs {
   notification_email {
     root@localhost
   }
   notification_email_from keepalived@localhost
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id node1
   vrrp_mcast_group4 224.1.101.23

}

#存在文件时,检测成功,即执行降级;否则不存在,全部退出;实现服务器切换
vrrp_script chk_down{
    script "[[ -f /etc/keepalived/down ]] && exit 1 || exit 0"
    interval 1
    weight -10
    fall 1
    rize 1
}


#脚本,健康状态检测,检测nginx是否存活
vrrp_script chk_nginx {   
    script "killall -0 nginx && exit 0 || exit 1"
    interval 1
    weight -10
    fall 1
    rise 1
}

vrrp_instance sr1 {
    state BACKUP
    interface ens33
    virtual_router_id 51
    priority 96
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass rEiszbuO
    }
    virtual_ipaddress {
        192.168.42.182/24 dev ens33 label ens33:0
    }

    #脚本调用
    track_script {
        chk_down
        chk_nginx
    }

    notify_master "/etc/keepalived/notify.sh master"
    notify_backup "/etc/keepalived/notify.sh backup"
    notify_fault "/etc/keepalived/notify.sh fault"

}

调用脚本notify.sh:

cd /etc/keepalived/
[root@centos703 keepalived]# vim notify.sh 
#!/bin/bash
#
contact='root@localhost'
notify() {
     mailsubject="vrrp:$(hostname) to be $1"
     mailbody="$(hostname) to be $1,vrrp transition, $(date)."
     echo "$mailbody" | mail -s "$mailsubject" $contact
}
    case $1 in
    master)
      notify master
      systemctl start nginx
      ;;
    backup)
      notify backup
      systemctl start nginx
      ;;
    fault)
      notify fault
      systemctl stop nginx
      ;;
    *)
      echo "Usage: $(basename $0) {master|backup|fault}"
      exit 1
      ;;
esac

6.node0,node1重新更改hosts解析

vim /etc/hosts
192.168.42.182  www.test.com
192.168.42.182  test.discuz.com

7.我们在node3,node4上是做了两个虚拟主机,因此我们在负载均衡上也是要做两个虚拟主机,并且需要把$host推送到后方的机器

test.conf 的内容如下:

server {
  listen 80;
  server_name www.test.com;
  location / {
        proxy_pass http://sshsrvs;
        proxy_set_header Host  $host;
        proxy_set_header X-Forwarded-For  $remote_addr;
        add_header X-Via  $server_addr;
        add_header X-Accel $server_name;

  }
}

discuz.conf的内容如下:

server {
  listen 80;
  server_name test.discuz.com;
  location / {
        proxy_pass http://sshsrvs;
        proxy_set_header Host  $host;
        proxy_set_header X-Forwarded-For  $remote_addr;
        add_header X-Via  $server_addr;
        add_header X-Accel $server_name;
  }
}

8.我们在浏览器中访问www.test.com ,test.discuz.com
是两个不同的网站记得在物理机需要做域名解析哦

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

(0)
sraybansrayban
上一篇 2017-06-25
下一篇 2017-06-25

相关推荐

  • N26-博客作业-week5

    1、显示当前系统上root、fedora或user1用户的默认shell; ~]# grep -E “^((root|fedora|user1)\>)” /etc/passwd | cut -d: -f7 2、找出/etc/rc.d/init.d/functions文件中某单词后面跟一组小括号的行,形如:hello(); ~]#…

    Linux干货 2017-03-05
  • 从Linux小白到大牛——与狼共舞的日子7

    马哥教育网络班21期+第7周课程练习 1、创建一个10G分区,并格式为ext4文件系统; (1) 要求其block大小为2048, 预留空间百分比为2, 卷标为MYDATA, 默认挂载属性包含acl; (2) 挂载至/data/mydata目录,要求挂载时禁止程序自动运行,且不更新文件的访问时间戳; [root@localhost ~]#&nbsp…

    Linux干货 2016-11-14
  • Linux三剑客之grep伐木累(正则表达式)

    一、Linux文本处理三剑客     Linux上有三种常用的文本处理工具,分别为:grep(egrep、fgrep)、sed、awk。今天主要给大家介绍一下三剑客中的第一剑:grep伐木累。 二、grep是什么?     grep 全称(Globally search a Re…

    Linux干货 2016-03-09
  • DNS服务器之理论基础

    一、什么是DNS     DNS全称为Domain Name System,即域名系统,其作用就是将我们经常使用的“网址”解析为IP地址。     在互联网上通信需要借助于IP地址,但人类对于数字的记忆能力远不如文字,那么将IP地址转换成容易记忆的文字是个好办法,可是计算机只能识…

    Linux干货 2015-04-29
  • 马哥教育网络班20期+第七周博客作业

    1、创建一个10G分区,并格式为ext4文件系统     (1) 要求其block大小为2048,预留空间百分比为2,卷标为MYDATA,默认挂载属性包含acl fdisk /dev/sdbnp1110Gw[root@llww3317 ~]# mke2fs -t ext…

    Linux干货 2016-08-02
  • Shell脚本自动部署(编译)LAMP平台

    Shell脚本自动部署(编译)LAMP平台 Shell脚本自动部署(编译)LAMP平台 为什么要用脚本进行部署? 脚本功能介绍 笔者环境 准备工作 声明 使用测试 脚本代码 Shell脚本自动部署(编译)LAMP平台 LAMP是当下非常流行的一套Web架构,我们可以在GNU/Linux下通过其他人打包的程序包来进行安装; 但是在生产环境中,很多时候都需要我们…

    Linux干货 2016-03-26