; “lnmap实战之负载均衡架构(无高可用)”之新增keepalived高可用 | Linux运维部落

“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,如若转载,请注明出处:/78538

发表评论

电子邮件地址不会被公开。 必填项已用*标注

联系我们

400-080-6560

在线咨询:点击这里给我发消息

邮件:1660809109@qq.com

工作时间:周一至周五,9:30-18:30,节假日同时也值班

友情链接:万达娱乐开户  万达娱乐平台  guoqibee.com  万达娱乐直属QQ  华宇招商  万达直属QQ  万达招商  万达娱乐招商  万达登录  万达娱乐注册