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

后端 未结 1 2038
故里飘歌
故里飘歌 2021-01-26 23:31

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

1条回答
  •  走了就别回头了
    2021-01-26 23:37

    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.

    0 讨论(0)
提交回复
热议问题