2

I have a single kubernetes service called MyServices which hold four deployments. Each deployment is running as a single pod and each pod has its own port number.

enter image description here

As mentioned all the pods are running inside one kubernetes service.

I am able to call the services through the external IP Address of that kubernetes service and port number.

Example : 92.18.1.1:3011/MicroserviceA Or 92.18.1.1:3012/MicroserviceB

I am now trying to develop and orchestration layer that calls these services and get a response from them, However, I am trying to figure out a way in which I do NOT need to specify every micro-service port number, instead I can call them through their endpoint/ServiceName. Example: 192.168.1.1/MicroserviceA

How can I achieve above statement?

From architecture perspective, is it a good idea to deploy all microservice inside a single kubenetes service (like my current approach) or each micro-service needs it's own service

Below is the kubernetes deployment file ( I removed the script for micro-service C and D since they are identical to A and B):

apiVersion: v1
kind: Service
metadata:
  name: myservice
spec:
  selector:
    app: microservice
  ports:
    - name: microserviceA
      protocol: TCP
      port: 3011
      targetPort: 3011
    - name: microserviceB
      protocol: TCP
      port: 3012
      targetPort: 3012
    - name: microserviceC
      protocol: TCP
      port: 3013
      targetPort: 3013
    - name: microserviceD
      protocol: TCP
      port: 3014
      targetPort: 3014
  type: LoadBalancer
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: microserviceAdeployment
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: microservice
    spec:
      containers:
      - image: dockerhub.com/myimage:v1
        name: microservice
        ports:
        - containerPort: 3011
      imagePullSecrets:
      - name: regcred
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: microserviceBdeployment
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: microservice
    spec:
      containers:
      - image: dockerhub.com/myimage:v1
        name: microservice
        ports:
        - containerPort: 3012
Benjamin
  • 3,499
  • 8
  • 44
  • 77

1 Answers1

1

There is a way to discover all the port of Kubernetes services.

So you could consider using kubectl get svc, as seen in "Source IP for Services with Type=NodePort"

NODEPORT=$(kubectl get -o jsonpath="{.spec.ports[0].nodePort}" services <yourService>)

, I am trying to figure out a way in which I do NOT need to specify every micro-service port number, instead I can call them through their endpoint/ServiceName

Then you need to expose those services through one entry point, typically a reverse-proxy like NGiNX.
The idea is to expose said services using the default ports (80 or 443), and reverse-proxy them to the actual URL and port number.

Check "Service Discovery in a Microservices Architecture" for the general idea.

https://cdn-1.wp.nginx.com/wp-content/uploads/2016/04/Richardson-microservices-part4-1_difficult-service-discovery.png

And "Service Discovery for NGINX Plus with etcd" for an implementation (using NGiNX plus, so could be non-free).
Or "Setting up Nginx Ingress on Kubernetes" for a more manual approach.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • It is mentioned in the [official documentation of Kubernetes](https://kubernetes.io/docs/concepts/services-networking/service/#discovering-services) that it is possible to discover services through : 1) ENV Variables, 2 )DNS. I am interested in the second approach by not sure why it's not working. – Benjamin May 07 '18 at 04:52
  • @Benjamin I don't see the port being mentioned in the DNS approach. Why a `kubectl get svc` would not work here? – VonC May 07 '18 at 04:54
  • Okay, I think there is a little bit of confusion here. What I mean as "discovering a service" is to be able to call the POD that is running. as of now, in order for me to trigger the function that is running by the POD is to include the port number in the URL path, However I am looking for an approach which let me trigger the same thing without specifying it's hard-coded port. I can edit my question if you found it to not be clear. – Benjamin May 07 '18 at 04:58
  • @Benjamin How would you able to contact a pod without the right port number though? – VonC May 07 '18 at 05:00
  • I have completely edited my question. I hope now I am able to explain what I want. – Benjamin May 07 '18 at 05:39
  • @Benjamin OK. i have completed my answer accordingly. – VonC May 07 '18 at 06:40