“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

相关推荐

  • 如何恢复误删除的rpm包命令

    如何恢复误删除的rpm包命令 在linux学习过程中,我们会遇到各种各样的问题,当然,如果能够将遇到的问题一一解决也是非常开心的,因为在解决问题的过程中我们的专业技能也得到了很大的提升。最近的测试中遇到了一个平时很少接触的问题:如何恢复误删除的rpm包命令。所以觉得有必要写下来,以防止下次出现类似的问题而不会解决。 首先模拟卸载rpm rpm -e &#82…

    2017-08-26
  • M20-1 8月3号 –权限、用户和组的管理

    一、权限管理:            ls -l                  rwxrwxrwx:               &nb…

    Linux干货 2016-08-08
  • 安装centos7

    在vmware安装centos7

    2018-03-26
  • linux用户与用户组

    一.用户 用户是能够获取系统资源的权限的集合. 二.linux用户组的分类: a.管理员root  :具有使用系统所有权限的用户,其UID为0. b.Centos 6普通用户:即一般用户,其使用系统的权限受限,其UID为500-60000之间Centos7UID为1000-60000之间。 c.Centos 6系统用户:保障系统运行的用户,一般不提…

    Linux干货 2016-10-24
  • 【N25第一周作业】Linux基础

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

    Linux干货 2016-12-03
  • 用户操作详解(附图)

                    关于用户和组的基本操作命令     在linux的操作系统中,对于用户以及组的配置是及其常用的和重要的,其中包括了对于用户的添加,删除以及修改用户的…

    Linux干货 2016-08-05