千家信息网

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

发表于:2024-09-22 作者:千家信息网编辑
千家信息网最后更新 2024年09月22日,下文给大家带来nginx负载均衡如何部署双机热备方法及步骤,希望能够给大家在实际运用中带来一定的帮助,负载均衡涉及的东西比较多,理论也不多,网上有很多书籍,今天我们就用在行业内累计的经验来做一个解答。
千家信息网最后更新 2024年09月22日nginx负载均衡如何部署双机热备方法及步骤

下文给大家带来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

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

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 "

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 重新加载配置

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






0