#90DaysOfDevOps Challenge - Day 34 - Working with Services in Kubernetes

Cloud Engineer and DevOps enthusiast, just talking about AWS, Docker, Linux, Kubernetes, Python, and automation.
Search for a command to run...

Cloud Engineer and DevOps enthusiast, just talking about AWS, Docker, Linux, Kubernetes, Python, and automation.
No comments yet. Be the first to comment.
The #90DaysOfDevOps Challenge is a dedicated initiative aimed at promoting continuous learning and skill development in the field of DevOps over a 90-day period.
Running your own home lab used to mean expensive hardware, complex networking, and hours of manual configuration. This project changes that. With a single Raspberry Pi, Docker Compose, and a few hundr

Welcome to an advanced hybrid directory demo where we'll guide you through setting up a hybrid environment, connecting to simulated on-premises infrastructure, creating a managed Microsoft AD within AWS, establishing a two-way forest trust, and integ...

Hello DevOps Community! Today marks the completion of the #90DaysOfDevOps Challenge, and it's a moment to reflect on the journey we've undertaken together. Throughout this period, I've engaged with various technologies, refining my skills in the DevO...

In a modern cloud infrastructure setup, creating a robust and scalable 2-tier architecture is essential. Such an architecture helps meet the demands of your applications while ensuring repeatability, reliability, and security. In this article, we'll ...

Welcome to Part 4 of my series on building a comprehensive CI/CD pipeline in AWS. In this article, I will explore AWS CodePipeline, a powerful service for building and deploying applications in a continuous integration and continuous delivery (CI/CD)...

Welcome to Day 34 of the #90DaysOfDevOps Challenge. In today's challenge, we will explore the concept of services in Kubernetes and learn how they enable communication between different components within a cluster.

A Service enables network access to a set of Pods in Kubernetes. Services select Pods based on their labels. When a network request is made to the service, it selects all pods in the cluster matching the service's selector, chooses one of them, and forwards the network request to it.
What's the difference between a Service and a Deployment in Kubernetes?
A deployment is responsible for keeping a set of pods running.
A service is responsible for enabling network access to a set of pods.
We could use a deployment without a service to keep a set of identical pods running in the Kubernetes cluster. The deployment could be scaled up and down and pods could be replicated. Each pod could be accessed individually via direct network requests (rather than abstracting them behind a service), but keeping track of this for a lot of pods is difficult.
We could also use a service without a deployment. We'd need to create each pod individually (rather than "all-at-once" like a deployment). Then our service could route network requests to those pods by selecting them based on their labels.
Services and Deployments are different, but they work together nicely.
In this example, YAML creates a Service that is available to external network requests. We’ve specified the NodePort value so that the service is allocated to that port on each Node in the cluster.

The type property in the Service's spec determines how the service is exposed to the network. It changes where a Service can be accessed from. The possible types are ClusterIP, NodePort, and LoadBalancer
ClusterIP – The default value. The service is only accessible from within the Kubernetes cluster – you can’t make requests to your Pods from outside the cluster!
NodePort – This makes the service accessible on a static port on each Node in the cluster. This means that the service can handle requests that originate from outside the cluster.
LoadBalancer – The service becomes accessible externally through a cloud provider's load balancer functionality. GCP, AWS, Azure, and OpenStack offer this functionality. The cloud provider will create a load balancer, which then automatically routes requests to your Kubernetes Service
Credits to Matthew Palmer for the explanation here
Create a Service definition for your app Deployment in a YAML file.
apiVersion: v1
kind: Service
metadata:
name: todo-app-service
namespace: dev
spec:
type: NodePort
selector:
app: todo
ports:
- protocol: TCP
port: 3000
targetPort: 3000
Apply the Service definition to your K8s (minikube) cluster using the below command.
kubectl apply -f service.yml -n dev

Verify that the Service is working by accessing the app using the Service's IP and Port in your Namespace
kubectl get svc -n dev
minikube service todo-app-service -n dev --url

We can access our pod by using the provided URL
apiVersion: v1
kind: Service
metadata:
name: todo-app-service
namespace: dev
spec:
type: ClusterIP
selector:
app: todo
ports:
- protocol: TCP
port: 3000
targetPort: 3000
Apply the ClusterIP Service definition to your K8s (minikube) cluster using the below command.
kubectl apply -f service.yml -n dev

Verify that the ClusterIP Service is working by accessing the app from another Pod in the cluster in your Namespace.
kubectl get svc -n dev
kubectl get pods -n dev
kubectl exec -it "pod-id" -n dev -- bash

Once we've accessed the pod, we can run the curl command to access the URL and see the 'Hello, World!' HTML code.

Create a LoadBalancer Service definition for your app Deployment in a YAML file.
apiVersion: v1
kind: Service
metadata:
name: todo-app-service
namespace: dev
spec:
type: LoadBalancer
selector:
app: todo
ports:
- protocol: TCP
port: 3000
targetPort: 3000
Apply the LoadBalancer Service definition to your K8s (minikube) cluster using the below command.
kubectl apply -f service.yml -n dev

Check the status of the newly created service:
kubectl get svc -n dev

As we can see, the Load Balancer service called "todo-app-service and its external IP assignment is in a pending state. To access and assign the external IP, we need to create a bridge between minikube and our machine by using a tunnel command.
minikube tunnel

We have to keep the tunnel running, so we are going to open a second terminal and check the status of the service again

We can use the External IP address and the port to access our app.

Services are an essential component in Kubernetes that enable communication between different parts of an application. They provide a reliable and scalable way to expose and access services within a cluster. In this article, we explored the concept of services, the different types available, and how to create and access a service in Kubernetes.
Stay tuned for Day 35 of the #90DaysOfDevOps Challenge, where we will dive deeper into ConfigMaps and Secrets in Kubernetes