kubernetes services

🌐 Kubernetes Services Explained 🚦

In this section, we’ll break down how Kubernetes Services work by exposing our existing nginx pod internally and externally. You’ll see how ClusterIP and NodePort services behave, how they select pods, and how traffic flows through the cluster. Let’s dive in! πŸ³βš™οΈ

1. Initial Setup πŸ”

First, we verified that our nginx pod was running and confirmed its labels:

kubectl get pods --show-labels

Observed output:

  • Pod name: nginx-pod
  • Status: Running 🟒
  • Labels: app=web, tier=frontend 🏷️

2. Creating Kubernetes Services 🧩

We created two different service types to expose the same pod in different ways.

A. ClusterIP Service (Internal Access) πŸ”’

Create the file 01-clusterip-service.yaml:

apiVersion: v1
kind: Service
metadata:
  name: nginx-service-internal
spec:
  type: ClusterIP
  selector:
    app: web
    tier: frontend
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80

B. NodePort Service (External Access) 🌍

Create the file 02-nodeport-service.yaml:

apiVersion: v1
kind: Service
metadata:
  name: nginx-service-external
spec:
  type: NodePort
  selector:
    app: web
    tier: frontend
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80
      nodePort: 30080

3. Deploying the Services πŸš€

We applied both service definitions to the cluster:

kubectl apply -f exercises/1-basics/services/01-clusterip-service.yaml
kubectl apply -f exercises/1-basics/services/02-nodeport-service.yaml

4. Verifying Services πŸ“‹

Next, we checked that the services were created successfully:

kubectl get services

Observed output:

  • nginx-service-internal (ClusterIP): 88.888.888.888:80 πŸ”’
  • nginx-service-external (NodePort): 88.888.888.888:30080 🌍
  • kubernetes (default): 88.88.8.8:443 βš™οΈ

5. Accessing the Application 🌐

# Get Minikube IP 🧭

minikube ip

Output:

  • Minikube IP: 192.168.49.2

# Open NodePort service using Minikube πŸšͺ

minikube service nginx-service-external

This automatically opened the service in the browser and created a tunnel.

Access Points Created πŸ”—

  • Internal Access (ClusterIP):
    • Accessible only inside the cluster
    • IP: 88.888.888.123
    • Port: 80
  • External Access (NodePort):
    • URL: http://192.168.49.2:30080
    • Minikube tunnel: http://127.0.0.1:54155

6. Key Concepts Covered 🧠

  • Service Types:
    • ClusterIP – internal cluster communication πŸ”’
    • NodePort – external access 🌍
  • Service Selection:
    • Uses pod labels: app=web, tier=frontend
  • Port Mapping:
    • Service port: 80
    • Target port: 80 (container port)
    • Node port: 30080

This setup allows the nginx pod to be:

  • Internally accessible via ClusterIP 🏠
  • Externally accessible via NodePort 🌍
  • Discoverable by other pods using service DNS πŸ”Ž

What’s Next? ⏭️

In the next part, we’ll move on to Deployments and see how Kubernetes helps us manage replicas, updates, and scaling automatically. Let’s keep going! πŸš€

Leave a Reply

Your email address will not be published. Required fields are marked *