nginx做调度器的的负载均衡安装及配置

Nginx负载均衡部署

说明

负载均衡分4层和7层,一般认为,nginx在7层转发,而lvs,haproxy作为4层的负载均衡。nginx转发效率高,且能够对业务处理控制力度更强,目前使用率很高。

本案例中,我们采用nginx的反向代理做负载均衡。

环境配置

IP角色说明备注
192.168.10.105调度器  
192.168.10.106工作机1  
192.168.10.107工作机1  
192.168.10.108工作机2  

安装

使用ansible进行nginx的安装,ansible 安装及使用见博主文档 ansible的安装及使用

配置集群信息

cat <<EOF >>/etc/ansible/hosts
[upstream]
192.168.10.[105:108]
EOF

安装相关工具。

ansible upstream -m shell -a " yum install epel-release -y "
ansible upstream -m shell -a " yum install nginx -y "
ansible upstream -m shell -a " systemctl enable nginx && systemctl start nginx "
ansible upstream -m shell -a " systemctl stop firewalld && setenforce 0 "

更改默认的html文件内容以便测试

[root@node ~]# ansible upstream -m shell -a "ip a |grep -oE '([0-9]+\.)+[0-9]+/[0-9]+' |grep -v 127 >/usr/share/nginx/html/index.html "
192.168.11.106 | CHANGED | rc=0 >>

192.168.11.108 | CHANGED | rc=0 >>

192.168.11.105 | CHANGED | rc=0 >>

192.168.11.107 | CHANGED | rc=0 >>

测试服务器是否全启动。


[root@node ~]# for i in 105 106 107 108
> do
> curl 192.168.11.$i
> done
192.168.11.105/23
192.168.11.106/23
192.168.11.107/23
192.168.11.108/23

增加负载均衡配置,进入调度器105,修改nginx.conf,将默认的location增加一行。

 location / {
         proxy_pass http://backend;
        }
cat <<EOF >/etc/nginx/conf.d/upstream.conf
upstream backend { 
server 192.168.11.106:80;
server 192.168.11.107:80;
server 192.168.11.108:80;
}
EOF

重新启动nginx

然后在调度器上运行

[root@localhost nginx]# for i in `seq  1  10 ` ; do curl 192.168.11.105; done
192.168.11.106/23
192.168.11.107/23
192.168.11.106/23
192.168.11.108/23
192.168.11.106/23
192.168.11.107/23
192.168.11.107/23
192.168.11.108/23
192.168.11.106/23
192.168.11.107/23

转发成功

评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

老骥又出发

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值