今天做了一个实验,nginx+keepalived实现双机热备份,由于自己也是新手,所以刚开始也遇到了很多的问题,虽然原理是看懂了,但是在操作过程中还是遇到了很多的问题,在此写这篇博客记录下操作过程的一些步骤以及问题记录下来。
在本博客中我也转载了一篇nginx+keepalived实现双机热备份,那篇文章的思想及原理写的很好,还有那个构架图,可以供参考,下来我就来贴出我实验过程的一些步骤吧:
在此如何安装机子的环境和软件安装这里就不做介绍了,这里只贴出一些步骤和配置文件:
一、实验准备:
front1:nginx机器 IP:192.168.1.26 (keepalived master)
front2: nginx机器 IP: 192.168.1.27 (keepalived backup)
VIP:192.168.1.50
后端机子:一台安装tomcat(IP:192.168.1.11) 一台安装 php(IP:192.168.1.17)
二、实验目的:
本实验的目的是利用nginx做反向代理,负载均衡,实现利用前端机子的nginx实现动态分离的效果,让.jsp文件丢给后端的tomcat机子处理,让.php和.html文件丢个后端的php机子处理,这样可以减少前端的负载,实现负载均衡,同时我们也需要在前端搭建两台front,目的是实现高可用,稳定性,实现故障转移,当前端front一台主挂掉的情况下在5秒内自动切换到从上,避免单点故障的发生
三、参考架构图(图为转载)
四、实验步骤:
准备四台机子,两台做前端(front),两台后端机子,一台运行tomcat,一台运行php
各自搭建好环境,这里环境搭建就不介绍了,
搭建好环境之后,我们来配置front机子的nginx配置文件:
- # vim /usr/local
- upstream test{
- server 172.28.14.142:81;
- }
- upstream tomcat {
- server 192.168.1.11:8080;
- }
- upstream nginx {
- server 192.168.1.17:80;
- }
- ############# server ################
- server {
- listen 80;
- server_name www.abc.com;
- root /usr/local/www/abc;
- index index.php index.jsp index.do index.html index.htm;
- location / {
- proxy_set_header Host $host;
- proxy_set_header X-Real-IP $remote_addr;
- proxy_set_header X-Forwarded-For $remote_addr;
- include fastcgi_params;
- proxy_pass http://nginx;
- }
- location ~ .*\.(php|php5)?$ {
- proxy_pass http://nginx;
- root /usr/local/www/abc;
- include fastcgi_params;
- index index.php;
- proxy_set_header Host $host;
- proxy_set_header X-Forwarded-For $remote_addr;
- include /usr/local/nginx/conf/proxy_store_off.conf;
- }
- location ~ .*\.(jsp|do|jspx)?$ {
- proxy_pass http://tomcat;
- proxy_set_header Host $host;
- proxy_set_header X-Forwarder-For $remote_addr;
- include /usr/local/nginx/conf/proxy_store_off.conf;
- }
- location ~ .*\.(html|htm)?$ {
- index index.html;
- proxy_pass http://nginx;
- proxy_set_header Host $host;
- proxy_set_header X-Forwarded-For $remote_addr;
- expires 1h;
- }
- location ~ .*\.(jpg|jpeg|gif|png|bwp|swf)?$ {
- expires 30d;
- }
- location ~ .*\.(css|js)?$ {
- expires 1h;
- }
- access_log /var/log/www.abc.com.log main;
- }
- # vim /usr/local/nginx/conf/proxy_store_off.conf
- proxy_redirect off;
- proxy_set_header Host $host;
- proxy_set_header X-Real-IP $remote_addr;
- proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
- client_max_body_size 50m;
- client_body_buffer_size 256k;
- proxy_connect_timeout 300;
- proxy_send_timeout 300;
- proxy_read_timeout 300;
- proxy_buffer_size 32k;
- proxy_buffers 4 64k;
- proxy_busy_buffers_size 128k;
- proxy_temp_file_write_size 128k;
- proxy_next_upstream error timeout invalid_header http_500 http_503 http_404;
- proxy_max_temp_file_size 128m;
- proxy_store off;
前端front两台的nginx配置文件完全一样,配置完后scp到另外一台,
如:scp nginx.conf [email protected]:/usr/local/nginx/conf/
接下来我们来为前端front机子配置keepalived,keepalived安装这里介绍一下,
- #编译安装keepalived
- #tar zxvf keepalived-1.2.2.tar.gz
- #cd keepalived-1.2.2
- # ./configure --sysconf=/etc \
- --with-kernel-dir=/usr/src/kernels/2.6.18-8.e15-i686
- # make
- #make install
- # ln -s /usr/local/sbin/keepalived /sbin
安装完后,我们需要给keepalived配置,配置文件如下:
- ! Configuration File for keepalived
- global_defs {
- notification_email {
- [email protected]
- }
- notification_email_from 244896418.com
- smtp_server 127.0.0.1
- smtp_connect_timeout 30
- router_id LVS_DEVEL
- }
- vrrp_script chk_http_port {
- script "/root/sh/nginx_pid.sh"
- interval 2
- weight 2
- }
- vrrp_instance VI_1 {
- state MASTER (从服务器改为BACKUP)
- interface eth0
- virtual_router_id 51
- mcast_src_ip 192.168.1.26 (此IP为本机的IP)
- priority 100 (从服务器的改成小于100就可以)
- advert_int 1
- authentication {
- auth_type PASS
- auth_pass 1111
- }
- track_script {
- chk_http_port
- }
- virtual_ipaddress {
- 192.168.1.50
- }
- }
- 标记为红色的为自己写的一个脚本,用来检测本机的nginx是否正常的运行,如果不nginx挂掉试着重新启动,如果启动后
- 又挂掉,那么就直接杀掉keepalived进程,将转移到keepalived从服务器上,实现故障转移
- # vim nginx_pid.sh
- #!/bin/bash
- 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
- killall keepalived ## 则结束 keepalived 进程
- fi
- fi
- # chmod +x nginx_pid.sh
- 前端front从服务器的配置也完全一样,只是在keepalived的配置文件中需要修改几个地方就可以
到此,前端的配置完成,如果没有出错的话都可以实现,接下来就来提出后端机子的nginx配置,其他的就不再做介绍了
- server {
- listen 80;
- server_name www.abc.com;
- root /usr/local/www/abc;
- index index.php index.jsp index.do index.html index.htm;
- location / {
- root /usr/local/www/abc;
- index index.html index.php index.jsp index.do default.do;
- proxy_set_header Host $host;
- proxy_set_header X-Real-IP $remote_addr;
- proxy_set_header X-Forwarded-For $remote_addr;
- include fastcgi_params;
- include /usr/local/nginx/conf/proxy_store_off.conf;
- }
- location ~ .*\.(php|php5)?$ {
- root /usr/local/www/abc;
- include fastcgi_params;
- index index.php;
- proxy_set_header Host $host;
- proxy_set_header X-Forwarded-For $remote_addr;
- include /usr/local/nginx/conf/proxy_store_off.conf;
- fastcgi_pass 127.0.0.1:9000;
- fastcgi_index index.php;
- fastcgi_param SCRIPT_FILENAME /usr/local/www/abc$fastcgi_script_name;
- fastcgi_param QUERY_STRING $query_string;
- fastcgi_param REQUEST_METHOD $request_method;
- fastcgi_param CONTENT_TYPE $content_type;
- fastcgi_param CONTENT_LENGTH $content_length;
- fastcgi_param REQUEST_URI $request_uri;
- }
- location ~ .*\.(jsp|do|jspx)?$ {
- proxy_pass http://tomcat;
- root /usr/local/www/abc;
- proxy_set_header Host $host;
- proxy_set_header X-Forwarder-For $remote_addr;
- include /usr/local/nginx/conf/proxy_store_off.conf;
- }
- location ~ .*\.(html|htm)?$ {
- root /usr/local/www/abc;
- index index.html ;
- proxy_set_header Host $host;
- proxy_set_header X-Forwarded-For $remote_addr;
- expires 1h;
- }
- location ~ .*\.(jpg|jpeg|gif|png|bwp|swf)?$ {
- expires 30d;
- }
- location ~ .*\.(css|js)?$ {
- expires 1h;
- }
- access_log /var/log/www.abc.com.log.log main;
- }
- }
接下来可以来启动下nginx和keepalived进行测试结果,首先我们来启动keepalived主服务器的,并且查看日志:# service keepalived start
- Jan 10 16:10:16 localhost Keepalived_vrrp: VRRP_Script(chk_http_port) succeeded
- Jan 10 16:10:17 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Transition to MASTER STATE
- Jan 10 16:10:18 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Entering MASTER STATE
- Jan 10 16:10:18 localhost Keepalived_vrrp: VRRP_Instance(VI_1) setting protocol VIPs.
- Jan 10 16:10:18 localhost avahi-daemon[3490]: Registering new address record for 192.168.1.50 on eth0.
- Jan 10 16:10:18 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.1.50
从服务器keepalived日志
- Jan 10 16:11:48 front2 Keepalived_vrrp[15836]: VRRP_Instance(VI_1) Entering BACKUP STATE
- Jan 10 16:11:48 front2 Keepalived_vrrp[15836]: VRRP sockpool: [ifindex(2), proto(112), fd(11,12)]
- Jan 10 16:11:48 front2 Keepalived_vrrp[15836]: VRRP_Script(chk_http_port) succeeded
接下来可以自己杀掉主的nginx进程看是否会自动启动nginx,也可以测试停掉主的keepalived进程,查看是否从keepalived日志有接管过来,在查看nginx日志
到此结束,由于自己能力有限,只能给大家提供这些参考了,哪里有错误的还请大家指正,谢谢