濮阳杆衣贸易有限公司

主頁 > 知識庫 > 使用Docker Compose 實現(xiàn)nginx負載均衡的方法步驟

使用Docker Compose 實現(xiàn)nginx負載均衡的方法步驟

熱門標(biāo)簽:廣州電銷機器人系統(tǒng)圖 高德地圖標(biāo)注無營業(yè)執(zhí)照 智能電話機器人線路 金融行業(yè)外呼線路 江蘇電銷外呼防封系統(tǒng)是什么 長沙開福怎么申請400電話 賀州市地圖標(biāo)注app 東莞人工智能電銷機器人供應(yīng)商 百度地圖標(biāo)注要不要錢

以Docker的網(wǎng)絡(luò)管理,容器的IP設(shè)置為基礎(chǔ)知識實現(xiàn)Nginx負載均衡

查看所有docker網(wǎng)絡(luò)

docker network ls

/*
NETWORK ID     NAME         DRIVER       SCOPE
b832b168ca9a    bridge        bridge       local
373be82d3a6a    composetest_default  bridge       local
a360425082c4    host         host        local
154f600f0e90    none         null        local

*/

// composetest_default 是上一篇介紹Compose時,docker-compose.yml文件所在的目錄名,
// 所以,用docker-compose創(chuàng)建的容器會默認創(chuàng)建一個以目錄名為網(wǎng)絡(luò)名的網(wǎng)絡(luò),并且是dridge(橋接)類型

指定容器IP地址

官網(wǎng)文檔地址:https://docs.docker.com/compose/compose-file/#ipv4_address-ipv6_address

繼續(xù)編寫上一篇《12.使用Docker Compose容器編排工具》文章中的docker-compose.yml

version: "3"
services:
  web1:
    container_name: web1
    image: "centos:httpd"
    ports:
      - "8080:80"
    privileged: true
    volumes:
      - "/app/www/web1/:/var/www/html/"
    command: ['/usr/sbin/init']
    networks:
      nginx-lsb:
        ipv4_address: 192.169.0.3
  web2:
    container_name: web2
    image: "centos:httpd"
    ports:
      - "8081:80"
    privileged: true
    volumes:
      - "/app/www/web2/:/var/www/html/"
    command: ['/usr/sbin/init']
    networks:
      nginx-lsb:
        ipv4_address: 192.169.0.2
networks:
  nginx-lsb:
    driver: bridge
    ipam:
      config:
        - subnet: 192.169.0.0/16

使用docker-compose啟動容器

docker-compose up -d

查看容器是否啟動,并確認是否創(chuàng)建了網(wǎng)絡(luò) nginx-lsb

// 可以查看當(dāng)前docker-compose.yml配置的容器組里的容器狀態(tài)
docker-compose ps

docker network ls

/*
NETWORK ID     NAME          DRIVER       SCOPE
b832b168ca9a    bridge         bridge       local
373be82d3a6a    composetest_default   bridge       local
de6f5b8df1c8    composetest_nginx-lsb  bridge       local
a360425082c4    host          host        local
154f600f0e90    none          null        local
*/

// 創(chuàng)建了nginx-lsb網(wǎng)絡(luò),命名是容器組項目的 文件名開頭_網(wǎng)絡(luò)名

查看網(wǎng)絡(luò) nginx-lsb的詳情

docker network inspect composetest_nginx-lsb

// 詳情里面可以看到使用這個網(wǎng)絡(luò)的每個容器的ip

如:

/*
...
 "Containers": {
      "039aa860ef04f20a7566fdc943fb4398a61d2ad6dd3e373b17c86ac778af89e3": {
        "Name": "web2",
        "EndpointID": "1bc206661179e65999015f132c2889d3d4365b8d42b8a89cf9c260016fedd5ee",
        "MacAddress": "02:42:c0:a9:00:02",
        "IPv4Address": "192.169.0.2/16",
        "IPv6Address": ""
      },
      "437ad7a07da8e46c0abaf845c4b08425338009fbe972bde69478cf47c75c315b": {
        "Name": "web1",
        "EndpointID": "5a36e602a5364ee9ad06e9816d09e3966d56ebf06b9394ebc25b8bcee9546607",
        "MacAddress": "02:42:c0:a9:00:03",
        "IPv4Address": "192.169.0.3/16",
        "IPv6Address": ""
      }
    },
...
*/

使用 env_file環(huán)境文件:

簡單可以理解為:在docker-compose.yml中定義變量,引用在外部.env文件中進行變量定義

