Congratulation🎊 on your learning on Deployments in K8s on Day-33
What are Services in K8s
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.
Types of services in k8s
ClusterIP(Default Service): Exposes the service on an internal IP in the cluster, making it accessible only within the cluster.
ExternalName: Maps a service to a DNS name, allowing the use of an external service.
NodePort: Exposes the service on each node's IP at a static port, making it accessible from outside the cluster.
LoadBalancer: Exposes the service externally using a cloud provider's load balancer.
Task-1:
Create a Service for your todo-app Deployment from Day-32
Create a Service definition for your todo-app Deployment in a YAML file.
Apply the Service definition to your K8s (minikube) cluster using the
kubectl apply -f service.yml -n <namespace-name>
command.Verify that the Service is working by accessing the todo-app using the Service's IP and Port in your Namespace.
Let's start Task-1
We will create service file for our previous django-todo app
Step 1: Service YAML Definition
Create a YAML file service.yml
with the following content:
apiVersion: v1
kind: Service
metadata:
name: django-app-service
namespace: app-deployment
spec:
type: NodePort
selector:
app: django-todo
ports:
- port: 80
targetPort: 8000
nodePort: 30009
Step 2: Apply the Service Definition
Start minikube
minikube start
Apply the Service definition to your Kubernetes cluster:
kubectl apply -f service.yml
Step 3: Verify the Service
Get the service details:
kubectl get svc -n app-deployment
Step 4: Update Security Group Inbound Rules
Edit the inbound rules of your worker node to allow traffic on port 30009
.
Step 5: Access the Service
Verify that the service is working by accessing the todo-app using the service’s IP and port:
minikube service django-app-service -n app-deployment --url
Creating a ClusterIP Service for Internal Access
Next, we'll create a ClusterIP Service to access the todo-app
from within the cluster.
Step 1: Service YAML Definition
Create a YAML file cluster-ip-service.yml
with the following content:
apiVersion: v1
kind: Service
metadata:
name: django-app-cluster
namespace: app-deployment
spec:
type: ClusterIP
selector:
app: django-todo
ports:
- name: http
protocol: TCP
port: 8000
targetPort: 8000
Step 2: Apply the Service Definition
Apply the ClusterIP Service definition:
kubectl apply -f cluster-ip-service.yml
Step 3: Verify the Service
List the services to verify that the ClusterIP service is running:
kubectl get svc
Step 4: Deploy a Test Pod
Create a test Pod to verify the ClusterIP service:
apiVersion: v1
kind: Pod
metadata:
name: test-pod
namespace: app-deployment
spec:
containers:
- name: busybox
image: busybox
command: ['sh', '-c', 'while true; do wget -q -O- django-app-cluster:8000; done']
Apply the test Pod definition:
kubectl apply -f test-pod.yml
Step 5: Access the Service from the Test Pod
Exec into the test Pod and verify access:
kubectl exec -it test-pod -n my-django-app -- /bin/sh
Use wget
to test access:
wget -qO- http://my-django-app-cluster:8000
Creating a LoadBalancer
LoadBalancer file Service for External Access let's create a LoadBalancer Service to access the todo-app
from outside the cluster.
Step 1: Service YAML Definition
Create a YAML file load-balancer-service.yml
with the following content:
apiVersion: v1
kind: Service
metadata:
name: my-django-app-loadbalancer
namespace: app-deployment
spec:
selector:
app: django-todo
ports:
- port: 80
targetPort: 8000
type: LoadBalancer
Step 2: Apply the Service Definition
Apply the LoadBalancer Service definition:
kubectl apply -f load-balancer-service.yml
Step 3: Verify the Service
Get the service details:
kubectl get service
Step 4: Access the Service
Copy the public IP address of the LoadBalancer and access the todo-app
through the browser:
Public-IPv4-DNS:<loadbalancer-port-number>
Thankyou for reading !!!!!!