Discovering Kubernetes Pod without specifying port number

5/7/2018

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
kubernetes

1 Answer

5/7/2018

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
Source: StackOverflow