Day 5: Kubernetes Services – Connecting Your Applications
Welcome to Day 5 of the Zero to Platform Engineer in 30 Days challenge! Yesterday, we explored Deployments and Scaling in Kubernetes, learning how to manage application lifecycles and scale them effectively. Today, we’ll dive into Kubernetes Services, which are essential for enabling communication between your applications and making them accessible to users.
What Are Kubernetes Services?
In Kubernetes, a Service is an abstraction that defines a logical set of Pods and a policy for accessing them. Services allow applications to:
- Communicate with each other inside the cluster.
- Be exposed to external traffic for user access.
Without Services, Pods would be difficult to communicate with because their IP addresses change frequently as they are created or destroyed.
Types of Kubernetes Services
There are four main types of Services, each with a specific purpose:
- ClusterIP (Default)
- Exposes the Service inside the cluster.
- Use case: Internal communication between applications.
- NodePort
- Exposes the Service on a specific port of each Node’s IP.
- Use case: Debugging or direct access during development.
- LoadBalancer
- Integrates with cloud provider load balancers to expose the Service externally.
- Use case: Production-ready applications needing external access.
- ExternalName
- Maps the Service to an external DNS name.
- Use case: Accessing external resources like APIs.
How Services Fit Into Platform Engineering
As a Platform Engineer, Kubernetes Services are critical for:
- Enabling seamless communication between microservices.
- Managing traffic between internal and external systems.
- Simplifying application access with stable endpoints.
Hands-On: Creating Kubernetes Services
Step 1: Deploy a Simple Application
Let’s deploy an example application to work with.
- Create a file named nginx-deployment.yaml:
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
- Apply the Deployment:
kubectl apply -f nginx-deployment.yaml
- Verify the Pods are running:
kubectl get pods
Step 2: Create a ClusterIP Service
- Create a file named nginx-service.yaml:
apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
selector:
app: nginx
ports:
- protocol: TCP
port: 80
targetPort: 80
type: ClusterIP
- Apply the Service:
kubectl apply -f nginx-service.yaml
- Verify the Service:
kubectl get services
- Access the Service inside the cluster:
kubectl exec -it nginx-deployment -- curl http://nginx-service
🎯 What’s happening? The Service provides a stable IP and DNS name (nginx-service) that can be used to communicate with the Pods.
Step 3: Create a NodePort Service
- Edit the nginx-service.yaml file and change the type to NodePort:
type: NodePort
- Apply the changes:
kubectl apply -f nginx-service.yaml
- Get the NodePort:
kubectl get services
- Access the application:
curl http://<node-ip>:<node-port>
🎯 Pro tip: Use minikube service nginx-service if you’re running Minikube.
Step 4: Expose the Service with a LoadBalancer (Optional)
If you’re using a cloud provider, you can change the type to LoadBalancer:
type: LoadBalancer
- Apply the changes:
kubectl apply -f nginx-service.yaml
- Verify the Service:
kubectl get services
Activity for Today
- Deploy the example application and create a ClusterIP Service to connect the Pods.
- Change the Service to NodePort and access it from your browser.
- If using a cloud provider, try creating a LoadBalancer Service for external access.
What’s Next?
With Services, you now know how to connect your applications and expose them to users. Tomorrow, we’ll explore ConfigMaps and Secrets, which help manage configuration and sensitive data in Kubernetes.
👉 Check it out here: Zero to Platform Engineer Repository
Feel free to clone the repo, experiment with the code, and even contribute if you’d like! 🚀
Follow the Series!
🎉 Don’t miss a single step in your journey to becoming a Platform Engineer! 🎉
This post is just the beginning. Here’s what we’ve covered so far and what’s coming up next:
- Day 0: Introduction - What’s Platform Engineering?
- Day 1: Introduction to the CNCF Landscape
- Day 2: Day 2: Containers and Docker - The Building Blocks of Cloud Native
- Day 3: Containers and Kubernetes - The Building Blocks of Cloud Native
- Day 4: Deployments and Scaling in Kubernetes - Let’s Get Practical
👉 Bookmark this blog and check back every day for new posts in the series. 📣 Share your progress on social media with the hashtag #ZeroToPlatformEngineer to connect with other readers!