Skip to content

Service Type in k8s

K8s-Service

Briefing

1 ๐‚๐ฅ๐ฎ๐ฌ๐ญ๐ž๐ซ๐ˆ๐ (๐ƒ๐ž๐Ÿ๐š๐ฎ๐ฅ๐ญ)

  • Exposes the Service internally within the cluster.
  • Pods in the same cluster can access the Service using its ClusterIP address or DNS name. Common use case: Microservices communicating with each other.

2 ๐๐จ๐๐ž๐๐จ๐ซ๐ญ

  • Exposes the Service externally by binding a port to each Node in the cluster.
  • Accessible using <NodeIP>:<NodePort>. Common use case: Direct access for debugging or development.

3 ๐‹๐จ๐š๐๐๐š๐ฅ๐š๐ง๐œ๐ž๐ซ

  • Creates a cloud provider’s external load balancer (e.g., AWS ELB, Google Cloud Load Balancer).
  • Exposes the Service to the internet with a public IP address. Common use case: Production applications requiring public access

4 ๐„๐ฑ๐ญ๐ž๐ซ๐ง๐š๐ฅ๐๐š๐ฆ๐ž

  • Maps the Service to an external DNS name.
  • Does not create a proxy but resolves the DNS name directly. Common use case: Accessing external services or legacy systems.

5 ๐‡๐ž๐š๐๐ฅ๐ž๐ฌ๐ฌ ๐’๐ž๐ซ๐ฏ๐ข๐œ๐ž

  • A Headless Service is a specialized type of Kubernetes Service that does not assign a ClusterIP.
  • Instead of routing traffic through a proxy (like kube-proxy), it directly returns the IP addresses of the Pods backing the Service. Common use case: Used with StatefulSets to ensure each Pod gets its own DNS entry.

Service-Type-Compare

Service Type ClusterIP

  • ClusterIP is the default service type
  • the service is assigned an IP from a designated service CIDR range
  • DNS pattern: <namespace_name>.svc.cluster.local.

ClusterIP-pod

e.g.

apiVersion: v1
kind: Service
metadata:
  name: web1-svc
spec:
  selector:
    app: web1
  ports:
  - port: 80
    targetPort: 8080

Service Type NodePort

NodePortโ€“pod

Service Type ExternalName

e.g.

kind: Service
apiVersion: v1
metadata:
  name: prod-mongodb
  namespace: prod
spec:
  type: ExternalName
  externalName: mymongodb.documents.azure.com

Service Type LoadBalancer

LoadBalancerโ€“pod

e.g.

kind: Service
apiVersion: v1
metadata:
  name: web-svc
spec:
  type: LoadBalancer
  selector:
    app: web
  ports:
  - protocol: TCP
    port: 80
    targetPort: 8081
  loadBalancerIP: 13.12.21.31
  loadBalancerSourceRanges:
  - "142.43.0.0/16"

Ingress and Ingress Controllers

The Ingress API is basically an HTTP-level router that allows for host-based and path-based rules to direct to specific backend services.

Reference

  • Kubernetes Best Practices, Brendan Burns
Feedback