How to use application host and port on runtime

12/9/2019

I’ve two different apps in k8s that need needs to read data,e.g. AppA from AppB, both are deployed on the same cluster

The tricky part here is that I need the both apps will deployed to any cluster and know the host and port to connect, I don't want to use hard-coded values.

e.g.

App A reads data from App B

App B is web application with rest API hence app A needs to call like http://10.26.131.136:9090/api/app/getconfig

App A know the service path: like api/app/getconfig of App B but how it can know the the host and the port of appB?

I cannot use it hardcoded , this works if I use type:LoadBalacer but this is hard-coded host and port, I need somehow to determine it on run-time maybe with serviceName, etc?

-- Nina S
go
kubernetes

5 Answers

12/9/2019

Note: The Kube-DNS naming convention is service.namespace.svc.cluster-domain.tld and the default cluster domain is cluster.local

So you can refer to your app as ..svc as long as the services are in the same cluster. You then need to check the ports on which the apps are listening by issuing:

kubectl -n <namespace> get svc

note the service identifier and issue:

kubectl -n <namespace> get svc <identifier> -o yaml

This will list you the service manifest where you can see which port the app is listening on.

-- julian
Source: StackOverflow

12/9/2019

Create a ClusterIP service for appB. hardcode the port, this port is only for that specific service, and will not have any conflict in different clusters. do kubectl get svc <your service> to get the ip of the service, then use nslookup <ip> you can see an fqdn in the result. Normally, the fqdn is <your-service>.<namespace>.svc.cluster.local Ref: https://kubernetes.io/docs/concepts/services-networking/service/

-- C Han
Source: StackOverflow

12/9/2019

If you want to make it so tolerant (from any cluster to any cluster), then you need a DNS, with an external IP address in front of the App B.

I would consider hardcoding somewhere. With hardcoding it in one ConfigMap, you could get some beautiful implementations.

-- suren
Source: StackOverflow

12/9/2019

Define a Service for your application B. Then you can access it from other pods with the following url: your-service-name:port/api/app/getconfig

-- Jadro
Source: StackOverflow

12/9/2019

If you have 2 apps on the same cluster then they can reference eachother via kubernetes' built in DNS.

If AppA needs to reference AppB, then as long as you have a Service defined for AppB, you can reference it with <service>.<namespace>. In your case, if AppB was running the in the default namespace you could call AppB.default/api/app/getconfig.

The port part can be managed by your service definition. Below I'm using port 80 so I don't have to manually specify the port in my requests

apiVersion: v1
kind: Service
metadata:
  name: AppB
spec:
  ports:
  - name: "80"
    port: 80
    targetPort: 9090
  selector:
    select: AppB

More information: https://kubernetes.io/docs/concepts/services-networking/dns-pod-service/

-- Connor Graham
Source: StackOverflow