问题描述
从kubernetes 文档:
From the kubernetes docs:
是否可以使用某些容器特定的名称代替locahost
?
例如,对于docker-compose up
,您使用服务名称进行通信. [docs]
For example, with docker-compose up
, you use name of the service to communicate. [docs]
因此,如果我的 docker-compose.yml 文件是
version: '2'
services:
web:
build: .
ports:
- "8000:8000"
srv:
build: .
ports:
- "3000:3000"
然后我通过调用http://srv:3000/
从web
内部访问srv
,不 http://localhost:3000
Then I access srv
from within web
by calling http://srv:3000/
, not http://localhost:3000
如何在kubernetes中实现相同的行为?有什么方法可以指定在Pod的Yaml配置中使用什么名称?
How can I achieve the same behaviour in kubernetes? Any way to specify what name to use in pods' yaml configuration?
推荐答案
localhost
只是网络环回设备的名称(对于IPv4,通常是127.0.0.1
;对于IPv6,通常是::1
).通常在您的/etc/hosts
文件中指定.
localhost
is just a name for the network loopback device (usually 127.0.0.1
for IPv4 and ::1
for IPv6). This is usually specified in your /etc/hosts
file.
pod有其自己的IP,因此内部的每个容器都共享该IP.如果这些容器应该是独立的(即不需要并置),则它们每个都应位于各自的容器中.然后,您可以为每个允许DNS的对象定义一个服务在同一名称空间中的Pod中查找为"$ SERVICENAME",在不同名称空间中的Pod中查找为"$ SERVICENAME.$ NAMESPACE".
A pod has its own IP, so each container inside shares that IP. If these containers should be independent (i.e. don't need to be collocated), they should each be in their own pod. Then, you can define a service for each that allows DNS lookups as either "$SERVICENAME" from pods in the same namespace, or "$SERVICENAME.$NAMESPACE" from pods in different namespaces.
这篇关于Kubernetes-使用名称而不是'localhost'的Pod中的容器通信?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!