Navigating Kubernetes: A Guide to Services
Tags: kubernetes, devops, orchestration, minikube
Published: 2023-11-13 12:09:39

**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
```
Navigate to examples direcctory
```
cd examples
```
Navigate to python-web-app
```
cd python-web-app
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uc05t5eg699nmxucxnsp.png)

**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
```
Apply the deployment using:

```
kubectl apply -f deployment.yml
```
Check the deployment list:
```
kubectl get deploy
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l0o1vz3wy8w4aacf3ivy.png)

**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
```


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/857b3mrime8yytdba1t3.png)

**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
```
Apply the service using:
```
kubectl apply -f service.yml
```
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8jno47k1fn4m9qc6iqup.png)

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


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k38fx58qhz1yqlhaaqlz.png)

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


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4q6dv15412iaxv7hbdix.png)

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


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/29zil4g21ypdwgghysc0.png)


This cannot be accessed via website

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mosikov9z7uqrohy22rh.png)