说明:本实验为双节点nginx为两台apache服务器提供负载均衡,本文不是做lvs,所以realserver不是配置在keepalived.conf而是在nginx的配置文件中upstream。
此架构需考虑的问题:
1)Master没挂,则Master占有vip且nginx运行在Master上
2)Master挂了,则backup抢占vip且在backup上运行nginx服务
3)如果master服务器上的nginx服务挂了,则vip资源转移到backup服务器上
4)检测后端服务器的健康状态
Master和Backup两边都开启nginx服务,无论Master还是Backup,当其中的一个keepalived服务停止后,vip都会漂移到keepalived服务还在的节点上,如果要想使nginx服务挂了,vip也漂移到另一个节点,则必须用脚本或者在配置文件里面用shell命令来控制。

配置步骤如下
1.初始化4台测试server,该关的关了

[root@host101 ~]# vim /etc/hosts
192.168.1.200 ng-vip
192.168.1.101 ng-master
192.168.1.102 ng-slave
192.168.1.161 web1
192.168.1.162 web2 [root@host101 ~]# yum clean all
[root@host101 ~]# systemctl stop firewalld.service
[root@host101 ~]# systemctl disable firewalld.service
[root@host101 ~]# sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config

2.配置web1,web2的apache服务,两台一样的方法

[root@host161 ~]# yum -y install httpd
[root@host161 ~]# systemctl start httpd
[root@host161 ~]# systemctl enable httpd
ln -s '/usr/lib/systemd/system/httpd.service' '/etc/systemd/system/multiuser.target.wants/httpd.service'
[root@host161 ~]# cat /var/www/html/index.html
hello this lvs-web1 [root@host162 ~]# yum -y install httpd
[root@host162 ~]# systemctl start httpd
[root@host162 ~]# systemctl enable httpd
ln -s '/usr/lib/systemd/system/httpd.service' '/etc/systemd/system/multiuser.target.wants/httpd.service'
[root@host162 ~]# cat /var/www/html/index.html
hello this lvs-web2

3.通过yum安装配置nginx节点,两台一样的方法

[root@host101 ~]# vim /etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/x86_64/
gpgcheck=0
enabled=1
[root@host101 ~]# yum clean all
[root@host101 ~]# yum -y install nginx
[root@host101 ~]# vim /usr/share/nginx/html/index.html
<h1>Welcome to ng-master!</h1>
[root@host101 ~]# cd /etc/nginx/conf.d/
[root@host101 conf.d]# mv default.conf default.conf.1
[root@host101 ~]# vim /etc/nginx/conf.d/web.conf
upstream myapp1 {
server web1;
server web2;
} server {
listen 80; location / {
proxy_pass http://myapp1;
}
}
[root@host101 ~]# systemctl restart nginx.service [root@host102 ~]# vim /etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/x86_64/
gpgcheck=0
enabled=1
[root@host102 ~]# yum clean all
[root@host102 ~]# yum -y install nginx
[root@host102 ~]# vim /usr/share/nginx/html/index.html
<h1>Welcome to ng-master!</h1>
[root@host102 ~]# cd /etc/nginx/conf.d/
[root@host102 conf.d]# mv default.conf default.conf.1
[root@host102 ~]# vim /etc/nginx/conf.d/web.conf
upstream myapp1 {
server web1;
server web2;
}
server {
listen 80; location / {
proxy_pass http://myapp1;
}
}
[root@host102 ~]# systemctl restart nginx.service

4.在主nginx服务器上安装keepalived,并配置nginx服务健康检测脚本

