Nginx+Keepalived负载均衡+后端LNMP网站集群

Centos6.4 x86,4台,地址是10.10.10.11-14,vip给的100,目标是在13和14安装nginx+keepalived,11和12安装nginx+mysql+php,做为web,以下为配置步骤

 

1,安装epel源。

rpm -Uvh http://mirrors.kernel.org/fedora-epel/6/i386/epel-release-6-8.noarch.rpm

编辑/etc/yum.repos.d/epel.repo,把基础的恢复,镜像的地址注释掉,就是

#baseurl

mirrorlist

改成

baseurl

#mirrorlist

 

# yum repolist//出现一下输出表示成功

repo id           repo name                                              status

base              CentOS-6 - Base                                         6,367

epel              Extra Packages for Enterprise Linux 6 - i386         10,335

………

repolist: 17,600

 

2,四台机器全部执行下面命令,一步到位安装nginx,php,mysql

#yum -y install gcc pcre-devel zlib-devel openssl-devel nginx mysql mysql-server php-mysql php-gd libjpeg* php-imap php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-mcrypt php-bcmath php-mhash libmcrypt libmcrypt-devel php-fpm

 

service nginx start

chkconfig  nginx on    #设为开机启动

 

chkconfig mysqld on  

cp /usr/share/mysql/my-medium.cnf   /etc/my.cnf

 

/usr/bin/mysqladmin -u root password '123456'       #设置密码

/etc/init.d/mysqld start  

 

chkconfig php-fpm on

vi /etc/php-fpm.d/www.conf  

user = nginx   #修改用户为nginx

group = nginx

service php-fpm  start

 

vi /etc/nginx/conf.d/default.conf

 

index  index.php index.html index.htm;   #增加

 

   location ~ \.php$ {

    root           html;

    fastcgi_pass   127.0.0.1:9000;

    fastcgi_index  index.php;

    fastcgi_param  SCRIPT_FILENAME   $document_root$fastcgi_script_name;

    include        fastcgi_params;

  }

 

service mysqld restart

service nginx  restart

访问phpinfo(),正常。

 

3,下面开始配置负载均衡,只在13和14这两台机器进行

先安装keepalived

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

tar xzf keepalived-1.2.15.tar.gz

cd keepalived-1.2.15

./configure

make && make install

cp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/

cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/

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

mkdir /etc/keepalived

cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/

chkconfig keepalived on

 

三处nginx要改的地方

   upstream fuckyou

    {

    server 10.10.10.11;

    server 10.10.10.12;

    }

 

server {

        listen       80;

        server_name  10.10.10.100;//vip

        location / {

            proxy_pass http://fuckyou;

        }

此时集群已经可以访问了,无论是访问13或者14的ip,都可以正常转到11或者12上面,下面开始配置keepalived了。

 

global_defs {

   notification_email {

     admin@xxx

   }

   notification_email_from keepalived@domain.com

   smtp_server 127.0.0.1

   smtp_connect_timeout 30

   router_id LVS_DEVEL

}

vrrp_script chk_http_port {

                script "/opt/nginx_pid.sh"

                interval 2

                weight 2

}

vrrp_instance VI_1 {

    state MASTER      // BACKUP

    interface eth1

    virtual_router_id 51

    mcast_src_ip 10.10.10.13

    priority 102                //值要比 back 高

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 1111

    }

track_script {

        chk_http_port //监控

        }

    virtual_ipaddress {

       10.10.10.100

    }

}

从服务器:

global_defs {

   notification_email {

     admin@xxx

   }

   notification_email_from keepalived@domain.com

   smtp_server 127.0.0.1

   smtp_connect_timeout 30

   router_id LVS_DEVEL

}

vrrp_script chk_http_port {

                script "/opt/nginx_pid.sh"

                interval 2

                weight 2

}

vrrp_instance VI_1 {

    state BACKUP

    interface eth1

    virtual_router_id 51

    mcast_src_ip 10.10.10.14

    priority 101             //值比 master 低。。

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 1111

    }

track_script {

        chk_http_port // 监控

        }

    virtual_ipaddress {

       10.10.10.100

    }

}

 

设置一下nginx的监控脚本

vi /opt/nginx_pid.sh

#!/bin/bash

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

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

                fi

fi

 

OK,启动

service keepalived start

 

# ip addr

1: lo: <LOOPBACK,UP,LOWER_UP> 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: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000

    link/ether 00:50:56:34:03:5c brd ff:ff:ff:ff:ff:ff

    inet 10.10.10.13/24 brd 10.10.10.255 scope global eth1

    inet 10.10.10.100/32 scope global eth1

    inet6 fe80::250:56ff:fe34:35c/64 scope link

       valid_lft forever preferred_lft forever

4: pan0: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN

    link/ether fe:2c:74:f5:26:70 brd ff:ff:ff:ff:ff:ff

 

vip已经起来了,访问一下10.10.10.100,OK。测试连接,此时nginx的负载均衡服务已经开启了,13,14两个负载均衡都已经启用(访问时候,都会自动转到11或者12的web页面上)

好久没配置这类东西了,还算顺利。

 

posted on 2016-07-25 20:25  iamqiu  阅读(1186)  评论(0编辑  收藏  举报

导航