Home > Net >  How to have pod to pod communication in kubernetes using service?
How to have pod to pod communication in kubernetes using service?

Time:11-25

Hi I am working in kubernetes. I have two pods running. I want to call another pod from one pod. I am trying as below

HttpClient req = new HttpClient();
            var content = await req.GetAsync("https://cepserviceone.cep-dev.svc.cluster.local/api/values");
            string response = await content.Content.ReadAsStringAsync();
            return response;

I have exposed both services as cluster IP as below.

apiVersion: v1
kind: Service
metadata:
  name: cep #### Insert your application service name here ####
  namespace: cep-dev #### Insert your application's namespace. Omit this line to use default namespace. ####
  labels:
    app: cep #### Insert your application service name here ####
spec:
  # Use one of ClusterIP, LoadBalancer or NodePort. See https://kubernetes.io/docs/concepts/services-networking/service/
  type: ClusterIP
  selector:
    app: cep   #### Insert your application deployment name here. This must match the deployment name specified in the deployment manifest ####
    instance: app
  ports:
    - port: 8080 #### Replace with appropriate port
      targetPort: 80 #### Replace with the port name defined in deployment

This is another service

apiVersion: v1
kind: Service
metadata:
  name: cepserviceone #### Insert your application service name here ####
  namespace: cep-dev #### Insert your application's namespace. Omit this line to use default namespace. ####
  labels:
    app: cepserviceone #### Insert your application service name here ####
spec:
  # Use one of ClusterIP, LoadBalancer or NodePort. See https://kubernetes.io/docs/concepts/services-networking/service/
  type: ClusterIP
  selector:
    app: cepservice   #### Insert your application deployment name here. This must match the deployment name specified in the deployment manifest ####
    instance: app
  ports:
    - port: 8080 #### Replace with appropriate port
      targetPort: 80 #### Replace with the port name defined in deployment

I have ingress which routes requests accordingly. When I try to access serviceone application I get below error

An invalid request URI was provided. Either the request URI must be an absolute URI or BaseAddress must be set.

May I know what wrong I am doing here? Any help would be greatly appreciated. Thanks

CodePudding user response:

Use your service port 8080:

var content = await req.GetAsync("https://cepserviceone.cep-dev.svc.cluster.local:8080/api/values");

  • Related