-1

I have created Deployment using Below Command

kubectl create deployment nginx --image=nginx --port=80 --replicas=3

I am looking for answer on how to Create Service to expose Individual Pods via NodePort on Nodes they are scheduled.

THIS COMMAND WILL NOT WORK: because this will send request to one of the pods

kubectl expose deploy nginx --name nginx-service --target-port=80 --type=NodePort

is there something that will create 3 separate service automatically one for each pod?

I tried this but thats not expected result

kubectl expose deploy nginx --name nginx-service --target-port=80 --type=NodePort
Amila Senadheera
  • 12,229
  • 15
  • 27
  • 43
Abhi
  • 1
  • when you create a node port service when you send traffic to that port in any node it will be internally load balanced and send to clusterip that the service has and then to any of the three pods in any node – Amila Senadheera Apr 01 '23 at 05:44
  • There are network plugins that give ip addresses to pods from host network CIDR. That will work for your case. Other option is you can create three deployments and three node port services – Amila Senadheera Apr 01 '23 at 05:52

1 Answers1

0

It's not clear to me what the need is, in the sense that the NodePort opens the port you define on all the nodes of the cluster, so if you have 3 replicas (3 Pods), you can reach the services individually by going through NODE-IP:NODE-PORT (NODE-IP of the nodes where the Pods reside obviously).

If you set the type field to NodePort, the Kubernetes control plane allocates a port from a range specified by --service-node-port-range flag (default: 30000-32767). Each node proxies that port (the same port number on every Node) into your Service.

https://kubernetes.io/docs/concepts/services-networking/service/#type-nodeport

https://kubernetes.io/docs/tasks/access-application-cluster/service-access-application-cluster/

Change slightly if you want to access NodePort services if you use minikube: https://minikube.sigs.k8s.io/docs/handbook/accessing/#getting-the-nodeport-using-the-service-command

glv
  • 994
  • 1
  • 1
  • 15