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 4 of 100

Explain the role of a Kubernetes Service.?

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

A Kubernetes Service is an abstraction that defines a logical set of Pods and a policy by which to access them. Services enable communication between Pods and external network endpoints, such as load balancers, other Services, and external clients. A Service provides a stable IP address and DNS name for a set of Pods, even as they are created, scaled, and destroyed over time.

The role of a Kubernetes Service is to provide a way to expose the Pods running in a cluster to other components within or outside the cluster. Services provide the following benefits:

Load balancing: Services can distribute network traffic across multiple Pods, providing load balancing for the application.

Service discovery: Services provide a stable IP address and DNS name for a set of Pods, making it easy for other components within or outside the cluster to discover and access the application.

Health checking: Services can monitor the health of the underlying Pods and automatically route traffic away from unhealthy Pods.

IP address management: Services provide a way to manage the IP addresses assigned to Pods, ensuring that applications can be accessed consistently even as Pods are created, scaled, and destroyed over time.

Here’s an example of defining a Service in Kubernetes:

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

In this example, we define a Service resource called my-service that will route traffic to Pods with the label app: my-app. The ports field specifies that incoming traffic on port 80 should be routed to the container port 8080. The type field specifies that the Service should be exposed externally as a load balancer.

We can deploy this resource to Kubernetes using the following command:

kubectl apply -f service.yaml

This command tells Kubernetes to create or update the Service resource defined in the service.yaml file.

Here’s an example of using kubectl to view the Services running in a Kubernetes cluster:

kubectl get services

This command shows the Services running in the cluster, including their name, type, cluster IP address, external IP address, and port mappings.

In conclusion, Kubernetes Services provide a way to expose the Pods running in a cluster to other components within or outside the cluster. Services enable load balancing, service discovery, health checking, and IP address management, making it easy to build and deploy scalable and resilient applications on a Kubernetes 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