I have to communicate between two PODs in minikube which are exposed in two different ports but are in a single node.
For example:
Now, in kubernetes it assigns a port dynamically eg: POD A: 30069 and POD B: 30070
Problem here is: it does not automatically map kubernetes port for POD B (30070) while accessing POD B from POD A(30069). Instead POD B tries to open in 8761 port.
Apologies if my description is confusing. Please feel free to recheck if you could not relate to my question.
Thanks for your help
If there is a correct mapping between the deployment and service name then just a curl request to name:port can be used for communication.
For example,
create a deployment
kubectl create deployment hello-node --image=gcr.io/hello-minikube-zero-install/hello-node
expose it on port 8080
kubectl expose deployment hello-node --type=NodePort --port=8080
another deployment with same image but different name
create deployment hello-node2 --image=gcr.io/hello-minikube-zero-install/hello-node
expose it on port 8080 expose deployment hello-node2 --type=NodePort --port=8080
get pods and start a terminal inside hello-node2 deployment
kubectl get pods
kubectl exec -it <hello-node2-pod-name> -- /bin/bash
you will enter in the container of hello world 2 pod
curl hello-node:8080 returns Hello World!
Also if you have a close look, kubectl describe service hello-node
gives you with an IP field (which is different form Endpoints). This is actually an exposed IP for communication to the pod. Which means inside hello world 2 container if you run
curl <IP from service> : 8080
returns Hello World!
Hope this helps.
I have to communicate between two PODs in minikube which are exposed in two different ports but are in a single node.
Based on the facts that you want inter-pod communication and that pods reside on the same node, you could have several (rather questionable and fragile) approaches such as hostname and nodePort exposures. In order to be more in line with kubernetes approach and recommendations I'd advise to use Service instead of exposing ports directly from Pod level.
You can read more about Services in the official documenatation and example for Service usage would be like so:
kind: Service
apiVersion: v1
metadata:
name: my-pod-b-service
spec:
selector:
app: MyPodBApp
ports:
- protocol: TCP
port: 80
targetPort: 8761
This specification will create a new Service object named my-pod-b-service
which targets TCP port 8761 on any Pod with the app=MyPodBApp
label. With that any request coming from pod A for host: my-pod-b-service
and port: 80
would be served by some pod B on port 8761
(note that port and targetPort can be the same, this is just an example).
As a side note, for pod A you would have something like:
kind: Service
apiVersion: v1
metadata:
name: my-pod-a-service
spec:
selector:
app: MyPodAApp
ports:
- protocol: TCP
port: 80
targetPort: 8080
Since you target services, you can map same incoming port (80) to both services, and kubernetes is taking care that each comes to appropriate pods, as long as pod selector is properly set on pods.