Working with Services in Kubernetes

ยท

3 min read

Working with Services in Kubernetes

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: Creating a Service for todo-app Deployment in Kubernetes

  • Create a Service for your todo-app Deployment from Day-32

  • Create a Service definition for your todo-app Deployment in a YAML file.

apiVersion: v1
kind: Service
metadata:
  name: todo-app-deployment
  namespace: todo-app
spec:
  type: NodePort
  selector:
    app: todo-app
  ports:
    - port: 80
      targetPort: 8000

  • Apply the Service definition to your K8s (minikube) cluster using the kubectl apply -f service.yml -n <namespace-name> command.

  • Verify that the Service is working by accessing the todo-app using the Service's IP and Port in your Namespace.

Task 2: Creating a ClusterIP Service for Accessing the todo-app within a Kubernetes Cluster

  • Create a ClusterIP Service for accessing the todo-app from within the cluster

  • Create a ClusterIP Service definition for your todo-app Deployment in a YAML file.

Step 1: Create ClusterIP Service Definition (cluster-ip-service.yml) Create a YAML file named cluster-svc.yml with the following content:

apiVersion: v1
kind: Service
metadata:
  name: todo-app-clusterip
  labels:
    app: todo-app
spec:
  selector:
    app: todo-app
  ports:
    - protocol: TCP
      port: 8000
      targetPort: 8080
  type: ClusterIP

Step 2: Apply the ClusterIP Service Definition

kubectl apply -f cluster-svc.yml -n <namespace-name>

Step 3: Verify ClusterIP Service To verify that the ClusterIP Service is working, follow these steps:

  1. Create a YAML file named test-pod.yml for a testing pod:
apiVersion: v1
kind: Pod
metadata:
  name: test-pod
spec:
  containers:
    - name: busybox
      image: busybox
      command: ['sh', '-c', 'while true; do wget -q -O- clusterip:8000; done']

Task 3: Creating a LoadBalancer Service and Accessing the todo-app from Outside the Kubernetes Cluster

  • Create a LoadBalancer Service for accessing the todo-app from outside the cluster

  • Create a LoadBalancer Service definition for your todo-app Deployment in a YAML file.

apiVersion: v1
kind: Service
metadata:
  name: todo-app-loadbalancer
spec:
  selector:
    app: todo-app
  ports:
    - protocol: TCP
      port: 80
      targetPort: 8000
  type: LoadBalancer

  • Apply the LoadBalancer Service definition to your K8s (minikube) cluster using the kubectl apply -f load-balancer-service.yml -n <namespace-name> command

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

"Thank you for enjoying my DevOps blog! Your positive response fuels my passion to dive deeper into technology and innovation.

Stay tuned for more captivating DevOps articles, where we'll explore this dynamic field together. Follow me on Hashnode and connect on LinkedIn (https://www.linkedin.com/in/som-shanker-pandey/ for the latest updates and discussions.

Did you find this article valuable?

Support Som Pandey's blog by becoming a sponsor. Any amount is appreciated!

ย