Kubernetes Objects
A comprehensive guide to Kubernetes objects including pods, services, deployments, statefulsets, daemonsets, jobs, cronjobs, and custom resources. Learn how each object works, when to use them, and see practical examples.
Kubernetes objects are persistent entities that represent the state of your cluster. They describe what containerized applications are running, the resources available to them, and the policies around their behavior. Every object has a spec (the desired state you provide) and a status (the actual state reported by Kubernetes).
Objects are created using the Kubernetes API, typically through YAML or JSON manifests, and are managed by the control plane's controllers that continuously work to match the desired state with the actual state.
apiVersion, kind, metadata (name, namespace, labels, annotations), and spec (desired state). Understanding this pattern helps you work with any Kubernetes resource.
Kubernetes objects are organized into several categories based on their purpose:
Workload Resources
Service Resources
Storage Resources
Configuration Resources
Identity & Security
Custom Resources
A Pod is the smallest and most basic deployable object in Kubernetes. It represents a single instance of a running process in your cluster. A pod can contain one or more containers that share storage, network, and execution context.
Single-Container Pods
Multi-Container Pods
Pod Lifecycle
# Basic Pod YAML
apiVersion: v1
kind: Pod
metadata:
name: nginx-pod
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
resources:
requests:
memory: "64Mi"
cpu: "250m"
limits:
memory: "128Mi"
cpu: "500m"
# Create a pod
kubectl apply -f pod.yaml
# Check pod status
kubectl get pods
kubectl describe pod nginx-pod
# View pod logs
kubectl logs nginx-pod
# Execute command in pod
kubectl exec -it nginx-pod -- /bin/sh
A Deployment provides declarative updates for Pods and ReplicaSets. It's the recommended way to manage stateless applications in production. Deployments handle scaling, rolling updates, and rollbacks automatically.
Rolling Updates
Rollbacks
Scaling
# Deployment YAML
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 1
maxUnavailable: 0
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.24
ports:
- containerPort: 80
# Create deployment
kubectl apply -f deployment.yaml
# Scale deployment
kubectl scale deployment nginx-deployment --replicas=5
# Update image (rolling update)
kubectl set image deployment/nginx-deployment nginx=nginx:1.25
# Check rollout status
kubectl rollout status deployment/nginx-deployment
# Rollback
kubectl rollout undo deployment/nginx-deployment
# View revision history
kubectl rollout history deployment/nginx-deployment
A StatefulSet manages the deployment and scaling of a set of pods, and provides guarantees about the ordering and uniqueness of these pods. Unlike Deployments, StatefulSets are designed for stateful applications like databases, message queues, and other services that require persistent storage and stable network identities.
Stable Network Identity
Persistent Storage
Ordered Operations
# StatefulSet YAML
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: mysql
spec:
serviceName: mysql
replicas: 3
selector:
matchLabels:
app: mysql
template:
metadata:
labels:
app: mysql
spec:
containers:
- name: mysql
image: mysql:8.0
env:
- name: MYSQL_ROOT_PASSWORD
valueFrom:
secretKeyRef:
name: mysql-secret
key: password
ports:
- containerPort: 3306
volumeMounts:
- name: mysql-data
mountPath: /var/lib/mysql
volumeClaimTemplates:
- metadata:
name: mysql-data
spec:
accessModes: ["ReadWriteOnce"]
resources:
requests:
storage: 10Gi
# Create headless service for StatefulSet
apiVersion: v1
kind: Service
metadata:
name: mysql
spec:
clusterIP: None
selector:
app: mysql
ports:
- port: 3306
A DaemonSet ensures that all (or some) nodes run a copy of a pod. As nodes are added to the cluster, pods are added to them. As nodes are removed, those pods are garbage collected. DaemonSets are perfect for node-level services.
Monitoring Agents
Security Agents
Network Plugins
# DaemonSet YAML
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: node-exporter
spec:
selector:
matchLabels:
app: node-exporter
template:
metadata:
labels:
app: node-exporter
spec:
hostNetwork: true
hostPID: true
containers:
- name: node-exporter
image: prom/node-exporter:latest
ports:
- containerPort: 9100
securityContext:
privileged: true
volumeMounts:
- name: proc
mountPath: /host/proc
readOnly: true
- name: sys
mountPath: /host/sys
readOnly: true
volumes:
- name: proc
hostPath:
path: /proc
- name: sys
hostPath:
path: /sys
A Service is an abstraction that defines a logical set of Pods and a policy by which to access them. Services enable loose coupling between dependent Pods by providing a stable endpoint (IP or DNS name) that doesn't change as pods are created, scaled, or replaced.
ClusterIP
NodePort
LoadBalancer
Headless
# Service YAML
apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
type: ClusterIP # or NodePort, LoadBalancer
selector:
app: nginx
ports:
- port: 80
targetPort: 80
protocol: TCP
# LoadBalancer service
apiVersion: v1
kind: Service
metadata:
name: nginx-lb
spec:
type: LoadBalancer
selector:
app: nginx
ports:
- port: 80
targetPort: 80
# Create service
kubectl apply -f service.yaml
# Check service
kubectl get svc
# Get service details
kubectl describe svc nginx-service
Jobs create one or more pods and ensure that they successfully terminate. They're ideal for one-off tasks and batch processing. CronJobs run Jobs on a scheduled basis.
Jobs
CronJobs
# Job YAML
apiVersion: batch/v1
kind: Job
metadata:
name: database-migration
spec:
template:
spec:
containers:
- name: migration
image: myapp:migration
env:
- name: DB_URL
valueFrom:
secretKeyRef:
name: db-secret
key: url
restartPolicy: Never
# CronJob YAML
apiVersion: batch/v1
kind: CronJob
metadata:
name: backup-job
spec:
schedule: "0 2 * * *" # Daily at 2 AM
jobTemplate:
spec:
template:
spec:
containers:
- name: backup
image: backup-image:latest
restartPolicy: Never
# Create Job
kubectl apply -f job.yaml
# Check job status
kubectl get jobs
kubectl describe job database-migration
Custom Resource Definitions (CRDs) allow you to extend the Kubernetes API with your own resources. This enables platform engineering teams to create domain-specific APIs.
# CustomResourceDefinition YAML
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
name: applications.mycompany.io
spec:
group: mycompany.io
versions:
- name: v1
served: true
storage: true
schema:
openAPIV3Schema:
type: object
properties:
spec:
type: object
properties:
replicas:
type: integer
image:
type: string
scope: Namespaced
names:
plural: applications
singular: application
kind: Application
shortNames:
- app
# Using the custom resource
apiVersion: mycompany.io/v1
kind: Application
metadata:
name: my-app
spec:
replicas: 3
image: nginx:latest
| Object | Use Case | Stateful? | Scaling | Update Strategy |
|---|---|---|---|---|
| Pod | Single container instance | No | Manual | N/A |
| Deployment | Stateless applications | No | Manual/Auto | Rolling update |
| StatefulSet | Stateful applications | Yes | Manual | Ordered updates |
| DaemonSet | Node-level services | No | Per node | Rolling update |
| Job | One-off tasks | No | Parallel | N/A |
| CronJob | Scheduled tasks | No | Parallel | N/A |
| Service | Network access | N/A | N/A | N/A |
kubectl rollout undo deployment/<name>. You can also rollback to a specific revision with kubectl rollout undo deployment/<name> --to-revision=<revision>.Understanding Kubernetes objects is fundamental to working with Kubernetes. Master these resources to build, deploy, and manage applications effectively in production.