Kubernetes Pods, Deployments & Services Explained

Understanding these three core Kubernetes objects — Pods, Deployments, and Services — is the foundation for everything else you'll do in Kubernetes. This guide explains each with practical examples.

Pods: The Smallest Unit

A Pod wraps one or more containers that share networking and storage — in practice, most Pods run exactly one container. You rarely create Pods directly; instead, a Deployment manages them for you.

apiVersion: v1
kind: Pod
metadata:
  name: my-app
spec:
  containers:
  - name: my-app
    image: nginx:latest
    ports:
    - containerPort: 80

Why You Don't Manage Pods Directly

A bare Pod, if it crashes, stays dead — nothing restarts it automatically. This is why Deployments exist: they continuously ensure the desired number of Pod replicas are running.

Deployments: Managing Pod Replicas

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app
spec:
  replicas: 3
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
      - name: my-app
        image: nginx:latest
        ports:
        - containerPort: 80
kubectl apply -f deployment.yaml

replicas: 3 tells Kubernetes to always keep 3 identical Pods running — if one crashes, a replacement is created automatically.

Services: Stable Networking for Pods

Pods are ephemeral — they get new IP addresses whenever recreated. A Service provides a stable, unchanging endpoint that automatically routes to whichever Pods currently match its selector.

apiVersion: v1
kind: Service
metadata:
  name: my-app-service
spec:
  selector:
    app: my-app
  ports:
  - port: 80
    targetPort: 80
  type: ClusterIP

Service Types

TypeUse Case
ClusterIP (default)Internal-only access, other Pods within the cluster
NodePortExposes the service on a static port on every node, accessible externally
LoadBalancerRequests an external load balancer (typically requires cloud provider integration)

Putting It Together: A Complete Simple App

kubectl apply -f deployment.yaml
kubectl apply -f service.yaml
kubectl get pods
kubectl get services

Scaling a Deployment

kubectl scale deployment my-app --replicas=5

Viewing Pod Logs

kubectl logs POD_NAME

Getting a Shell Inside a Running Pod

kubectl exec -it POD_NAME -- /bin/bash

Common Errors

Service isn't routing traffic to Pods — verify the Service's selector exactly matches the labels defined in the Deployment's Pod template; a mismatch is the most common cause.

Pods stuck in "Pending" — usually insufficient cluster resources (CPU/RAM) to schedule the Pod; check with kubectl describe pod POD_NAME for the specific reason.

FAQ

Do I need a separate YAML file for every object?
No — multiple objects can be combined in one file separated by ---, though separate files are often clearer for larger projects.

Continue Reading

Browse more articles in Kubernetes & Container Orchestration.

  • kubernetes pods, kubernetes deployments, kubernetes services, kubernetes basics
  • 0 کاربر این را مفید یافتند
آیا این پاسخ به شما کمک کرد؟

مقالات مربوطه

What Is Kubernetes and When Do You Need It on a VPS?

Kubernetes is a container orchestration platform — it automates deploying, scaling, and...

How to Install a Single-Node Kubernetes Cluster with k3s

k3s is a lightweight, certified Kubernetes distribution designed to run efficiently on modest...

How to Install kubeadm and Set Up a Multi-Node Kubernetes Cluster

kubeadm is the official tool for bootstrapping a standard, full-featured Kubernetes cluster. This...

How to Expose Applications with a Kubernetes Ingress Controller (Nginx Ingress)

An Ingress lets you route external HTTP/HTTPS traffic to multiple services within your cluster...

How to Manage Persistent Storage in Kubernetes (PersistentVolumes & PVCs)

Containers are ephemeral by design — data written inside them disappears when the Pod is...