Service

Service

Kubernetes Services are abstractions that define a logical set of Pods and a policy by which to access them, ensuring consistent and reliable networking. Services enable communication between different components of an application, both within the cluster and externally, by providing a stable IP address and DNS name, regardless of the changing set of Pods. This allows seamless access to applications even as they scale or undergo rolling updates.

Key Points:

  • ClusterIP: Exposes the Service on an internal IP within the cluster. This is the default Service type.
  • NodePort: Exposes the Service on each Node’s IP at a static port.
  • LoadBalancer: Exposes the Service externally using a cloud provider's load balancer.
  • ExternalName: Maps a Service to a DNS name. This is useful for integrating Kubernetes applications with external services.
💡

The default load balancing algorithm for Kubernetes Ingress is Round Robin. This algorithm distributes incoming requests evenly across all available backend instances, ensuring a balanced load distribution.

Hello

Example Service YAML

selector: Selects Pods with the label app: crazy-backend.

service.yaml
apiVersion: v1
kind: Service
metadata:
  name: crazy-backend-service
spec:
  selector:
    app: crazy-backend
  ports:
    - protocol: TCP
      port: 80
      targetPort: 8000
  type: ClusterIP

Port forward service

To access the crazy-backend-service locally without exposing it to the internet, use the kubectl port-forward command. This command forwards a local port to a port on the Service, allowing you to access the Service through your local machine.

kubectl port-forward svc/crazy-backend-service 2137:80

This command forwards local port 2137 to port 80 on the crazy-backend-service. You can now access the Service by navigating to http://localhost:2137 (opens in a new tab) in your browser or through any HTTP client.