Day 34 Task: Working with Services in Kubernetes

Pooja BhavaniPooja Bhavani
4 min read

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 and Create a Service definition for your todo-app Deployment in a YAML file.

vim service.yml
-------------------------------------------------------------------------------
apiVersion: v1 # Specifies the Kubernetes API version used in the configuration.
kind: Service #  Indicates that this is a Kubernetes Service resource.
metadata: # Contains metadata about the Service.
  name: todo-service #  Names the Service as "todo-service".
  namespace: django-app # Specifies the namespace where the Service exists (a logical partition within the cluster).
spec: # Defines the desired state of the Service.
  type: NodePort # Specifies the type of Service. It exposes the Service on each node's IP at a specific static port (NodePort).
  selector: # Defines how the Service identifies the Pods it routes traffic to.
    app: todo-app # Matches Pods with the label app: todo-app.
  ports: #Defines the ports that the Service will listen on and forward traffic to.
      # By default and for convenience, the `targetPort` is set to the same value as the `port` field.
    - port: 80 # Specifies that the Service will listen on port 80.
      targetPort: 8000 # Directs incoming traffic to the Pods' port 8000.
      # Optional field
      # By default and for convenience, the Kubernetes control plane will allocate a port from a range (default: 30000-32767)
      nodePort: 30007 # Optionally specifies a static port on each node (30007) for external access. If not specified, Kubernetes assigns a port from a default range (30000-32767).

Apply the Service definition to your K8s (minikube) cluster using this command.

kubectl apply -f service.yaml -n django-app

kubectl get svc -n django-app
kubectl get svc -o wide -n django-app

  • To enter into that cluster use this command in minikube you can do like this
minikube ssh
curl -L http://<cluster-IP>

Task-2:

Create a ClusterIP Service for accessing the todo-app from within the cluster and Create a ClusterIP Service definition for your todo-app Deployment in a YAML file.

vim cluster-ip-service.yml
-------------------------------------------------------------------------------
apiVersion: v1 # Specifies the Kubernetes API version used in the configuration.
kind: Service # Defines the type of Kubernetes resource, specifically a Service.
metadata: # Contains metadata about the Service.
  name: todo-app-cluster-ip-service # Names the Service as "todo-app-cluster-ip-service".
  namespace: django-app # Specifies the namespace where the Service exists, providing a scope for the Service within the cluster.
spec: # Defines the desired state of the Service.
  type: ClusterIP # Specifies the type of Service as ClusterIP, meaning the Service is only accessible within the cluster.
  selector: # Specifies the criteria for selecting the Pods that the Service will direct traffic to.
    app: todo-app # Matches Pods with the label app: todo-app.
  ports: # Specifies the ports that the Service will use.
  - port: 80 # Defines the port that the Service listens on within the cluster.
    targetPort: 8000 # Routes incoming traffic to the Pods' port 8000, where the application is running.

Apply the ClusterIP Service definition to your K8s (minikube) cluster using this command.

kubectl apply -f cluster-ip-service.yml -n django-app

Verify that the ClusterIP Service is working by accessing the todo-app from another Pod in the cluster in your Namespace.

kubectl get svc -n django-app && kubectl get svc -o wide -n django-app

Task-3:

Create a LoadBalancer Service for accessing the todo-app from outside the cluster and Create a LoadBalancer Service definition for your todo-app Deployment in a YAML file.

vim load-balancer-service.yml
-------------------------------------------------------------------------------
apiVersion: v1 # Specifies the Kubernetes API version used in the configuration.
kind: Service # Defines the type of Kubernetes resource, specifically a Service.
metadata: # Contains metadata about the Service.
  name: todo-app-load-balancer-service # Names the Service as "todo-app-load-balancer-service".
  namespace: django-app # Specifies the namespace where the Service exists, providing a scope for the Service within the cluster.
spec: # Defines the desired state of the Service.
  type: LoadBalancer # Specifies the Service type as LoadBalancer, allowing external access to the Service.
  selector: # Defines the criteria for selecting the Pods that the Service will direct traffic to.
    app: todo-app # Matches Pods with the label app: todo-app.
  ports: # Specifies the ports that the Service will use.
  - protocol: TCP
    port: 80 # Defines the port that the Service will listen on externally.
    targetPort: 8000 # Routes incoming external traffic to the Pods' port 8000 where the application is running.

Apply the LoadBalancer Service definition to your K8s (minikube) cluster using this command.

kubectl apply -f load-balancer-service.yml -n django-app

Verify that the LoadBalancer Service is working by accessing the todo-app from outside the cluster in your Namespace.

kubectl get svc -n django-app && kubectl get svc -o wide -n django-app

minikube ssh
curl -L http://<cluster-IP>

0
Subscribe to my newsletter

Read articles from Pooja Bhavani directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Pooja Bhavani
Pooja Bhavani