Nginx+Keepalived高可用负载均衡

转自  https://www.jianshu.com/p/da26df4f7d60

Keepalived+Nginx实现高可用Web负载均衡

Master  192.168.0.69    nginx、keepalived    Centos7.4
backup    192.168.0.70    nginx、keepalived    Centos7.4
vip(虚拟IP)
192.168.0.180   192.168.0.181

1、两台都安装nginx,参照安装nginx文档

2、  两台都安装Keepalived

yum install -y libnl libnl-devel libnfnetlink-devel popt-devel

cd /usr/local/src/

tar -zxvf keepalived-1.2.18.tar.gz

cd keepalived-1.2.18

./configure --prefix=/usr/local/keepalived

make && make install

3、将keepalived安装成Linux系统服务

mkdir /etc/keepalived
cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
ln -s /usr/local/sbin/keepalived /usr/sbin/
ln -s /usr/local/keepalived/sbin/keepalived /sbin/
ln -s /usr/local/keepalived/sbin/keepalived /usr/local/sbin/
chkconfig keepalived on

4、修改keepalived配置文件

接下来就是配置了,很简单,之前的删除,直接复制下面配置文件

先是主服务器:

vi /etc/keepalived/keepalived.conf

global_defs
{
notification_email    #通知email,根据实际情况配置
{
[email protected]
}
notification_email_from [email protected]
smtp_server 127.0.0.1
stmp_connect_timeout 30
router_id node1         #节点名标识,主要用于通知中
}

vrrp_script chk_http_port {
   script "/usr/local/keepalived/nginx.sh"  #在这里添加脚本链接
   interval 3       #脚本执行间隔
   weight 2         #脚本结果导致的优先级变更
}

vrrp_script chk_http_port {
   script "/etc/keepalived/chk_haproxy.sh"  #在这里添加脚本链接
   interval 3       #脚本执行间隔
   weight 2         #脚本结果导致的优先级变更
}

vrrp_instance VI_NODE_1 {
  state MASTER          #配置为主服务器
  interface ens33        #通讯网卡
  virtual_router_id 100 #路由标识
  priority 200          #优先级,0-254
  advert_int 5          #通知间隔,实际部署时可以设置小一点,减少延时

  authentication {
    auth_type PASS
    auth_pass 123456    #验证密码,用于通讯主机间验证
  }

  track_script {
        chk_http_port     #添加脚本执行
    }

  virtual_ipaddress {
    192.168.0.180       #虚拟ip,可以定义多个
  }
}

vrrp_instance VI_NODE_2 {
  state MASTER          #配置为主服务器
  interface ens33        #通讯网卡
  virtual_router_id 101 #路由标识
  priority 100          #优先级,0-254
  advert_int 5          #通知间隔,实际部署时可以设置小一点,减少延时

  authentication {
    auth_type PASS
    auth_pass 123456    #验证密码,用于通讯主机间验证
  }

  track_script {
        chk_http_port     #添加脚本执行
    }

  virtual_ipaddress {
    192.168.0.181       #虚拟ip,可以定义多个
  }
}

接下是从服务器设置:

vi /etc/keepalived/keepalived.conf

global_defs {
  notification_email {
    [email protected]
  }
  notification_email_from [email protected]
  smtp_server 127.0.0.1
  stmp_connect_timeout 30

  router_id node2
}

vrrp_script chk_http_port {
   script "/usr/local/keepalived/nginx.sh"  #在这里添加脚本链接
   interval 3       #脚本执行间隔
   weight 2         #脚本结果导致的优先级变更
}

vrrp_script chk_http_port {
   script "/etc/keepalived/chk_haproxy.sh"  #在这里添加脚本链接
   interval 3       #脚本执行间隔
   weight 2         #脚本结果导致的优先级变更
}

vrrp_instance VI_NODE_1 {
  state BACKUP           #与主服务器对应
  interface ens33         #从服务器的通信网卡
  virtual_router_id 100  #路由标识,和主服务器相同
  priority 100           #优先级,小于主服务器即可
  advert_int 5           #这里是接受通知间隔,与主服务器要设置相同

  authentication {
   auth_type PASS
    auth_pass 123456     #验证密码,与主服务器相同
  }

  track_script {
        chk_http_port     #添加脚本执行
    }

  virtual_ipaddress {
    192.168.0.180        #虚拟IP,也要和主服务器相同
  }
}

