Bird
Raised Fist0
Kubernetesdevops~20 mins

NodePort service type in Kubernetes - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
NodePort Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
Understanding NodePort Service Exposure

What is the primary purpose of a NodePort service in Kubernetes?

ATo expose a service on a static port on each node's IP, allowing external traffic to access the service.
BTo expose a service only via DNS inside the cluster.
CTo automatically assign a load balancer IP from a cloud provider.
DTo create an internal-only service accessible only within the cluster.
Attempts:
2 left
💡 Hint

Think about how external users can reach a service running inside Kubernetes nodes.

💻 Command Output
intermediate
1:00remaining
NodePort Service Port Range

What is the default port range for NodePort services in Kubernetes?

A80-8080
B1024-65535
C1-1023
D30000-32767
Attempts:
2 left
💡 Hint

NodePort ports are usually in a high range to avoid conflicts with well-known ports.

❓ Configuration
advanced
2:30remaining
Correct NodePort Service YAML

Which YAML snippet correctly defines a NodePort service exposing port 8080 on target pods?

A
apiVersion: v1
kind: Service
metadata:
  name: my-nodeport-service
spec:
  type: NodePort
  selector:
    app: myapp
  ports:
    - port: 8080
      targetPort: 8080
      nodePort: 30080
B
apiVersion: v1
kind: Service
metadata:
  name: my-nodeport-service
spec:
  type: ClusterIP
  selector:
    app: myapp
  ports:
    - port: 8080
      targetPort: 8080
      nodePort: 30080
C
apiVersion: v1
kind: Service
metadata:
  name: my-nodeport-service
spec:
  type: NodePort
  selector:
    app: myapp
  ports:
    - port: 80
      targetPort: 8080
      nodePort: 30080
D
apiVersion: v1
kind: Service
metadata:
  name: my-nodeport-service
spec:
  type: NodePort
  selector:
    app: myapp
  ports:
    - port: 8080
      targetPort: 80
      nodePort: 30080
Attempts:
2 left
💡 Hint

Check that the service type is NodePort and ports match the target pod port.

❓ Troubleshoot
advanced
2:00remaining
NodePort Service Not Accessible Externally

You created a NodePort service on port 30080, but external clients cannot access it. Which is the most likely cause?

AThe service type is incorrectly set to ClusterIP.
BThe cluster nodes' firewall is blocking port 30080.
CThe pod selector labels do not match any pods.
DThe nodePort value is outside the allowed range.
Attempts:
2 left
💡 Hint

Think about network rules outside Kubernetes that might block traffic.

✅ Best Practice
expert
3:00remaining
Best Practice for NodePort Usage in Production

Which practice is recommended when using NodePort services in a production Kubernetes environment?

AExpose NodePort services on all nodes without any firewall restrictions.
BAlways assign nodePort values below 1024 for better compatibility.
CUse NodePort only for development or testing; use LoadBalancer or Ingress for production traffic.
DUse NodePort services to expose database ports directly to the internet.
Attempts:
2 left
💡 Hint

Consider security and scalability for production environments.

Practice

(1/5)
1. What does a Kubernetes NodePort service do?
easy
A. Exposes the service on a static port on each node's IP address
B. Creates an internal cluster IP only accessible inside the cluster
C. Routes traffic through an external load balancer automatically
D. Runs the service only on a single node in the cluster

Solution

  1. Step 1: Understand NodePort service purpose

    A NodePort service exposes the application on a fixed port on every node's IP, allowing external access.
  2. Step 2: Compare with other service types

    ClusterIP is internal only, LoadBalancer uses external balancers, and NodePort is not limited to a single node.
  3. Final Answer:

    Exposes the service on a static port on each node's IP address -> Option A
  4. Quick Check:

    NodePort = fixed port on all nodes [OK]
Hint: NodePort always opens a fixed port on every node IP [OK]
Common Mistakes:
  • Confusing NodePort with ClusterIP (internal only)
  • Thinking NodePort uses external load balancers automatically
  • Assuming NodePort restricts service to one node
2. Which of the following is the correct YAML snippet to define a NodePort service?
easy
A. apiVersion: v1\nkind: Pod\nmetadata:\n name: my-service\nspec:\n containers:\n - name: app\n image: my-app-image
B. apiVersion: v1\nkind: Service\nmetadata:\n name: my-service\nspec:\n type: ClusterIP\n selector:\n app: my-app\n ports:\n - port: 80\n targetPort: 8080\n nodePort: 30007
C. apiVersion: v1\nkind: Service\nmetadata:\n name: my-service\nspec:\n type: NodePort\n selector:\n app: my-app\n ports:\n - port: 80\n targetPort: 8080\n nodePort: 30007
D. apiVersion: v1\nkind: Service\nmetadata:\n name: my-service\nspec:\n type: LoadBalancer\n selector:\n app: my-app\n ports:\n - port: 80\n targetPort: 8080\n nodePort: 30007

