Pod vs Container vs Deployment
Pod:
A Pod is the smallest deployable unit in Kubernetes. It represents a single instance of a running process in a cluster.
Example Code:
apiVersion: v1
kind: Pod
metadata:
name: my-pod
spec:
containers:
- name: main-app
image: nginx:latest
- name: sidecar
image: busybox:latest
Diagram:
+----------------+
| my-pod |
| |
| +------------+ | +-------------+
| | main-app | | | sidecar |
| | Container | | | Container |
| | | | | |
| +------------+ | +-------------+
+----------------+
In this example, we have a Pod named my-pod
that contains two containers: main-app
and sidecar
. These containers share the same network namespace and can communicate with each other using localhost
. They are tightly coupled within the same Pod.
Container:
A Container is a lightweight, standalone executable package that includes everything needed to run a piece of software.
Example Code:
Dockerfile for a simple Node.js application:
# Use an official Node.js runtime as the base image
FROM node:14
# Set the working directory in the container
WORKDIR /app
# Copy package.json and package-lock.json to the container
COPY package*.json ./
# Install application dependencies
RUN npm install
# Copy the rest of the application code to the container
COPY . .
# Expose a port for the application to listen on
EXPOSE 8080
# Define the command to run the application
CMD [ "node", "app.js" ]
Deployment:
A Deployment is a higher-level Kubernetes resource used for managing and scaling applications. It provides declarative updates to applications and manages the rollout of changes.
Example Code:
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-deployment
spec:
replicas: 3
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: my-app-container
image: my-app:latest
Diagram:
+------------------------+
| my-deployment |
| |
| +------- Pod 1 -------+ | +------- Pod 2 --------+ +------- Pod 3 -------+
| | my-app-container | | | my-app-container | | my-app-container |
| | Container | | | Container | | Container |
| | | | | | | |
| +----------------------+ | +----------------------+ +----------------------+
+------------------------+
In this example, we have a Deployment named my-deployment
that manages three replica Pods. Each Pod contains a container running the my-app
application. The Deployment ensures that the desired number of replicas (in this case, 3) is running, and it can automatically replace failed Pods or scale the application horizontally.