使用docker容器搭建nginx負載均衡

運用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

架構原理圖

在這裏插入圖片描述

準備構建鏡像的Dockerfile文件

準備編排容器項目的yaml文件

搭建環境

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.第三次

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章