温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

nginx负载均衡如何部署双机热备方法及步骤

发布时间:2020-04-20 14:26:48 来源:亿速云 阅读:213 作者:三月 栏目:建站服务器

下文给大家带来nginx负载均衡如何部署双机热备方法及步骤,希望能够给大家在实际运用中带来一定的帮助,负载均衡涉及的东西比较多,理论也不多,网上有很多书籍,今天我们就用亿速云在行业内累计的经验做一个解答。

部署

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

nginx负载均衡如何部署双机热备方法及步骤

修改/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 { 

        #baojing@163.com  设置报警

        }

    router_id LVS_DEVEL

    }

    vrrp_script chk_http_port {

        script "</dev/tcp/127.0.0.1/8087"

        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 { 

        #baojing@163.com  设置报警

        }

    router_id LVS_DEVEL

    }

    vrrp_script chk_http_port {

        script "</dev/tcp/127.0.0.1/8087"

        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

    }

    }

nginx负载均衡如何部署双机热备方法及步骤

启动查看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        重新加载配置

看了以上关于nginx负载均衡如何部署双机热备方法及步骤,如果大家还有什么地方需要了解的可以在亿速云行业资讯里查找自己感兴趣的或者找我们的专业技术工程师解答的,亿速云技术工程师在行业内拥有十几年的经验了。






向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI