1. 程式人生 > >nginx +keepalived

nginx +keepalived

nginx keepalived

nginx +keepalived 自己搭建

規劃

準備兩臺服務器192.168.10.199/192.168.10.200199上安裝tomcat1nginxkeepalived主;200上安裝tomcatnginxkeepalived備;192.168.10.230作為浮點ip

部署

1部署tomcat

tar xzvf apache-tomcat-7.0.59.tar

./jdk-6u37-linux-x64-rpm.bin

2 安裝nginx

yum install pcre-devel pcre -y

yum install gcc pcre pcre-devel zlib zlib-devel openssl openssl-devel

#下載Nginx源碼包

cd /usr/src

wget -c http://nginx.org/download/nginx-1.6.2.tar.gz

#解壓Nginx源碼包

tar -xzf nginx-1.6.2.tar.gz

#進入解壓目錄,然後sed修改Nginx版本信息為WS

cd nginx-1.6.2 ; sed -i -e ‘s/1.6.2//g‘ -e ‘s/nginx\//WS/g‘ -e

‘s/"NGINX"/"WS"/g‘ src/core/nginx.h

#預編譯Nginx

useradd www ;./configure --user=www --group=www --prefix=/usr/local/nginx --with-

http_stub_status_module --with-http_ssl_module

(由於是系統是min的centos遇到如下問題,一般不會出問題:

1 ./configure: error: SSL modules require the OpenSSL library.

You can either do not enable the modules, or install the OpenSSL library

into the system, or build the OpenSSL library statically from the source

with nginx by using --with-openssl=<path> option.

解決方法:

1 )yum -y install openssl-libs偷懶的辦法:yum -y install openssl* (本地yum源安裝不了),後面發現可以用yum -y install openssl-devel安裝

2) 借鑒網站http://www.centoscn.com/nginx/2015/0304/4782.html

wget http://www.openssl.org/source/openssl-1.0.2.tar.gz

tar zxf openssl-1.0.2.tar.gz

./configure --user=www --group=www --prefix=/usr/local/nginx --with-

http_stub_status_module --with-http_ssl_module --with-openssl=/home/openssl-1.0.2

2 用方法2後,又遇到問題:

Operating system: x86_64-whatever-linux2 You need Perl 5.

解決方法:

安裝開發工具:

yum -y groupinstall "Development Tools"yum -y groupinstall "Development Tools"

#.configure預編譯成功後,執行make命令進行編譯

make

#make執行成功後,執行make install 正式安裝

make install

#自此Nginx安裝完畢

/usr/local/nginx/sbin/nginx -t 檢查nginx配置文件是否正確,返回OK即正確。

[[email protected] ~]# /usr/local/nginx/sbin/nginx -t

nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok

nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

[[email protected] ~]#

然後啟動nginx,/usr/local/nginx/sbin/nginx 回車即可。查看進程是否已啟動:

[[email protected] ~]# ps -ef |grep nginx

nobody 5381 30285 0 May16 ? 00:04:31 nginx: worker process

root 30285 1 0 2014 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx

root 32260 32220 0 12:34 pts/0 00:00:00 grep nginx

[[email protected] ~]#

1 Nginx常用命令管理及升級

查看nginx進程

ps -ef|grep nginx

說明:nginx的進程由主進程和工作進程組成。

啟動nginx

nginx

啟動結果顯示nginx的主線程和工作線程,工作線程的數量跟nginx.conf中的配置參數worker_processes有關。

平滑啟動nginx

kill -HUP `cat /var/run/nginx.pid`

或者

nginx -s reload

其中進程文件路徑在配置文件nginx.conf中可以找到。

平滑啟動的意思是在不停止nginx的情況下,重啟nginx,重新加載配置文件,啟動新的工作線程,完美停止舊的工作線程。

完美停止nginx

kill -QUIT `cat /var/run/nginx.pid`

快速停止nginx

kill -TERM `cat /var/run/nginx.pid`

或者

kill -INT `cat /var/run/nginx.pid`

