Communication Between Pods in Different Cluster in K8s using yaml

2.3k views Asked by At

Can some one provide References/Basic Idea how communication is done between pods in different clusters. Suppose Cluster A has Pod A and Cluster B has Pod B. So how we can ensure Pod A can communicate with Pod B using yaml? -Thanks in Advance

1

There are 1 answers

0
Dawid Kruk On

Posting this answer as a community wiki for the better visibility and to add some additional resources as the solution was posted in the comments by user @David Maze:

If the pods are in different clusters, they can't directly communicate with each other (without using NodePort or LoadBalancer services, or otherwise making the destination service accessible from outside its own cluster).


With the most common setups the way to communicate Pod1 from Cluster1 with Pod2 with Cluster2 would be to use:

  • Service of type NodePort
  • Service of type LoadBalancer
  • Ingress resource - specific to HTTP/HTTPS traffic

All of the above solutions will heavily depend on where your Kubernetes cluster is deployed.

For example:

With cloud solutions like GKE, AKS, EKS you can use service type of LoadBalancer or Ingress resource to direct the traffic to your pod.

With bare metal solution you would need to use additional tools like MetalLB to use service of type LoadBalancer

You could also look on this resources:


As for an example assume that you have 2 Kubernetes clusters that can expose traffic with service of type LoadBalancer.

Apply on first cluster:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  selector:
    matchLabels:
      app: nginx
  replicas: 3 
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx
        ports:
        - containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
  name: nginx-service
spec:
  selector:
    app: nginx
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80
  type: LoadBalancer

Check the EXTERNAL-IP associated with the service:

  • $ kubectl get service nginx-service
NAME            TYPE           CLUSTER-IP    EXTERNAL-IP   PORT(S)        AGE
nginx-service   LoadBalancer   10.92.10.48   A.B.C.D     80:30994/TCP   26s

Switch to second cluster and run:

  • $ kubectl run -it ubuntu --image=ubuntu -- /bin/bash
  • $ apt update && apt install curl
  • $ curl A.B.C.D

You should be able to see:

<--- REDACTED ---> 
<p><em>Thank you for using nginx.</em></p>
<--- REDACTED ---> 

Additional resources: