Day 34: Working with Services in Kubernetes

Radheya Zunjur
4 min readJul 25, 2023

--

Welcome back to our Kubernetes learning journey! In the previous article, we explored the concepts of Namespaces and how they can help us organize and isolate resources within a Kubernetes cluster. Today, we will focus on an integral part of Kubernetes networking — Services. Services play a crucial role in enabling communication and load balancing between different components in a Kubernetes environment.

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.

Tasks

Task 1) Create a Service for your todo-app Deployment from Day-32

Here in this task, we will use the service NodePort.

In the NodePort service type, the K8s control plane allocates a port range from 30000–32767.

Let’s create a service named service.yml.

vim service.yml

In the text editor, write the following:

apiVersion: v1
kind: Service
metadata:
name: my-service
namespace: deploy1
spec:
type: NodePort
selector:
app: django-todo-app
ports:
- protocol: TCP
port: 80
targetPort: 8000
nodePort: 30009

Now let’s apply this service to the K8s cluster using the:

kubectl apply -f service.yml

Let’s verify if the service is created:

kubectl get svc -n=deploy1

You can check the app running by your public IP

Task 2) Create a ClusterIP Service for accessing the todo-app from within the cluster.

Let’s create a ClusterIP service in k8s, which is the default service type in k8s.

Create a manifest file named cluster-ip-service.yml.

apiVersion: v1
kind: Service
metadata:
name: my-service
namespace: deploy1
spec:
type: ClusterIP
selector:
app: django-todo-app
ports:
- NAME : http
protocol: TCP
port: 80
targetPort: 8000

Let’s apply this ClusterIP to the k8s cluster. Also, check the service created using the ‘kubectl get svc’ command.

kubectl apply -f cluster-ip-servic.yml -n <namespace>
kubectl get svc -n <namespace>

Now let’s try accessing the application internally. First, Connect to any of the pods using the following command:

kubectl exec -it <Pod_name> bash -n <namespace>

Let’s create another Pod to test the service in the same namespace.

apiVersion: v1
kind: Pod
metadata:
name: podtest
namespace: deploy1
spec:
containers:
- name: busybox
image: busybox
command: ['sh', '-c', 'while true; do wget -q -O- my-django-app-cluster-ip:8000; done']

Apply this pod.yml to the k8s cluster. Along with this, I have opened port 10250 for the pod to be accessible.

Task 3: Create a LoadBalancer Service for accessing the todo-app from outside the cluster

Create a load-balancer-service.yml file.

apiVersion: v1
kind: Service
metadata:
name: loadbalacer-service
namespace: deploy1
spec:
selector:
app: django-todo-app
ports:
- port: 80
targetPort: 8000
type: LoadBalancer

Let’s apply this service to the k8s cluster and verify if the service is created using the:

kubectl apply -f load-balancer-service.yml -n <namespace>
kubectl get svc -n deploy1

Copy the port that is exposed for the load balancer and edit the inbound rules for your EC2 instance. i.e 31290

Connect to the application in your web browser using node-PublicIP:loadbalancer-port.

--

--

Radheya Zunjur
Radheya Zunjur

Written by Radheya Zunjur

Database Engineer At Harbinger | DevOps | Cloud Ops | Technical Writer

No responses yet