首页 > 代码库 > keepalived高可用nginx

keepalived高可用nginx

实验拓扑:

技术分享


1.在两台realserver上配置web服务

技术分享


2.keepalived配置

master的配置

global_defs {
   notification_email {
       root@localhost
   }
   notification_email_from keepalived@localhost
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id node1
}

vrrp_script chk_down {    #keepalived控制脚本       
	script "[[ -f /etc/keepalived/down ]] && exit 1 || exit 0"      #脚本内容
	interval 2
	weight -6
}

vrrp_script chk_nginx {            #检测nginx服务脚本
	script "killall -0 nginx && exit 0 || exit 1"   #脚本内容     
	interval 2
	weight -6
}

vrrp_instance VI_1 {    #配置实例
    state MASTER
    interface eno16777736
    virtual_router_id 88
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }

    virtual_ipaddress {
		10.1.88.88/16 dev eno16777736 label eno16777736:0
    }

	track_script {    #调用脚本
		chk_down
		chk_nginx
	}

	notify_master "/etc/keepalived/notify.sh master"    #定义通告脚本
	notify_backup "/etc/keepalived/notify.sh backup"    
	notify_fault  "/etc/keepalived/notify.sh fault"
}

backup的配置:

global_defs {
   notification_email {
       root@localhost
   }
   notification_email_from keepalived@localhost
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id node1
}

vrrp_script chk_down {
	script "[[ -f /etc/keepalived/down ]] && exit 1 || exit 0"
	interval 2
	weight -6
}

vrrp_script chk_nginx {
	script "killall -0 nginx && exit 0 || exit 1"
	interval 2
	weight -6
}

vrrp_instance VI_1 {
    state BACKUP    
    interface eno16777736
    virtual_router_id 88
    priority 98
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }

    virtual_ipaddress {
		10.1.88.88/16 dev eno16777736 label eno16777736:0
    }

	track_script {
		chk_down
		chk_nginx
	}

	notify_master "/etc/keepalived/notify.sh master"
	notify_backup "/etc/keepalived/notify.sh backup"
	notify_fault  "/etc/keepalived/notify.sh fault"
}

3.nginx调度器配置

两台nginx调度器的配置文件

#http段中的配置
http {
    include       /etc/nginx/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  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;

	upstream webserver {
		server 10.1.68.5 weight=1 max_fails=2;
		server 10.1.68.6 weight=1 max_fails=2;
	}

	server {
		listen 80;
		server_name localhost;

		location / {
			index index.html index.htm;
			proxy_set_header Host $host;
			proxy_set_header X-Real-IP $remote_addr;
			proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
			proxy_pass http://webserver;
		}
	}

}

在两台调度器上同时启动nginx和keepalived,此时VIP在nginx1主机上

技术分享


4.测试

通过VIP访问,可正常调度

技术分享


停止调度器1上面的nginx服务,VIP漂移至nginx2主机上

技术分享

技术分享


依然可以正常调度

技术分享





keepalived高可用nginx