[root@host101 conf.d]# yum -y install keepalived
[root@host101 conf.d]# cd /etc/keepalived/
[root@host101 keepalived]# cp keepalived.conf keepalived.conf.1
[root@host101 keepalived]# vim keepalived.conf
global_defs {
notification_email {
abc@mail.com
}
notification_email_from abc@mail.com
smtp_server smtp.mail.com
smtp_connect_timeout 30
router_id HA_MASTER1 #表示运行keepalived服务器的一个标识,发邮件时显示在邮件主题中的信息
}
vrrp_script chk_http_port {
script "/usr/local/keepalived/nginx.sh" ####检测nginx状态的脚本链接
interval 2
weight 2
}
vrrp_instance VI_2 { #vrrp实例
state MASTER #MASTER/BACKUP
interface eno16777736 ####HA 监测网络接口
virtual_router_id 51 #虚拟路由标识,是一个数字,同一个VRRP实例使用唯一的标识,master和backup要一样
priority 100 #用于主从模式,优先级主高于100,从低于100
advert_int 1 #主备之间的通告间隔秒数
authentication { #认证用于主从模式,mater和backup配置一样
auth_type PASS ###主备切换时的验证
auth_pass 1111 #密码
}
track_script {
chk_http_port ### 执行监控的服务
}
virtual_ipaddress { 192.168.1.200/24 dev eno16777736 label eno16777736:1 ###########虚拟ip
}
}
[root@host101 keepalived]# mkdir -p /usr/local/keepalived
[root@host101 keepalived]# vim /usr/local/keepalived/nginx.sh
#!/bin/bash
if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
killall keepalived
fi
[root@host101 keepalived]# chmod 755 /usr/local/keepalived/nginx.sh
[root@host101 keepalived]# systemctl start keepalived
[root@host101 keepalived]# ifconfig -a
eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.1.101 netmask 255.255.255.0 broadcast 192.168.1.255
inet6 fe80::20c:29ff:fefe:6f3 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:fe:06:f3 txqueuelen 1000 (Ethernet) eno16777736:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.1.200 netmask 255.255.255.0 broadcast 0.0.0.0
ether 00:0c:29:fe:06:f3 txqueuelen 1000 (Ethernet) lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 0 (Local Loopback)

5.在备nginx服务器上安装keepalived,并配置nginx服务健康检测脚本,与主略有不同

[root@host102 conf.d]# yum -y install keepalived
[root@host102 conf.d]# cd /etc/keepalived/
[root@host102 keepalived]# cp keepalived.conf keepalived.conf.1
[root@host102 keepalived]# vim keepalived.conf
global_defs {
notification_email {
abc@mail.com
}
notification_email_from abc@mail.com
smtp_server smtp.mail.com
smtp_connect_timeout 30
router_id HA_MASTER1 #表示运行keepalived服务器的一个标识,发邮件时显示在邮件主题中的信息
}
vrrp_script chk_http_port {
script "/usr/local/keepalived/nginx.sh" ####检测nginx状态的脚本链接
interval 2
weight 2
}
vrrp_instance VI_2 { #vrrp实例
state BACKUP #MASTER/BACKUP
interface eno16777736 ####HA 监测网络接口
virtual_router_id 51 #虚拟路由标识,是一个数字,同一个VRRP实例使用唯一的标识,master和backup要一样
priority 80 #用于主从模式,优先级主高于100,从低于100
advert_int 1 #主备之间的通告间隔秒数
authentication { #认证用于主从模式,mater和backup配置一样
auth_type PASS ###主备切换时的验证
auth_pass 1111 #密码
}
track_script {
chk_http_port ### 执行监控的服务
}
virtual_ipaddress { 192.168.1.200/24 dev eno16777736 label eno16777736:1 ###########虚拟ip
}
}
[root@host102 keepalived]# mkdir -p /usr/local/keepalived
[root@host102 keepalived]# vim /usr/local/keepalived/nginx.sh
#!/bin/bash
if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
killall keepalived
fi
[root@host102 keepalived]# chmod 755 /usr/local/keepalived/nginx.sh
[root@host102 keepalived]# systemctl start keepalived
[root@host102 keepalived]# ifconfig -a
eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.1.102 netmask 255.255.255.0 broadcast 192.168.1.255
inet6 fe80::20c:29ff:fe87:fd0e prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:87:fd:0e txqueuelen 1000 (Ethernet) lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 0 (Local Loopback)

6.测试:通过浏览器访问测试http://192.168.1.200/,可发现流量在web1和web2之间跳转.