vrrp_instance VI_NODE_2 {
  state BACKUP           #与主服务器对应
  interface ens33         #从服务器的通信网卡
  virtual_router_id 101  #路由标识,和主服务器相同
  priority 100           #优先级,小于主服务器即可
  advert_int 5           #这里是接受通知间隔,与主服务器要设置相同

  authentication {
   auth_type PASS
    auth_pass 123456     #验证密码,与主服务器相同
  }

  track_script {
        chk_http_port     #添加脚本执行
    }

  virtual_ipaddress {
    192.168.0.181        #虚拟IP,也要和主服务器相同
  }
}

5 两个节点配置check_ngixn脚本

当脚本检测到nginx没有运行的时候会尝试去启动nginx以此,如果失败则停掉keepalived进程

vi /usr/local/keepalived/nginx.sh

#!/bin/bash
A=`ps -C nginx –no-header |wc -l`
if [ $A -eq 0 ];then
  /usr/local/nginx/sbin/nginx #nginx命令的路径
  sleep 3
  if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
    killall keepalived
  fi
fi

6、启动keepalived
[[email protected] ~]# service keepalived start
Starting keepalived (via systemctl):                       [  OK  ]

7、查看虚拟ip在哪台上, 注意过两分钟才出现vip

[[email protected] ~]# ip a
[[email protected] ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:24:21:c4 brd ff:ff:ff:ff:ff:ff
    inet 192.168.0.69/24 brd 192.168.0.255 scope global noprefixroute ens33
       valid_lft forever preferred_lft forever
    inet 192.168.0.180/32 scope global ens33
       valid_lft forever preferred_lft forever
    inet 192.168.0.181/32 scope global ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::d675:3ae7:5113:ad71/64 scope link noprefixroute
       valid_lft forever preferred_lft forever

8、我们修改两个nginx的首页信息,在首页中加入各自的IP地址

vi /usr/local/nginx/html/index.html

<h1>Welcome to nginx! 192.168.0.69</h1>

9、访问:http://192.168.0.180/      http://192.168.0.181/

Welcome to nginx! 192.168.0.69

If you see this page, the nginx web server is successfully installed and working. Further configuration is required.

For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.

Thank you for using nginx.

10、高可用切换

在192.168.0.69停止keepalived , 查看192.168.0.180飘到了192.168.0.70上

service keepalived stop

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:ba:67:74 brd ff:ff:ff:ff:ff:ff
    inet 192.168.0.70/24 brd 192.168.0.255 scope global noprefixroute ens33
       valid_lft forever preferred_lft forever
    inet 192.168.0.180/32 scope global ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::6c33:a5d6:2ea9:d781/64 scope link noprefixroute
       valid_lft forever preferred_lft forever
    inet6 fe80::d675:3ae7:5113:ad71/64 scope link tentative noprefixroute dadfailed
       valid_lft forever preferred_lft forever

报错解决:

1、
[[email protected] keepalived-1.3.5]# ./configure --prefix=/usr/local/keepalived


*** WARNING - this build will not support IPVS with IPv6. Please install libnl/libnl-3 dev libraries to support IPv6 with IPVS


yum -y install libnl libnl-devel


2、


configure: error: libnfnetlink headers missing


yum install -y libnfnetlink-devel


3、


安装keepalived出错./configure: error: Popt libraries is required


yum? -y? install? popt-devel


4、


[[email protected] keepalived-1.2.8]# /etc/rc.d/init.d/keepalived start
Starting keepalived (via systemctl): Job for keepalived.service failed because the control process exited with error code. See "systemctl status keepalived.service" and "journalctl -xe" for details.
[FAILED]


[[email protected] keepalived-1.2.8]# ln -s /usr/local/keepalived/sbin/keepalived /usr/bin/
[[email protected] keepalived-1.2.8]# /etc/init.d/keepalived start
Starting keepalived (via systemctl): [ OK ]



原文地址:https://www.cnblogs.com/effortsing/p/10012222.html

时间: 2024-10-12 23:20:05

Nginx+Keepalived高可用负载均衡的相关文章

配置nginx+keepalived高可用负载均衡的时候。主从服务器都出现了VIP 阿里云问题

配置nginx+keepalived高可用负载均衡的时候.主从服务器都出现了VIP 出现这问题的场景是在阿里VPS云服务器网络环境中,因为路由交换层禁用了ARP的广播限制,造成KEEPALIVE主备协议无法通过广播的方式进行通信,造成主备两台服务器都强占HAVIP地址,出现同时两台服务器都有VIP地址的情况出现,必须通过配置来指定IP的两台服务器间进行通讯(阿里说明文档中解释只能支持两台使用同一个HAVIP地址),基于以下方法可以的情况下,多备方式用同样的方式也应该可行 ,有需要的兄弟可以测试下

nginx + keepalived 高可用负载均衡

虚拟IP:    192.168.2.52 nginx-1:   192.168.2.50 nginx-2:   192.168.2.51 关闭/etc/sysconfig/selinux SELINUX=disabled nginx-1 yum install keepalived vim /etc/keepalived/keepalived.conf--------------------------------------------------- ! Configuration File

虚拟机 搭建LVS + DR + keepalived 高可用负载均衡

一:环境说明:   LVS-DR-Master:    10.3.0.82   LVS-DR-Backup:    10.3.0.70   VIP:                10.3.0.60   RealServer1:    10.3.0.69   RealServer1:    10.3.0.83   Centos:5.7    二:安装LVS和Keepalvied软件包   1. 下载相关软件包    mkdir /usr/local/src/lvs    cd /usr/loca

(2)LVS+Keepalived高可用负载均衡架构原理及配置

1.keepalived 介绍2.keepalived 优缺点3.keepalived 应用场景4.keepalived 安装配置5.keepalived+lvs 高可用6.keepalived+nginx 高可用7.keepalived 切换原理8.性能优化9.常见故障 一.keepalived 介绍 1.keepalived 定义keepalived是一个基于VRRP(virtual route redundent protocol)协议来实现的LVS服务高可用方案,可以利用其来避免单点故障

案例一(haproxy+keepalived高可用负载均衡系统)【转】

1.搭建环境描述: 操作系统: [[email protected] ~]# cat /etc/redhat-release CentOS release 6.7 (Final) 地址规划: 主机名 IP地址 集群角色 虚拟IP haproxy-server 10.0.0.35 主HAProxyServer 10.0.0.40 haproxy-backup 10.0.0.36 备用HAProxyServer webapp1 10.0.0.150 Backend Server 无 webapp2

Keepalived+Nginx实现高可用负载均衡集群

一 环境介绍 1.操作系统CentOS Linux release 7.2.1511 (Core) 2.服务keepalived+lvs双主高可用负载均衡集群及LAMP应用keepalived-1.2.13-7.el7.x86_64nginx-1.10.2-1.el7.x86_64httpd-2.4.6-45.el7.centos.x86_64 二 原理及拓扑图 1.vrrp协议在现实的网络环境中,两台需要通信的主机大多数情况下并没有直接的物理连接.对于这样的情况,它们之间路由怎样选择?主机如何

基于HAProxy+Keepalived高可用负载均衡web服务的搭建

一 原理简介 1.HAProxyHAProxy提供高可用性.负载均衡以及基于TCP和HTTP应用的代理,支持虚拟主机,它是免费.快速并且可靠的一种解决方案.HAProxy特别适用于那些负载特大的web站点,这些站点通常又需要会话保持或七层处理.HAProxy运行在时下的硬件上,完全可以支持数以万计的并发连接.并且它的运行模式使得它可以很简单安全的整合进当前的架构中, 同时可以保护web服务器不被暴露到网络上.2.KeepalivedKeepalived 是一个基于VRRP协议来实现的LVS服务高

LVS+Keepalived高可用负载均衡集群架构

实验环境: LVS和keepalived介绍: 1.keepalived的设计目标是构建高可用的LVS负载均衡群集,可以调用ipvsadm工具来创建虚拟服务器,管理服务器池,而不仅仅用来做双机热备. 使用keepalived构建LVS群集更加简便易用,主要优势体现在: 对LVS负载调度器实现热备切换,提高可用性: 对服务器池中的节点进行健康检查,自动移除失效节点,恢复后再重新加入. 2.在基于LVS+Keepalived实现的LVS群集结构中,至少包括两台热备的负载调度器,两台以上的节点服务器,

HAProxy+Keepalived 高可用负载均衡

转自 https://www.jianshu.com/p/95cc6e875456 Keepalived+haproxy实现高可用负载均衡 Master 192.168.0.69 haproxy.keepalived Centos7.4 backup 192.168.0.70 haproxy.keepalived Centos7.4 vip(虚拟IP) 192.168.0.180 192.168.0.181 1.两台都安装haproxy,参照安装haproxy文档 2. 两台都安装Keepali