Solution

  1. Step 1: Identify correct service type

    The service type must be NodePort to expose on a node port.
  2. Step 2: Check YAML structure and fields

    The YAML must define kind: Service, type: NodePort, and specify nodePort under ports.
  3. Final Answer:

    YAML with type NodePort and nodePort field -> Option C
  4. Quick Check:

    NodePort type + nodePort field = correct YAML [OK]
Hint: NodePort service YAML must have type: NodePort and nodePort set [OK]
Common Mistakes:
  • Using ClusterIP or LoadBalancer instead of NodePort
  • Defining nodePort in a Pod instead of Service
  • Missing the nodePort field in ports
3. Given this NodePort service YAML, what port will you use to access the app externally?
apiVersion: v1
kind: Service
metadata:
  name: example-service
spec:
  type: NodePort
  selector:
    app: example
  ports:
    - port: 80
      targetPort: 8080
      nodePort: 31000
medium
A. Port 31000 on the node IP
B. Port 80 on the node IP
C. Port 8080 on the node IP
D. Port 80 on the pod IP

Solution

  1. Step 1: Understand NodePort port usage

    The nodePort field defines the port exposed on each node's IP for external access.
  2. Step 2: Identify the correct port from YAML

    The YAML sets nodePort: 31000, so external access uses port 31000 on any node IP.
  3. Final Answer:

    Port 31000 on the node IP -> Option A
  4. Quick Check:

    External access uses nodePort value = 31000 [OK]
Hint: Use the nodePort value to access service externally [OK]
Common Mistakes:
  • Using targetPort or port instead of nodePort externally
  • Trying to access pod IP directly
  • Confusing port and nodePort fields
4. You created a NodePort service but cannot access your app externally. Which is the most likely cause?
medium
A. The pod selector labels do not match any pods
B. All of the above
C. The service type is set to ClusterIP instead of NodePort
D. The nodePort value is outside the allowed range (30000-32767)

Solution

  1. Step 1: Check nodePort range validity

    NodePort must be between 30000 and 32767; outside this range causes failure.
  2. Step 2: Verify pod selector and service type

    If selector labels don't match pods or service type is not NodePort, external access fails.
  3. Step 3: Combine all issues

    Any of these issues can cause access failure, so all are likely causes.
  4. Final Answer:

    All of the above -> Option B
  5. Quick Check:

    Invalid port, selector, or type = no external access [OK]
Hint: Check port range, selector labels, and service type [OK]
Common Mistakes:
  • Ignoring nodePort range limits
  • Not verifying pod labels match selector
  • Assuming service type is correct without checking
5. You want to expose a web app on port 80 inside the cluster and port 32000 externally using NodePort. Which YAML snippet correctly achieves this?
hard
A. apiVersion: v1\nkind: Service\nmetadata:\n name: webapp-service\nspec:\n type: ClusterIP\n selector:\n app: webapp\n ports:\n - port: 80\n targetPort: 80\n nodePort: 32000
B. apiVersion: v1\nkind: Service\nmetadata:\n name: webapp-service\nspec:\n type: LoadBalancer\n selector:\n app: webapp\n ports:\n - port: 32000\n targetPort: 80\n nodePort: 80
C. apiVersion: v1\nkind: Service\nmetadata:\n name: webapp-service\nspec:\n type: NodePort\n selector:\n app: webapp\n ports:\n - port: 32000\n targetPort: 32000\n nodePort: 80
D. apiVersion: v1\nkind: Service\nmetadata:\n name: webapp-service\nspec:\n type: NodePort\n selector:\n app: webapp\n ports:\n - port: 80\n targetPort: 80\n nodePort: 32000

Solution

  1. Step 1: Match internal and external ports correctly

    The port is the internal cluster port (80), targetPort is the container port (80), and nodePort is the external port (32000).
  2. Step 2: Verify service type and port fields

    Service type must be NodePort and ports must be set accordingly to expose externally on 32000.
  3. Final Answer:

    YAML with type NodePort, port 80, targetPort 80, nodePort 32000 -> Option D
  4. Quick Check:

    NodePort type + nodePort 32000 + port 80 = correct setup [OK]
Hint: Set port=internal, targetPort=container, nodePort=external port [OK]
Common Mistakes:
  • Swapping port and nodePort values
  • Using LoadBalancer or ClusterIP instead of NodePort
  • Setting nodePort outside allowed range
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard