這篇文章主要介紹了怎么使用Docker Compose實(shí)現(xiàn)nginx負(fù)載均衡的相關(guān)知識(shí),內(nèi)容詳細(xì)易懂,操作簡(jiǎn)單快捷,具有一定借鑒價(jià)值,相信大家閱讀完這篇怎么使用Docker Compose實(shí)現(xiàn)nginx負(fù)載均衡文章都會(huì)有所收獲,下面我們一起來看看吧。
專注于為中小企業(yè)提供網(wǎng)站建設(shè)、成都網(wǎng)站制作服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)崇川免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了上1000+企業(yè)的穩(wěn)健成長(zhǎng),幫助中小企業(yè)通過網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。
以docker的網(wǎng)絡(luò)管理,容器的ip設(shè)置為基礎(chǔ)知識(shí)實(shí)現(xiàn)nginx負(fù)載均衡
查看所有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時(shí),docker-compose.yml文件所在的目錄名, // 所以,用docker-compose創(chuàng)建的容器會(huì)默認(rèn)創(chuàng)建一個(gè)以目錄名為網(wǎng)絡(luò)名的網(wǎng)絡(luò),并且是dridge(橋接)類型
指定容器ip地址
官網(wǎng)文檔地址:
繼續(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啟動(dòng)容器
docker-compose up -d
查看容器是否啟動(dòng),并確認(rèn)是否創(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ò),命名是容器組項(xiàng)目的 文件名開頭_網(wǎng)絡(luò)名
查看網(wǎng)絡(luò) nginx-lsb的詳情
docker network inspect composetest_nginx-lsb // 詳情里面可以看到使用這個(gè)網(wǎng)絡(luò)的每個(gè)容器的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)境文件:
簡(jiǎn)單可以理解為:在docker-compose.yml中定義變量,引用在外部.env文件中進(jìn)行變量定義
官方文檔地址:
// 還是在composetest目錄中定義個(gè) .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
重新啟動(dòng)composetest項(xiàng)目,并查看網(wǎng)絡(luò)詳情,確認(rèn)容器ip是否設(shè)置成功
// 重新啟動(dòng)composetest項(xiàng)目 docker-compose up -d // 查看網(wǎng)絡(luò)詳情 docker network inspect composetest_nginx-lsb
在composetest項(xiàng)目中添加一臺(tái)nginx服務(wù)器作為負(fù)載均衡服務(wù)器
// 在.env文件里添加一個(gè)變量 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 // 重新啟動(dòng)composetest項(xiàng)目 docker-compose up -d
修改nginx.conf配置文件,配置負(fù)載均衡
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; } }
重新啟動(dòng)nginx-lsb,加載配置文件
docker-composer restart nginx-lsb
訪問 http://服務(wù)器ip地址:8000,測(cè)試服務(wù)器負(fù)載均衡!
關(guān)于“怎么使用Docker Compose實(shí)現(xiàn)nginx負(fù)載均衡”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對(duì)“怎么使用Docker Compose實(shí)現(xiàn)nginx負(fù)載均衡”知識(shí)都有一定的了解,大家如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
分享名稱:怎么使用DockerCompose實(shí)現(xiàn)nginx負(fù)載均衡
URL網(wǎng)址:http://bm7419.com/article32/gejdpc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)建站、品牌網(wǎng)站設(shè)計(jì)、動(dòng)態(tài)網(wǎng)站、面包屑導(dǎo)航、定制網(wǎng)站、營(yíng)銷型網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)