完美停止工作進程(主要用於平滑升級)

kill -WINCH `cat /var/run/nginx.pid`

強制停止nginx

pkill -9 nginx

檢查對nginx.conf文件的修改是否正確

nginx -t -c /etc/nginx/nginx.conf 或者 nginx -t

停止nginx的命令

nginx -s stop或者pkill nginx

查看nginx的版本信息

nginx -v

查看完整的nginx的配置信息

nginx -V

3 安裝keepalived

Keepalived 安裝

-----------------------

地址: http://www.keepalived.org/download.html

wget http://www.keepalived.org/software/keepalived-1.2.7.tar.gz

tar -zxf keepalived-1.2.7.tar.gz

cd keepalived-1.2.7

64 位系統:

./configure --sysconf=/etc --prefix=/usr/local/keepalived --with-kernel-dir=/usr/src/kernels/2.6.32-358.2.1.el6.x86_64/

32 位系統:

./configure --sysconf=/etc --prefix=/usr/local/keepalived --with-kernel-dir=/usr/src/kernels/2.6.32-358.6.2.el6.i686/

註意:Configure遇到報錯: !!! OpenSSL is not properly installed on your system. !!!

可通過 getconf LONG_BIT 得到系統位數。

參數解釋:

--sysconf 指定了配置文件的地址.即:/etc/keepalived/keepalived.conf

--prefix 指定了安裝目錄

--with-kernel-dir 指定使用內核源碼中的頭文件,include 目錄.只有使用 LVS 時才需要這個參數,其它的時候不需要。

(遇到報錯: !!! OpenSSL is not properly installed on your system. !!!

!!! Can not include OpenSSL headers files.

解決辦法:yum install -y openssl openssl-devel

解決:

configue .成功後提示:

Keepalived configuration

------------------------

Keepalived version : 1.2.7

Compiler : gcc

Compiler flags : -g -O2

Extra Lib : -lpopt -lssl -lcrypto

Use IPVS Framework : Yes

IPVS sync daemon support : Yes

IPVS use libnl : No

Use VRRP Framework : Yes

Use VRRP VMAC : Yes

SNMP support : No

Use Debug flags : No

安裝:

make

make install

設置成為服務並開機啟動:

cp /usr/local/keepalived/sbin/keepalived /usr/sbin/

/etc/rc.d/init.d/keepalived status

chkconfig --add keepalived

chkconfig keepalived on

註意:cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf 沒有指向這個命令,配置文件要在特定目錄下

4 配置nginx

/usr/local/nginx/conf/nginx.conf

#user www www;

user nobody;

#worker_processes 1;

worker_processes auto;

#error_log logs/error.log;

#error_log logs/error.log notice;

#error_log logs/error.log info;

error_log /home/nginx/logs/nginx_error.log crit; #全局錯誤日誌及PID文件

pid /home/nginx/nginx.pid;

#pid logs/nginx.pid;

events {

use epoll;

worker_connections 65535; #工作模式及連接數上限

}

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" ‘

large_client_header_buffers 4 32k;

client_body_buffer_size 8m; #256k

server_tokens off;

ignore_invalid_headers on;

recursive_error_pages on;

server_name_in_redirect off;

sendfile on;

#timeouts

keepalive_timeout 60;

#client_body_timeout 3m;

#client_header_timeout 3m;

#send_timeout 3m;

#fastcgi_connect_timeout 60;

#fastcgi_send_timeout 180;

#fastcgi_read_timeout 180;

#fastcgi_buffers 4 256k;

#fastcgi_buffer_size 128k;

#fastcgi_busy_buffers_size 256k;

#fastcgi_temp_file_write_size 256k;

#fastcgi_intercept_errors on;

#TCP Options

tcp_nopush on;

tcp_nodelay on;

#size limits

client_max_body_size 50m;

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;

proxy_redirect off;

proxy_set_header Host $host;

proxy_set_header X-Real-IP $remote_addr; #獲取真實ip

#proxy_connect_timeout 90;

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

proxy_connect_timeout 90;

proxy_read_timeout 180;

proxy_send_timeout 180;

proxy_buffer_size 256k;

proxy_buffers 8 256k;

proxy_busy_buffers_size 256k;

proxy_temp_file_write_size 256k;

upstream myserver {

server 192.168.10.199:8080 weight=10 max_fails=0;

server 192.168.10.200:8080 weight=10 max_fails=0; #8080tomcat端口

}

server {

listen 9090;

# server_name 192.168.10.230;

#charset koi8-r;

#access_log logs/host.access.log main;

location /

{

#root html;

#index index.html index.htm;

index index.jsp;

proxy_pass http://myserver; #轉向tomcat處理

#proxy_set_header Host $host;

proxy_set_header Host $host:$server_port;

proxy_set_header X-Real-IP $remote_addr;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

}

}

}

