gpt4 book ai didi

port - 端口和服务端口的区别?

转载 作者:行者123 更新时间:2023-12-04 16:58:56 24 4
gpt4 key购买 nike

在探索马拉松 REST API 时,我遇到了以下 API 调用给出的信息(JSON 输出)中定义的两个端口号(端口和服务端口)-curl http://x.y.z.w:8080/v2/tasks | python -m json.tool | less示例输出如下:

{
"tasks":[
{
"appId":"/test",
"host":"172.20.75.145",
"id":"test.1fc922a9-f4c8-11e5-8bff-005056a76a7f",
"ipAddresses":[

],
"ports":[
31313
],
"servicePorts":[
10000
],
"slaveId":"2130f59b-7289-40eb-b24d-72f0c6fe94c8-S1",
"stagedAt":"2016-03-28T09:33:26.859Z",
"startedAt":"2016-03-28T09:33:26.936Z",
"version":"2016-03-28T09:33:26.800Z"
}
]
}

有人知道这里的端口和服务端口之间的区别吗?还请添加更多信息。

最佳答案

根据马拉松文件:

servicePort: When you create a new application in Marathon (either through the REST API or the front end), you may assign one or more service ports to it. You can specify all valid port numbers as service ports or you can use 0 to indicate that Marathon should allocate free service ports to the app automatically. If you do choose your own service port, you have to ensure yourself that it is unique across all of your applications.



让我根据 Marathon 的两个主要网络配置来详细说明这一点,并在我做这些的时候提供一些关于它们的信息。

主机模式

使用 :Docker 应用程序的默认选项,非 Docker 应用程序的唯一选项

在此配置下,您的应用程序将直接绑定(bind)到主机的端口。

不使用服务端口

您可以要求 Marathon 为您提供主机的任意两个端口,您希望将其提供给您的应用程序。在应用程序配置文件中有两种方法可以做到这一点:
"ports": [
0, 0
],

或者
"portDefinitions": [
{"port": 0}, {"port": 0}
],

通过这样做,Marathon 将从可用端口范围中保留两个端口并将它们分配给环境变量 PORT1 , 和 PORT2 .
然后很容易直接在您的 Dockerfile 中调用它们。 :
CMD ./launch.sh --listen-on $PORT1 --monitor-on $PORT2

或在您的 cmd Marathon 配置中的定义:
"cmd": "./launch.sh --listen-on $PORT1 --monitor-on $PORT2"

使用服务端口

假设您在多个主机上运行您的应用程序(运行多个实例),并且您希望能够在特定端口的任何主机上连接到您的应用程序。那就是服务端口出现在图片中的时候。
通过写入配置文件:
"ports": [
3000, 3001
],

或者:
"portDefinitions": [
{"port": 3000}, {"port": 3001}
],

... Marathon 仍然会在主机上分配随机端口,它仍然会将它们分配给环境变量 PORT1PORT2 , 而且它还会保留端口 3000 和 3001 供您使用。

您可以使用服务发现机制将流量从这些服务端口路由到 $PORT1$PORT2 .

您可以使服务端口等于主机端口(例如,如果您想避免使用服务发现机制),并且跨主机为您的应用程序提供一致的端口。您可以添加 "requirePorts": true在您的端口规范之后。
这里需要注意的是,Marathon 只能将您的应用程序安排在具有这些端口可用的节点中。

桥接模式

使用 : 仅适用于 Docker 应用程序

在此配置下,一些指定的容器端口绑定(bind)到主机端口。

不使用服务端口

在这种模式下,您不使用“ports”或“portDefinitions”指令,而是使用“portMappings”。通过这样做,您实际上是在告诉 Docker 将流量从特定容器端口映射到主机端口,反之亦然。

您可以通过指定将容器端口映射到主机端口:
"portMappings": [
{ "containerPort": 80, "hostPort": 0, "protocol": "tcp"},
{ "containerPort": 443, "hostPort": 0, "protocol": "tcp"}
]

在这种情况下:
- 设置 hostPort为 0 使其再次从
可用端口范围。这些端口再次分配给 PORT1
PORT2 env 变量。
- 设置 containerPort为 0 将使其等于 hostPort .

使用服务端口

和以前一样,您可以通过在配置中指定它们来启用跨主机的应用程序一致的服务端口,如下所示:
"portMappings": [
{ "containerPort": 80, "hostPort": 0, "protocol": "tcp", "servicePort": 3000},
{ "containerPort": 443, "hostPort": 0, "protocol": "tcp", "servicePort": 3001}
]

再次由您决定使用服务发现机制将流量从这些服务端口路由到 $PORT1。和 $PORT2 .

有关更多信息,请参阅:

马拉松端口:
https://mesosphere.github.io/marathon/docs/ports.html
Docker主机模式:
http://www.dasblinkenlichten.com/docker-networking-101-host-mode/
Docker 桥接模式:
http://www.dasblinkenlichten.com/docker-networking-101/

关于port - 端口和服务端口的区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36265502/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com