What is a Service in Kubernetes?

A Service is basically a round-robin load balancer for all pods, which matches with its name or selector. It constantly monitors the pods; in case a pod gets unhealthy, the service will start deploying the traffic to other healthy pods.


Types of Services in Kubernetes

Kubernetes offers multiple types of Services, each serving different connectivity requirements:


1. ClusterIP (Default)

  • Exposes the Service only within the cluster.
  • Ideal for internal communication between microservices.
  • Example: Frontend connecting to backend.

2. NodePort

  • Exposes the Service on each node's IP at a static port.
  • Accessible from outside the cluster using <NodeIP>:<NodePort>.
  • Example: Frontend connecting to backend.

3. LoadBalancer

  • Creates an external load balancer using your cloud provider.
  • Automatically routes traffic from the internet to your Service.
  • Commonly used in production and cloud setups (AWS, Azure, GCP).

4. ExternalName

  • Maps a Service to an external DNS name.
  • No proxying of traffic; it just redirects DNS queries.

Creating a Service in Kubernetes

To create a Service, we use a YAML configuration file or the kubectl command.

A Service selects Pods based on their labels and routes traffic to them.


Here's a simple example of a Service configuration:


apiVersion: v1
kind: Service
metadata:
 name: nginx-service
spec:
 selector:
   app: nginx
 ports:
  - protocol: TCP
   port: 80
   targetPort: 80
 type: ClusterIP


In this example:

  • selectorconnects the Service to Pods labeled app: nginx.
  • port is the port exposed by the Service.
  • targetPortis the port inside the Pod.
  • type defines how the Service is exposed (discussed next).

To deploy this service:
kubectl create -f nginx-service.yaml


What is an Ingress in Kubernetes?

While Services expose applications, Ingress provides a smarter and centralized way to manage external HTTP and HTTPS access.

It allows you to define routing rules for multiple Services using hostnames and paths - similar to a reverse proxy.


For example:

  • demo.com/image → Image Processing Service
  • demo.com/video → Video Processing Service

So instead of creating multiple NodePorts or LoadBalancers, you define an Ingress that routes traffic to the correct Service based on the request URL.


Installing an Ingress Controller

Ingress requires an Ingress Controller to function

One of the most used controllers is NGINX Ingress Controller.


To install it, run:

  • kubectl apply -f https://github.com/kubernetes/ingress-nginx/blob/master/docs/deploy/index.md

Once installed, the controller listens for Ingress resources and processes incoming HTTP/S traffic accordingly.


Creating an Ingress Rule

Here's an example YAML configuration for a simple Ingress setup:


apiVersion: networking.k8s.io/v1
kind: Service
metadata:
 name: simple-ingress
annotations:
 nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
  - host: demo.com
   http:
    paths:
    - path: /foo
     pathType: Prefix
     backend:
      service:
       name: nginx-service
       port:
        number: 80


To deploy the Ingress rule:
kubectl create -f ingress.yaml

To verify:
kubectl get ing


Conclusion

In Kubernetes, Services and Ingress are the core components that handle networking and connectivity.