1.搭建keepalived+nginx
实现双机热备+负载均衡(主从模式)
lb-01:172.21.3.186 nginx+keepalived-master
lb-02:172.21.3.187 nginx+keepalived-backup
VIP:172.21.3.194
2.下载安装软件
分别在两台主机172.21.3.186,172.21.3.187上安装依赖和下载安装包
* 安装依赖包
# yum -y install gcc gcc+ gcc-c++ pcre-devel zlib-devel openssl-devel popt-devel
- 下载
keepalived
安装包
# wget http://www.keepalived.org/software/keepalived-1.1.17.tar.gz
- 下载
nginx
安装包
# wget http://nginx.org/download/nginx-1.8.1.tar.gz
3.安装nginx
2台主机都使用默认配置进行安装
* 解压
# tar -zxvf ./nginx-1.8.1.tar.gz
安装软件
注:源码的安装一般由3个步骤组成:配置
configure
、编译make
、安装make install
# cd ./nginx-1.8.1
# ./configure && make && make install
# whereis nginx #可查看安装目录
- 运行nginx
# cd /usr/local/nginx/sbin/
# ./nginx
- 测试nginx是否正常启动
# curl http://172.21.3.186 | grep nginx
<title>Welcome to nginx!</title>
4.安装keepalived
- 解压
# tar -zxvf ./keepalived-1.1.17.tar.gz
- 安装
# cd ./keepalived-1.1.17
# ./configure --prefix=/usr/local/keepalived
# make && make install
- 添加配置文件、设置
keepalived
为自启服务
# mkdir /etc/keepalived
# cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf
# cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
# cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
# cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
# chkconfig --level 2345 keepalived on
- 修改配置文件
配置172.21.3.186文件(Master):
# vim /etc/keepalived/keepalived.conf #修改配置文件
! Configuration File for keepalived
global_defs {
router_id NGINX_DEMO
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 101
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.21.3.194
}
}
修改172.21.3.187文件(Backup):
# vim /etc/keepalived/keepalived.conf #修改配置文件
! Configuration File for keepalived
global_defs {
router_id NGINX_DEMO
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.21.3.194
}
}
- 启动服务
# service keepalived start
- 使用
ip addr
检查虚拟ip
# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
link/ether 00:0c:29:04:89:2c brd ff:ff:ff:ff:ff:ff
inet 172.21.3.186/24 brd 172.21.3.255 scope global eth0
inet 172.21.3.194/32 scope global eth0 #看这里,已经可以看到虚拟ip了
inet6 fe80::20c:29ff:fe04:892c/64 scope link
valid_lft forever preferred_lft forever
5.让keepalived监控nginx状态
经过前面的配置,如果master
主服务的keepalived
停止服务,slave
从服务会自动接管VIP对外服务。一旦主服务器的keepalived
恢复,会重新接管VIP。但这并不是我们需要的,我们需要当nginx
停止服务的时候能自动切换。我们需要使用keepalived来运行监控脚本。
- 创建监听nginx状态脚本
# vim /opt/chk_nginx.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
# chmod 755 /opt/chk_nginx.sh
- 同时修改主从服务器上的
keepalived
配置文件
! Configuration File for keepalived
global_defs {
router_id NGINX_DEVEL
}
vrrp_script chk_http_port { #检测nginx服务是否在运行。
script "/opt/chk_nginx.sh" #这里通过脚本监测
interval 2 #脚本执行间隔,每2s检测一次
weight -5 #脚本结果返回非零则优先级-5
fall 2 #监测连续失败2次,才算失败会用weight减少优先级
rise 1 #检测1次成功就算成功
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 101
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.21.3.194
}
track_script { #执行监控的服务。
chk_http_port #引用VRRP脚本,
}
}
6.测试
- 打开网址输入VIP地址就可以了
- 测试关闭172.21.3.186中的
nginx
服务
# killall nginx
可以看到我们在keepalived
中的监控脚本启动正常。
- 测试关闭172.21.3.186中的
keepalived
服务
# killall keepalived
- 再次打开网址
说明我们配置热备成功。