yum -y install gcc pcre-devel zlib-devel openssl-devel popt-devel
wget http://nginx.org/download/nginx-1.8.0.tar.gz
wget http://www.keepalived.org/software/keepalived-1.2.19.tar.gz
tar zxvf keepalived-1.2.19.tar.gz
cd keepalived-1.2.19
./configure --prefix=/usr/local
make&&make install
cp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
mkdir /etc/keepalived
cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/
cp /usr/local/sbin/keepalived /usr/sbin/
echo "/usr/local/nginx/sbin/nginx" >> /etc/rc.local
echo "/etc/init.d/keepalived start" >> /etc/rc.local
Nginx配置文件
http {
upstream imgserver{
ip_hash;
server 183.131.54.25:80 weight=1 max_fails=2 fail_timeout=3;
server 183.131.54.26:80 weight=1 max_fails=2 fail_timeout=3;
}
server {
location /
{
proxy_pass http:
}
}
}
keepalived配置文件
yum -y install nmap
vi /opt/chk_nginx.sh
#!/bin/bash
NGINX=/usr/local/nginx/sbin/nginx
PORT=80
nmap localhost -p $PORT | grep "$PORT/tcp open"
if [ $? -ne 0 ];then
$NGINX -s stop
$NGINX
sleep 3
nmap localhost -p $PORT | grep "$PORT/tcp open"
[ $? -ne 0 ] && /etc/init.d/keepalived stop
fi
vi /etc/keepalived/keepalived.conf
主服务器配置完后运行
service keepalived start开启主查看ip addr是否绑定主服务器是否接管
service keepalived status查看是否运行
service keepalived stop关闭主查看ip addr是否绑定从服务器是否接管
! Configuration File for keepalived
global_defs {
router_id NGINX_UPSTREAM
}
vrrp_script chk_http_port {
script "/opt/chk_nginx.sh"
interval 1
weight -2
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.100
}
}
track_script {
chk_http_port
}
从服务器配置完后运行service keepalived start
! Configuration File for keepalived
global_defs {
router_id NGINX_UPSTREAM
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 98 #每个从服务器数字要改变
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.100
}
}