Kubernetes Services - Nodeport

Kubernetes Services - Nodeport

NodePort Services in Kubernetes. 🚀

  1. NodePort Service:

    • A NodePort service is the most basic way to get external traffic directly to your service.

    • It opens a specific port on all the Nodes (the VMs) in your Kubernetes cluster.

    • Any traffic sent to this port is forwarded to the associated service

  2. How It Works:

    • When you create a Node Port Service, Kubernetes assigns a port within the range of 30000-32767.

    • End-users can access your application using the node’s IP address and the assigned port

Node Port Services provide a straightforward way to expose your application to the internet. They’re especially useful during development and testing. 🌐🔗

How do I create a Node Port Service in Kubernetes?

NodePort Service in Kubernetes, you have a couple of options:

  1. Using kubectl Command:

    • You can use the kubectl create service nodeport command to create a NodePort service.

    • Here’s an example:

        kubectl create service nodeport my-service --tcp=5678:8080
      
    • In this example, a new NodePort service named my-service is created, and traffic on port 5678 is forwarded to port 8080 in your Pods

  2. Using kubectl expose Command:

    • Another approach is to use the kubectl expose command with the --type="NodePort" parameter.

    • For instance:

    •   kubectl expose deployment/kubernetes-bootcamp --type="NodePort" --port 8080
      

How do I expose multiple ports using a Node Port service?

To expose multiple ports using a Node Port Service in Kubernetes, you can define multiple port definitions within the same service. Each port definition can have the same protocol or a different one. Here’s how you can achieve this:

  1. Using YAML Manifest:

    • Create a YAML file (e.g., my-service.yaml) with the following content:

        apiVersion: v1
        kind: Service
        metadata:
          name: my-service
        spec:
          selector:
            app: my-app
          ports:
          - name: http
            port: 80
            targetPort: 8080
          - name: custom-port
            port: 8081
            targetPort: 8081
          type: NodePort
      
    • In this example, we define two ports: 80 (for HTTP traffic) and 8081 (custom port).

    • Apply the YAML using kubectl apply -f my-service.yaml.

  2. Using kubectl expose Command:

    • Execute the following commands to expose multiple ports:

        kubectl expose deployment my-app --type=NodePort --name=my-service --port=80 --target-port=8080
        kubectl expose service my-service --port=8081 --target-port=8081
      
    • Replace my-app with your actual deployment name.

Remember to adjust the port numbers (80, 8080, and 8081) and labels (app: my-app) according to your specific use case. This way, your Node Port Service will handle traffic on both ports effectively. 🌐🔗