Our day 34 task is all about working with Services, which is the next logical step after managing Deployments. Services are how we make our applications accessible and robust.
Here is a breakdown of our tasks.
Task 1: Create a Service for your todo-app
Deployment
The first task is a general service creation. For simplicity, we'll create a simple ClusterIP service.
Here is a YAML file for the service. You can save this as todo-service.yml
.
---
apiVersion: v1
kind: Service
metadata:
name: todo-app-service
namespace: todo-space # Use the same namespace as your Deployment
spec:
selector:
app: todo-app # This must match the 'app' label in your Pods/Deployment
ports:
- protocol: TCP
port: 80
targetPort: 3000 # The port your todo-app is running on inside the container
Apply the Service Definition
First, ensure you have the todo-space
namespace created:
kubectl create namespace todo-space
Now, apply the service:
kubectl apply -f todo-service.yml -n todo-space
Verify the Service
To verify, get the service details and find its ClusterIP
.
kubectl get services -n todo-space
You should see an output.
Task 2: Create a ClusterIP Service
This task is a more explicit version of Task 1. The YAML is identical to the one above since ClusterIP
is the default Service type.
Save the content below to cluster-ip-service.yml
.
---
apiVersion: v1
kind: Service
metadata:
name: todo-app-clusterip
namespace: todo-space
spec:
selector:
app: todo-app
ports:
- protocol: TCP
port: 80
targetPort: 3000
type: ClusterIP # Explicitly declaring the type
Apply and Verify
Apply it with the same command as before:
kubectl apply -f cluster-ip-service.yml -n todo-space
To verify, you can get the service's details. The CLUSTER-IP
will be a unique IP only accessible from within the cluster.
kubectl get services -n todo-space
To test, you would typically exec
into an existing Pod and use curl
to the ClusterIP or the service's DNS name (todo-app-clusterip.todo-space.svc.cluster.local
).
Task 3: Create a LoadBalancer Service
The LoadBalancer
Service type is used to expose your application externally. On a cloud provider like GCP or AWS, this automatically provisions a cloud load balancer.
Save the content below to load-balancer-service.yml
.
---
apiVersion: v1
kind: Service
metadata:
name: todo-app-loadbalancer
namespace: todo-space
spec:
selector:
app: todo-app
ports:
- protocol: TCP
port: 80
targetPort: 3000
type: LoadBalancer # Changed from ClusterIP
Apply and Verify
Apply the file:
kubectl apply -f load-balancer-service.yml -n todo-space
To verify, get the service details. This time, you'll see an EXTERNAL-IP
.
kubectl get services -n todo-space
In a cloud environment, it may take a few moments for the EXTERNAL-IP
to be provisioned. Once it appears, you can access your todo-app
from outside the cluster using that IP address in your web browser. In minikube, the EXTERNAL-IP
will often be pending
, and you'll need to use the minikube service
command to get the external URL.
minikube service todo-app-loadbalancer -n todo-space
This will open your web browser to the correct URL for the todo-app
.
Top comments (0)