nginx负载均衡+keepalived三主(多主)配置

Posted wsjhk

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了nginx负载均衡+keepalived三主(多主)配置相关的知识,希望对你有一定的参考价值。

nginx负载均衡+keepalived三主(多主)配置

1.实验环境,实现目标
三台主机分别配置nginx负载均衡对后端多台主机做转发,同时配置keepalived实现HA,保证任意主机出现故障时其他主机接管
serverA 192.168.1.10 VIP1:192.168.1.110
serverB 192.168.1.20 VIP2:192.168.1.120
serverC 192.168.1.30 VIP3:192.168.1.130
2.配置nginx
分别在三台主机安装nginx,配置文件相同
tar zxvf nginx-1.2.2.tar.gz
cd nginx-1.2.2
./configure –prefix=/opt/nginx –user=daemon –group=daemon
make && make install
vi /opt/nginx.conf
user daemon daemon;

worker_processes 2;

error_log /opt/nginx/logs/nginx_error.log crit;

pid /opt/nginx/logs/nginx.pid;

worker_rlimit_nofile 65535;

events
{
use epoll;
worker_connections 65535;
}

http
{
include mime.types;
default_type application/octet-stream;

#charset utf-8;

server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;

#client_max_body_size 8m;

sendfile on;
send_timeout 60;
tcp_nopush on;

keepalive_timeout 60;

tcp_nodelay on;

fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 8 128k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;

gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;

#limit_zone crawler $binary_remote_addr 10m;

client_max_body_size 8m; #允许客户端请求的最大单个文件字节数
client_body_buffer_size 128k; #缓冲区代理缓冲请求的最大字节数,可以理解为先保存到本地再传给用户
proxy_connect_timeout 600; #跟后端服务器连接超时时间,发起握手等候响应时间
proxy_read_timeout 600; #连接成功后等待后端服务器的响应时间,已经进入后端的排队之中等候处理
proxy_send_timeout 600; #后端服务器回传时间,就是在规定时间内后端服务器必须传完所有数据
proxy_buffer_size 16k; #代理请求缓冲区,会保存用户的头信息以供nginx进行处理
proxy_buffers 4 32k; #同上,告诉nginx保存单个用几个buffer最大用多少空间
proxy_busy_buffers_size 64k; #如果系统很忙时候可以申请最大的proxy_buffers
proxy_temp_file_write_size 64k; #proxy缓存临时文件的大小

log_format access ‘$remote_addr – $remote_user [$time_local] “$request” ‘
‘$status $body_bytes_sent “$http_referer” ‘
‘”$http_user_agent” $http_x_forwarded_for’;
access_log /opt/nginx/logs/access.log access;

##max_fails = 3 为允许失败的次数,默认值为1
##fail_timeout = 30s 当max_fails次失败后,暂停将请求分发到该后端服务器的时间
upstream ylx_api {
ip_hash;
server 192.168.1.124:80 max_fails=3 fail_timeout=30s;
server 192.168.1.125:80 max_fails=3 fail_timeout=30s;
}

upstream yc_api {
ip_hash;
server 192.168.1.124:80 max_fails=3 fail_timeout=30s;
server 192.168.1.125:80 max_fails=3 fail_timeout=30s;
}

server {
listen 80;
server_name ylxapi.linuxsee.com;

location / {
proxy_next_upstream http_502 http_504 error timeout invalid_header; #如果后端返回502、504、超时自动转发到负载均衡池
proxy_pass http://ylx_api;
proxy_set_header Host ylxapi.linuxsee.com;
proxy_set_header X-Forwarded-For $remote_addr;
}

access_log /opt/nginx/logs/ylxapi.linuxsee.com_access.log access;
}

server {
listen 80;
server_name ycapi.linuxsee.com;

location / {
proxy_pass http://yc_api;
proxy_set_header Host $host;
}

access_log /opt/nginx/logs/ ycapi.linuxsee.com_access.log access;
}
}
3.keepalived配置
tar zxvf keepalived-1.1.15.tar.gz
cd keepalived-1.1.15
./configure –prefix=/opt/keepalived –with-kernel-dir=/usr/src/kernels/2.6.18-194.el5-x86_64 && make && make install
keepalived在同一virtual_router_id中priority(0-255)最大的会成为master,也就是接管VIP,当priority最大的主机发生故障后次priority将会接管,对于以下配置,3台主机的keepalived每个实例中priority分别为serverA(200,180,160),serverB(160,200,180),serverC(180,160,200),当serverA发生故障后serverC接管VIP,serverB发生故障后serverA接管,serverC发生故障后serverB接管;

