Kubernetes Deployment¶
A Kubernetes Deployment is a controller object that provides a declarative method for managing [[Pods]] and [[ReplicaSets]].^[400-devops__06-Kubernetes__k8s-ithelp__Day8__README.md] It allows users to declare a desired state for the application, and the controller works to maintain that state automatically.[1][400-devops__06-Kubernetes__k8s-ithelp__Day8__README.md]
Because the Deployment is a higher-level abstraction, official documentation recommends managing ReplicaSets and Pods through Deployments rather than using ReplicaSets directly.[2][400-devops__06-Kubernetes__k8s-ithelp__Day8__README.md]
Core Architecture¶
The Deployment controller manages the lifecycle of Pods by creating and maintaining ReplicaSets.^[400-devops__06-Kubernetes__k8s-ithelp__Day8__README.md]
- ReplicaSet: Ensures that a specified number of Pod replicas are running at any given time, matching the user's desired status.[3][400-devops__06-Kubernetes__k8s-ithelp__Day8__README.md]
- Pod: The smallest deployable unit in Kubernetes.
When a Deployment is updated, the controller typically creates a new ReplicaSet with the updated configuration and gradually transitions the Pods from the old ReplicaSet to the new one.[4][400-devops__06-Kubernetes__k8s-ithelp__Day8__README.md]
Common Use Cases¶
Deployments are designed to facilitate the full lifecycle of stateless applications:
- Creation: Defining a Deployment automatically creates the underlying ReplicaSet and Pods.[5][400-devops__06-Kubernetes__k8s-ithelp__Day8__README.md]
- Scaling: Applications can be scaled up or down to handle load, a process often referred to as horizontal scaling.[6][400-devops__06-Kubernetes__k8s-ithelp__Day8__README.md]
- Updates: Deployments support rolling updates, allowing an application to be updated with zero downtime by controlling the rate at which new Pods replace old ones.[7][400-devops__06-Kubernetes__k8s-ithelp__Day8__README.md]
- Rollback: If an update causes instability, Deployments allow for rolling back to a previous stable version.[8][400-devops__06-Kubernetes__k8s-ithelp__Day8__README.md]
Configuration Example¶
Deployments are typically defined using YAML configuration files.^[400-devops__06-Kubernetes__k8s-ithelp__Day8__README.md]
apiVersion: apps/v1
kind: Deployment
metadata:
name: foo-deployment
labels:
app: foo
spec:
replicas: 2
selector:
matchLabels:
app: foo
template:
metadata:
labels:
app: foo
spec:
containers:
- name: foo
image: mikehsu0618/foo
ports:
- containerPort: 8080
Key Fields:
spec.replicas: Defines the number of Pod replicas desired (e.g., 2).^[400-devops__06-Kubernetes__k8s-ithelp__Day8__README.md]spec.selector: Defines how the Deployment identifies which Pods to manage (must match the template labels).^[400-devops__06-Kubernetes__k8s-ithelp__Day8__README.md]spec.template: Defines the Pod specification (metadata and container settings).
Deployment Updates and Rollback¶
The Deployment controller tracks the history of changes applied to the Pod template.^[400-devops__06-Kubernetes__k8s-ithelp__Day8__README.md]
Revision History
Only changes to the spec.template field (such as changing the container image or environment variables) trigger the creation of a new Revision.[9][400-devops__06-Kubernetes__k8s-ithelp__Day8__README.md]
* Changes to spec.replicas (scaling) do not create a new revision.[10][400-devops__06-Kubernetes__k8s-ithelp__Day8__README.md]
Rollback
If a new Deployment revision is unstable (e.g., ImagePullBackOff errors due to a bad image tag), the rollout undo command can revert the Deployment to a previous, stable revision.[11][400-devops__06-Kubernetes__k8s-ithelp__Day8__README.md]
Related Concepts¶
- Pod
- ReplicaSet
- Kubernetes
- [[Rolling Update]]
Sources¶
- 400-devops__06-Kubernetes__k8s-ithelp__Day8__README.md
- 400-devops__06-Kubernetes__k8s-learning__04.deployment__deployment.md