Services — Kubernetes

Always learning
4 min readNov 10, 2023

--

In Kubernetes, a Service is a method for exposing a network application that is running as one or more Pods in your cluster.

Pod IP are ephemeral in nature.

Pod IP’s are not static they keep changing.

Service ← Theory Part

Service 2

Advantages

Maintaining a infront of static IP

Load Balancing

Loose Coupling

Kubernetes Service types allow you to specify what kind of Service you want.

ClusterIP — Is accessible only within the cluster

NodePort — Exposes the Service on each Node’s IP at a static port

LoadBalancer — Exposes the Service externally using an external load balancer

Selector → Should match with the pod label ← which pod to be forward?

TargetPort → Is the actual port on which your app is running inside the container ← which port to be forward?

ClusterIP → accessible only within the cluster

apiVersion: v1
kind: Pod
metadata:
name: nginx-pod
spec:
containers:
- image: nginx
name: nginx-ctr

Execute the pod

kubectl apply -f nginx-pod.yml
kubectl get pods

Get the pod full details

kubectl get pods -o wide

Create a deployment file

apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 2
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- image: nginx
name: nginx-ctr

Execute the deployment file

kubectl apply -f nginx-deployment.yml
kubectl get pods

Create a service file

apiVersion: v1
kind: Service
metadata:
name: nginx-svc
spec:
selector:
app: nginx
ports:
- name: nginx-port
protocol: TCP
port: 32767
targetPort: 80

Get the full information of pods

kubectl get all

Get teh pod IP address

kubectl get pods -o wide (or)
kubectl get endpoints

POD → 1

POD → 2

Login to the container and change nginx file

kubectl -it exec nginx-deployment-7bb9945d7c-75nc5 -- /bin/sh

Get the services IP

kubectl get svc

Automatically load balancing selected

Accessible only within the cluster → ClusterIP → Internal load balancer

Cluster IP service identifies pod using the selector → How pods are identified

Target port helps in identifying pod port → how port is identified

Nodeport → Should range from 30000–32767

Kubernetes will automatically allocate nodeport value if not specified

Beware of port conflicts when assigning nodeports manually

apiVersion: v1
kind: Service
metadata:
name: nginx-np-svc
spec:
selector:
app: nginx
type: NodePort
ports:
- name: nginx-port
protocol: TCP
port: 32000
targetPort: 80
nodePort: 30000

Create a NodePort pod

kubectl apply -f nginx-np-svc.yml

Get full information

kubectl get all

Enter external-ip:30000 you will get nginx page

Load Balancer

Create a loadbalancer pod

apiVersion: v1
kind: Service
metadata:
name: nginx-lb-svc
spec:
selector:
app: nginx
type: LoadBalancer
ports:
- name: nginx-port
protocol: TCP
port: 30000
targetPort: 80
nodePort: 32000

Apply the load balancer deployments

kubectl apply -f nginx-lb-svc.yml
kubectl get all

NodePort is not safe to use in realtime or production

ReplicaSet blog click here

--

--

Always learning
Always learning

Written by Always learning

கற்றுக் கொள்ளும் மாணவன்...