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

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_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_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/10012230.html

时间: 2024-10-25 16:42:51

Nginx+Keepalived双主架构实现的相关文章

nginx+keepalived双主高可用负载均衡

实验环境及软件版本:CentOS版本: 6.6(2.6.32.-504.el6.x86_64)nginx版本: nginx-1.6.3keepalived版本:keepalived-1.2.7 主LB1:LB-110-05 主LB2:LB-111-06 一.安装准备及依赖(用SecureCRT的交互窗口同时对两台LB操作,只贴出LB1的操作过程在此) [[email protected] ~]# mkdir tools [[email protected] ~]# mkdir /applicat

Nginx + keepalived 双主解决方案

工作原理:两台Nginx通过Keepalived生成二个实例,二台Nginx的VIP互为备份,任何一台Nginx机器如果发生硬件损坏,Keepalived会自动将它的VIP地址切换到另一台机器,不影响客户端的访问. IP 信息列表: 名称     IP -----------------------------------VIP1      192.168.200.254 VIP2      192.168.200.253Nginx1     192.168.200.202 Nginx2    

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

Keepalived+Nginx实现双主高可用负载均衡 一.部署Nginx+keepalived高可用有两种配置方案: 1.Nginx+keepalived 主从配置 前端使用两台机器,使用一个VIP,即其中一台为主负责全部请求,一台为备,只用在主出现故障时,才会替代主处理请求,平常处于闲置状态,此方案不够理想.  2.Nginx+keepalived 双主配置 前端使用2台机器,使用两个VIP,互为主备,不考虑其他情况时均衡处理请求,当其中一台机器出现故障时,另一台负责处理对两台机器的请求,此

nginx+keepalived构建主备负载均衡代理服务器

一.架构 二.唠叨一会原理: 1.nginx Nginx进程基于于Master+Slave(worker)多进程模型,自身具有非常稳定的子进程管理功能.在Master进程分配模式下,Master进程永远不进行业务处理,只是进行任务分发, 从而达到Master进程的存活高可靠性,Slave(worker)进程所有的业务信号都 由主进程发出,Slave(worker)进程所有的超时任务都会被Master中止,属于非阻塞式任务模型. 2.keepalived Keepalived是Linux下面实现V

[转] Haproxy、Keepalived双主高可用负载均衡

http://blog.chinaunix.net/uid-25266990-id-3989321.html 在测试了Nginx+Keepalived的负载均衡后,也对Haproxy+Keepalived双主模式做了测试,双主的模式充分利用了服务器资源,这样不会导致服务器浪费. 这里举例说明: 默认情况下,第一台负载均衡器主要分发www.zhirs.com的请求,第二台负载均衡器主要分发img.zhirs.com的请求. 任意一台宕机都不会影响网站分发. 实验环境 VIP1=192.168.13

keepalived双主模式实现nginx高可用及LNAMMP架构

keepalived双主模式实现nginx高可用及LNAMMP架构 一.利用keepalived实现nginx调度器高可用: 二.构建LNAMMP架构: 1) Nginx既是前端调度器,又是缓存服务器: 2) 将php的session缓存于memcached中: 3) 在Apache和php上部署Discuz论坛程序: 4) 使用https连接,即使用户使用的是http协议也可以以https协议进行访问: ---------------------------------------------

keepalived实现mysql双主架构

5 keepalived实现mysql双主架构 5.1 架构介绍 企业级Mysql集群具备高可用.可扩展.以管理.低成本的特点.下面将介绍企业环境中经常应用的一个解决方案,即Mysql的双主互备架构,主要设计思想是通过Mysql复制技术将两台Mysql服务器互相将对方作为自己的主服务器,自己又同时作为对方的从服务器来进行复制.这样就实现了高可用架构中的数据同步功能,同时,将采用keepalived来实现mysql的自动故障切换.在这个架构中,虽然两台Mysql服务器互为主从,但同一时刻只有一个M

haproxy+keepalived双主模型及动静分离的实现

实验目标: 1.haproxy统计页面的输出机制: 2.haproxy动静分离机制: 3.基于keepalived的高可用实现: 环境: vm8虚拟机 操作系统: centos 6.4 内核版本: 2.6.32-358.el6.x86_64 注: (1) 每个haproxy各有两块网卡,外网网卡选择Bridge,内网网卡选择Vmnet2; (2) 内部两台web服务器的网卡都是选择Vmnet2; 一.准备工作: 1.各节点IP地址相关设置 node1:  ifconfig eth1 192.16

Nginx + Keepalived(主备模式)实现负载均衡高可用浅析

概述 目前关于负载均衡和高可用的架构方案能找到相当多且详尽的资料,此篇是自己学习相关内容的一个总结,防止将来遗忘再次重新查找资料,也避免踩相同的坑. 此次配置的负载均衡与高可用架构:Nginx + Keepalived(主备模式),Nginx 使用反向代理实现七层负载均衡. 众所周知,Nginx 是一款自由的.开源的.高性能HTTP服务器和反向代理服务器,也是一个IMAP.POP3.SMTP代理服务器. 也就是说Nginx本身就可以托管网站(类似于Tomcat一样),进行HTTP服务处理,也可以