WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Kubernetes · Basic · question 20 of 100

Explain the difference between a LoadBalancer and a NodePort service type in Kubernetes.?

📕 Buy this interview preparation book: 100 Kubernetes questions & answers — PDF + EPUB for $5

In Kubernetes, a Service is an abstraction that provides a stable IP address and DNS name for accessing a set of Pods. There are several types of Services in Kubernetes, including ClusterIP, NodePort, and LoadBalancer.

NodePort and LoadBalancer are both types of Services that allow external traffic to access Pods in a Kubernetes cluster, but they have different use cases and functionality.

Here are the differences between a LoadBalancer and a NodePort Service type in Kubernetes:

LoadBalancer:

A LoadBalancer Service type is used to expose a Service externally, outside the cluster, to the internet or another network. LoadBalancer Services create an external load balancer in the cloud provider’s network that forwards traffic to the Service. LoadBalancer Services are typically used when you need to expose a Service to the public internet, or when you need to distribute traffic evenly across multiple nodes in a cluster. LoadBalancer Services are often used in cloud environments, such as AWS, Google Cloud, or Azure, which provide load balancers as a managed service.

Here’s an example of creating a LoadBalancer Service in Kubernetes:

apiVersion: v1
kind: Service
metadata:
  name: my-loadbalancer-service
spec:
  type: LoadBalancer
  selector:
    app: my-app
  ports:
  - name: http
    port: 80
    targetPort: 8080

This LoadBalancer Service exposes a Service called my-app with port 80 and forwards traffic to port 8080 on the Pods that match the selector app: my-app. When this Service is created, a load balancer is provisioned by the cloud provider to distribute traffic to the Pods.

NodePort:

A NodePort Service type is used to expose a Service on a specific port on all Nodes in a cluster.

NodePort Services create a high-numbered port on each Node in the cluster that forwards traffic to the Service.

NodePort Services are typically used when you need to expose a Service externally for testing or debugging purposes, or when you need to access the Service from outside the cluster, such as from a web browser or a command-line tool.

NodePort Services are not recommended for production use, as they can expose Pods directly to the internet without authentication or encryption.

Here’s an example of creating a NodePort Service in Kubernetes:

apiVersion: v1
kind: Service
metadata:
  name: my-nodeport-service
spec:
  type: NodePort
  selector:
    app: my-app
  ports:
  - name: http
    port: 80
    targetPort: 8080
    nodePort: 30080

This NodePort Service exposes a Service called my-app with port 80 and forwards traffic to port 8080 on the Pods that match the selector app: my-app. The Service is also exposed on port 30080 on all Nodes in the cluster. When traffic is sent to port 30080 on a Node, it is forwarded to the Service on port 80.

In conclusion, LoadBalancer and NodePort are both types of Services in Kubernetes that allow external traffic to access Pods, but they have different use cases and functionality. LoadBalancer Services are used to expose a Service externally to the internet or another network, while NodePort Services are used to expose a Service on a specific port on all Nodes in a cluster.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Kubernetes interview — then scores it.
📞 Practice Kubernetes — free 15 min
📕 Buy this interview preparation book: 100 Kubernetes questions & answers — PDF + EPUB for $5

All 100 Kubernetes questions · All topics