express - 带有 traefik 的 docker swarm 自定义容器
问题描述
我正在尝试在数字海洋中使用 traefik 建立一个 docker swarm。我按照本教程进行操作,直到我添加了一个定制的容器。我试图简单地先添加一个(总共有 14 个),它们都非常相似,它们都是快速应用程序,用作 restful API,每个服务处理一个资源。但是,当尝试访问该特定子域时,我收到连接被拒绝错误。
这是我的 docker-stack.yml 文件:
version: '3.6'
services:
traefik:
image: traefik:latest
networks:
- mynet
volumes:
- /var/run/docker.sock:/var/run/docker.sock
- ./traefik.toml:/traefik.toml
ports:
- "80:80"
- "8080:8080"
command: --api
main:
image: nginx
networks:
- mynet
deploy:
labels:
- "traefik.port=80"
- "traefik.backend=main"
- "traefik.frontend.rule=Host:domain.com"
two:
image: jwilder/whoami
networks:
- mynet
deploy:
labels:
- "traefik.port=8000"
- "traefik.backend=two"
- "traefik.frontend.rule=Host:two.domain.com"
three:
image: emilevauge/whoami
networks:
- mynet
deploy:
labels:
- "traefik.port=80"
- "traefik.backend=three"
- "traefik.frontend.rule=Host:three.domain.com"
user-service:
image: hollarves/users:latest
env_file:
- .env.user
networks:
- mynet
deploy:
labels:
- "traefik.port=80"
- "traefik.backend=users"
- "traefik.frontend.rule=Host:users.domain.com"
networks:
mynet:
driver: overlay
正如我所说,访问 two.domain.com 和 three.domain.com 可以正常工作,并且 whoami 容器会响应他们的信息。但是,尝试 users.domain.com 时出现连接被拒绝错误
注意:domain.com 是我正在使用的实际域,它指向一个 digitalocean 集群,我只是出于隐私原因将其隐藏。
此用户服务的入口点是:
if (process.env.NODE_ENV !== "production") {
require("dotenv").load()
}
const express = require("express"),
bodyParser = require("body-parser"),
logger = require("morgan"),
//helmet = require("helmet"),
cors = require("cors"),
PORT = parseInt(process.env.PORT, 10) || 80
const server = express(),
routes = require("./server/routes")
//server.use(helmet())
server.use(cors())
server.use(logger("dev"))
server.use(bodyParser.json())
server.use("/", routes)
/*eslint no-console: ["error", { allow: ["log"] }] */
const serverObj = server.listen(PORT, () => { console.log("Server listening in PORT ", PORT) })
module.exports = serverObj
我还可以确认该服务正在侦听 PORT 80,因为它是使用以下命令从中获取日志时输出的内容docker service logs test-stack_user-service
:
test-stack_user-service.1.35p3lxzovphr@node-2 | > users-mueve@0.0.1 start /usr/src/app
test-stack_user-service.1.35p3lxzovphr@node-2 | > node server.js
test-stack_user-service.1.35p3lxzovphr@node-2 |
test-stack_user-service.1.35p3lxzovphr@node-2 | Server listening in PORT 80
这是我的 traefik.toml 配置文件,以防万一:
debug = true
logLevel = "DEBUG"
defaultEntryPoints = ["http"]
[entryPoints]
[entryPoints.http]
address = ":80"
[retry]
[docker]
endpoint="unix:///var/run/docker.sock"
exposedByDefault=true
watch=true
swarmmode=true
我还可以像以前在本地环境中一样在 traefik 仪表板中看到容器。
我觉得我错过了一个非常小的细节,这会阻止我的服务正常工作。任何指针将不胜感激。
谢谢!
解决方案
推荐阅读
- css - CSS问题:在带有图像的框中居中文本
- google-cloud-platform - 异步批处理作业卡住了?
- angular - 如何在 Angular 6 中使用 select2?
- html - 自动调整和同步 HTML Bootstrap 表中的列宽
- reactjs - 如何避免在 create-react-app 中“找不到有效的摘要 ..”?
- c# - Unity/C#防止列表中每个字母多个条目(来自输入字段)
- javascript - 如何使用 javascript 在表中动态创建列
- c# - 即使 Inherits 匹配文件名,该名称在当前上下文中也不存在
- tomcat - 为什么 Tomcat 页面没有在 Firefox 和 chrome 中显示?
- react-native - 从 API 获取数据时遇到问题