Kubernetes-使用名称而不是'localhost'的Pod中的容器通信? [英] Kubernetes - container communication within a pod using names instead of 'localhost'?

查看:137
本文介绍了Kubernetes-使用名称而不是'localhost'的Pod中的容器通信?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从kubernetes 文档:

From the kubernetes docs:

pod中的所有应用程序都使用相同的网络名称空间(相同的IP和端口空间),因此可以彼此"查找"并使用localhost进行通信.

The applications in a pod all use the same network namespace (same IP and port space), and can thus "find" each other and communicate using localhost.

是否可以使用某些容器特定的名称代替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中的容器通信?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