“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 21:39
下一篇 2017-06-25 23:25

相关推荐

  • 采用二进制包安装mysql

    本文是在CentOS7系统平台下安装Mysql5.6.26版本数据库的操作说明,如有错误,请指正。 系统平台:CentOS-7-x86_64    数据库版本:mysql-5.6.26   Mysql镜像文件下载地址:http://dev.mysql.com/downloads/mirrors.html 本次演示的Mysql安装…

    Linux干货 2017-05-04
  • 计算机的组成及其功能

    1.计算机的组成及其功能 1.1计算机的硬件基本结构 从ENIAC到当前最先进的计算机都采用的是冯·诺依曼体系结构分别是:控制器,运算器,存储器,输入与输出设备 控制器:将指令逐条从存储器中取出,经译码分析后向全机发出相应         的操作控制信号作用于其他部件,使各部件有…

    Linux干货 2016-10-28
  • grep,find用法-2

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

    Linux干货 2017-08-04
  • 学习宣言

      人生还有很多精彩时刻,需要你去探寻,linux 就是这个深渊的入口,我其实已迫不及待,伙伴们别怕,跟我来,一路披荆斩棘,踏寻生命的足迹!Fighting!

    Linux干货 2016-10-25
  • grep&正则表达式

    grep&正则表达式 grep(global search regular expression(RE) and print out the line,全面搜索正则表达式并把行打印出来)是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来。 语法 grep [选项]… PATTERN [FILE]… 选项 -a 不要忽略…

    Linux干货 2018-03-23
  • N26-第五周

    1、显示当前系统上root、fedora或user1用户的默认shell;[root@localhost ~]# grep -E ‘^(root|fedora|user1)\>’ /etc/passwdroot:x:0:0:root:/root:/bin/bashfedora:x:4002:4002:Fedora Core:/h…

    Linux干货 2017-03-13