This repository demonstrates the use of Kubernetes services to manage and expose a Python-based web application. The project includes deployment configurations, service definitions, an Ingress resource, and a Dockerfile to containerize the application.
- Purpose: Manages the lifecycle of pods in Kubernetes.
- What it Does:
- Ensures the application runs with the desired number of replicas.
- Automatically replaces failed pods to maintain availability.
- Key Components:
- Replicas: Defines the number of pod instances to run.
- Labels and Selectors: Used to match pods with the service for communication.
- Purpose: Exposes the application to the network, enabling communication between users and pods.
- What it Does:
- Handles dynamic IP changes of pods using labels and selectors.
- Provides different levels of accessibility (internal, organizational, external).
- Types:
- ClusterIP: Internal communication within the cluster.
- NodePort: Exposes the service on each node’s IP address and a static port.
- LoadBalancer: Provides external access to the service.
- Purpose: Manages external access to services inside the cluster via HTTP and HTTPS.
- What it Does:
- Provides a single entry point to multiple services using domain-based routing.
- Reduces the need for multiple
LoadBalancer
services. - Enables SSL/TLS termination for secure connections.
Example: Nginx Ingress Controller
A common way to manage Ingress in Kubernetes is by using the Nginx Ingress Controller. After installing it, you can define rules to route traffic to specific services based on the requested domain or path.
Example Ingress configuration:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: demo-ingress
spec:
rules:
- host: demo.example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: demo-service
port:
number: 80
This routes all traffic from demo.example.com
to the demo-service
running on port 80
.
minikube start
– Starts a local Kubernetes cluster.minikube status
– Checks the status of the Minikube cluster.
kubectl apply -f deployment.yml
– Deploys the application based on the configuration file.kubectl get deploy
– Displays information about the deployment.
kubectl apply -f service.yml
– Creates a service to expose the application.kubectl get svc
– Retrieves details of the service, including its IP and port.
kubectl apply -f ingress.yml
– Deploys the Ingress resource to route external traffic to the service.kubectl get ing
– Displays the Ingress resource details, including its associated hostname and rules.
- Use the
curl
command or a web browser to access the application at the service’s or Ingress’s exposed IP and port.
deployment.yml
– Contains the deployment configuration, including replica settings and pod template.service.yml
– Defines the service configuration for exposing the application.ingress.yml
– Configures routing and external access for the application.Dockerfile
– Instructions for building the Docker image for the application.
Clone the repository to your local machine:
git clone https://github.com/dhanikaa/K8s-services.git
cd K8s-services
Build the Docker image for the Python application:
docker build -t your-username/python-sample-app:v1 .
Start a local Kubernetes cluster:
minikube start
minikube status
Apply the deployment configuration:
kubectl apply -f deployment.yml
kubectl get deploy
Apply the service configuration to expose the app:
kubectl apply -f service.yml
kubectl get svc
Apply the Ingress configuration:
kubectl apply -f ingress.yml
kubectl get ing
This will configure routing for external access.
Use the service’s IP and port:
curl -L http://<Service-IP>:<Exposed-Port>/demo
Or, if using Ingress with a configured domain:
curl -L http://demo.example.com
- Modify
service.yml
to change the service type (e.g., ClusterIP, NodePort, LoadBalancer). - Modify
ingress.yml
to update routing rules based on the domain and path requirements.