人妖在线一区,国产日韩欧美一区二区综合在线,国产啪精品视频网站免费,欧美内射深插日本少妇

新聞動(dòng)態(tài)

Docker多個(gè)容器不能有相同端口號(hào)的處理方案

發(fā)布日期:2021-12-25 06:23 | 文章來(lái)源:CSDN

問(wèn)題背景

在docker里用同一份鏡像創(chuàng)建4個(gè)容器,網(wǎng)絡(luò)選擇bridge模式,A服務(wù)在四個(gè)容器中都使用了同一個(gè)端口號(hào)(6000);為了減少對(duì)外暴露的端口數(shù),另外使用了nginx對(duì)這4個(gè)服務(wù)實(shí)例做代理,4個(gè)服務(wù)實(shí)例分屬4個(gè)upstream,使用了類(lèi)似/service1、/service2的路徑來(lái)訪問(wèn)4個(gè)實(shí)例。

此時(shí)從本地訪問(wèn)任一服務(wù),則會(huì)報(bào)502錯(cuò)誤,百思不得其解。

connect() failed (111: Connection refused) while connecting to upstream

compose文件

version: '2'
networks:
 nn:
  driver: bridge
services:
 service-1:
  container_name: service-1
  image: foo
  networks:
   - nn
  volumes:
   - ./logs/1:/apps/aaa/bbb-logs
   - ./common:/apps/aaa/bbb
   - ./xxx/1.xml:/ccc/targets.xml
  entrypoint: foo.sh
  command: start app=foo port=6000
  
 service-2:
  container_name: service-2
  image: foo
  networks:
   - nn
  volumes:
   - ./logs/2:/apps/aaa/bbb-logs
   - ./common:/apps/aaa/bbb
   - ./xxx/2.xml:/ccc/targets.xml
  entrypoint: foo.sh
  command: start app=foo port=6000
 
 service-3:
  container_name: service-3
  image: foo
  networks:
   - nn
  volumes:
   - ./logs/3:/apps/aaa/bbb-logs
   - ./common:/apps/aaa/bbb
   - ./xxx/3.xml:/ccc/targets.xml
  entrypoint: foo.sh
  command: start app=foo port=6000
 
 service-4:
  container_name: service-4
  image: foo
  networks:
   - nn
  volumes:
   - ./logs/4:/apps/aaa/bbb-logs
   - ./common:/apps/aaa/bbb
   - ./xxx/4.xml:/ccc/targets.xml
  entrypoint: foo.sh
  command: start app=foo port=6000  
 
 nginx:
  container_name: nginx
  image: nginx:1.15-alpine
  ports:
   - 6001:6001
  networks:
   - nn
  volumes:
   - ./nginx/nginx.conf:/etc/nginx/nginx.conf
   - ./logs/nginx:/var/log/nginx

nginx.conf

worker_processes 8;
worker_rlimit_nofile 65535; 
events {
    use epoll;
    worker_connections 65535;
 }
 
http {
    include mime.types;
    default_type aplication/octet-stream;
    sendfile on;
    log_format main '[$time_local]$remote_addr-$upstream_addr "$request" $status $body_bytes_sent';
 
    upstream service1.local {
      server service-1:6000;
    }
    upstream service2.local {
     server service-2:6000;
    }
    upstream service3.local {
      server service-3:6000;
    }
    upstream service4.local {
      server service-4:6000;
    }
 
    server {
      listen 6001;
      client_max_body_size 100M;
      proxy_set_header Host $host:$server_port;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
 
      location /service1/ {
        proxy_pass http://service1.local/;
      }
      location /service2/ {
        proxy_pass http://service2.local/;
      }
      location /service3/ {
        proxy_pass http://service3.local/;
      }
      location /service4/ {
        proxy_pass http://service4.local/;
      }
      location /nginx_status {
        stub_status on;
        access_log off;
      }
    }
}
 

此時(shí)curl localhost:6001/service1/api/v1/....就會(huì)報(bào)上面的502錯(cuò)誤,按理說(shuō)每一個(gè)容器都是有各自的網(wǎng)卡,不同容器的端口號(hào)應(yīng)該不沖突才對(duì)。

解決方案

暫時(shí)沒(méi)有較好的方案,只能對(duì)4個(gè)服務(wù)使用不同的端口號(hào),nginx也相應(yīng)地修改。

補(bǔ)充:同臺(tái)服務(wù)器部署多套docker容器,端口重定向問(wèn)題

在生成環(huán)境,部署多個(gè)容器,訪問(wèn)多個(gè)端口;

例如:-p 80:80 -p 81:81

81地址退出的時(shí)候,直接訪問(wèn)到80端口的地址。

誤區(qū):開(kāi)始以為是cookie的問(wèn)題,因?yàn)樗⑿铝薱ookie(cookie是不區(qū)分端口號(hào))

最終找到了原因:redirect問(wèn)題,因?yàn)橥顺鲋囟ㄏ虻降卿涰?yè)面

解決方案:配置nginx參數(shù)

proxy_set_header HOST $host; 改成 proxy_set_header HOST $host:81;

因?yàn)椴还茉鯓樱?qǐng)求參數(shù)投都帶上了端口號(hào)。

網(wǎng)上還有一種方法:修改proxy_redirect 參數(shù)(不過(guò)試過(guò)沒(méi)有生效)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持本站。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。

版權(quán)聲明:本站文章來(lái)源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來(lái)源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來(lái)源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來(lái),僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。

相關(guān)文章

實(shí)時(shí)開(kāi)通

自選配置、實(shí)時(shí)開(kāi)通

免備案

全球線路精選!

全天候客戶服務(wù)

7x24全年不間斷在線

專(zhuān)屬顧問(wèn)服務(wù)

1對(duì)1客戶咨詢顧問(wèn)

在線
客服

在線客服:7*24小時(shí)在線

客服
熱線

400-630-3752
7*24小時(shí)客服服務(wù)熱線

關(guān)注
微信

關(guān)注官方微信
頂部