nginx+keepalived安装配置(整理中)
线上环境安装测试说明两台测试机
nginx+keepalived做后端LNMP及tomcat+jdk+mysql的负载高可用
测试环境
centos 6.2 x86_64
IP:192.168.101.77
vip:10.10.10.8
centos 6.2 x86_64
IP:192.168.101.88
vip:10.10.10.8
由于测试环境,两台都安装同样的服务
安装过程
192.168.101.77和192.168.101.88 安装nginx+keepalived
安装依赖包
yum -y install cmake gcc gcc-c++ make zlib zlib-devel openssl* popt popt-devel pcre pcre-devel
安装pcre
# tar jxvf pcre-8.20.tar.bz2
# cd pcre-8.20
# ./configure --prefix=/usr/local/app/pcre
# make && make install
# cd ..
安装nginx
# tar zxvf nginx-1.5.6.tar.gz
# cd nginx-1.5.6
# ./configure --prefix=/usr/local/app/lbnginx --with-pcre=/usr/src/pcre-8.20
# make
# make install
# cd ..
安装keepalived
# tar zxvf keepalived-1.2.8.tar.gz
# cd keepalived-1.2.8
# ./configure --prefix=/usr/local/app/keepalived
# make
# make install
配置文件,启动脚本
# cp /usr/local/app/keepalived/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
# cp /usr/local/app/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
# mkdir /etc/keepalived
# cp /usr/local/app/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
# cp /usr/local/app/keepalived/sbin/keepalived /usr/sbin/
# chkconfig --level 2345 keepalived on
# service keepalived start
Starting keepalived:
# ip a
1: lo:mtu 16436 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0:mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:c5:78:f3 brd ff:ff:ff:ff:ff:ff
inet 192.168.101.77/24 brd 192.168.101.255 scope global eth0
inet 192.168.101.7/32 scope global eth0
inet 10.10.10.8/32 scope global eth0 #查看VIP是否启动
inet6 fe80::20c:29ff:fec5:78f3/64 scope link
valid_lft forever preferred_lft forever
192.168.101.77 keepalived配置,192.168.101.88配置一样只是换一下IP
# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
leeforget@test.com
}
notification_email_from leeforget@test.com
smtp_server 192.168.101.77
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
mcast_src_ip 192.168.101.77
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass testpass
}
virtual_ipaddress {
10.10.10.8
}
}
nginx 配置
# cat nginx.conf
userwww;
worker_processes4;
worker_rlimit_nofile 65535;
error_log /var/log/lbnginx/error.log;
#error_loglogs/error.loginfo;
#pid logs/nginx.pid;
events {
use epoll;
worker_connections102400;
}
http {
include mime.types;
default_typeapplication/octet-stream;
sendfile on;
tcp_nopush on;
tcp_nodelay off;
gzipon;
gzip_comp_level 1;
gzip_proxied any;
gzip_types text/plain text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript
keepalive_timeout30;
log_format access '$remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent $request_body "$http_referer" "$http_user_agent" $http_x_forwarded_for';
#for sites enabled
include/usr/local/app/lbnginx/conf/lb-enabled/*;
}
一个测试server的配置文件
#gy wwww.mytest.com
upstream mytest10000 {
# simple round-robin
server 192.168.101.77:10000;
server 192.168.101.88:10000;
}
server {
listen 80;
server_name mytest.com;
access_log /var/log/lbnginx/mytest_access.log access;
location ~ / {
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header HTTP_HOST $server_name;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 50m;
client_body_buffer_size 256k;
proxy_connect_timeout 30;
proxy_send_timeout 30;
proxy_read_timeout 60;
proxy_buffer_size 256k;
proxy_buffers 4 256k;
proxy_busy_buffers_size 256k;
proxy_temp_file_write_size 256k;
proxy_next_upstream error timeout invalid_header http_500 http_503;
proxy_max_temp_file_size 128m;
proxy_pass http://mytest10000;
}
}
测试
本机添加一个10.10.10.9的地址
安装过程中报错及解决办法
1. # ./configure --prefix=/usr/local/app/lbnginx
错误提示PCRE库
./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre= option.
安装pcre
# tar jxvf pcre-8.20.tar.bz2 ^C
# cd pcre-8.20^C
# cd pcre-8.20
# ./configure --prefix=/usr/local/pcre
# ./configure --prefix=/usr/local/app/lbnginx --with-pcre=/usr/local/pcre/
还是PCRE报错
# make
make -f objs/Makefile
make: Entering directory `/usr/src/nginx-1.5.6'
cd /usr/local/pcre/lib/ \
&& if [ -f Makefile ]; then make distclean; fi \
&& CC="cc" CFLAGS="-O2 -fomit-frame-pointer -pipe " \
./configure --disable-shared
/bin/sh: line 2: ./configure: No such file or directory
make: *** Error 127
make: Leaving directory `/usr/src/nginx-1.5.6'
make: *** Error 2
google了一下应该制定pcre源码目录
# ./configure --prefix=/usr/local/app/lbnginx --with-pcre=/usr/src/pcre-8.20
#make && make install
安装keepalived
# tar zxvf keepalived-1.2.8.tar.gz
# cd keepalived-1.2.8
# ./configure --prefix=/usr/local/app/keepalived
2. # ./configure --prefix=/usr/local/app/keepalived
报错
configure: error: Popt libraries is required
# yum -y install popt popt-devel
# ./configure --prefix=/usr/local/app/keepalived
# make && make install
页:
[1]