keepalived+nginx负载均衡高可用

一. 准备两台服务器

主:内网IP 10.41.24.134 ,外网IP 122.13.1.75

副:内网IP 10.41.24.146 ,外网IP 122.13.1.91

配置虚拟IP:10.41.24.211

二. 在两台服务器上安装nginx 配置许一致(也可不一致,个人建议),配置如下:

###################配置信息START################################

user www www;

worker_processes 16; #设置值和CPU核心数一致

#Specifies the value for maximum file descriptors that can be opened by this process.

worker_rlimit_nofile 65535;

events

{

use epoll;

# worker_connections 65535;

worker_connections 204800;

}

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';

#charset gb2312;

server_names_hash_bucket_size 128;

client_header_buffer_size 32k;

large_client_header_buffers 4 32k;

client_max_body_size 8m;

sendfile on;

tcp_nopush on;

keepalive_timeout 60;

tcp_nodelay on;

fastcgi_connect_timeout 14400;

fastcgi_send_timeout 14400;

fastcgi_read_timeout 14400;

fastcgi_buffer_size 128k;

#fastcgi_buffers 4 64k;

fastcgi_buffers 256 128k;

fastcgi_busy_buffers_size 256k;

fastcgi_temp_file_write_size 256k;

gzip on;

gzip_min_length 1k;

gzip_buffers 4 16k;

gzip_http_version 1.0;

gzip_comp_level 2;

gzip_types text/plain application/javascript application/x-javascript text/javascript text/css application/xml application/xml+rss;

gzip_vary on;

gzip_proxied expired no-cache no-store private auth;

gzip_disable "MSIE [1-6]\.";

server_tokens off;

access_log off;

#limit_zone crawler $binary_remote_addr 10m;

#下面是server虚拟主机的配置

#include vhost/*.conf;

######################负载均衡START#########################

upstream linuxidc{

# server 122.13.1.79:8088;

# ip_hash;

server 118.89.59.33:81 weight=10;

server 118.89.59.33:82 weight=20;

server 118.89.59.33:83;

}

server

{

listen 8082;#监听端口

server_name localhost;#域名

index index.html index.php index.htm;

#root /data/www/;#站点目录

error_page 500 502 503 504 /50x.html;

location = /50x.html {

root html;

}

location / {

proxy_pass http://linuxidc;

}

}

######################负载均衡END#########################

#反向代理服务

server

{

listen 3307;#监听端口

server_name localhost;#域名

index index.html index.php index.htm;

# root /data/www/;#站点目录

error_page 500 502 503 504 /50x.html;

location = /50x.html {

root html;

}

#反向代理

location / {

proxy_pass http://10.41.24.211:8082;

}

}

}

###################配置信息END################################

三. 在两台服务器上安装keepalived

##############主:内网IP 10.41.24.134 ,外网IP 122.13.1.##75##配置信息START#####

! Configuration File for keepalived

global_defs {

router_id lvs-01 ##keepalived ID 唯一即可

}

##############监控nginx的运行状态START###############

vrrp_script chk_nginx {

script "/etc/keepalived/nginx_check.sh"

#每2秒检测一次nginx的运行状态

interval 2

#失败一次,将自己的优先级调整为-20

weight -20

}

##############监控nginx的运行状态END###############

vrrp_instance VI_1 {

state MASTER

interface ens160

virtual_router_id 51

priority 100

advert_int 1

authentication {

auth_type PASS

auth_pass 1111

}

unicast_src_ip 10.41.24.134 ##(本地IP地址)

unicast_peer {

10.41.24.146 ##(对端IP地址)此地址一定不能忘记

}

virtual_ipaddress {

10.41.24.211 ##虚拟IP

}

track_script {

chk_nginx ##监控chk_nginx

}

}

##############主:内网IP 10.41.24.134 ,外网IP 122.13.1.75####配置信息END#####

##############副:内网IP 10.41.24.146 ,外网IP 122.13.1.91####配置信息START#####

! Configuration File for keepalived

global_defs {

router_id lvs-02

}

####################副服务器被启用时执行发送邮件到管理员##############START

vrrp_sync_group VG_1 {

group {

VI_1

}

#节点变为master时执行发送邮件到管理员

notify_master /etc/keepalived/send_mail.pl

}

####################副服务器被启用时执行发送邮件到管理员##############END

vrrp_instance VI_1 {

state BACKUP

interface ens160

virtual_router_id 51

priority 90

advert_int 1

authentication {

auth_type PASS

auth_pass 1111

}

unicast_src_ip 10.41.24.146 ##(本地IP地址)

unicast_peer {

10.41.24.134 ##(对端IP地址)此地址一定不能忘记

}

virtual_ipaddress {

10.41.24.211 虚拟ip

}

}

##############副:内网IP 10.41.24.146 ,外网IP 122.13.1.91####配置信息END########

四. 主服务器监控nginx服务脚本

#新建文件

vim /etc/keepalived/nginx_check.sh

############脚本内容START #########################

#!/bin/bash

A=`ps -C nginx -no-header |wc -l`

if [ $A -eq 1 ];then

#试着启动nginx

/usr/local/nginx/sbin/nginx

sleep 3

#如果重启不成功关闭keepalived

if [ `ps -C nginx -no-header |wc -l` -eq 1 ];then

pkill keepalived

fi

fi

############脚本内容END #########################

五. 副服务器被启用邮件通知脚本

#安装perl

yum -y install perl-CPAN

#安装模块

cpan Net::SMTP_auth

#新建perl文件

vim etc/keepalived/send_mail.pl

###################etc/keepalived/send_mail.pl 邮件内容 SATRT######################

#!/usr/bin/perl -w

use Net::SMTP_auth;

use strict;

#smtp服务器

#发送邮件的邮箱

#接收邮件的邮箱

#邮件主题

my $subject = 'keepalived up on backup';

#邮件正文

my $text = "正文\n nginx-1服务器宕机!!nginx-2变为master!!!";

#发送邮件的用户名

#发送邮件的邮箱密码

my $passwd = 'xxxxxx';

&SendMail();

##############################

# Send notice mail

##############################

sub SendMail() {

my $smtp = Net::SMTP_auth->new( $mailhost, Timeout => 120, Debug => 1 )

or die "Error.\n";

$smtp->auth( 'LOGIN', $user, $passwd );

foreach my $mailto (@mailto) {

$smtp->mail($mailfrom);

$smtp->to($mailto);

$smtp->data();

$smtp->datasend("To: $mailto\n");

$smtp->datasend("From:$mailfrom\n");

$smtp->datasend("Subject: $subject\n");

$smtp->datasend("\n");

$smtp->datasend("$text\n\n");

$smtp->dataend();

}

$smtp->quit;

}

###################etc/keepalived/send_mail.pl 邮件内容 END######################

#测试是否能发送邮件

#如果不能发送邮件重新

#重新安装模块

cpan Net::SMTP_auth

再次测试即可

  • 发表于:
  • 原文链接https://kuaibao.qq.com/s/20190214G0MWQH00?refer=cp_1026
  • 腾讯「云+社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。

扫码关注云+社区

领取腾讯云代金券

年度创作总结 领取年终奖励