註意 server_name 192.168.10.230;

如果nginx中只配置一個server域的話,則nginx是不會去進行server_name的匹配的。因為只有一個server域,也就是這有一個虛擬主機,那麽肯定是發送到該nginx的所有請求均是要轉發到這一個域的,即便做一次匹配也是沒有用的。還不如幹脆直接就省了。如果一個http域的server域有多個,nginx才會根據$hostname去匹配server_name進而把請求轉發到匹配的server域中。此時的匹配會按照匹配的優先級進行,一旦匹配成功進不會再進行匹配,關於具體的匹配規則可以參見nginx官網提供的文檔。

5 配置keepalived

#######MASTER#####################

! Configuration File for keepalived

global_defs {

notification_email {

[email protected]

}

notification_email_from [email protected]

smtp_server 127.0.0.1

router_id LVS_DEVEL

}

vrrp_instance VI_1 {

state MASTER #BACKUP

interface ens160 #對外網口

virtual_router_id 22

priority 100 #BACKUP上修改為88

advert_int 1

authentication {

auth_type PASS

auth_pass 7777

}

virtual_ipaddress {

192.168.10.230 #虛擬ip

}

}

6 編輯nginx檢測腳本

/home/nginx/ nginx_pid.sh

#!/bin/bash

while :

do

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

if [ $nginxpid -eq 0 ];then

/usr/local/nginx/sbin/nginx

sleep 5

if [ $nginxpid -eq 0 ];then

/etc/init.d/keepalived stop

fi

fi

sleep 5

done

nohup /bin/bash /root/nginx_pid.sh &

檢測

1 網頁測試

/home/apache-tomcat-7.0.59/webapps下新建文件夾test,再在test下面新建個文本,就可以直接通過http://192.168.10.199/test/a 訪問測試。

2查看keepalived直接是否互相通信

tcpdump -v -i ens160 host 192.168.10.199 查看是否有vrrp

cat /var/log/messages

3 查看keepalived是否能接換

斷掉nginxkeepalived檢測,通過ip add查看浮點ip是否漂移

借鑒網站

http://blog.sina.com.cn/s/blog_5f54f0be0101eyff.html keepalived建議主要按這個)

https://wenku.baidu.com/view/4011c9de7cd184254b3535d3.html(安裝nginx看這個)

http://freeloda.blog.51cto.com/2033581/1189143 nginx檢測腳本)

http://www.oschina.net/question/922543_91357?sort=time server name 無效)

http://www.linuxidc.com/Linux/2013-07/88025.htm (檢測兩臺keepalived直接的通信)

http://freeloda.blog.51cto.com/2033581/1189130 (下次可按這個博客一步步操作)

問題:我安裝虛擬ip一直沒綁上

原因:cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf 沒有指向這個命令,配置文件要在特定目錄下

keepalived 放通端口

/sbin/iptables -I INPUT -i eth0 -d 224.0.0.0/8 -j ACCEPT
/sbin/iptables -A INPUT -p 112 -i eth0 -j ACCEPT
/sbin/iptables -A OUTPUT -p 112 -o eth0 -j ACCEPT


nginx +keepalived