角色 内网ip 外网ip
keepalived+nginx 192.168.8.81 192.168.8.201
keepalived+nginx 192.168.8.82 192.168.8.201
web1 192.168.8.83 -
web2 192.168.8.84 -
yum -y install nginx
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
log_format main ‘$remote_addr - $remote_user [$time_local] "$request" ‘
‘$status $body_bytes_sent "$http_referer" ‘
‘"$http_user_agent" "$http_x_forwarded_for"‘;
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
include /etc/nginx/conf.d/*.conf;
upstream myProxy {
server 192.168.8.83:80;
server 192.168.8.84:80;
}
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /usr/share/nginx/html;
include /etc/nginx/default.d/*.conf;
location / {
proxy_pass http://myProxy;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
}
systemctl start nginx
yum -y install keepalived
! Configuration File for keepalived
global_defs {
}
vrrp_script chk_nginx {
script "/etc/keepalived/check.sh"
interval 1
weight -15
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
192.168.8.201
}
track_script {
chk_nginx
}
notify_master "/etc/keepalived/notify.sh master"
notify_backup "/etc/keepalived/notify.sh backup"
notify_fault "/etc/keepalived/notify.sh fault"
}
#!/bin/bash
# 心跳检测:如果服务还在运行返回0,否则返回1
if [ "`ps -aux | grep nginx | wc -l`" == "0" ] ; then
exit 1
else
exit 0
fi
#!/bin/bash
# 通知脚本:具体操作
if [ "$1" == "master" ] ; then
echo "`date +%F` `date +%T`:切换到主机模式" >> /var/log/keepalived/keepalived.log
elif [ "$1" == "backup" ] ; then
/usr/bin/systemctl start nginx
echo "`date +%F` `date +%T`:切换到备机模式" >> /var/log/keepalived/keepalived.log
else
/usr/bin/systemctl start nginx
echo "`date +%F` `date +%T`:宕机" >> /var/log/keepalived/keepalived.log
fi
chmod +x /etc/keepalived/check.sh
chmod +x /etc/keepalived/notify.sh
mkdir /var/log/keepalived
systemctl start keepalived
yum -y install keepalived
! Configuration File for keepalived
global_defs {
}
vrrp_script chk_nginx {
script "killall -0 nginx"
interval 1
weight -15
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
192.168.8.201
}
track_script {
chk_nginx
}
notify_master "/etc/keepalived/notify.sh master"
notify_backup "/etc/keepalived/notify.sh backup"
notify_fault "/etc/keepalived/notify.sh fault"
}
#!/bin/bash
# 心跳检测:如果服务还在运行返回0,否则返回1
if [ "`ps -aux | grep nginx | wc -l`" == "0" ] ; then
exit 1
else
exit 0
fi
#!/bin/bash
# 通知脚本:具体操作
if [ "$1" == "master" ] ; then
echo "`date +%F` `date +%T`:切换到主机模式" >> /var/log/keepalived/keepalived.log
elif [ "$1" == "backup" ] ; then
/usr/bin/systemctl start nginx
echo "`date +%F` `date +%T`:切换到备机模式" >> /var/log/keepalived/keepalived.log
else
/usr/bin/systemctl start nginx
echo "`date +%F` `date +%T`:宕机" >> /var/log/keepalived/keepalived.log
fi
chmod +x /etc/keepalived/check.sh
chmod +x /etc/keepalived/notify.sh
mkdir /var/log/keepalived
systemctl start keepalived
systemctl stop nginx
tail /var/log/keepalived/keepalived.log
原文地址:http://blog.51cto.com/12173069/2120638