Access pod from another pod kubernetes

4/24/2019

I have 2 pods created. one is grafana and another is influx pod. I need to configure influx in grafana. I did see the below example. I got bit confused by the way its configured. Below is deployment and service file.

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: influxdb
  labels:
    app: influxdb
spec:
  template:
    metadata:
      labels:
        app: influxdb
    spec:
      containers:
      - name: influxdb
        image: influxdb
        ports:
        - containerPort: 8083
          name: admin
        - containerPort: 8086
          name: http
        resources:
          limits:
            memory: 2048Mi
            cpu: 100m  
        volumeMounts:
        - name: influxdb-data
          mountPath: /var/lib/influxdb
      volumes:
      - name: influxdb-data
        persistentVolumeClaim:
          claimName: influxdb-pvc-vol

Service file

apiVersion: v1
kind: Service
metadata:
  name: influxdb
  labels:
    app: influxdb
spec:
  ports:
    - port: 3306
  selector:
    app: influxdb
  clusterIP: None

What does clusterIP: None do? he has exposed 3306 port and mapped it to node port 3306. So i believe i can access from other pod using 3306 port and its IP. But here i see i am able to access via http://influxdb:8086 How am i able to access via http://influxdb:8086?

-- Hacker
kubernetes

2 Answers

4/24/2019

A service that defines the ClusterIP:none is known as a headless service

For headless services that define selectors, the endpoints controller creates Endpoints records in the API, and modifies the DNS configuration to return A records (addresses) that point directly to the Pods backing the Service

Since there is only one influxdb pod, there is no need to load balance it and so no need for a service proxy, requests are routed directly to the pod.

-- Dan Murphy
Source: StackOverflow

4/24/2019

I can explain what's happening and why this works, but I still think this configuration doesn't make sense.

The Deployment creates a Pod that runs InfluxDB which listens by default on port 8086. The containerPort here is purely informational, see the following from the Pod spec reference:

primarily informational. Not specifying a port here DOES NOT prevent that port from being exposed. Any port which is listening on the default "0.0.0.0" address inside a container will be accessible from the network.

Now to the Service, which is created with a port 3306, which is odd but in this case doesn't matter because this is a Headless Service. A headless service is a means to tell Kubernetes you don't want it's fancy networking features (like kube-proxy load balancing), instead you just want it to create DNS records for you. By specifying ClusterIP: None you essentially make this a headless service. Given that this service is not actually serving any traffic, the "Port" field here is meaningless.

Now let's review what happens when you access http://influxdb:8086:

  1. your http client resolves the host influxdb to the Pod IP. This is possible thanks to the headless service. Note again that the host resolves to the Pod IP, not a Service IP.
  2. Since the Pod is serving on 8086, and since you reached it directly in it's private IP, it accepts your request and you have your reply.
-- itaysk
Source: StackOverflow