6.1测试关闭主nginx节点上的keepalived服务器,发绑定的vip在主节点消失

[root@host101 keepalived]# systemctl stop keepalived.service
[root@host101 keepalived]# ifconfig -a
eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.1.101 netmask 255.255.255.0 broadcast 192.168.1.255
inet6 fe80::20c:29ff:fefe:6f3 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:fe:06:f3 txqueuelen 1000 (Ethernet) lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 0 (Local Loopback) vip在却在备节点上出现
[root@host102 keepalived]# ifconfig -a
eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.1.102 netmask 255.255.255.0 broadcast 192.168.1.255
inet6 fe80::20c:29ff:fe87:fd0e prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:87:fd:0e txqueuelen 1000 (Ethernet) eno16777736:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.1.200 netmask 255.255.255.0 broadcast 0.0.0.0
ether 00:0c:29:87:fd:0e txqueuelen 1000 (Ethernet) lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 0 (Local Loopback) 通过浏览器访问测试http://192.168.1.200/,可发现流量依然在web1和web2之间跳转。

6.2再次启动主节点的keepalived服务,发现vip又重新漂移会主节点

[root@host101 keepalived]# systemctl start keepalived.service
[root@host101 keepalived]# ifconfig -a
eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.1.101 netmask 255.255.255.0 broadcast 192.168.1.255
inet6 fe80::20c:29ff:fefe:6f3 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:fe:06:f3 txqueuelen 1000 (Ethernet) eno16777736:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.1.200 netmask 255.255.255.0 broadcast 0.0.0.0
ether 00:0c:29:fe:06:f3 txqueuelen 1000 (Ethernet)
通过浏览器访问测试http://192.168.1.200/,可发现流量依然在web1和web2之间跳转。

6.3关闭nginx主节点上的nginx服务,发现vip从主节点消失,keepalived服务关闭,vip在备节点上出现。

[root@host101 keepalived]# systemctl stop nginx.service
[root@host101 keepalived]# ifconfig -a
eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.1.101 netmask 255.255.255.0 broadcast 192.168.1.255
inet6 fe80::20c:29ff:fefe:6f3 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:fe:06:f3 txqueuelen 1000 (Ethernet) lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 0 (Local Loopback) [root@host101 keepalived]# systemctl status keepalived
keepalived.service - LVS and VRRP High Availability Monitor
Loaded: loaded (/usr/lib/systemd/system/keepalived.service; disabled)
Active: inactive (dead) 通过浏览器访问测试http://192.168.1.200/,可发现流量依然在web1和web2之间跳转。

6.4再次启动主节点的nginx和keepalived服务后,VIP又漂回主节点。

[root@host101 keepalived]# systemctl start nginx.service
[root@host101 keepalived]# systemctl start keepalived
通过浏览器访问测试http://192.168.1.200/,可发现流量依然在web1和web2之间跳转。

参考:

http://www.linuxdiyf.com/linux/12955.html
http://nginx.org/en/linux_packages.html
http://blog.csdn.net/e421083458/article/details/30086413
http://my.oschina.net/u/1458120/blog/208740

