原创Nignx+Keepalived实现前端高可用
Posted 嘉盛泰科技
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了原创Nignx+Keepalived实现前端高可用相关的知识,希望对你有一定的参考价值。
嘉盛泰科技为IT企业客户提供多方面的运维维护,经常会遇到客户的前端服务无法满足突然增长的访问量,寻求嘉盛泰的技术支持。嘉盛泰高级技术人员面对这种问题,通常会为客户提供多种解决方案。
这两种方法分别为纵向和横向的。第二种方法才是实现高可用性的解决方案。本篇介绍如何通过Nignx和Keepalived实现高可用的前端负载均衡。
案例介绍
客户前端服务端口为15944和15945
Nignx Keepalived 服务器主 172.16.20.179
Nignx Keepalived 服务器备172.16.20.172
yum install epel-release
yum install nginx keepalived
编辑keepalive配置文件 /etc/keepalived/keepalived.conf修改以下内容:
! Configuration file for keepalived
global_defs {
notification_email {
#这里填写接收keepalive通知的邮箱
}
notification_email_from qiluce
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
#监控服务.NGINX mysql等
vrrp_script chk_nginx {
script "/home/check_nginx.sh"
interval 2
weight 2
}
vrrp_instance VI_1 {
state MASTER #主从设置 MASTER
interface eth2 #网卡名
virtual_router_id 51
mcast_src_ip 172.16.20.179 #本机ip
priority 50 #从机小于主机
advert_int 1
authentication {
auth_type PASS
auth_pass qiluce
}
virtual_ipaddress {
172.16.20.168 #VIP 的IP
}
track_script {
chk_nginx #检测脚本
}
}
编辑keepalive对nginx的监控文件/home/check_nginx.sh
#!/bin/bash
if [ "$(ps -ef | grep "nginx: master process"| grep -v grep )" == "" ]
then
nginx -c /etc/nginx/nginx.conf
sleep 5
if [ "$(ps -ef | grep "nginx: master process"| grep -v grep )" == "" ]
then
service keepalived stop
fi
fi
配置keepalive的原理是通过vip连接nginx,并且检测nginx的运行状态,当检测到nginx运行异常会重启nginx,重启不成功会切换nginx服务器,保证nginx的服务不中断。
编辑nginx配置文件/etc/nginx/nginx.conf修改以下内容:
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;
# Load dynamic modules. See /usr/share/nginx/README.dynamic.
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;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
}
##下面创建负载均衡服务器采用
stream {
upstream tcp16955 {
hash $remote_addr consistent;
server 172.16.20.179:15945;
server 172.16.20.172:15945;
}
upstream tcp16956 {
hash $remote_addr consistent;
server 172.16.20.179:15944;
server 172.16.20.172:15944;
}
upstream tcp16951 {
hash $remote_addr consistent;
server 172.16.20.179:16961;
server 172.16.20.172:16961;
}
##下面定义要负载的端口
server {
listen 16955;
proxy_connect_timeout 1s;
proxy_timeout 3s;
proxy_pass tcp16955;
}
server {
listen 16956;
proxy_connect_timeout 1s;
proxy_timeout 3s;
proxy_pass tcp16956;
}
server {
listen 16951;
proxy_connect_timeout 1s;
proxy_timeout 3s;
proxy_pass tcp16951;
}
}
nginx负载均衡要在不同的服务器是配置两套程序,通过使用keepalive进行故障切换,消除nginx的单点故障。
以上是关于原创Nignx+Keepalived实现前端高可用的主要内容,如果未能解决你的问题,请参考以下文章
Nginx +keepalived+varnish+lamp实现高可用负载均衡集群