Can I reach a container by it's hostname from another container running on another node in Kubernetes?

懵懂的女人 提交于 2019-12-04 05:17:21

问题


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

Let's say I have:

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

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

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?

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

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

Many thanks


回答1:


Yes,

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

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 configure:

---
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

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

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

Hope this can help you.



来源:https://stackoverflow.com/questions/55677937/can-i-reach-a-container-by-its-hostname-from-another-container-running-on-anoth

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!