nginx实现负载均衡,keepalived则主要用来监控nginx的健康状态(keepalived其实不止可以监控nginx其实可以监控redis,memcached等等需要监控的服务),官网:https://www.keepalived.org,nginx的使用方法见我另外一篇文章nginx实现负载均衡,keepalived下载地址https://www.keepalived.org/download.html,下载好了之后,我们先开始安装。
一、使用源码安装
[root@localhost src]# cd /usr/local
[root@localhost src]# mkdir keepalived
[root@localhost src]# wget http://www.keepalived.org/software/keepalived-2.0.20.tar.gz
[root@localhost src]# tar xvf keepalived-2.0.20.tar.gz
[root@localhost src]# cd keepalived-2.0.20
[root@localhost keepalived-2.0.20]# ./configure --prefix=/usr/local/keepalived#编译后安装
[root@localhost keepalived-2.0.20]# make && make install# keepalived启动脚本变量引用文件,默认文件路径是/etc/sysconfig/,也可以不做软链接,直接修改启动脚本中文件路径即可(安装目录下)
[root@localhost /]# cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/keepalived
# 将keepalived主程序加入到环境变量(安装目录下)
[root@localhost /]# cp /usr/local/keepalived/sbin/keepalived /usr/sbin/keepalived
# keepalived启动脚本(源码目录下),放到/etc/init.d/目录下就可以使用service命令便捷调用
[root@localhost /]# cp /usr/local/keepalived/keepalived-2.0.20/keepalived/etc/init.d/keepalived /etc/init.d/keepalived
# 将配置文件放到默认路径下
[root@localhost /]# mkdir /etc/keepalived
[root@localhost /]# cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf
二、配置
发现很多第三方软件其实安装了主要就是配置了,比如nginx也是nginx.conf,redis的redis.conf,我实验过如果KeepAlived一个MASTER一个BACKUP,其中MASTER停机后,会出现虚拟ip不会漂移到BACKUP,所以后面我采用的方式是两台都采用backup方式,
这里给出backup01和backup02的配置
BACKUP01配置
! Configuration File for keepalived
vrrp_script chk_nginx {
script "/etc/keepalived/nginx_check.sh" ## 检测 nginx 状态的脚本路径
interval 5 ## 检测时间间隔秒
##weight -20 ## 如果条件成立,权重-20
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51 ##两台必须一致
priority 100 ##数字越大优先级越高
advert_int 1 ##检测是否正常 单位秒
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_nginx ## 执行 Nginx 监控的服务
}
nopreempt ##非抢占模式
unicast_src_ip 100.252.95.101 ##当前服务器ip
unicast_peer {
100.252.95.109 ##另外一台备机ip
}
virtual_ipaddress {
100.252.95.88 ##虚拟ip
}
}
BACKUP02配置
! Configuration File for keepalived
vrrp_script chk_nginx {
script "/etc/keepalived/nginx_check.sh" ## 检测 nginx 状态的脚本路径
interval 5 ## 检测时间间隔
##weight -20 ## 如果条件成立,权重-20
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_nginx ## 执行 Nginx 监控的服务
}
nopreempt
unicast_src_ip 100.252.95.109
unicast_peer {
100.252.95.101
}
virtual_ipaddress {
100.252.95.88
}
}
这个配置的意思每隔5秒执行一次nginx_check.sh的脚本,即检查nginx是否是健康的,nginx_check.sh的脚本如下
#!/bin/bash
counter=$(ps -C nginx --no-heading|wc -l)
if [ "${counter}" = "0" ]; then
/usr/local/nginx/sbin/nginx
sleep 2
counter=$(ps -C nginx --no-heading|wc -l)
if [ "${counter}" = "0" ]; then
/etc/init.d/keepalived stop
fi
fi
这段代码的意思是入门发现nginx进程不存在了,那么启动异常,启动后发现还不存在,那么keepalived这进程也停止,免得不断的尝试启动Nginx。
三、相关命令
相关命令
keepalived安装在哪里
whereis keepalived
查看当前keepalived 的状态 service keepalived status
启动start,停止stop,重启restart,改了配置文件后先reload,再start service keepalived start/restart/stop/reload
四、测试
http://100.252.95.88:8018/Test/
配置好之后,启动了keepalived和nginx,就可以使用上面虚拟Ip打开站点,端口号需要nginx配置的监听一样
我实验的时候
两个test站点的部署服务器ip是
http://100.252.83.107:8088/Test/
http://100.252.95.109:8088/Test/
两台nginx的地址是
http://100.252.95.101:8018/Test/ 随机路由至100.252.83.107,100.252.95.109
http://100.252.95.109:8018/Test/ 随机路由至100.252.83.107,100.252.95.109
两台keepalived和nginx部署在一起,只是设置了一个相同的虚拟ip 100.252.95.88
这里就可以通过虚拟ip访问站点了http://100.252.95.88:8018/Test/,去查看实际服务器100.252.83.107,100.252.95.109,就会发现确实随机访问他们了,这里就能保证高可用,当然希望更高的可用性,可以多部署几台nginx和keepalived,实现站点的软负载!