kubernetes

How to create NodePort service in Kubernetes?

Let’s understand what contents make a NodePort service and what exactly it means.

apiVersion: v1
kind: Service
metadata:
  name: <service_name>
  namespace: <namespace_name>
spec:
  type: NodePort
  selector:
          app: <value name>
  ports:
  - protocol: TCP
    port: 18080
    targetPort: 18080
    nodePort: 30001
  • apiVersion represents from which group Kind belongs.
  • kind represents the type of Kubernetes objects.
  • metadata defines the details of the service, such as, in which namespace you want to create the service, what should be the name of the service, etc.
  • spec defines the specification for the service.
    • The service type is defined by type.
      • NodePort – It serves as the external entry point for incoming requests for your app.
    • selector, which is in charge of selecting all pods whose values are defined in the key(app).
    • ports, define the port on which communication establish.
      • protocol, define which type of protocol wants to use.
      • port, define for cluster mode access.
      • targetPort, defines the container port, on which the application is running.
      • nodePort, defines the port by which external users can communicate with the application. It’s open on all nodes of the cluster. If you do not add nodePort spec then it will randomly choose a port for it.

To create the service in the Kubernetes cluster run the below command

kubectl create -f <service.yaml>

MtroView team is giving its best & working hard to provide the content to help you understand things better in simple terms. We are heartily open to receiving suggestions to improve our content to help more people in society. Please write to us in the form of a comment or can also send an email to mtroview@gmail.com

Leave a Comment

Your email address will not be published. Required fields are marked *