Kubernetes Services
Cluster IP, Node Port, LoadBalancer, Headless Service
Think of Kubernetes Services as the receptionist in a big office building. Just like how the receptionist directs visitors to the right offices, Kubernetes Services directs incoming traffic to the right places (called Pods) in a Kubernetes cluster.
“Kubernetes Services are an essential component for enabling communication between different parts of an application running on a Kubernetes cluster.”
There are four types of Kubernetes services — ClusterIP
, NodePort
, LoadBalancer
and ExternalName]
COMMANDS:
Create a Service: kubectl create -f service.yaml
Get Information about Services: kubectl get services
Describe a Service: kubectl describe service <service-name>
Delete a Service: kubectl delete service <service-name>
Edit a Service: kubectl edit service <service-name>
Check Service Endpoints: kubectl get endpoints <service-name>
Edit Service Endpoints: kubectl edit endpoints <service-name>
Expose a Deployment as a service : kubectl expose deployment <deployment-name> --type=NodePort --port=<port> --target-port=<target-port>
ClusterIP:
Cluster IP (By Default): Expose port Inside the cluster ( Provide Internal Connectivity )
Let’s create a service:
# Service
apiVersion: v1
kind: Service
metadata:
name: my-service
spec:
selector:
app: my-app
ports:
- protocol: TCP
port: 80
targetPort: 8080
Deployment YAML File:
# Deployment
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-deployment
spec:
replicas: 3
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: my-container
image: my-image:latest
ports:
- containerPort: 8080
Attaching service and Deployment to POD:
# Pod
apiVersion: v1
kind: Pod
metadata:
name: my-pod
labels:
app: my-app
spec:
containers:
- name: my-container
image: my-image:latest
ports:
- containerPort: 8080
NodePort:
NodePort: It enables access to the service from outside the cluster (Provides access to external traffic)
# service.yaml
apiVersion: v1
kind: Service
metadata:
name: my-service
spec:
selector:
app: my-app
ports:
- protocol: TCP
port: 80
targetPort: 8080
type: NodePort
# Pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: my-pod
labels:
app: my-app
spec:
containers:
- name: my-container
image: my-image:latest
ports:
- containerPort: 8080
LoadBalancer:
LoadBalancer provides internal and external connectivity
#Service.yaml
apiVersion: v1
kind: Service
metadata:
name: my-loadbalancer-service
spec:
selector:
app: my-app
ports:
- protocol: TCP
port: 80 # External connectivity: Exposes port 80 externally
targetPort: 8080 # Internal connectivity: Forwards traffic to port 8080 on selected pods
type: LoadBalancer # External connectivity: Creates an external load balancer
Headless Service:
Headless service allows the client to directly communicate with the Pods. Do not assign IP to itself.
Headless Services are used in deploying Stateful Applications in Kubernetes.
#Service.yaml
apiVersion: v1
kind: Service
metadata:
name: my-headless-service
spec:
clusterIP: None # Specifies that this is a headless service
selector:
app: my-app
ports:
- protocol: TCP
port: 80
targetPort: 8080
If there’s a specific topic you’re curious about, feel free to drop a personal note or comment. I’m here to help you explore whatever interests you!