首页 > 解决方案 > 带有 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 仪表板中看到容器。

我觉得我错过了一个非常小的细节,这会阻止我的服务正常工作。任何指针将不胜感激。

谢谢!

标签: expressubuntumicroservicesdocker-swarmtraefik

解决方案


推荐阅读