Table of contents
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
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.
apiVersion: v1
kind: Service
metadata:
name: django-todo-services
namespace: python-django-app
spec:
type: NodePort
selector:
app: django-app
ports:
- protocol: TCP
port: 8000
targetPort: 30007
In this example, the Service type is set to NodePort, and a nodePort is defined as 30080. This will make the Service accessible from outside the cluster by using the IP address of any node in the cluster and the nodePort defined in the Service.
apiVersion: v1:This section defines the API version to use, in this case, v1.
Kind: Service: This section specifies the type of resource you are creating, in this case a Service.
metadata: name: django-todo-service: This section gives the Service a name, in this case django-todo-service
spec: selector: app: django-app: This section defines the selector for the pods that the Service should target. In this case, it will target pods with the app label set to django-app.
type: NodePort: This section specifies the type of Service you want to create, in this case NodePort.
ports: - protocol: TCP port: 8001 targetPort: 8001 nodePort: 30080:
This section defines the port mapping for the Service. The Service will listen on port 8001 and forward traffic to the target port 8001 on the pods. Additionally, a nodePort is defined as 30080, which will make the Service accessible from outside the cluster by using the IP address of any node in the cluster and the nodePort defined in the Service.
- Now let's create a namespace first for the deployment.
kubectl create namespace python-django-app1
As we have created services and namespace now lets create a deployment.
kubectl create -f service.yaml -n python-django-app1
- Now verify the deployment and service.
Verify that the Service is working by accessing the todo-app using the Service’s IP and Port in your Namespace.
Task-2:
Create a ClusterIP service for the deployment.
- Now let's create a deployment.
Syntax: kubectl create -f <filename> -n <namespace>
Now verify the deployment and service.
Task-3:
- Create a LoadBalancer service for the deployment.
step-1
: Create a file loadbalancer-service.yaml
and write the code for the service.
- Now let's create a deployment.
syntax:
kubectl create -f <filename> -n <namespace>
Check of the Service is created or not.
kubectl get svc -n python-django-app
minikube service list
Now verify the deployment and service.
kubectl get all -n python-django-app