<p>Suppose I have a service containing two pods. One of the pods is an HTTP server, and the other pod needs to hit a REST endpoint on this pod. Is there a hostname that the second pod can use to address the first pod?</p>

Alex Flint
<p>I'm assuming when you say "service" you aren't referring to the Kubernetes lexicon of a <code>Service</code> object, otherwise your two Pods in the <code>Service</code> would be identical, so let's start by teasing out what a "Service" means in Kubernetes land. </p>

<p>You will have to create an additional <a href="https://kubernetes.io/docs/concepts/services-networking/service/" rel="noreferrer">Kubernetes object called a <code>Service</code></a> to get your hostname for your HTTP server's <code>Pod</code>. When you create a <code>Service</code> you will define a <code>.spec.selector</code> that points to a set of labels on the HTTP service's Pod. For the sake of example, let's say the label is <code>app: nginx</code>. The name of that <code>Service</code> object will become the internal DNS record that can be queried by the second <code>Pod</code>.</p>

<p>A simplified example:</p>

<pre><code>apiVersion: v1
kind: Pod
metadata:
  name: http-service
  labels:
    app: nginx
spec:
  containers:
  - name: nginx
    image: nginx:1.7.9
    ports:
    - containerPort: 80
---
kind: Service
apiVersion: v1
metadata:
  name: my-http-service
spec:
  selector:
    app: nginx
  ports:
  - protocol: TCP
    port: 80
    targetPort: 80
</code></pre>

<p>Now your second <code>Pod</code> can make requests to the HTTP service by the <code>Service</code> name, <code>my-http-service</code>. </p>

<p>It's also worth mentioning that Kubernetes best practice dictates that these <code>Pod</code>s be managed by controllers such as <a href="https://kubernetes.io/docs/concepts/workloads/controllers/deployment/" rel="noreferrer">Deployment</a>s or <a href="https://kubernetes.io/docs/concepts/workloads/controllers/replicaset/" rel="noreferrer">ReplicaSet</a>s for all sorts of reasons, including high availability of your applications.</p>
