Kubernetes to find Pod IP from another Pod

8/30/2018

I have the following pods hello-abc and hello-def.

And I want to send data from hello-abc to hello-def.

How would pod hello-abc know the IP address of hello-def?

And I want to do this programmatically.

What's the easiest way for hello-abc to find where hello-def?

---
apiVersion: extensions/v1beta1
kind: Deployment

metadata:
name: hello-abc-deployment

spec:
replicas: 1

template:
    metadata:
    labels:
        app: hello-abc
    spec:
    containers:
    - name: hello-abc
        image: hello-abc:v0.0.1
        imagePullPolicy: Always
        args: ["/hello-abc"]
        ports:
        - containerPort: 5000

---
apiVersion: extensions/v1beta1
kind: Deployment

metadata:
name: hello-def-deployment

spec:
replicas: 1

template:
    metadata:
    labels:
        app: hello-def
    spec:
    containers:
    - name: hello-def
        image: hello-def:v0.0.1
        imagePullPolicy: Always
        args: ["/hello-def"]
        ports:
        - containerPort: 5001

---
apiVersion: v1
kind: Service

metadata:
name: hello-abc-service

spec:
ports:
- port: 80
    targetPort: 5000
    protocol: TCP

selector:
    app: hello-abc
type: NodePort

---
apiVersion: v1
kind: Service

metadata:
name: hello-def-service

spec:
ports:
- port: 80
    targetPort: 5001
    protocol: TCP

selector:
    app: hello-def
type: NodePort
-- enerudfwqenq
kubernetes
kubernetes-ingress

2 Answers

8/30/2018

Preface

Since you have defined a service that routes to each deployment, if you have deployed both services and deployments into the same namespace, you can in many modern kubernetes clusters take advantage of kube-dns and simply refer to the service by name.

Unfortunately if kube-dns is not configured in your cluster (although it is unlikely) you cannot refer to it by name.

You can read more about DNS records for services here

In addition Kubernetes features "Service Discovery" Which exposes the ports and ips of your services into any container which is deployed into the same namespace.

Solution

This means, to reach hello-def you can do so like this

curl http://hello-def-service:${HELLO_DEF_SERVICE_PORT}

based on Service Discovery https://kubernetes.io/docs/concepts/services-networking/service/#environment-variables

Caveat: Its very possible that if the Service port changes, only pods that are created after the change in the same namespace will receive the new environment variables.

External Access

In addition, you can also reach this your service externally since you are using the NodePort feature, as long as your NodePort range is accessible from outside.

This would require you to access your service by node-ip:nodePort

You can find out the NodePort which was randomly assigned to your service with kubectl describe svc/hello-def-service

Ingress

To reach your service from outside you should implement an ingress service such as nginx-ingress

https://github.com/helm/charts/tree/master/stable/nginx-ingress https://github.com/kubernetes/ingress-nginx

Sidecar

If your 2 services are tightly coupled, you can include both in the same pod using the Kubernetes Sidecar feature. In this case, both containers in the pod would share the same virtual network adapter and accessible via localhost:$port

https://kubernetes.io/docs/concepts/workloads/pods/pod/#uses-of-pods


Service Discovery

When a Pod is run on a Node, the kubelet adds a set of environment variables for each active Service. It supports both Docker links compatible variables (see makeLinkVariables) and simpler {SVCNAME}_SERVICE_HOST and {SVCNAME}_SERVICE_PORT variables, where the Service name is upper-cased and dashes are converted to underscores.

Read more about service discovery here: https://kubernetes.io/docs/concepts/services-networking/service/#environment-variables

-- yosefrow
Source: StackOverflow

8/30/2018

You should be able to reach hello-def-service from pods in hello-abc via DNS as specified here: https://kubernetes.io/docs/concepts/services-networking/dns-pod-service/#services

However, kube-dns or CoreDNS has to be configured/installed in your k8s cluster before DNS records can be utilized in your cluster.

Specifically, you should be reach hello-def-service via the DNS record http://hello-def-service for the service running in the same namespace as hello-abc-service

And you should be able to reach hello-def-service running in another namespace ohter_namespace via the DNS record hello-def-service.other_namespace.svc.cluster.local.

If, for some reason, you do not have DNS add-ons installed in your cluster, you still can find the virtual IP of the hello-def-service via environment variables in hello-abc pods. As is documented here.

-- Xiao Liang
Source: StackOverflow