Docker部署Nginx+Tomcat實現負載均衡
docker部署nginx+tomcat實現負載均衡
1,部署一臺Nginx和三臺Tomcat伺服器
2,Ngnix需要實現三種策略:
1)輪詢;
2)權重,三臺伺服器的權重為1,3,5;
3)IP Hash。
最終實現的效果是,本地電腦通過網頁訪問雲伺服器時,網頁能夠體現Ngnix三種策略的結果。
1 申請華為雲伺服器並配置環境
1.1 申請伺服器
本實驗申請的版本是1vCPUs, 記憶體1Gib,執行系統為Ubuntu 18.04的伺服器
在申請後使用ssh進行登入
![image-20220526232213855](實驗11 - 副本/image-20220526232213855.png)
1.2 安裝Docker
安裝Docker的步驟主要參考官方文件:https://docs.docker.com/engine/install/ubuntu/
# 1. Update the apt package index and install packages to allow apt to use a repository over HTTPS: sudo apt-get update sudo apt-get install \ ca-certificates \ curl \ gnupg \ lsb-release # 2. Add Docker’s official GPG key: sudo mkdir -p /etc/apt/keyrings curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg # 3. Use the following command to set up the stable repository. echo \ "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \ $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null # 4. install docker engine sudo apt-get update sudo apt-get install docker-ce docker-ce-cli containerd.io docker-compose-plugin
安裝完成後可以檢視一下docker的版本
docker --version
1.3 拉取nginx和tomcat映象
docker pull nginx
docker pull tomcat
檢視已經拉取到的映象
2 建立nginx和tomcat容器
2.1 建立1個nginx容器
在建立nginx容器之前,我們先在本地建立用於放置nginx配置的資料夾~/docker/nginx
mkdir docker
cd docker
mkdir nginx
啟動nginx容器,將配置檔案複製到資料夾中
docker cp 0c025fba7dc5:/etc/nginx/nginx.conf ~/docker/nginx/ docker cp 0c025fba7dc5:/etc/nginx/conf.d/default.conf ~/docker/nginx/
然後停掉並刪除容器
docker stop 0c025fba7dc5
docker rm 0c025fba7dc5
然後重新啟動nginx容器,並將配置掛載到容器上
docker run --name myNginx -p 80:80 -v ~/docker/nginx/nginx.conf:/etc/nginx/nginx.conf -v ~/docker/nginx/default.conf:/etc/nginx/conf.d/default.conf -itd nginx
2.2 建立3個tomcat容器
建立三個tomcat容器myTomcat1,myTomcat2,myTomcat3,埠號分別為8081,8082,8083。
docker run -itd --name myTomcat1 -p 8081:8080 tomcat
docker run -itd --name myTomcat2 -p 8082:8080 tomcat
docker run -itd --name myTomcat3 -p 8083:8080 tomcat
檢視已經建立的容器:
建立好的tomcat,還要設定webapps資料夾用於訪問,進入每個tomcat容器,然後執行下面的命令就行
cp -r webapps.dist/* ./webapps
rm -rf webapps.dist
新增防火牆的埠:
firewall-cmd --zone=public --add-port=80/tcp --permanent # 這個是nginx的埠
firewall-cmd --zone=public --add-port=8081/tcp --permanent
firewall-cmd --zone=public --add-port=8082/tcp --permanent
firewall-cmd --zone=public --add-port=8083/tcp --permanent
然後reload
firewall-cmd --reload
檢視結果
firewall-cmd --list-all
為了能夠訪問華為雲伺服器的8081,8082,8083埠,需要對伺服器新增入口規則(80埠已經預設有了就不用添加了):
新增完成後就能訪問tomcat了:
為了區分不同的tomcat容器,我們建立3個index.jsp檔案,內容分別為this is tomcat1
,this is tomcat2
,this is tomcat3
,然後複製到tomcat容器中
docker cp index1.jsp myTomcat1:/usr/local/tomcat/webapps/ROOT/index.jsp
docker cp index2.jsp myTomcat2:/usr/local/tomcat/webapps/ROOT/index.jsp
docker cp index3.jsp myTomcat3:/usr/local/tomcat/webapps/ROOT/index.jsp
這樣我們就能區分不同的tomcat容器了:
3 nginx負載均衡
3.1 輪詢方式
在本地目錄建立容器中nginx.conf檔案的副本nginx1.conf,修改後再掛載到容器中,下面是對nginx1.conf所做的修改:
這種方式預設就會使用輪詢的方式訪問3個tomcat容器
用nginx1.conf作為配置啟動nginx:
docker run --name myNginx -p 80:80 -v ~/docker/nginx1.conf:/etc/nginx/nginx.conf -itd nginx
然後訪問nginx伺服器,發現會輪詢地對3個tomcat容器進行訪問:
使用shell指令碼每隔一秒對nginx伺服器訪問的結果,可以更清楚地看到負載均衡的方式
while true; do wget -O - -q http://120.46.179.205;sleep 1; done
3.2 權重,三臺伺服器的權重為1,3,5
建立檔案nginx2.conf,nginx2.conf在nginx1.conf基礎上做了如下修改
即增加了權重處理,每個server被訪問的頻率與weight成正比
將nginx2.conf掛載到容器並啟動nginx:
docker run --name myNginx -p 80:80 -v ~/docker/nginx2.conf:/etc/nginx/nginx.conf -itd nginx
還是使用上面的指令碼觀察結果:
while true; do wget -O - -q http://120.46.179.205;sleep 1; done
可以看到三個tomcat1,tomcat2, tomcat3被訪問的頻率近似為1:3:5
3.3 IP Hash
IP Hash會按照IP進行訪問,這樣每個訪客固定訪問一個後端伺服器,可以解決session問題
nginx3.conf
在輪詢(nginx1.conf
)的基礎上只要加上ip_hash
就能實現IP hash訪問:
將nginx3.conf
檔案掛載到nginx容器中並啟動nginx容器:
docker run --name myNginx -p 80:80 -v ~/docker/nginx3.conf:/etc/nginx/nginx.conf -itd nginx
訪問nginx伺服器,無論怎麼重新整理都只會訪問tomcat1
總結
通過這次實驗,我學會了在雲端用docker部署nginx和tomcat實現負載均衡的三種策略:輪詢、權重和IP hash。熟悉了伺服器,docker的操作和使用。