【问题标题】:How not to expose port outside docker swarm cluster?如何不暴露 docker swarm 集群之外的端口?
【发布时间】:2021-12-06 14:13:26
【问题描述】:

您好,我如何在撰写端口中定义仅用于集群内的通信?我不想在集群外公开我的应用程序?

version: "3.8"
services:
  management:
    image: ${IMAGE}
    env_file:
      - vars.env
    networks:
      - my-network
    ports:
      - 8080:80
    deploy:
      placement:
        constraints: [node.role == manager]
      replicas: 1
      update_config:
        parallelism: 2
      restart_policy:
        condition: on-failure

networks:
  my-network:
    external: true
ports:
  - 8080:80

会将我的应用程序暴露在 swarm 集群之外。我可以让我的应用程序只能通过端口 80 在集群内部访问吗?

【问题讨论】:

    标签: docker docker-compose docker-swarm


    【解决方案1】:

    只需从ports 部分中省略您不希望在入口网络之外公开的端口。 nginx.yaml 示例:

    version: "3.9"
    services:
      nginx-1:
        image: nginx:alpine 
      nginx-2:
        image: nginx:alpine
    

    docker stack deploy -c nginx.yaml nginx

    docker stack ps nginx

    docker exec -t <nginx-1 container ID> wget -qO- nginx-2

    该命令将相应地打印从 nginx-2 到 nginx-1 的响应。不必在ports 部分下列出端口以访问 Swarm 网络中的容器端口。

    对于您的情况,这意味着您的示例规范不需要 ports 部分,因为您不希望将 80 暴露在 Swarm 网络之外。

    【讨论】:

    • 但是在这种情况下我如何访问集群内的服务呢?如果我什至不定义目标/容器端口?
    • 使用工作示例检查更新的答案。
    • 感谢您的精彩解释!
    猜你喜欢
    • 2019-06-22
    • 2019-06-04
    • 1970-01-01
    • 2018-09-03
    • 1970-01-01
    • 2020-11-13
    • 2019-10-24
    • 1970-01-01
    • 2019-11-21
    相关资源
    最近更新 更多