部署

ip分配 nginx192.168.1.5主机  192.168.1.6备机  浮动ip192.168.1.7

首先设置浮动ip(主备机器都要设置)

打开网卡eth0 配置好网络参数

复制eth0为eth0:1

修改vim eth0:1 

NAME = eth0:1

DEVICE = eth0:1

IPADDR = 192.168.1.7

开启转发

echo "1" > /proc/sys/net/ipv4/ip_forward


修改/etc/sysctl.conf文件,让包转发功能在系统启动时自动生效:

# Controls IP packet forwarding

net.ipv4.ip_forward = 1

重启网络

service network restart


安装keepalived

yum install keepalived -y

修改配置文件

vim /etc/keepalived/keepalived.conf

主机配置情况

global_defs {

    notification_email { 

        #[email protected]  设置报警

        }

    router_id LVS_DEVEL

    }

    vrrp_script chk_http_port {

        script "

        interval 1

        weight -10

    }

    vrrp_instance VI_1 {

        state MASTER

        interface eth0

        virtual_router_id 51

        priority 100

        advert_int 1

        authentication {

        auth_type PASS

        auth_pass 1111

    }

    virtual_ipaddress {

    192.168.1.7

    }

    track_script {

        chk_http_port

    }

    }


备用机上面配置

global_defs {

    notification_email { 

        #[email protected]  设置报警

        }

    router_id LVS_DEVEL

    }

    vrrp_script chk_http_port {

        script "

        interval 1

        weight -10

    }

    vrrp_instance VI_1 {

        state BACKUP

        interface eth0

        virtual_router_id 51

        priority 90

        advert_int 1

        authentication {

        auth_type PASS

        auth_pass 1111

    }

    virtual_ipaddress {

    192.168.1.7

    }

    track_script {

        chk_http_port

    }

    }


启动查看keepalived情况

killall keepalived

service keepalived start

tail -f /var/log/messages


安装nginx配置转发负载均衡

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

添加负载均衡配置

       upstream mytest {

                #least_conn;

                #ip_hash;  

            server 1.test.com weight=3 max_fails=2 fail_timeout=30s;

            server 2.test.com weight=3 max_fails=2 fail_timeout=30s;

            server 3.test.com weight=5 max_fails=3 fail_timeout=30s;

                    }

        location ^~/piwik/ {

            proxy_pass http://mytest;

            }


/usr/local/nginx/sbin/nginx -t

/usr/local/nginx/sbin/nginx 

/usr/local/nginx/sbin/nginx -s reload        重新加载配置