DEV Community

Cover image for Navigating Kubernetes: A Guide to Services
bharatrajtj
bharatrajtj

Posted on

Navigating Kubernetes: A Guide to Services

Introduction
In the dynamic world of Kubernetes, managing pod IPs can be a tricky business. When a pod goes down and is replaced, the new pod often comes with a new IP address, leaving users in the dark about the updated address. Enter Kubernetes Services, the unsung heroes of seamless communication and accessibility.

The Challenge
Picture this: a pod in your deployment crashes, and a fresh replacement takes its place. But wait, the new pod has a different IP address. How do you ensure users seamlessly transition to the replacement without any hiccups?

The Kubernetes Service Solution
Kubernetes addresses this challenge with the introduction of services. These are like traffic managers, ensuring that users don't need to keep track of ever-changing pod IPs. Instead, users interact with the service's IP address, and the service efficiently forwards their requests to the available pods.

Labels and Service Discovery
Each pod comes with a label, and services use these labels for efficient traffic routing. Even if a new pod with a new IP address replaces a failed one, the label remains constant. This magic is made possible by selectors, allowing services to identify pods based on these labels—enter the world of service discovery.

Advantages of Using Services
Let's break down why services are a game-changer:

Load Balancing: Services distribute traffic among available pods, ensuring optimal resource utilization.

Service Discovery: Thanks to labels and selectors, automatic discovery and routing of traffic become a breeze.

Exposure: Services provide various exposure options, including Cluster IP, Load Balancer, and Node Port.

Hands-On Experience
Step 1: Build Image
Clone the repository and navigate to the Python web app directory to build the Docker image.

git clone https://github.com/iam-veeramalla/Docker-Zero-to-Hero.git
Enter fullscreen mode Exit fullscreen mode

Navigate to examples direcctory

cd examples
Enter fullscreen mode Exit fullscreen mode

Navigate to python-web-app

cd python-web-app
Enter fullscreen mode Exit fullscreen mode

Image description

Step 2: Deployment
Create a deployment using the provided YAML file, then check the deployment list.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: pythonapp
  labels:
    app: demo
spec:
  replicas: 3
  selector:
    matchLabels:
      app: demo
  template:
    metadata:
      labels:
        app: demo
    spec:
      containers:
      - name: application
        image: bharatdevops/images:v1
        ports:
        - containerPort: 8000
Enter fullscreen mode Exit fullscreen mode

Apply the deployment using:

kubectl apply -f deployment.yml
Enter fullscreen mode Exit fullscreen mode

Check the deployment list:

kubectl get deploy
Enter fullscreen mode Exit fullscreen mode

Image description

Step 3: Pod IP Address

Obtain pod IP addresses, delete pods, and witness the creation of new pods with different IP addresses.

kubectl get pods -o wide
Enter fullscreen mode Exit fullscreen mode

Image description

Step 4: Create Service
Create a NodePort service using the provided YAML file.

apiVersion: v1
kind: Service
metadata:
  name: my-service
spec:
  type: NodePort
  selector:
    app: demo
  ports:
    - port: 80
      targetPort: 8000
      nodePort: 30007
Enter fullscreen mode Exit fullscreen mode

Apply the service using:

kubectl apply -f service.yml
Enter fullscreen mode Exit fullscreen mode

Image description

Step 5: Access the Application
Access the application using the Minikube IP address mapped to the NodePort.

Image description

This can not be accessed in website out of the organizations network.

Image description

Step 6: Cluster IP
Explore the Cluster IP within the Minikube cluster.

Image description

This cannot be accessed via website

Image description

Top comments (0)