我可以从Kubernetes中另一个节点上运行的另一个容器通过其主机名访问一个容器吗? [英] Can I reach a container by it's hostname from another container running on another node in Kubernetes?

查看:427
本文介绍了我可以从Kubernetes中另一个节点上运行的另一个容器通过其主机名访问一个容器吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我相信我的问题很简单.我正在做在裸机上安装Kubernetes集群的先决条件.

I believe my question is pretty straightforward. I'm doing my prerequisites to install Kubernetes cluster on bare metal.

假设我有:

master -Docker DB容器的主机名,该主机名固定在第一个节点上

master - hostname for Docker DB container which is fixed on first node

从属-Docker DB容器的主机名,固定在第二个节点上

slave - hostname for Docker DB container which is fixed on second node

无论集群是否在同一节点上运行,我是否可以从集群中的任何容器(应用程序等)与 master 通信?

Can I communicate with master from any container (app, etc.) in a cluster regardless it's running on the same node or not?

这是默认行为吗? 还是应该采取其他措施?

Is this a default behaviour? Or anything additional should be done?

我假设我需要在YAML或JSON文件中设置hostname参数,以便Kubernetes知道主机名是什么.

I assume that I need to setup hostname parameter in YAML or JSON file so Kubernetes is aware what the hostname is.

可能这不是因素,但我计划使用Kubespray安装方法,以便为k8s获得Calico网络.

Probably this is not the factor, but I plan to use Kubespray installation method so it gets Calico networking for k8s.

非常感谢

推荐答案

您可以通过主机名从namespace中的任何容器访问和通信.

You can access and communication from any container in a namespace via hostname.

以下是有关Kubernetes Service configure的示例:

Here is an example about Kubernetes Service configure:

---
apiVersion: v1
kind: Service
metadata:
  name: master
  labels:
    name: master
  namespace: smart-office
spec:
  ports:
  - port: 5672
    name: master
    targetPort: 5672
  selector:
    name: master

Deployment配置:

---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: master
  labels:
    name: master
  namespace: smart-office
spec:
  replicas: 1
  template:
    metadata:
      labels:
        name: master
      annotations:
        prometheus.io/scrape: "false"
    spec:
      containers:
      - name: master
        image: rabbitmq:3.6.8-management
        ports:
        - containerPort: 5672
          name: master
      nodeSelector:
        beta.kubernetes.io/os: linux

从其他服务中,例如,您的slaver .env将是:

And from other services, for e.g your slaver .env will be:

AMQP_HOST=master <---- The hostname
AMQP_PORT=5672
AMQP_USERNAME=guest
AMQP_PASSWORD=guest
AMQP_HEARTBEAT=60

即使您不发布外部IP,它也可以在Cluster内部工作.

It's will work inside Cluster even if you not publish External IP.

希望这可以为您提供帮助.

Hope this can help you.

这篇关于我可以从Kubernetes中另一个节点上运行的另一个容器通过其主机名访问一个容器吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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