In Kubernetes, a Service is an abstraction that defines a logical set of Pods and a policy by which to access them. Services provide stable networking and routing to pods even as they are created or destroyed, ensuring seamless communication.
π Understanding Kubernetes Services
What are Kubernetes Services?
A Service in Kubernetes is a resource that exposes a set of Pods to network traffic. It ensures that communication between Pods or external clients can happen reliably, even as Pods are created or removed. Services can be classified into different types based on how they expose applications to the outside world.
Types of Services:
ClusterIP: Exposes the service on an internal IP within the cluster (default).
NodePort: Exposes the service on each nodeβs IP at a static port.
LoadBalancer: Exposes the service externally using a cloud provider's load balancer.
ExternalName: Maps the service to a DNS name external to the cluster.
Example: Nginx Service Manifest
apiVersion: v1
kind: Service
metadata:
name: nginx-service
namespace: nginx
spec:
selector:
app: nginx-app
ports:
- protocol: TCP
port: 80
targetPort: 80
type: ClusterIP
This example defines a ClusterIP service that exposes the Nginx deployment on port 80 inside the cluster.
Apply the Service:
kubectl apply -f nginx-service.yml -n nginx
port forward to access nginx application
sudo -E kubectl port-forward service/nginx-service -n nginx 80:80 --address=0.0.0.0
π Small Django Project: Django-Notes-App
Let's take a hands-on approach by deploying a simple Django application to Kubernetes. In this example, weβll build and deploy a basic Django Notes App to Kubernetes.
git clone
https://github.com/imkiran13/Django-Notes-App.git
Project Overview:
The django-notes-app
will allow users to add and view notes. It will include:
A simple Django web application.
A database to store notes (using SQLite for simplicity).
Dockerize the app and deploy it to Kubernetes.
Step 1: DockerHub Login
Make sure you are logged into Docker Hub before pushing the image, which you can do with the following command:
docker login
This will prompt you for your Docker Hub credentials.
Step 2: Build and Push the Docker Image
Build the Docker image:
docker build -t notes-app .
Tag and push the image to a container registry (e.g., Docker Hub):
docker tag notes-app imkiram13/notes-app
docker push imkiran13/notes-app
Step 3: Kubernetes Deployment for Django-Notes-App
Create a Deployment Manifest:
apiVersion: apps/v1
kind: Deployment
metadata:
name: notes-app
spec:
replicas: 1
selector:
matchLabels:
app: notes-app
template:
metadata:
labels:
app: notes-app
spec:
containers:
- name: notes-app
image: imkiran13/notes-app
ports:
- containerPort: 8000
Apply the Deployment:
kubectl apply -f django-deployment.yml -n nginx
Step 4: Expose the Django App via Service
Create a Service Manifest:
apiVersion: v1
kind: Service
metadata:
name: notes-app-service
spec:
selector:
app: notes-app
ports:
- protocol: TCP
port: 80
targetPort: 8000
type: ClusterIP
Apply the Service:
kubectl apply -f django-service.yml -n nginx
This configuration exposes the Django app to the outside world using a LoadBalancer service, providing external access.
port forward to access nginx application
sudo -E kubectl port-forward service/notes-app-service -n nginx 80:80 --address=0.0.0.0
Step 5: Access the Application
Get the external IP:
kubectl get services -n nginx
Visit the IP address in your browser.
π Conclusion
By following the steps outlined above, we successfully deployed a Django Notes App to Kubernetes. We learned how to create a Service for exposing our app and deployed it using a Docker container. Kubernetes provides an elegant way to manage deployments and services, making it easy to scale and expose your applications.
Let me know if you need help with any of these steps or have any additional questions! π