nginx自带负载均衡可以实现服务的高可用,但是如果nginx挂了高可用就会有问题,nginx的高可用通过如下来组件实现:
LVS:Linux Virtual Server(Linux虚拟服务器),可以实现虚拟ip,利用服务器集群来实现高可用,与nginx区别在于LVS是OSI四层负载均衡(传输层TCP、UDP),Nginx用于OSI七层负载均衡(应用层http)
KeepAlived:KeeAlived是在LVS上面做扩展功能,可用于提供监听服务的心跳,自动重启服务。通常与nginx、haproxy搭配使用。支持负载均衡的除了nginx还有haproxy,是专门做负载均衡,相比nginx负载均衡功能更强大
KeepAlived
准备两台Linux服务器:主节点(192.168.126.157)、备节点(192.168.126.161)。分别都安装好KeepAlived并配置,详细步骤如下:
安装步骤
wget http://www.keepalived.org/software/keepalived-2.0.18.tar.gz --no-check-certificate
tar -zxvf keepalived-2.0.18.tar.gz && cd keepalived-2.0.18
./configure --prefix=/usr/local/keepalived
make && make install
常见报错 Issued certificate has expired. To connect to www.keepalived.org insecurely,
wget下载https开头的网址域名 时报错,你需要加上 --no-check-certificate (不检查证书)选项
环境搭建
KeepAlived 默认会从 /etc/keepalived/keepalived.conf 路径读取配置文件,所以需要额外cp一份到文件夹中
mkdir /etc/keepalived
cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
拷贝相关文件
# 从安装包目录拷贝
[root@localhost keepalived-2.0.18]# cp keepalived/etc/init.d/keepalived /etc/init.d/
# 从编译好的目录拷贝
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
配置文件
编写nginx自检测启动脚本
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
授权脚本
chmod +x /etc/keepalived/nginx_check.sh
查看网卡信息
[root@localhost keepalived]
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:5c:33:98 brd ff:ff:ff:ff:ff:ff
inet 192.168.126.157/24 brd 192.168.126.255 scope global noprefixroute dynamic ens33
valid_lft 1351sec preferred_lft 1351sec
inet6 fe80::4f2e:8438:92e8:f2dd/64 scope link tentative noprefixroute dadfailed
valid_lft forever preferred_lft forever
inet6 fe80::64f9:8f45:f3bf:7da/64 scope link tentative noprefixroute dadfailed
valid_lft forever preferred_lft forever
inet6 fe80::72a5:e5bc:df85:5ef0/64 scope link tentative noprefixroute dadfailed
valid_lft forever preferred_lft forever
KeepAlived配置文件内容
- 主节点配置文件内容
vim /etc/keepalived/keepalived.conf
内容如下
vrrp_script chk_nginx {
script "/etc/keepalived/nginx_check.sh" #运行脚本,脚本内容下面有,就是起到一个nginx宕机以后,自动开启服务
interval 2 #检测时间间隔
weight -20 #如果条件成立的话,则权重 -20
}
# 定义虚拟路由,VI_1 为虚拟路由的标示符,自己定义名称
vrrp_instance VI_1 {
state MASTER #来决定主从
interface ens33 # 绑定虚拟 IP 的网络接口,根据自己的机器填写
virtual_router_id 111 # 虚拟路由的 ID 号, 两个节点设置必须一样
mcast_src_ip 192.168.126.157 #填写本机ip
priority 100 # 节点优先级,主要比从节点优先级高
nopreempt # 优先级高的设置 nopreempt 解决异常恢复后再次抢占的问题
advert_int 1 # 组播信息发送间隔,两个节点设置必须一样,默认 1s
authentication {
auth_type PASS
auth_pass 1111
}
# 将 track_script 块加入 instance 配置块
track_script {
chk_nginx #执行 Nginx 监控的服务
}
virtual_ipaddress {
192.168.126.156 # 虚拟ip,从当前网段找一个没被占用的ip,也就是解决写死程序的ip怎么能切换的ip,也可扩展,用途广泛。可配置多个。
}
}
2.备节点配置文件内容
vim /etc/keepalived/keepalived.conf
配置内容如下
vrrp_script chk_nginx {
script "/etc/keepalived/nginx_check.sh" #运行脚本,脚本内容下面有,就是起到一个nginx宕机以后,自动开启服务
interval 2 #检测时间间隔
weight -20 #如果条件成立的话,则权重 -20
}
# 定义虚拟路由,VI_1 为虚拟路由的标示符,自己定义名称
vrrp_instance VI_1 {
state BACKUP #来决定主从
interface ens33 # 绑定虚拟 IP 的网络接口,根据自己的机器填写
virtual_router_id 111 # 虚拟路由的 ID 号, 与主节点配置一样
mcast_src_ip 192.168.126.157 #填主节点IP
priority 90 # 节点优先级,主要比从节点优先级高
nopreempt
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_nginx
}
virtual_ipaddress {
192.168.126.156 # 虚拟ip,与主节点配置一样
}
}
开启服务
service keepalived start
成功提示
Starting keepalived (via systemctl): [ OK ]
访问虚拟IP:192.168.126.156,测试主备
可停止keepalived用于测试
service keepalived stop
通常云服务器(阿里云)不支持安装KeepAlived虚拟IP,需要额外购买
参考文献:
https://blog.csdn.net/l1028386804/article/details/72801492
https://blog.csdn.net/qq_28533563/article/details/107932712
|