Exposing a k8s serivce with tcp

1/27/2021

I have an eks cluster, all up and working. I want to run a service which listens to tcp request on port 5000. I'm trying to read about it but all guides I could find are using http for the examples. I think I'm a bit confused with all the different concepts. If I define:

apiVersion: v1
kind: Service
metadata:
  name: my-service
spec:
  type: LoadBalancer
  ports:
    - port: 5000
      targetPort: 5000
      protocol: TCP

and run this (actually using helm), I can see on aws-console that a classic load balancer is created. but i can't ping it.

So basically, how do I create a network load balancer that forwards port 5000 to my service? it seems like this should be simple by I can't find how to do this. Eventually I want to have a staticIP for the nlb so I can send requests to that port. Do I need to install nginx (or other ingress controller) to make this work?

-- J. Doe
amazon-eks
kubernetes
nlb
tcp

1 Answer

1/27/2021

First of all you are on the correct way of creating a service with LoadBalancer type. Only missing thing by looking at Service.yaml contents is selector field in the Service object is missing by which your service doesn't know at which pods to forward traffic. Try adding it.

If you are going to have only single service exposed publicly, then LoadBalancer type makes sense. But if you are going to have multiple services then it would be costly to create multiple LoadBalancer services. In that case Nginx Ingress helps you with requiring only one LoadBalancer type service. Refer https://medium.com/better-programming/how-to-expose-your-services-with-kubernetes-ingress-7f34eb6c9b5a

-- Rushikesh
Source: StackOverflow