说明:本实验为双节点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. python窗体——pyqt初体验

    连续两周留作业要写ftp的作业,从第一周就想实现一个窗体版本的,但是时间实在太短,qt零基础选手表示压力很大,幸好又延长了一周时间,所以也就有了今天这篇文章...只是为了介绍一些速成的方法,还有初学者 ...

  2. Queue的push和front操作

    #include <queue> #include <cstdlib> using namespace std; int main(){ queue<int> qu ...

  3. mysql 处理查询请求过程

    需要搞清楚查询为什么会慢,就要搞清楚mysql处理查询请求的过程: 1.客户端发送SQL请求给服务器 2.服务器检查是否可以在查询缓存中命中该SQL   查询缓存对SQL性能的影响. 1.需要对缓存加 ...

  4. 从0到1---“保多多”APP的开发(一)

    2015年8月份,我正式接手了公司保多多APP的开发(和另一个同事一起). 我之前并没有过从0开始创建一款APP,这次能有这样的机会,实在让我感到兴奋.因为我相信,作为这款APP的主要开发人员,在这一 ...

  5. mysql之存储过程

    一.存储过程     迄今为止,使用的大多数 SQL语句都是针对一个或多个表的单条语句.并非所有操作都这么简单,经常会有一个完整的操作需要多条语句才能完成.例如,考虑以下的情形.         1. ...

  6. python3 使用pyperclip读写剪贴板(windows)

    2016年5月14日 03:41:38 codegay 使用pyperclip库读写剪贴板非常简单~, 1.使用命令安装: pip install pyperclip 2.然后...就可以了: 以下是 ...

  7. iOS6的旋屏控制技巧

    在iOS5.1 和 之前的版本中, 我们通常利用 shouldAutorotateToInterfaceOrientation: 来单独控制某个UIViewController的旋屏方向支持,比如: ...

  8. MySQL主机127.0.0.1与localhost区别总结

    1. mysql -h 127.0.0.1 的时候,使用TCP/IP连接, mysql server 认为该连接来自于127.0.0.1或者是"localhost.localdomain&q ...

  9. LeetCode189——Rotate Array

    Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array  ...

  10. segues的类型

    Name Interface Builder symbol Description Show Present the content in the detail or master area depe ...