NGINX本身只能单点反向代理,当NGINX本身挂掉的话,就会导到整个服务都无法提供服务,当NGINX+KEEPALIVED 时可以解决nginx单点的问题
Keepavlived通过一个虚拟IP来代理两台NGINX,两台主备Keepalived服务共用 一个虚拟IP来进行平滑切换
keepalived每三秒钟执行一个检查脚本,判断nginx是否存活,如果不存活,则执行启动NGINX脚本,在脚本中让线程等待3秒后再判断,如果还是不存活,则关闭keepalived,然后备用keepalived平滑接管虚拟VIP,由于NGINX监听的是虚拟VIP,所以前端并无影响,备用keepalived监管的是备用的nginx,但前端访问的虚拟VIP,所以后端主KEEPALIVE与NGINX挂掉后,备用keepalived继续接管虚拟IP,并对应备用NGINX,实现了高可用
环境Ubuntu 12.04
二台虚拟机:
192.168.228.129 主keepalived + nginx
192.168.228.130 备用keepalived + nginx
共用虚拟IP :192.168.228.80
安装步骤略过,只说明一些重要环境的注意点
1、安装NGINX
启动/usr/local/nginx/sbin/nginx
主、备配置nginx.conf文件一样
################################开始配置############################################
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#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 logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
upstream tomcat_server{
server 192.168.228.129:8080 weight=1 max_fails=2 fail_timeout=20s; #一个TOMCAT服务
server 192.168.228.130:8080 weight=1 max_fails=2 fail_timeout=20s;
}
server {
listen 80;
server_name 192.168.228.80; //VIP 虚拟IP
#charset koi8-r;
#access_log logs/host.access.log main;
# root /home/hadoop/html
#index index.html index.htm;
location / {
root /home/hadoop/html;
index index.html index.htm;
proxy_pass http://tomcat_server;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
2、安装keepalived
在主keepalived服务器中创建/etc/keepalived/keepalived.conf文件,默认是没有
! Configuration File for keepalived
vrrp_script chk_nginx{
script "/etc/keepalived/check_nginx.sh" #检查NGINX是否存活的脚本并进行停止
interval 2
weight 2
}
global_defs {
notification_email {
}
}
vrrp_instance VI_1 {
state MASTER #主keepavlived 是MASTER 备用是BACKUP,配置不一样
interface eth0
virtual_router_id 51 #主备Keepalived 保持一致
priority 100 #主keepalived要比备用的值高,如备用可以为99
advert_int 1
mcast_src_ip 192.168.228.129 #主keepalived是的IP地址
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.228.80 #虚拟VIP
}
track_script{
chk_nginx
}
}
在备用服务器上添加文件/etc/keepalived/keepalived.conf:
! Configuration File for keepalived
vrrp_script chk_nginx{
script "/etc/keepalived/check_nginx.sh"
interval 2
weight 2
}
global_defs {
notification_email {
}
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 90
advert_int 1
mcast_src_ip 192.168.228.130
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.228.80
}
track_script{
chk_nginx
}
}
分别在主备服务器启用:nginx ,keepalived
在主服务器上使用命令:ip a查看是否存在虚拟IP 192.168.228.80 ,存在则成功
并访问http://192.168.228.80/html/index.html能够访问,配置成功
关掉主服务器的nginx,keepalived,继续访问,如果成功则平滑过度