`
pluto418
  • 浏览: 166074 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

Nginx和Keepalived 主备

阅读更多

1. 机器
主机 CentOS 6.4  192.168.1.138
备机 CentOS 6.4  192.168.1.139

2. NGINX安装
sudo apt-get update
sudo apt-get install libpcre3 libpcre3-dev
可能还需要安装
sudo apt-get install openssl libssl-dev

tar zxvf xxx
./configure –prefix=/usr/local/nginx
make
sudo make install


nginx.conf

user www www;
worker_processes 8;
error_log /usr/nginx/logs/nginx_error.log error;
pid       /usr/nginx/nginx.pid;
worker_rlimit_nofile 65535;
events {
	use epoll;
	worker_connections 65535;
}

http {
	include mime.types;
	default_type application/octet-stream;
	#设置请求缓冲
	
	#gzip压缩
	
	#禁止通过IP访问站点
	server {
		server_name_;
		return 404;
	}
	
	server {
		listen 80;
		server_name www.1paituan.com;
		index index.html index.htm index.jsp index.do;
		root /data/htdocs/www/shop;
		
		#所以jsp页面请求交由tomcat处理
		location ~ \.(jsp|jspx|do)?$ {
			proxy_set_header Host $host;
			proxy_set_header X-Real-IP $remote_addr;
			proxy_pass http://127.0.0.1:8080;
		}
		
		location ~ .*\.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)$ {
			expires 1d;
		}
		
		location ~ .*\.(js|css)?$ {
			expires 1h;
		}
		
		#定义访问日志的格式
		log_format
		access_log /usr/nginx/logs/nginx_access.log access;	
	}
}

 

启动: cd   /usr/local/nginx
./nginx

3. 安装Keepalived
yum install Keepalived


keepalived.conf-mastre

 

vrrp_script chk_http_port {
	script "/opt/nginx_pid.sh"  ###监控脚本
	interval 2                  ###监控时间
	weight 2                    ###目前搞不清楚
}
vrrp_instance VI_1 {
	state MASTER                ### 设置为 主
	interface eth0              ### 监控网卡    
	virtual_router_id 51        ### 这个两台服务器必须一样
	priority 101                ### 权重值 MASTRE 一定要高于 BAUCKUP
	authentication {
		 auth_type PASS         ### 加密
		 auth_pass eric         ### 加密的密码,两台服务器一定要一样,不然会出错
	}
	track_script {
		chk_http_port           ### 执行监控的服务
	}
	virtual_ipaddress {
		 192.168.219.100        ###    VIP 地址
	}
} 

 
keepalived.conf-backup

 

 

vrrp_script chk_http_port {
	script "/opt/nginx_pid.sh"
	interval 2
	weight 2
}
vrrp_instance VI_1 {
	state BACKUP             ### 设置为 辅机
	interface eth0
	virtual_router_id 51     ### 与 MASTRE 设置 值一样
	priority 100             ### 比 MASTRE权重值 低
	authentication {
		auth_type PASS
		auth_pass eric       ### 密码 与 MASTRE 一样
	}
	track_script {
		chk_http_port
	}
	virtual_ipaddress {
		192.168.219.100
	}
}

 

4. Shell脚本
nginx_pid.sh

#nohup /bin/bash /opt/nginx_pid.sh &
nginxpid='ps -C nginx --no-header |wc -l'
if [ $nginxpid -eq 0 ];then
   /usr/nginx/sbin/nginx
   sleep 3
   if [ 'ps -C nginx --no-header |wc -l' -eq 0 ];then
       killall keepalived
   fi
fi

 
5. 启动顺序
先启动Nginx        /usr/nginx/sbin/nginx
然后nginx_pid      nohup /bin/bash /opt/nginx_pid.sh
后启动Keepalived   /etc/keepalived/keepalived.conf
or  service keepalived start

都可以用 ps -ef|grep nginx查询运行情况
测试  killall  nginx

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics