nginx+keepalived(双主)

一、环境

nginx1   192.168.40.211

nginx2   192.168.40.132

vip1     192.168.40.223  主为keep1,从为keep2

vip2     192.168.40.222  主为keep2,从为keep1

(此处安装过程皆采用yum方式,不作累述)

二、keepalived配置文件

nginx1

vi /etc/keepalived/keepalived.conf

global_defs {

notification_email {

[email protected]

}

notification_email_from [email protected]

smtp_server 127.0.0.1

smtp_connect_timeout 30

route_id LVS_DEVEL

}

vrrp_script_chk_nginx {

script "/etc/keepalived/ck_ng.sh"

interval 2

weight 2

}

#VIP1

vrrp_instance VI_1 {

state MASTER

interface ens33

lvs_sync_daemon_interface ens33

virtual_router_id 151

priority 100

advert_int 5

nopreempt

authentication {

auth_type PASS

auth_pass 1111

}

virtual_ipaddress {

192.168.40.223

}

track_script {

chk_nginx

}

}

#VIP2

vrrp_instance VI_2 {

state BACKUP

interface ens33

lvs_sync_daemon_interface ens33

virtual_router_id 152

priority 90

advert_int 5

nopreempt

authentication {

auth_type PASS

auth_pass 2222

}

virtual_ipaddress {

192.168.40.222

}

track_script {

chk_nginx

}

}



nginx2

#vi /etc/keepalived/keepalived.conf

global_defs {

notification_email {

[email protected]

}

notification_email_from [email protected]

smtp_server 127.0.0.1

smtp_connect_timeout 30

route_id LVS_DEVEL

}

vrrp_script_chk_nginx {

script "/etc/keepalived/ck_ng.sh"

interval 2

weight 2

}

#VIP1

vrrp_instance VI_1 {

state BACKUP

interface ens33

lvs_sync_daemon_interface ens33

virtual_router_id 151

priority 90

advert_int 5

nopreempt

authentication {

auth_type PASS

auth_pass 1111

}

virtual_ipaddress {

192.168.40.223

}

track_script {

chk_nginx

}

}

#VIP2

vrrp_instance VI_2 {

state MASTER

interface ens33

lvs_sync_daemon_interface ens33

virtual_router_id 152

priority 100

advert_int 5

nopreempt

authentication {

auth_type PASS

auth_pass 2222

}

virtual_ipaddress {

192.168.40.222

}

track_script {

chk_nginx

}

}

三、nginx检测脚本(两台机子一样)

#vi /etc/keepalived/ck_ng.sh

#!/bin/bash

#检查nginx进程是否存在

counter=$(ps -C nginx --no-heading|wc -l)

if [ "${counter}" = "0" ]; then

#尝试启动一次nginx,停止5秒后再次检测

service nginx start

sleep 5

counter=$(ps -C nginx --no-heading|wc -l)

if [ "${counter}" = "0" ]; then

#如果启动没成功,就杀掉keepalive触发主备切换

service keepalived stop

fi

fi

#chmod +x /etc/keepalived/ck_ng.sh

四、测试

[[email protected] ~]# ip a (nginx1)

....

2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000

link/ether 00:0c:29:17:c0:db brd ff:ff:ff:ff:ff:ff

inet 192.168.40.211/24 brd 192.168.40.255 scope global ens33

valid_lft forever preferred_lft forever

inet 192.168.40.223/32 scope global ens33

valid_lft forever preferred_lft forever

inet6 fe80::20c:29ff:fe17:c0db/64 scope link

valid_lft forever preferred_lft forever

[[email protected] ~]# ip a  (nginx2)

....

2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000

link/ether 00:0c:29:5e:ba:a3 brd ff:ff:ff:ff:ff:ff

inet 192.168.40.132/24 brd 192.168.40.255 scope global ens33

valid_lft forever preferred_lft forever

inet 192.168.40.222/32 scope global ens33

valid_lft forever preferred_lft forever

inet6 fe80::20c:29ff:fe5e:baa3/64 scope link

valid_lft forever preferred_lft forever

停掉nginx1的keepalived,此时nginx1的vip转移到nginx2,如下

[[email protected] ~]# ip a

....

2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000

link/ether 00:0c:29:5e:ba:a3 brd ff:ff:ff:ff:ff:ff

inet 192.168.40.132/24 brd 192.168.40.255 scope global ens33

valid_lft forever preferred_lft forever

inet 192.168.40.222/32 scope global ens33

valid_lft forever preferred_lft forever

inet 192.168.40.223/32 scope global ens33

valid_lft forever preferred_lft forever

inet6 fe80::20c:29ff:fe5e:baa3/64 scope link

valid_lft forever preferred_lft forever

两个虚拟IP跑到一台机子上,实验成功

原文地址:https://www.cnblogs.com/zjz20/p/11563885.html

时间: 2024-10-12 00:28:40

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双主架构实现

参照   https://www.jianshu.com/p/da26df4f7d60 Keepalived+Nginx实现高可用Web负载均衡 Master 192.168.0.69 nginx.keepalived Centos7.4backup 192.168.0.70 nginx.keepalived Centos7.4vip(虚拟IP)192.168.0.180 1.两台都安装nginx,参照安装nginx文档 2. 两台都安装Keepalived yum install -y lib

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,互为主备,不考虑其他情况时均衡处理请求,当其中一台机器出现故障时,另一台负责处理对两台机器的请求,此

[转] 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协议进行访问: ---------------------------------------------

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构建主备负载均衡代理服务器

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

MySQL keepalived 双主.md

MySQL keepalived 双主搭建 环境说明 系统 IP 主机名 mysql keepalived VIP CentOS 6.8 192.168.197.61 C6-node1 5.6.36 1.2.13 192.168.197.88 CentOS 6.8 192.168.197.62 C6-node2 5.6.36 1.2.13 192.168.197.88 安装MySQL双主 C6-node1 MySQL的安装及初始化这里不做详细介绍,下面贴出node1的my.cnf的配置文件: [

keepalived双主互备和自定义日志

自定义日志: keepalived的日志默认存储在/var/log/messages 有时我们会有需要将keepalived日志拿出来单独分析,那么这就需要我做自己去定义 /etc/sysconfig/keepalived #编辑keepalived的环境配置文件 KEEPALIVED_OPTIONS="-D -d -S 0" #修改内容 # --dump-conf -d  导出配置数据 # --log-detail -D  详细日志信息 # --log-facility -S  设置