双节点(nginx+keepalived)为两台apache服务器提供负载均衡的更多相关文章

  1. 单节点nginx为两台apache服务器提供负载均衡

    需求:本实验为单节点nginx为两台apache服务器提供负载均衡,所有配置为最简单 1.初始化3台测试server,该关的关了 [root@host101 ~]# vim /etc/hosts 19 ...

  2. EG:nginx反向代理两台web服务器,实现负载均衡 所有的web服务共享一台nfs的存储

    step1: 三台web服务器环境配置:iptables -F; setenforce 0 关闭防火墙:关闭setlinux step2:三台web服务器 装软件 step3: 主机修改配置文件:vi ...

  3. Nginx+Keepalived(双机热备)搭建高可用负载均衡环境(HA)

    原文:https://my.oschina.net/xshuai/blog/917097 摘要: Nginx+Keepalived搭建高可用负载均衡环境(HA) http://blog.csdn.ne ...

  4. Nginx+Keepalived(双机热备)搭建高可用负载均衡环境(HA)-转帖篇

    原文:https://my.oschina.net/xshuai/blog/917097 摘要: Nginx+Keepalived搭建高可用负载均衡环境(HA) http://blog.csdn.ne ...

  5. 客户有两台windows服务器要做sql server双机切换

    基本架构 2 windows 2008 server:安装成域控制器,实现故障转移(虚拟ip访问,共享磁盘阵列卷链接主服务器),安装sqlserver2012 1磁盘阵列共享卷:数据库文件放于其中,两 ...

  6. sqlserver2014两台不同服务器上数据库同步

    sqlserver2014两台不同服务器上数据库同步   同步了快一个月了,哈哈,因为途中比较麻烦,第一次,遇到烦的地方就停下了,今天终于同步成功了,哈哈,下面我就来介绍一下我实现两台数据库同步的过程 ...

  7. socket实现两台FTP服务器指定目录下的文件转移(不依赖第三方jar包)

    通过socket实现两台FTP服务器指定目录下的文件转移,其中包含了基础了ftp文件列表显示.上传和下载.这里仅供学习用,需掌握的点有socket.ftp命令.文件流读取转换等 完整代码如下: Ftp ...

  8. 两台linux服务器之间实现挂载

    https://blog.csdn.net/lpp_dd/article/details/78743862 两台linux服务器之间实现挂载: 服务端: 1.首先需要在主机上设置允许挂载的目录 (1) ...

  9. 两台linux服务器之间免密scp,在A机器上向B远程拷贝文件

    两台linux服务器之间免密scp,在A机器上向B远程拷贝文件 操作步骤:1.在A机器上,执行ssh-keygen -t rsa,一路按Enter,不需要输入任何内容.(如有提示是否覆盖,可输入y后按 ...

随机推荐

  1. C#日常总结1

    Rows:行的集合: Columns:列的集合: Gridview:用来显示数据的表格{ //设置 AutoGenerateColumns="false":表示不允许自动产生列,列 ...

  2. python--分布式爬虫

    //server import socket, select, re, queue, redis from multiprocessing import Pool, cpu_count from py ...

  3. 整理一下以前的Html+css3复习笔记

    一.html5新特性  常用语义标签:nav footer header section mark  功能标签 video audio iframe canvas(画布和绘图功能)  input新ty ...

  4. DES MAC PIN HEX

    /* void DesEncrypt( UCHAR * auchInput,UCHAR * auchKey,UCHAR * auchOutput=NULL); Function: DesEncrypt ...

  5. oracle中schema指的是什么?

    看来有的人还是对schema的真正含义不太理解,现在我再次整理了一下,希望对大家有所帮助. 我们先来看一下他们的定义:A schema is a collection of database obje ...

  6. 常见JAVA框架

     Spring Framework [Java开源JEE框架] Spring是一个解决了许多在J2EE开发中常见的问题的强大框架. Spring提供了管理业务对象的一致方法并且鼓励了注入对接口编程而不 ...

  7. iis 301重定向

    把www.a.com重定向到www.b.com 只需在www.a.com上面右键属性---主目录,重定向到url,下面填上www.b.com,再把资源永久重定向勾选上即可. 注意,如果你需要把域名后面 ...

  8. AC自动机最好讲解

    http://www.cs.uku.fi/~kilpelai/BSA05/lectures/slides04.pdf

  9. enmo_day_06

    RAC Data Guard (DG) EMC NAS SAN 双活 数据完整性 约束 : 主键 : 非空 且 唯一 非空 : 唯一 : 外键 : 检查 : DISABLE, ENABLE VALID ...

  10. 对HTML+CSS+JavaScript的个人理解

    HTML就像人的骨头架子,是人的根基,要有个人样呀,一个网站,一个WebApp要是缺根儿骨头,那就像人少个胳膊少个腿儿的,行动不方便啊:CSS就像人穿得衣服.鞋子,男人的纹身,女人擦得粉儿,好看呀,一 ...