使用nginx+keepalived实现高性能集群
搭建环境概述:拓扑图: 不知怎么的图片今天发不了啊!如果有需要的可以联系我。
各个应该功能描述:这里使用nginx作负载均衡和web代理,用keepalived做双机互备
nginx_MASTER:192.168.1.100
nginx_BACKUP:192.168.1.101
VIP:192.168.1.118
web1:192.168.1.131
web2:192.168.1.132
这里主要讲解集群和主从备份配置,对于web服务器和数据库配置略过,如有需要帮助的可以给我留言,或联系我都可以。
<多话不说,直接来操作流程,如有疑问可以直接联系我qq:448987046>
一、nginx安装配置(两台服务器完全一样)
1、nginx安装(分别在两台服务器上执行以下操作)
# wget http://nginx.org/download/nginx-1.2.0.tar.gz
# tar -zxvf nginx-1.2.0.tar.gz
# cd nginx-1.2.0
# ./configure --prefix=/usr/local/nginx
# make && make install
注意:安装过程中可能需要安装一些其他的库(如:pcre),按照他的提示安装就行了
2、nginx配置(两台服务器配置一样)
nginx配置文件在 /usr/local/nginx/conf/nginx.conf,下面是nginx完整配置
user nobody nobody;
worker_processes 1;
pid /usr/local/nginx/logs/nginx.pid;
worker_rlimit_nofile 51200;
events
{
use epoll;
worker_connections 51200;
}
http{
include mime.types;
default_typeapplication/octet-stream;
#log_formatmain'$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_loglogs/access.logmain;
sendfile on;
#tcp_nopush on;
#keepalive_timeout0;
keepalive_timeout65;
#gzipon;
#include /usr/local/nginx/conf/proxy.conf;
upstream backend
{
ip_hash;
server 192.168.1.131:8080;
server 192.168.1.132:8080;
}
server {
listen 1111;
server_name localhost;
location / {
index index.html;
proxy_pass http://backend;
proxy_redirect off;
proxy_set_header Host $host:1111;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 100m;
client_body_buffer_size 128k;
proxy_connect_timeout 10;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}
#location /nginx {
#access_log on;
#auth_basic "NginxStatus";
#auth_basic_user_file /usr/local/nginx/htpasswd;
#}
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 /var/log/access.log access;
}
}
附带nginx下载地址http://nginx.org/en/download.html,你可以在这里找到合适你环境内nginx版本
二、keepalived安装配置
1、keepalived安装 (两台服务器安装过程一样)
安装keepalived之前确保i的openssl正确安装,同时需要ipvsadm支持
# wget http://www.keepalived.org/software/keepalived-1.2.2.tar.gz
# ./configure --prefix=/usr/local/keepalived
# make && make install
# cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
# cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
# mkdir /etc/keepalived
# cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
# cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
这是安装就完成了,可以使用下面命了启动
# service keepalived start 或 # /etc/init.d/keepalived start
2、keepalived配置 (两台服务器配置略有不同,一台主服务器,一台从服务器)
a、主服务器配置
! Configuration File for keepalived
!nginx 监控脚本
vrrp_script chk_http_port {
script "/usr/local/keepalived/nginx_pid.sh"
interval 2
weight 2
}
vrrp_instance VI_INET1 {
state MASTER
interface eth0
virtual_router_id 53
priority 200
advert_int 1
authentication {
auth_type pass
auth_pass yourpass
}
track_script {
chk_http_port
}
virtual_ipaddress {
192.168.1.118
}
}
b、从服务器配置
! Configuration File for keepalived
!nginx 监控脚本
vrrp_script chk_http_port {
script "/usr/local/keepalived/nginx_pid.sh"
interval 2
weight 2
}
vrrp_instance VI_INET1 {
state BACKUP
interface eth0
virtual_router_id 53
priority 199
advert_int 1
authentication {
auth_type pass
auth_pass yourpass
}
track_script {
chk_http_port
}
virtual_ipaddress {
192.168.1.118
}
}
c、最后还有一个监听脚本配置 /usr/local/keepalived/nginx_pid.sh (两台服务器配置一样)
# vim /usr/local/keepalived/nginx_pid.sh 监听脚本内容如下:
#!/bin/bash
# varsion 0.0.2
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
service keepalived stop ## 则结束 keepalived 进程
fi
fi
至此,我们的安装和配置过程,全部完成,剩下的就只是测试验证了。
测试验证可以参考此文:http://deidara.blog.iyunv.com/400447/302402
本文参考文章:http://deidara.blog.iyunv.com/400447/302402
工作之余,来点娱乐(大家拍砖):小窝影院 www.xwdy.cc
欢迎大家拍砖,留言,讨论!
页:
[1]