运用docker搭建1台nginx负载均衡反向代理服务器、3台web应用服务器
设计docker部署方案
容器名称 | 容器IP | 端口映射 | nginx服务模式 |
---|
nginx-loadbalance | 192.168.5.10 | 10080-80 | proxy |
nginx-web1 | 192.168.5.11 | 10081-80 | web |
nginx-web2 | 192.168.5.12 | 10082-80 | web |
nginx-web3 | 192.168.5.13 | 10083-80 | web |
架构原理图

搭建环境
1.构建镜像
docker build -t mynginx .
2.使用docker-compose编排项目
docker-compose -p nginxbalancepro up -d
3.修改各个容器的index.html,通过映射的端口访问nginx-loadbalance这个容器的页面

设置负载均衡
1.在 nginx-loadbalance的nginx.conf的http模块中添加upstream设置
upstream balanceweb{
least_conn;#开启连接数记录,将新的请求转发到连接数少的节点
server 36.112.201.233:10081;
server 36.112.201.233:10082;
server 36.112.201.233:10083;
}
2.在 nginx-loadbalance的nginx.conf的server中的location中通过proxy_pass来使用刚才的upstream设置
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
proxy_pass http://balanceweb;
}
...
3.热重启 nginx-loadbalance的nginx服务
nginx -s reload
访问nginx-loadbalance被均匀的代理访问到3台应用服务器上了
1.第一次

2.第二次

3.第三次
