<p>I am running a simple app based on an api and web interface in Kubernetes. However, I can't seem to get the api to talk to the web interface. In my local environment, I just define a variable API_URL in the web interface with eg. localhost:5001 and the web interface correctly connects to the api. As api and web are running in different pods I need to make them talk to each other via services in Kubernetes. So far, this is what I am doing, but without any luck.</p>

<p>I set-up a deployment for the API</p>

<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: api-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      component: api
  template:
    metadata:
      labels:
        component: api
    spec:
      containers:
      - name: api
        image: gcr.io/myproject-22edwx23/api:latest
      ports:
      - containerPort: 5001
</code></pre>

<p>I attach a service to it:</p>

<pre><code>apiVersion: v1
kind: Service
metadata:
  name: api-cluster-ip-service
spec:
  type: NodePort
  selector:
    component: api
  ports:
    - port: 5001
      targetPort: 5001
</code></pre>

<p>and then create a web deployment that should connect to this api.</p>

<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: web-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      component: web
  template:
    metadata:
      labels:
        component: web
    spec:
      containers:
      - name: web
        image: gcr.io/myproject-22edwx23/web:latest
        ports:
        - containerPort: 5000
        env:
          - name: API_URL
            value: http://api-cluster-ip-service:5001
</code></pre>

<p>afterwards, I add a service for the web interface + ingress etc but that seems irrelevant for the issues. I am wondering if the setting of API_URL correctly picks up the host of the api via <a href="http://api-cluster-ip-service:5001" rel="nofollow noreferrer">http://api-cluster-ip-service:5001</a>?</p>

<p>Or can I not rely on Kubernetes getting the appropriate dns for the api and should the web app call the api via the public internet.</p>

<p>If you want to check <em>API_URL</em> variable value, simply run</p>
<pre><code>kubectl exec -it web-deployment-pod env | grep API_URL
</code></pre>
<p>The kube-dns service listens for service and endpoint events from the Kubernetes API and updates its DNS records as needed. These events are triggered when you create, update or delete Kubernetes services and their associated pods.</p>
<p>kubelet sets each new pod's search option in <strong>/etc/resolv.conf</strong></p>
<p>Still, if you want to http from one pod to another via cluster service it is recommended to refer service's ClusterIP as follows</p>
<pre><code>api-cluster-ip-service.default.svc.cluster.local
</code></pre>
<p>You should have service IP assigned to env variable within your web pod, so there's no need to re-invent it:</p>
<pre><code>sukhoversha@sukhoversha:~/GCP$ kk exec -it web-deployment-675f8fcf69-xmqt8 env | grep -i service
API_CLUSTER_IP_SERVICE_PORT=tcp://10.31.253.149:5001
API_CLUSTER_IP_SERVICE_PORT_5001_TCP=tcp://10.31.253.149:5001
API_CLUSTER_IP_SERVICE_PORT_5001_TCP_PORT=5001
API_CLUSTER_IP_SERVICE_PORT_5001_TCP_ADDR=10.31.253.149
</code></pre>
<p>To read more about <a href="https://kubernetes.io/docs/concepts/services-networking/dns-pod-service/#a-records" rel="nofollow noreferrer">DNS for Services</a>.  A service defines <a href="https://kubernetes.io/docs/concepts/services-networking/service/#environment-variables" rel="nofollow noreferrer">environment variables naming the host and port</a>.</p>
