Workloads & Deployments
In Kubernetes, a workload is an application running on Kubernetes. This page covers how to manage your application deployments.
Writing a Deployment YAML
A Deployment is a Kubernetes object that manages a set of identical Pods. Here is an example of a simple Deployment that runs an NGINX web server.
# nginx-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.14.2
ports:
- containerPort: 80
To create this Deployment, you would save the YAML to a file (e.g., nginx-deployment.yaml) and run kubectl apply -f nginx-deployment.yaml.
Managing Application Updates (Rolling Updates)
By default, Deployments use a rolling update strategy. This means that when you update the Deployment (e.g., by changing the container image), the Deployment Controller will gradually replace the old Pods with new ones, ensuring that the application remains available during the update.
You can update a Deployment by modifying the YAML file and running kubectl apply again, or by using the kubectl set image command:
kubectl set image deployment/nginx-deployment nginx=nginx:1.16.1
StatefulSets for Stateful Applications
While Deployments are great for stateless applications, stateful applications (like databases) require a different approach. A StatefulSet is a Kubernetes object used to manage stateful applications.
StatefulSets provide:
- Stable, unique network identifiers.
- Stable, persistent storage.
- Ordered, graceful deployment and scaling.
- Ordered, automated rolling updates.
Here is a snippet of a StatefulSet definition:
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: web
spec:
serviceName: "nginx"
replicas: 3
# ... rest of the definition