简介
这种方案,使用一个虚拟VIP地址,前端使用2台机器,一台做主,一台做备,但同时只有一台机器工作,另一台备机在主机器不出现故障的时候,永远处于浪费状态,对于服务器不多的网站,该方案并不经济实惠,但能快速切换。
关于Nginx版本
Mainline version:开发版
Stable version:稳定版
Legacy versions:遗留的老版本
官方地址:http://nginx.org/,找到“news”中,最新的一个stable version
下载地址:http://nginx.org/download/,找到这个包的下载链接,右键复制链接地址
规划:
LB-01:16.155.199.223 nginx+keepalived-master
LB-02:16.155.197.42 nginx+keepalived-backup
VIP:16.155.197.100
OS:CentOS 7.5
(tips:master,backup机器一定要在同一个网段内,vip也要设置在同一个网段内,不知道怎么在同一个网段内的小伙伴请自行百度一下)
Nginx+Keepalived主从架构
Tips:
在下面的部署过程中,为了节省篇幅,只显示了LB-01:16.155.199.223上的部署过程。新手请注意,按照部署过程,凡是在LB-01:16.155.199.223上做的所有配置(准备工作、部署Nginx、部署Keepalived),都需要在LB-02:16.155.197.42上,再部署一次,并保持两边的配置过程一样!
1.准备工作
查看centos版本命令:
cat /etc/centos-release
1.1 关闭SELinux
[root@example01 ~]# vim /etc/sysconfig/selinux
SELINUX=disabled
1.2 关闭IPTABLES防火墙
[root@example01 ~]# systemctl unmask firewalld
[root@example01 ~]# systemctl start firewalld
[root@example01 ~]# firewall-cmd --zone=public --add-port=80/tcp --permanent 开放80端口
[root@example01 ~]# firewall-cmd --direct --permanent --add-rule ipv4 filter INPUT 0 --in-interface em1 --destination 224.0.0.18 --protocol vrrp -j ACCEPT
'[root@example01 ~]# firewall-cmd --reload
1.3 安装wget
[root@example01 ~]# yum -y install wget
准备工作到此为止,reboot命令,重启两台服务器,使得SELinux配置生效
2.部署nginx
2.1 安装编译工具及库文件
[root@example01 ~]# yum -y install gcc gcc-c++ make automake autoconf libtool pcre pcre-devel zlib zlib-devel openssl openssl-devel
2.2 安装nginx
[root@example01 ~]# cd /usr/local/src/
[root@example01 src]# wget http://nginx.org/download/nginx-1.6.2.tar.gz
[root@example01 src]# tar -zxvf nginx-1.6.2.tar.gz
[root@example01 src]# cd nginx-1.6.2
[root@example01 nginx-1.6.2]# ./configure --prefix=/usr/local/nginx \--with-http_stub_status_module \ --with-http_ssl_module
[root@example01 nginx-1.6.2]# make && make install
配置报错:
./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre= option.
解决办法:
[root@example01 nginx-1.6.2]# ./configure --prefix=/usr/local/nginx \> --without-http_rewrite_module
2.3 配置nginx
2.3.1 创建nginx运行使用的用户www
[root@example01 nginx-1.6.2]# /usr/sbin/groupadd www
[root@example01 nginx-1.6.2]# /usr/sbin/useradd -g www www
2.3.2 启动nginx
启动服务
[root@example01 nginx-1.6.2]# /usr/local/nginx/sbin/nginx
重载nginx配置
[root@example01 nginx-1.6.2]# /usr/local/nginx/sbin/nginx -s reload
开机启动
[root@example01 src]# vim /etc/rc.local # Nginx/usr/local/nginx/sbin/nginx
2.3.3 编辑index.html文件
编辑LB-01:16.155.199.223
[root@example01 nginx-1.6.2]# vim /usr/local/nginx/html/index.html 14
Welcome to nginx!Server01
编辑LB-02:16.155.197.42
[root@example02 nginx-1.6.2]# vim /usr/local/nginx/html/index.html 14
Welcome to nginx!Server02
2.3.4 配置nginx.conf
2 user www www;
3 worker_processes 1;
35 upstream my Server {
36 ip_hash;
37 server 16.155.199.223;
38 server 16.155.197.42;
39 }
Tips:
负载均衡模块用于从”upstream”指令定义的后端主机列表中选取一台主机。nginx先使用负载均衡模块找到一台主机,再使用upstream模块实现与这台主机的交互。
从配置我们可以看出负载均衡模块的使用场景:
1.核心指令”ip_hash”只能在upstream {}中使用。这条指令用于通知nginx使用ip hash负载均衡算法。如果没加这条指令,nginx会使用默认的round robin负载均衡模块。
2.upstream {}中的指令可能出现在”server”指令前,可能出现在”server”指令后,也可能出现在两条”server”指令之间。
2.3.5 浏览器访问:
http://16.155.199.223/
LB-01:16.155.199.223
http://16.155.197.42
LB-02:16.155.197.42
nginx其它命令:
/usr/local/nginx/sbin/nginx -s reload# 重新载入配置文件
/usr/local/nginx/sbin/nginx -s reopen# 重启 Nginx
/usr/local/nginx/sbin/nginx -s stop# 停止 Nginx
3.部署keepalived
3.1 安装keepalived
[root@example01 src]# yum -y install keepalived
查看keepalived版本
[root@example01 src]# keepalived -vKeepalived v1.2.13(03/19,2015)
3.2 修改keepalived的配置文件
LB-01:16.155.199.223/的配置
[root@example01 src]# vim /etc/keepalived/keepalived.conf
vrrp_script chk_nginx {
script "/etc/keepalived/nginx_check.sh" # 检测nginx状态的脚本路径
interval 2 # 检测时间间隔2s
weight -20 # 如果脚本的条件成立,权重-20
}
vrrp_instance VI_1 {
state MASTER # 服务状态;MASTER(工作状态)BACKUP(备用状态)
interface ens192 # VIP绑定网卡,大家默认的一般是eth0,我这里个性化设置是ens192
virtual_router_id 51 # 虚拟路由ID,主、备节点必须一致
mcast_src_ip 16.155.199.223 # 本机IP
nopreempt # 优先级高的设置,解决异常回复后再次抢占的问题
priority 100 # 优先级;取值范围:0~254;MASTER > BACKUP
advert_int 1 # 组播信息发送间隔,主、备节点必须一致,默认1s
authentication { # 验证信息;主、备节点必须一致
auth_type PASS # VRRP验证类型,PASS、AH两种
auth_pass 1111 # VRRP验证密码,在同一个vrrp_instance下,主、从必须使用相同的密码才能正常通信
}
track_script { # 将track_script块加入instance配置块
chk_nginx # 执行Nginx监控的服务
}
virtual_ipaddress { # 虚拟IP池,主、备节点必须一致,可以定义多个VIP
16.155.197.100 # 虚拟IP
}
}
LB-02:16.155.197.42的配置
[root@example02 src]# vim /etc/keepalived/keepalived.conf
vrrp_script chk_nginx {
script "/etc/keepalived/nginx_check.sh"
interval 2
weight -20
}
vrrp_instance VI_1 {
state BACKUP
interface ens192
virtual_router_id 51
mcast_src_ip 16.155.197.42
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_nginx
}
virtual_ipaddress {
16.155.197.100
}
}
3.3 编写nginx状态监测脚本
[root@example01 keepalived]# vim /etc/keepalived/nginx_check.sh
#!/bin/bash
A=`ps -C nginx --no-header |wc -l`
if [ $A -eq 0 ];then
/usr/local/nginx/sbin/nginx
sleep 2
if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
killall keepalived
fi
fi
脚本要求:如果 nginx 停止运行,尝试启动,如果无法启动则杀死本机的 keepalived 进程, keepalied将虚拟 ip 绑定到 BACKUP 机器上。
3.4 保存脚本,赋予执行权限
[root@example01 keepalived]# chmod +x /etc/keepalived/nginx_check.sh
[root@example01 keepalived]# ll
total 8
-rw-r--r--. 1 root root 3602 Mar 27 23:46 keepalived.conf
-rwxr-xr-x. 1 root root 191 Mar 27 23:53 nginx_check.sh
3.5 启动keepalived
开机启动
[root@example02 src]# chkconfig keepalived on
启动服务
[root@example01 keepalived]# service keepalived startStartingkeepalived:[ OK ]
4.keepalived+nginx的高可用测试
4.1 查看服务器上的地址
查看MASTER的地址:
[root@example01 keepalived]# ip add
1: lo: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 forever2: ens192: mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 00:50:56:b0:2e:17 brd ff:ff:ff:ff:ff:ff
inet 16.155.199.223/21 brd 16.155.199.255 scope global noprefixroute dynamic ens192
valid_lft 113604sec preferred_lft 113604sec
inet 16.155.197.100/32 scope global ens192 # 注意,此时MASTER上存在一个VIP
valid_lft forever preferred_lft forever
inet6 fe80::c453:9e3c:8efd:1d73/64 scope link noprefixroute
valid_lft forever preferred_lft forever
查看BACKUP的地址:
[root@example02 src]# ip add
1: lo: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 forever2: ens192: mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 00:50:56:b0:3b:c4 brd ff:ff:ff:ff:ff:ff
inet 16.155.197.42/21 brd 16.155.199.255 scope global noprefixroute dynamic ens192
valid_lft 110123sec preferred_lft 110123sec
inet6 fe80::c5d2:fa65:bf3c:e8a8/64 scope link tentative dadfailed
valid_lft forever preferred_lft forever
inet6 fe80::4440:cea8:b176:daa2/64 scope link tentative dadfailed
valid_lft forever preferred_lft forever
inet6 fe80::c865:c307:7968:5daf/64 scope link tentative dadfailed
valid_lft forever preferred_lft forever
浏览器访问:http://16.155.197.100。
4.2 关闭MASTER上的nginx,keepalived会将它重新启动
[root@example01 keepalived]# /usr/local/nginx/sbin/nginx -s stop
4.3 关闭MASTER上的keepalived,VIP会切换到BACKUP上
[root@example01 keepalived]# service keepalived stopStoppingkeepalived:[ OK ]
4.4 验证VIP的漂移
验证方法1:通过ip add查看VIP的漂移
[root@example01 keepalived]# ip add
1: lo: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 forever2: ens192: mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 00:50:56:b0:2e:17 brd ff:ff:ff:ff:ff:ff
inet 16.155.199.223/21 brd 16.155.199.255 scope global noprefixroute dynamic ens192
valid_lft 113216sec preferred_lft 113216sec
inet6 fe80::c453:9e3c:8efd:1d73/64 scope link noprefixroute
valid_lft forever preferred_lft forever
[root@example02 src]# ip add
1: lo: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 forever2: ens192: mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 00:50:56:b0:3b:c4 brd ff:ff:ff:ff:ff:ff
inet 16.155.197.42/21 brd 16.155.199.255 scope global noprefixroute dynamic ens192
valid_lft 110002sec preferred_lft 110002sec
inet 16.155.197.100/32 scope global ens192 #注意此时vip漂浮到backup机器上了
valid_lft forever preferred_lft forever
inet6 fe80::c5d2:fa65:bf3c:e8a8/64 scope link tentative dadfailed
valid_lft forever preferred_lft forever
inet6 fe80::4440:cea8:b176:daa2/64 scope link tentative dadfailed
valid_lft forever preferred_lft forever
inet6 fe80::c865:c307:7968:5daf/64 scope link tentative dadfailed
valid_lft forever preferred_lft forever
验证方法2:浏览器访问:http://16.155.197.100。
刷新页面,显示“Welcome to nginx!Server02”,表示已经VIP已经漂移到了BACKUP服务器上
高可用
到这里,整个部署就已经完成了!