nginx+keepalived主辅切换
更新时间:2023-10-31 11:04:01 阅读量: 综合文库 文档下载
以前写过一篇,nginx+keepalived 双机互备的文章,写那篇文章的时候没有想过如果apache或者nginx 挂了,而 keepalived 或者 机器没有死,那么主辅是不会切换的,今天就研究了一下该如何监控 nginx进程呢,看官方站看到了。vrrp_script 功能,但是用他的方法实在形不通,可能是我的方法不对,或者是个BUG。所以后来我自己写了个小脚本来完成工作。 环境
Server 1 : ubuntu-server 8.04.4 192.168.6.162 Server 2 : userver-server 8.04.4 192.168.6.188 软件
Keepalived 1.1.15 nginx-0.8.35 pcre-8.02
1.分别在两台服务器上安装nginx 装nginx
tar jxvf pcre-8.02.tar.bz2 cd pcre-8.02
./configure --prefix=/usr --enable-utf8 --enable-pcregrep-libbz2 --enable-pcregrep-libz make
make install
tar zxvf nginx-0.8.35.tar.gz cd nginx-0.8.35
--prefix=/usr/local/nginx --with-pcre --user=www --group=www --with-file-aio --with-http_ssl_module --with-http_flv_module --with-http_gzip_static_module --with-http_stub_status_module --with-cc-opt=' -O3' make
make install
2.分别在两台服务器编写配置文件
vim /usr/local/nginx/conf/nginx.conf user www www;
worker_processes 1;
error_log logs/error.log notice; pid logs/nginx.pid; events {
worker_connections 1024; }
http {
include mime.types;
default_type application/octet-stream; sendfile on; tcp_nopush on; keepalive_timeout 65; gzip on; server {
listen 80;
server_name localhost;
index index.html index.htm; root /var/www;
error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }
3.分别在两台机器创建测试文件
echo \> /var/www/index.html echo \> /var/www/index.html
4.安装 keepalived
5.在server 1服务器编写配置文件
vrrp_script chk_http_port {
script \###监控脚本 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.6.7 ### VIP 地址 } }
apt-get install keepalived
6.在 server 2 服务器 keepalived 配置
vrrp_script chk_http_port {
script \ 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.6.7 } }
7.编写监控nginx监控脚本
vim /opt/nginx_pid.sh #!/bin/bash # varsion 0.0.2
# 根据一网友说这样做不科学,如果nginx服务起来了,但是我把keepalived 杀掉了,我的理由是,如果nginx死掉了,我觉得就很难在起来,再有就是nagios 当然要给你报警了啊。不过这位同学说的有道理,所以就稍加改了一下脚本
A=`ps -C nginx --no-header |wc -l` ## 查看是否有 nginx进程 把值赋给变量A
if [ $A -eq 0 ];then ## 如果没有进程值得为 零 /usr/local/nginx/sbin/nginx sleep 3
if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
killall keepalived ## 则结束 keepalived 进程 fi fi
8、测试,分别在两个服务器 启动 nginx 和 keepalived
/usr/local/nginx/sbin/nginx /etc/init.d/keepalived start
监控 server 1 的日志
Apr 20 18:37:39 nginx Keepalived_vrrp: Registering Kernel netlink command channel
Apr 20 18:37:39 nginx Keepalived_vrrp: Registering gratutious ARP shared channel
Apr 20 18:37:39 nginx Keepalived_vrrp: Opening file '/etc/keepalived/keepalived.conf'.
Apr 20 18:37:39 nginx Keepalived_healthcheckers: Opening file '/etc/keepalived/keepalived.conf'.
Apr 20 18:37:39 nginx Keepalived_healthcheckers: Configuration is using : 3401 Bytes
Apr 20 18:37:39 nginx Keepalived_vrrp: Configuration is using : 35476 Bytes Apr 20 18:37:40 nginx Keepalived_vrrp: VRRP_Instance(VI_1) Transition to MASTER STATE
Apr 20 18:37:41 nginx Keepalived_vrrp: VRRP_Instance(VI_1) Entering MASTER STATE
Apr 20 18:37:41 nginx Keepalived_vrrp: Netlink: skipping nl_cmd msg...
Apr 20 18:37:41 nginx Keepalived_vrrp: VRRP_Script(chk_http_port) succeeded
监控 server 2的日志
Apr2018:38:23 varnish Keepalived_healthcheckers: Opening file '/etc/keepalived/keepalived.conf'.
Apr 20 18:38:23 varnish Keepalived_healthcheckers: Configuration is using : 3
405 Bytes
Apr 20 18:38:23 varnish Keepalived_vrrp: Using MII-BMSR NIC polling thread...
Apr 20 18:38:23 varnish Keepalived_vrrp: Registering Kernel netlink reflector Apr 20 18:38:23 varnish Keepalived_vrrp: Registering Kernel netlink command channel
Apr 20 18:38:23 varnish Keepalived_vrrp: Registering gratutious ARP shared channel
Apr 20 18:38:23 varnish Keepalived_vrrp: Opening file '/etc/keepalived/keepalived.conf'.
Apr 20 18:38:23 varnish Keepalived_vrrp: Configuration is using : 35486 Bytes
Apr 20 18:38:23 varnish Keepalived_vrrp: VRRP_Instance(VI_1) Entering BACKUP STATE
Apr 20 18:38:25 varnish Keepalived_vrrp: VRRP_Script(chk_http_port) succeeded
看日志可以看出,两台服务器的 MASTRE 和 BACUKUP 已经都正常了 现在我们在 server 1 把 nginx 服务器停到
Server 1 $> killall nginx
这时候看server 1的日志
Apr 20 18:41:26 nginx Keepalived_healthcheckers: Terminating Healthchecker child process on signal
Apr 20 18:41:26 nginx Keepalived_vrrp: Terminating VRRP child process on signal
可以看出keepalived 的进程已经停到
这时候看server 2的日志,看是否已经接管
Apr 20 18:41:23 varnish Keepalived_vrrp: VRRP_Instance(VI_1) Transition to MASTER STATE
Apr 20 18:41:24 varnish Keepalived_vrrp: VRRP_Instance(VI_1) Entering MASTER STATE
Apr 20 18:41:24 varnish Keepalived_vrrp: Netlink: skipping nl_cmd msg...
很明显的看出 server 2 已经接管了,已经变为 MASTER 了
正在阅读:
nginx+keepalived主辅切换10-31
靖安县城城市人口规模及用地规模专题报告01-17
东北三省三校2015届高三第一次高考模拟考试 文科数学试卷 Word版 - 图文01-19
热工基础期末考试试题03-13
公司员工培训学习心得体会(精选5篇)03-31
怎么写有的人教学反思03-10
【军训感悟】初中军训感悟500字、高中军训感悟800字、大学军训感悟1000字08-01
高三生物滚动练习04-20
医疗机构更新乙类大型医用设备08-27
- 多层物业服务方案
- (审判实务)习惯法与少数民族地区民间纠纷解决问题(孙 潋)
- 人教版新课标六年级下册语文全册教案
- 词语打卡
- photoshop实习报告
- 钢结构设计原理综合测试2
- 2014年期末练习题
- 高中数学中的逆向思维解题方法探讨
- 名师原创 全国通用2014-2015学年高二寒假作业 政治(一)Word版
- 北航《建筑结构检测鉴定与加固》在线作业三
- XX县卫生监督所工程建设项目可行性研究报告
- 小学四年级观察作文经典评语
- 浅谈110KV变电站电气一次设计-程泉焱(1)
- 安全员考试题库
- 国家电网公司变电运维管理规定(试行)
- 义务教育课程标准稿征求意见提纲
- 教学秘书面试技巧
- 钢结构工程施工组织设计
- 水利工程概论论文
- 09届九年级数学第四次模拟试卷
- keepalived
- 切换
- nginx