Kubernetes
Service Types
Expose Pods via ClusterIP, NodePort, and LoadBalancer services.
By EZ4Code Team
servicenetworkingdiscovery
Code
# service.yaml
apiVersion: v1
kind: Service
metadata:
name: api
spec:
type: ClusterIP # default, internal only
selector:
app: api
ports:
- port: 80 # service port
targetPort: 8080 # container port
protocol: TCP
---
# Headless service for StatefulSet DNS
apiVersion: v1
kind: Service
metadata:
name: api-headless
spec:
clusterIP: None
selector: { app: api }
ports: [{ port: 80, targetPort: 8080 }]
# kubectl get svc
# kubectl get endpoints apiExplanation
A Service exposes a stable IP and DNS name that load-balances across matching Pods. ClusterIP is the default and is reachable only inside the cluster, while NodePort and LoadBalancer expose it externally. Setting clusterIP to None creates a headless service used by StatefulSets for per-pod DNS.
More Kubernetes Snippets
Pod Manifest
Define a standalone Pod with a container, env, and probe.
Deployment & Rollout
Manage a replicated, rolling-updated workload.
ConfigMap & Secret
Inject configuration and sensitive data into containers.
Ingress Routing
Route external HTTP traffic to services by host and path.
Labels & Selectors
Tag resources and query them with selectors.
kubectl Basics
Common kubectl commands for everyday operations.