In Kubernetes, Services are objects that provide stable network identities to Pods and abstract away the details of Pod IP addresses. Services allow Pods to receive traffic from other Pods, Services, and external clients.
There are four types of service in Kubernetes for exposing the network application that is running as one or more Pods in your cluster:
NordPort
ClusterIP
LoadBalancer
ExtrernalName
In this blog, the first three would be discussed with examples/tutorials.
Before we begin, my deployment file creates a Todo web app and I already have created a namespace called "django-todo-ns". The file is as follows:
apiVersion: apps/v1
kind: Deployment
metadata:
name: django-todo-deploy
labels:
app: django-todo
namespace: django-todo-ns
spec:
replicas: 1
selector:
matchLabels:
app: django-todo
template:
metadata:
labels:
app: django-todo
spec:
containers:
- name: django-todo-app
image: neelsoni26/django-todo-app
ports:
- containerPort: 8001
Let's start with NordPort
NordPort
Exposes the Service on each Node's IP at a static port (the NodePort).
Example: Create a Service for Deployment
apiVersion: v1
kind: Service
metadata:
name: django-todo-nodeport
spec:
type: NodePort
selector:
app: django-todo
ports:
- port: 8001
targetPort: 8001
nodePort: 30070
After creating the file, write the command:
kubectl apply -f service.yml
To verify this, get services with a specific namespace.
kubectl get service -n=django-todo-ns
To verify further, get URL and check.
minikube service django-todo-nodeport -n=django-todo-ns --url
Now hit this URL with curl.
curl -L http://192.168.49.2:30070
can also try in the browser:
ClusterIP
For creating ClusterIP Service, I have created a config file:
apiVersion: v1
kind: Service
metadata:
name: django-todo-clusterip
namespace: django-todo-ns
spec:
selector:
app: django-todo
type: ClusterIP
ports:
- port: 8001
targetPort: 8001
Now, apply the config and check whether it is working or not.
kubectl apply -f clusterip-service.yml
kubectl get services -n=django-todo-ns
For further verification, get the pod list and execute it. Then access the URL for ClusterIP.
kubectl get pods -n=django-todo-ns
kubectl exec django-todo-deploy-77485cfcbd-mjjk2 -it -n=django-todo-ns bash
curl -L 10.111.162.46:8001
LoadBalancer
To create LoadBalancer Service, create a config file for it with the type: LoadBalancer
apiVersion: v1
kind: Service
metadata:
name: django-todo-loadbalancer
namespace: django-todo-ns
spec:
selector:
app: django-todo
ports:
- port: 8001
targetPort: 8001
type: LoadBalancer
Next, apply the file and verify by get service.
kubectl apply -f loadbalancer-service.yml
kubectl get services -n=django-todo-ns
There will be pending written in the external IP section for LoadBalancer. To get the IP, write the below-given command, it will return an IP address of your application with LoadBalancer service.
minikube service django-todo-loadbalancer -n=django-todo-ns --url
You may write minikube service list
for a list of services with name, namespace, port and URL.
You can copy and access the URL in your browser:
Or verify by curl -L <IP>:<Port>
If you get this code, then your app is working correctly with the Service.
Thank You.