我正在嘗試使用單個復制服務部署三個主機節點的 Docker Swarm,并在其前面放置一個 HAProxy。我希望客戶端能夠通過 SSL 進行連接。
我的docker-compose.yml:
version: '3.9'
services:
proxy:
image: haproxy
ports:
- 443:8080
volumes:
- haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg
deploy:
placement:
constraints: [node.role == manager]
networks:
- servers-network
node-server:
image: glusk/hackathon-2021:latest
ports:
- 8080:8080
command: npm run server
deploy:
mode: replicated
replicas: 2
networks:
- servers-network
networks:
servers-network:
driver: overlay
我的haproxy.cfg(基于官方示例):
# Simple configuration for an HTTP proxy listening on port 80 on all
# interfaces and forwarding requests to a single backend "servers" with a
# single server "server1" listening on 127.0.0.1:8000
global
daemon
maxconn 256
defaults
mode http
timeout connect 5000ms
timeout client 50000ms
timeout server 50000ms
frontend http-in
bind *:80
default_backend servers
backend servers
server server1 127.0.0.1:8000 maxconn 32
我的主機是 Lightsail VPS Ubuntu 實體并共享相同的專用網路。
node-service運行在其自己的容器內的每個HTTPS服務器任務:0.0.0.0:8080。
我目前嘗試進行這項作業的方法是ssh進入管理器節點(它也有一個靜態和公共 IP),從上面復制我的組態檔,然后運行:
docker stack deploy --compose-file=docker-compose.yml hackathon-2021
但它不起作用。
uj5u.com熱心網友回復:
好吧,首先,關于SSL(因為它是您提到的第一件事),您需要使用證書配置它并偵聽 port 443,而不是 port 80。
通過該修改,您的代理配置將已更改為:
global
daemon
maxconn 256
defaults
mode http
timeout connect 5000ms
timeout client 50000ms
timeout server 50000ms
frontend http-in
bind *:80
default_backend servers
frontend https-in
bind *:443 ssl crt /etc/ssl/certs/hackaton2021.pem
default_backend servers
這將是一個真正簡化的允許SSL連接的配置。
現在,讓我們去訪問不同的服務。
首先,您無法訪問 上的服務localhost,實際上您甚至不應該將您擁有的服務的埠暴露給主機。原因?您已經在與 相同的網路中擁有這些應用程式haproxy,因此理想的情況是利用 Docker DNS 直接訪問它們
為此,首先我們需要能夠決議服務名稱。為此,您需要將以下部分添加到您的配置中:
resolvers docker
nameserver dns1 127.0.0.11:53
resolve_retries 3
timeout resolve 1s
timeout retry 1s
hold other 10s
hold refused 10s
hold nx 10s
hold timeout 10s
hold valid 10s
hold obsolete 10s
Docker Swarm DNS 服務始終在127.0.0.11.
現在到您之前存在的配置,我們必須添加服務器,但使用服務名稱發現:
backend servers
balance roundrobin
server-template node- 2 node-server:8080 check resolvers docker init-addr libc,none
如果您檢查我們在做什么,我們正在為node-server服務中Swarm 中發現的每個容器(因此是副本)創建一個服務器,我們將創建那些為每個容器添加前綴node-的服務器。
基本上,這等同于獲取每個副本的實際 IP 并將它們堆疊添加為基本server配置。
對于部署,您也有一些錯誤,因為我們對實際node-server向主機公開埠不感興趣,而是創建兩個副本并使用 HAProxy 進行網路連接。
為此,我們應該使用以下 Docker Compose:
version: '3.9'
services:
proxy:
image: haproxy
ports:
- 80:80
- 443:443
volumes:
- hackaton2021.pem:/etc/ssl/certs/hackaton2021.pem
- haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg
deploy:
placement:
constraints: [node.role == manager]
node-server:
image: glusk/hackathon-2021:latest
command: npm run server
deploy:
mode: replicated
replicas: 2
請記住haproxy.cfg在部署堆疊之前將您的和您的應用程式的自簽名(或真實)證書復制到實體。
Also, when you create that stack it will automatically create a network with the name <STACK_NAME>-default, so you don't need to define a network just for connecting both services.
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/347326.html
標籤:码头工人 docker-compose 码头群 代理