由于keepalived只检测本机和他机keepalived是否正常并实现VIP的漂移,而如果本机nginx出现故障不会则不会漂移VIP,所以编写脚本来判断本机nginx是否正常,如不正常则关闭keepalived,其他主机此时会接管VIP;

serverA配置:
vi /opt/keepalived/etc/keepalived/check_http.sh
#!/bin/bash
url=”http://192.168.1.10/index.html
status=$(/usr/bin/curl -s –head “$url” | awk ‘/HTTP/ {print $2}’)
if [ "$status" != "200" ]; then
pkill keepalived
fi
vi /opt/keepalived/etc/keepalived/keepalived.conf.nginx.3vip
global_defs {
router_id Nginx_Id_1
}

vrrp_script chk_nginx {
script “/opt/keepalived/etc/keepalived/check_http.sh” #定义检测脚本
interval 2 #检测间隔
weight 2
}

vrrp_instance Nginx1 { #定义一个实例
state MASTER #定义为master
interface eth0
virtual_router_id 138 # 0-255 在同一个instance 中一致在整个vrrp 中唯一
priority 200 #优先级,优先级最大的会成为master

authentication {
auth_type PASS
auth_pass 1111
}
track_script { #检查脚本
chk_nginx
}
virtual_ipaddress { #此实例的浮动IP
192.168.1.110
}
}

vrrp_instance Nginx2 {
state BACKUP
interface eth0
virtual_router_id 139
priority 180

authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.120
}
}

vrrp_instance Nginx3 {
state BACKUP
interface eth0
virtual_router_id 140
priority 160

authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.130
}
}
serverB配置:
vi /opt/keepalived/etc/keepalived/check_http.sh
#!/bin/bash
url=”http://192.168.1.20/index.html”
status=$(/usr/bin/curl -s –head “$url” | awk ‘/HTTP/ {print $2}’)
if [ "$status" != "200" ]; then
pkill keepalived
fi
vi /opt/keepalived/etc/keepalived/keepalived.conf.nginx.3vip
global_defs {
router_id Nginx_Id_2
}

vrrp_script chk_nginx {
script “/opt/keepalived/etc/keepalived/check_http.sh”
interval 2
weight 2
}

vrrp_instance Nginx1 {
state BACKUP
interface eth0
virtual_router_id 138
priority 160

authentication {
auth_type PASS
auth_pass 1111
}

virtual_ipaddress {
192.168.1.110
}
}

vrrp_instance Nginx2 {
state MASTER
interface eth0
virtual_router_id 139
priority 200

authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_nginx
}

virtual_ipaddress {
192.168.1.120
}
}

vrrp_instance Nginx3 {
state BACKUP
interface eth0
virtual_router_id 140
priority 180

authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.130
}
}
serverC配置:
vi /opt/keepalived/etc/keepalived/check_http.sh
#!/bin/bash
url=”http://192.168.1.30/index.html”
status=$(/usr/bin/curl -s –head “$url” | awk ‘/HTTP/ {print $2}’)
if [ "$status" != "200" ]; then
pkill keepalived
fi
vi /opt/keepalived/etc/keepalived/keepalived.conf.nginx.3vip
global_defs {
router_id Nginx_Id_3
}

vrrp_script chk_nginx {
script “/opt/keepalived/etc/keepalived/check_http.sh”
interval 2
weight 2
}

vrrp_instance Nginx1 {
state BACKUP
interface eth0
virtual_router_id 138
priority 180

authentication {
auth_type PASS
auth_pass 1111
}

virtual_ipaddress {
192.168.1.110
}
}

vrrp_instance Nginx2 {
state BACKUP
interface eth0
virtual_router_id 139
priority 160

authentication {
auth_type PASS
auth_pass 1111
}

virtual_ipaddress {
192.168.1.120
}
}

vrrp_instance Nginx3 {
state MASTER
interface eth0
virtual_router_id 140
priority 200

authentication {
auth_type PASS
auth_pass 1111
}

track_script {
chk_nginx
}

virtual_ipaddress {
192.168.1.130
}
}

转载请保留固定链接: http://www.linuxeye.com/configuration/1859.html


































































































































































































































以上是关于nginx负载均衡+keepalived三主(多主)配置的主要内容,如果未能解决你的问题,请参考以下文章

Nginx负载均衡+Keepalived高可用集群

Nginx负载均衡+keepalived高可用

nginx负载均衡+keepalived高可用

keepalived+nginx双机热备+负载均衡

nginx+keepalived实现高可用负载均衡

负载均衡Nginx+KeepAlived