官方文檔地址:https://docs.docker.com/compose/compose-file/#env_file

// 還是在composetest目錄中定義個 .env文件,用來存放變量
web1_addr=192.169.0.2
web2_addr=192.169.0.3

// 修改docker-compose.yml文件,加入變量定義
version: "3"
services:
  web1:
    container_name: web1
    image: "centos:httpd"
    ports:
      - "8080:80"
    privileged: true
    volumes:
      - "/app/www/web1/:/var/www/html/"
    command: ['/usr/sbin/init']
    networks:
      nginx-lsb:
        ipv4_address: ${web1_addr}
  web2:
    container_name: web2
    image: "centos:httpd"
    ports:
      - "8081:80"
    privileged: true
    volumes:
      - "/app/www/web2/:/var/www/html/"
    command: ['/usr/sbin/init']
    networks:
      nginx-lsb:
        ipv4_address: ${web2_addr}
networks:
  nginx-lsb:
    driver: bridge
    ipam:
      config:
        - subnet: 192.169.0.0/16

重新啟動composetest項目,并查看網(wǎng)絡(luò)詳情,確認容器ip是否設(shè)置成功

// 重新啟動composetest項目
docker-compose up -d

// 查看網(wǎng)絡(luò)詳情
docker network inspect composetest_nginx-lsb

在composetest項目中添加一臺nginx服務(wù)器作為負載均衡服務(wù)器

// 在.env文件里添加一個變量 nginx_lsb
web1_addr=192.169.0.2
web2_addr=192.169.0.3
nginx_lsb=192.169.0.100

// 修改docker-compose.yml文件,加入變量定義
version: "3"
services:
  nginx-lsb:
    container_name: nginx-lsb
    image: "centos:nginx"
    ports: 
      - "8000:80"
    privileged: true
    volumes:
      - "/app/nginx/nginx.conf:/etc/nginx/nginx.conf"
    networks:
      nginx-lsb:
        ipv4_address: ${nginx_lsb}
  web1:
    container_name: web1
    image: "centos:httpd"
    ports:
      - "8080:80"
    privileged: true
    volumes:
      - "/app/www/web1/:/var/www/html/"
    command: ['/usr/sbin/init']
    networks:
      nginx-lsb:
        ipv4_address: ${web1_addr}
  web2:
    container_name: web2
    image: "centos:httpd"
    ports:
      - "8081:80"
    privileged: true
    volumes:
      - "/app/www/web2/:/var/www/html/"
    command: ['/usr/sbin/init']
    networks:
      nginx-lsb:
        ipv4_address: ${web2_addr}
networks:
  nginx-lsb:
    driver: bridge
    ipam:
      config:
        - subnet: 192.169.0.0/16

// 重新啟動composetest項目
docker-compose up -d

修改nginx.conf配置文件,配置負載均衡

upstream mydocker {
  server 192.169.0.2;
  server 192.169.0.3;
}

server {
  listen 80;
  server_name mydocker;
  location / {
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_buffering off;
    proxy_pass http://mydocker;
  }
}

重新啟動nginx-lsb,加載配置文件

docker-composer restart nginx-lsb

訪問 http://服務(wù)器IP地址:8000,測試服務(wù)器負載均衡!

注意:上一篇已經(jīng)在兩臺httpd服務(wù)器上放置了不同的web文件

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

標(biāo)簽:洛陽 玉樹 松原 張家界 北京 永州 滄州 廊坊

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《使用Docker Compose 實現(xiàn)nginx負載均衡的方法步驟》,本文關(guān)鍵詞  使用,Docker,Compose,實現(xiàn),nginx,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《使用Docker Compose 實現(xiàn)nginx負載均衡的方法步驟》相關(guān)的同類信息!
  • 本頁收集關(guān)于使用Docker Compose 實現(xiàn)nginx負載均衡的方法步驟的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    卓资县| 惠州市| 晋城| 大埔县| 沙湾县| 容城县| 襄城县| 普格县| 合阳县| 鄂托克前旗| 山阴县| 绥化市| 柘荣县| 枣庄市| 城市| 冷水江市| 武隆县| 苏尼特右旗| 偏关县| 宜川县| 贺兰县| 宁都县| 阿坝县| 红安县| 来凤县| 大埔县| 沧州市| 商水县| 咸丰县| 红安县| 明溪县| 双峰县| 枣强县| 汉源县| 云和县| 大足县| 东至县| 孟津县| 富裕县| 会东县| 甘孜|